@accounts/module-core
Version:
Server side GraphQL transport for accounts
31 lines (29 loc) • 1.22 kB
text/typescript
import { authenticated } from './authenticated-resolver';
import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils';
import { defaultFieldResolver, type GraphQLSchema } from 'graphql';
export function authDirective(directiveName = 'auth') {
const typeDirectiveArgumentMaps: Record<string, any> = {};
return {
authDirectiveTypeDefs: `directive @${directiveName} on FIELD_DEFINITION | OBJECT`,
authDirectiveTransformer: (schema: GraphQLSchema) =>
mapSchema(schema, {
[MapperKind.TYPE]: (type) => {
const authDirective = getDirective(schema, type, directiveName)?.[0];
if (authDirective) {
typeDirectiveArgumentMaps[type.name] = authDirective;
}
return undefined;
},
[MapperKind.OBJECT_FIELD]: (fieldConfig, _fieldName, typeName) => {
const authDirective =
getDirective(schema, fieldConfig, directiveName)?.[0] ??
typeDirectiveArgumentMaps[typeName];
if (authDirective) {
const { resolve = defaultFieldResolver } = fieldConfig;
fieldConfig.resolve = authenticated(resolve);
return fieldConfig;
}
},
}),
};
}