Disable subscriptions by default, change how to handle the type in wrapResolvers

This commit is contained in:
Convly 2021-11-10 11:18:35 +01:00
parent cf5e4078b5
commit 69d8b20f1b
6 changed files with 120 additions and 80 deletions

View File

@ -31,9 +31,9 @@ module.exports = async ({ strapi }) => {
return;
}
const { config } = strapi.plugin('graphql');
const { config } = strapi.plugin('graphql').service('utils');
const path = config('endpoint', '/graphql');
const path = config.endpoint;
const defaultServerConfig = {
// Schema
@ -46,7 +46,7 @@ module.exports = async ({ strapi }) => {
}),
// Validation
validationRules: [depthLimit(config('depthLimit'))],
validationRules: [depthLimit(config.depthLimit)],
// Errors
formatError: formatGraphqlError,
@ -63,10 +63,10 @@ module.exports = async ({ strapi }) => {
],
};
const serverConfig = merge(defaultServerConfig, config('apolloServer', {}));
const serverConfig = merge(defaultServerConfig, config.apolloServer);
// Handle subscriptions
if (config('subscriptions', true)) {
if (config.subscriptions) {
const subscriptionServer = SubscriptionServer.create(
{ schema, execute, subscribe },
{ server: strapi.server.httpServer, path }

View File

@ -95,7 +95,7 @@ module.exports = ({ strapi }) => {
* Apply basic transform to GQL args
*/
transformArgs(args, { contentType, usePagination = false } = {}) {
const { mappers } = getService('utils');
const { mappers, config } = getService('utils');
const { pagination = {}, filters = {} } = args;
// Init
@ -103,8 +103,7 @@ module.exports = ({ strapi }) => {
// Pagination
if (usePagination) {
const defaultLimit = strapi.plugin('graphql').config('defaultLimit');
const maxLimit = strapi.plugin('graphql').config('maxLimit', -1);
const { defaultLimit, maxLimit } = config;
Object.assign(
newArgs,

View File

@ -5,6 +5,7 @@ const {
makeExecutableSchema,
addResolversToSchema,
} = require('@graphql-tools/schema');
const { pruneSchema } = require('@graphql-tools/utils');
const { makeSchema } = require('nexus');
const { prop, startsWith } = require('lodash/fp');
@ -26,7 +27,7 @@ const {
module.exports = ({ strapi }) => {
const { service: getGraphQLService } = strapi.plugin('graphql');
const { config } = strapi.plugin('graphql');
const { config } = getGraphQLService('utils');
const { KINDS, GENERIC_MORPH_TYPENAME } = getGraphQLService('constants');
const extensionService = getGraphQLService('extension');
@ -37,7 +38,7 @@ module.exports = ({ strapi }) => {
let builders;
const buildSchema = () => {
const isShadowCRUDEnabled = !!config('shadowCRUD', true);
const isShadowCRUDEnabled = !!config.shadowCRUD;
// Create a new empty type registry
registry = getGraphQLService('type-registry').new();
@ -71,7 +72,11 @@ module.exports = ({ strapi }) => {
// Wrap resolvers if needed (auth, middlewares, policies...) as configured in the extension
const wrappedSchema = wrapResolvers({ schema, strapi, extension });
return wrappedSchema;
// Prune schema, remove unused types
// eg: removes registered subscriptions if they're disabled in the config)
const prunedSchema = pruneSchema(wrappedSchema);
return prunedSchema;
};
const buildSchemas = ({ registry }) => {

View File

@ -25,22 +25,19 @@ const introspectionQueries = [
* @return {GraphQLSchema}
*/
const wrapResolvers = ({ schema, strapi, extension = {} }) => {
const { config: graphQLConfig } = strapi.plugin('graphql').service('utils');
// Get all the registered resolvers configuration
const { resolversConfig = {} } = extension;
// Fields filters
const isValidFieldName = ([field]) => !field.startsWith('__');
const typesMaps = [schema.getTypeMap()];
const typeMap = schema.getTypeMap();
const subscriptionType = schema.getSubscriptionType();
if (subscriptionType) {
typesMaps.push(subscriptionType.getFields());
if (!graphQLConfig.subscriptions) {
delete typeMap.Subscription;
}
typesMaps.forEach(typeMap =>
// Iterate over every field from every type within the
// schema's type map and wrap its resolve attribute if needed
Object.entries(typeMap).forEach(([type, definition]) => {
const isGraphQLObjectType = definition instanceof GraphQLObjectType;
const isIgnoredType = introspectionQueries.includes(type);
@ -117,8 +114,7 @@ const wrapResolvers = ({ schema, strapi, extension = {} }) => {
return first(boundMiddlewares).call(null, parent, args, context, info);
};
}
})
);
});
return schema;
};

View File

@ -0,0 +1,38 @@
'use strict';
/**
* GraphQL config helper with consistent defaults values
*/
module.exports = ({ strapi }) => {
const { config: graphQLConfig } = strapi.plugin('graphql');
return {
get shadowCRUD() {
return graphQLConfig('shadowCRUD', true);
},
get subscriptions() {
return graphQLConfig('subscriptions', false);
},
get endpoint() {
return graphQLConfig('endpoint', '/graphql');
},
get defaultLimit() {
return graphQLConfig('defaultLimit');
},
get maxLimit() {
return graphQLConfig('maxLimit', -1);
},
get depthLimit() {
return graphQLConfig('depthLimit');
},
get apolloServer() {
return graphQLConfig('apolloServer', {});
},
};
};

View File

@ -3,9 +3,11 @@
const mappers = require('./mappers');
const attributes = require('./attributes');
const naming = require('./naming');
const config = require('./config');
module.exports = context => ({
naming: naming(context),
attributes: attributes(context),
mappers: mappers(context),
config: config(context),
});