Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Strapi GraphQL an implementing service for Federation #6146

Merged
merged 11 commits into from
Jul 16, 2020
5 changes: 4 additions & 1 deletion docs/3.0.0-beta.x/plugins/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Security limits on maximum number of items in your response by default is limite

You can also enable the Apollo server tracing feature, which is supported by the playground to track the response time of each part of your query. To enable this feature just change/add the `"tracing": true` option in the GraphQL settings file. You can read more about the tracing feature from Apollo [here](https://www.apollographql.com/docs/apollo-server/federation/metrics/).

You can also enable Strapi as an implementing service for Apollo Federation, which allows you to set up Strapi as a service behind an Apollo Federation API gateway and allows you to add Strapi to a single exposed data graph across your organization. You can read more about Apollo Federation [here](https://www.apollographql.com/docs/apollo-server/federation/introduction/).

You can edit these configurations by creating following file.

**Path —** `./extensions/graphql/config/settings.json`.
Expand All @@ -59,7 +61,8 @@ You can edit these configurations by creating following file.
"shadowCRUD": true,
"playgroundAlways": false,
"depthLimit": 7,
"amountLimit": 100
"amountLimit": 100,
"federation": false
}
```

Expand Down
3 changes: 2 additions & 1 deletion packages/strapi-plugin-graphql/config/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"playgroundAlways": false,
"depthLimit": 7,
"amountLimit": 100,
"shareEnabled": false
"shareEnabled": false,
"federation": false
}
14 changes: 12 additions & 2 deletions packages/strapi-plugin-graphql/hooks/graphql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Public node modules.
const _ = require('lodash');
const { ApolloServer } = require('apollo-server-koa');
const { buildFederatedSchema } = require('@apollo/federation');
const depthLimit = require('graphql-depth-limit');
const loadConfigs = require('./load-config');

Expand Down Expand Up @@ -84,9 +85,18 @@ module.exports = strapi => {
return;
}

// Get federation config
const isFederated = _.get(strapi.plugins.graphql, 'config.federation', false);
const schemaDef = {};
if (isFederated) {
schemaDef.schema = buildFederatedSchema([{ typeDefs, resolvers }]);
} else {
schemaDef.typeDefs = typeDefs;
schemaDef.resolvers = resolvers;
}

const serverParams = {
typeDefs,
resolvers,
...schemaDef,
context: ({ ctx }) => {
// Initiliase loaders for this request.
// TODO: set loaders in the context not globally
Expand Down
1 change: 1 addition & 0 deletions packages/strapi-plugin-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "echo \"no tests yet\""
},
"dependencies": {
"@apollo/federation": "^0.15.0",
"apollo-server-koa": "2.9.12",
"dataloader": "^1.4.0",
"glob": "^7.1.4",
Expand Down
6 changes: 4 additions & 2 deletions packages/strapi-plugin-graphql/services/schema-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ const generateSchema = () => {
writeGenerateSchema(graphql.printSchema(schema));
}

// Remove custom scalar (like Upload);
typeDefs = Types.removeCustomScalar(typeDefs, resolvers);
// Remove custom scalar (like Upload), if not using Federation
if (!strapi.plugins.graphql.config.federation) {
shaheinm marked this conversation as resolved.
Show resolved Hide resolved
typeDefs = Types.removeCustomScalar(typeDefs, resolvers);
}

return {
typeDefs: gql(typeDefs),
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
# yarn lockfile v1


"@apollo/federation@^0.15.0":
version "0.15.0"
resolved "https://registry.yarnpkg.com/@apollo/federation/-/federation-0.15.0.tgz#385f9fd4473e7ec1c3dbd251f80a054d47e28614"
integrity sha512-L6n41obKc0fLQdZ0uSHhHRJNCIAxZ5uDeqQON+Q+ewiUfS3XetCCf/cGpiiL50TIY/n8Bvh5ZPWLMTtP0tyw5g==
dependencies:
apollo-graphql "^0.4.0"
apollo-server-env "^2.4.3"
core-js "^3.4.0"
lodash.xorby "^4.7.0"

"@apollo/protobufjs@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@apollo/protobufjs/-/protobufjs-1.0.3.tgz#02c655aedd4ba7c7f64cbc3d2b1dd9a000a391ba"
Expand Down Expand Up @@ -5890,6 +5900,11 @@ core-js@^3.0.1, core-js@^3.2.0:
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.6.4.tgz#440a83536b458114b9cb2ac1580ba377dc470647"
integrity sha512-4paDGScNgZP2IXXilaffL9X7968RuvwlkK3xWtZRVqgd8SYNiVKRJvkFd1aqqEuPfN7E68ZHEp9hDj6lHj4Hyw==

core-js@^3.4.0:
version "3.6.5"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.6.5.tgz#7395dc273af37fb2e50e9bd3d9fe841285231d1a"
integrity sha512-vZVEEwZoIsI+vPEuoF9Iqf5H7/M3eeQqWlQnYa8FSKKePuYTf5MWnxb5SDAzCa60b3JBRS5g9b+Dq7b1y/RCrA==

core-util-is@1.0.2, core-util-is@^1.0.2, core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
Expand Down Expand Up @@ -11806,6 +11821,11 @@ lodash.uniq@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=

lodash.xorby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.xorby/-/lodash.xorby-4.7.0.tgz#9c19a6f9f063a6eb53dd03c1b6871799801463d7"
integrity sha1-nBmm+fBjputT3QPBtocXmYAUY9c=

lodash@4.17.10:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
Expand Down