Mirror of https://github.com/roostorg/coop github.com/roostorg/coop
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at 557ff54b2b435e5f1e789c6a8a4e1bebf2d7deb6 42 lines 1.2 kB view raw
1import { getDirective } from '@graphql-tools/utils'; 2import { AuthenticationError } from 'apollo-server-express'; 3import { 4 defaultFieldResolver, 5 type GraphQLFieldConfig, 6 type GraphQLResolveInfo, 7 type GraphQLSchema, 8} from 'graphql'; 9 10import type { Context } from '../resolvers.js'; 11 12export function shouldSkipAuth( 13 schema: GraphQLSchema, 14 fieldConfig: GraphQLFieldConfig<unknown, unknown>, 15): boolean { 16 const directives = getDirective(schema, fieldConfig, 'publicResolver'); 17 18 return directives ? directives.length > 0 : false; 19} 20 21export function authSchemaWrapper( 22 fieldConfig: GraphQLFieldConfig<unknown, unknown>, 23 schema: GraphQLSchema, 24) { 25 const originalResolver = fieldConfig.resolve ?? defaultFieldResolver; 26 return { 27 ...fieldConfig, 28 resolve: shouldSkipAuth(schema, fieldConfig) 29 ? originalResolver 30 : async function ( 31 source: unknown, 32 args: unknown, 33 context: Context, 34 info: GraphQLResolveInfo, 35 ) { 36 if (!context.getUser()) { 37 throw new AuthenticationError('No user in context.'); 38 } 39 return originalResolver(source, args, context, info); 40 }, 41 }; 42}