Directives
To write directives, you have to use yoga or apollo adapter.
directives.ts
import { createDirectives } from '@/src/axolotl.js';
import { defaultFieldResolver } from 'graphql';
import { YogaInitialContext } from 'graphql-yoga';
export default createDirectives({
auth: (schema, getDirective) => ({
'MapperKind.OBJECT_FIELD': (fieldConfig) => {
const directive = getDirective(schema, fieldConfig, 'auth');
if (directive) {
return ((fieldConfig) => {
const directive = getDirective(schema, fieldConfig, 'auth');
if (directive) {
const { resolve = defaultFieldResolver } = fieldConfig;
return {
...fieldConfig,
resolve: async (source, args, context: YogaInitialContext, info) => {
const headerValue = context.request.headers.get('Auth');
if (headerValue !== 'password') {
throw new Error('You are not authorized');
}
return resolve(source, args, context, info);
},
};
}
})(fieldConfig);
}
},
}),
});
As you can see directives use great helpers from @graphql-tools
package.