Handle subscriptions in wrapResolvers

This commit is contained in:
Convly 2021-09-29 13:35:26 +02:00
parent c1e3c41cbd
commit 96ac7e314b

View File

@ -30,8 +30,9 @@ const wrapResolvers = ({ schema, strapi, extension = {} }) => {
// Fields filters // Fields filters
const isValidFieldName = ([field]) => !field.startsWith('__'); const isValidFieldName = ([field]) => !field.startsWith('__');
const typeMap = schema.getTypeMap(); const typesMaps = [schema.getTypeMap(), schema.getSubscriptionType().getFields()];
typesMaps.forEach(typeMap =>
// Iterate over every field from every type within the // Iterate over every field from every type within the
// schema's type map and wrap its resolve attribute if needed // schema's type map and wrap its resolve attribute if needed
Object.entries(typeMap).forEach(([type, definition]) => { Object.entries(typeMap).forEach(([type, definition]) => {
@ -81,12 +82,12 @@ const wrapResolvers = ({ schema, strapi, extension = {} }) => {
const authConfig = get('auth', resolverConfig); const authConfig = get('auth', resolverConfig);
const authContext = get('state.auth', context); const authContext = get('state.auth', context);
const isMutationOrQuery = ['Mutation', 'Query'].includes(type); const isValidType = ['Mutation', 'Query', 'Subscription'].includes(type);
const hasConfig = !isNil(authConfig); const hasConfig = !isNil(authConfig);
const isAuthDisabled = authConfig === false; const isAuthDisabled = authConfig === false;
if ((isMutationOrQuery || hasConfig) && !isAuthDisabled) { if ((isValidType || hasConfig) && !isAuthDisabled) {
try { try {
await strapi.auth.verify(authContext, authConfig); await strapi.auth.verify(authContext, authConfig);
} catch (error) { } catch (error) {
@ -111,7 +112,8 @@ const wrapResolvers = ({ schema, strapi, extension = {} }) => {
return first(boundMiddlewares).call(null, parent, args, context, info); return first(boundMiddlewares).call(null, parent, args, context, info);
}; };
} }
}); })
);
return schema; return schema;
}; };