mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 06:04:29 +00:00
Allow more options to the Apollo Server config (#7665)
* 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>
This commit is contained in:
parent
5364191ea1
commit
d2ef1d4b8c
@ -46,10 +46,14 @@ 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
|
||||
@ -57,11 +61,13 @@ module.exports = {
|
||||
//
|
||||
graphql: {
|
||||
endpoint: '/graphql',
|
||||
tracing: false,
|
||||
shadowCRUD: true,
|
||||
playgroundAlways: false,
|
||||
depthLimit: 7,
|
||||
amountLimit: 100,
|
||||
apolloServer: {
|
||||
tracing: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
@ -2,5 +2,8 @@ module.exports = ({ env }) => ({
|
||||
graphql: {
|
||||
amountLimit: 5,
|
||||
depthLimit: 10,
|
||||
apolloServer: {
|
||||
tracing: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 }]);
|
||||
@ -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 }) => {
|
||||
@ -108,27 +125,24 @@ 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,
|
||||
};
|
||||
}
|
||||
|
||||
@ -136,7 +150,7 @@ module.exports = strapi => {
|
||||
|
||||
server.applyMiddleware({
|
||||
app: strapi.app,
|
||||
path: strapi.plugins.graphql.config.endpoint,
|
||||
path: config.endpoint,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user