Skip to content

Commit

Permalink
Allow more options to the Apollo Server config (strapi#7665)
Browse files Browse the repository at this point in the history
* Extract server config to a constant

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Omit options to server config

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Move default config to serverParams

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Simplify Apollo server config

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Simplify with a new apolloServerConfig property

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Update tracing documentation

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Extract deprecated options

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Add default options

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Update documentation

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Rename to deprecatedApolloServerConfig

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Empty default options

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Update default config

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Add warning note

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Add documentation note

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Update documentation note

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Fix the config check

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Fix the config check

Signed-off-by: Abdón Rodríguez Davila <a@abdonrd.com>

* Refactor old code and rename param to appoloServer to avoid redondancy

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>

* Fix typo

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>

Co-authored-by: Alexandre Bodin <bodin.alex@gmail.com>
Signed-off-by: Garrett Fritz <garrettfritz@garretts-mbp.home>
  • Loading branch information
2 people authored and Garrett Fritz committed Sep 7, 2020
1 parent aa4388e commit 934c461
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
10 changes: 8 additions & 2 deletions docs/v3.x/plugins/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,28 @@ By default, the [Shadow CRUD](#shadow-crud) feature is enabled and the GraphQL i

Security limits on maximum number of items in your response by default is limited to 100, however you can change this on the following config option `amountLimit`. This should only be changed after careful consideration of the drawbacks of a large query which can cause what would basically be a DDoS (Distributed Denial of Service). And may cause abnormal load on your Strapi server, as well as your database server.

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 setup any [Apollo Server options](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#apolloserver) with the `apolloServer` option. For example, you can enable the 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 edit these configurations by creating following file.

::: warning
Please note the setting for GraphQL `tracing` as changed and has been moved to `apolloServer.tracing`
:::

**Path —** `./config/plugins.js`

```js
module.exports = {
//
graphql: {
endpoint: '/graphql',
tracing: false,
shadowCRUD: true,
playgroundAlways: false,
depthLimit: 7,
amountLimit: 100,
apolloServer: {
tracing: false,
},
},
};
```
Expand Down
3 changes: 3 additions & 0 deletions examples/getstarted/config/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ module.exports = ({ env }) => ({
graphql: {
amountLimit: 5,
depthLimit: 10,
apolloServer: {
tracing: true,
},
},
});
6 changes: 4 additions & 2 deletions packages/strapi-plugin-graphql/config/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"endpoint": "/graphql",
"tracing": false,
"shadowCRUD": true,
"playgroundAlways": false,
"depthLimit": 7,
"amountLimit": 100,
"shareEnabled": false,
"federation": false
"federation": false,
"apolloServer": {
"tracing": false
}
}
40 changes: 27 additions & 13 deletions packages/strapi-plugin-graphql/hooks/graphql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ module.exports = strapi => {
return;
}

const config = _.get(strapi.plugins.graphql, 'config', {});

// Get federation config
const isFederated = _.get(strapi.plugins.graphql, 'config.federation', false);
const isFederated = _.get(config, 'federation', false);
const schemaDef = {};
if (isFederated) {
schemaDef.schema = buildFederatedSchema([{ typeDefs, resolvers }]);
Expand All @@ -95,6 +97,21 @@ module.exports = strapi => {
schemaDef.resolvers = resolvers;
}

// TODO: Remove these deprecated options in favor of `apolloServer` in the next major version
const deprecatedApolloServerConfig = {
tracing: _.get(config, 'tracing', false),
introspection: _.get(config, 'introspection', true),
engine: _.get(config, 'engine', false),
};

if (['tracing', 'introspection', 'engine'].some(key => _.has(config, key))) {
strapi.log.warn(
'The `tracing`, `introspection` and `engine` options are deprecated in favor of the `apolloServer` object and they will be removed in the next major version.'
);
}

const apolloServerConfig = _.get(config, 'apolloServer', {});

const serverParams = {
...schemaDef,
context: ({ ctx }) => {
Expand All @@ -108,35 +125,32 @@ module.exports = strapi => {
};
},
formatError: err => {
const formatError = _.get(strapi.plugins.graphql, 'config.formatError', null);
const formatError = _.get(config, 'formatError', null);

return typeof formatError === 'function' ? formatError(err) : err;
},
validationRules: [depthLimit(strapi.plugins.graphql.config.depthLimit)],
tracing: _.get(strapi.plugins.graphql, 'config.tracing', false),
validationRules: [depthLimit(config.depthLimit)],
playground: false,
cors: false,
bodyParserConfig: true,
introspection: _.get(strapi.plugins.graphql, 'config.introspection', true),
engine: _.get(strapi.plugins.graphql, 'config.engine', false),
// TODO: Remove these deprecated options in favor of `apolloServerConfig` in the next major version
...deprecatedApolloServerConfig,
...apolloServerConfig,
};

// Disable GraphQL Playground in production environment.
if (
strapi.config.environment !== 'production' ||
strapi.plugins.graphql.config.playgroundAlways
) {
if (strapi.config.environment !== 'production' || config.playgroundAlways) {
serverParams.playground = {
endpoint: `${strapi.config.server.url}${strapi.plugins.graphql.config.endpoint}`,
shareEnabled: strapi.plugins.graphql.config.shareEnabled,
endpoint: `${strapi.config.server.url}${config.endpoint}`,
shareEnabled: config.shareEnabled,
};
}

const server = new ApolloServer(serverParams);

server.applyMiddleware({
app: strapi.app,
path: strapi.plugins.graphql.config.endpoint,
path: config.endpoint,
});
},
};
Expand Down

0 comments on commit 934c461

Please sign in to comment.