Search code examples
graphqlapollodirectivegraphql-modules

How to use custom directives graphql-modules with apollo


I need help with custom directives when using graphql-modules library. Have no idea where to place my custom directives so it is combined with overall schema


Solution

  • I would like to post answer from Discord community, from user Maapteh.

    here it the quote from him

    in our app with old version we had everything in common module. We kept that approach partly when using the latest version of modules. See some parts of our codebase:

    import { ApolloServer, SchemaDirectiveVisitor } from 'apollo-server-express';

    const schema = AppModule.createSchemaForApollo();

    SchemaDirectiveVisitor.visitSchemaDirectives(schema, { isMember: IsMemberDirective, deprecated: isDeprecated, ...SNIP... });

    as you can see we create the schema we pass eventually to the apollo server (example using apollo). We add our generic directives like so. The schema for them is in our common module. Read further...

    For common scalars we added a common module. With their schema (so in schema directory we also have the directives schemas) and their resolver.

    const typeDefsArray = loadFilesSync(${__dirname}/schema/*.graphql, { useRequire: true, }); const typeDefs = mergeTypeDefs(typeDefsArray, { useSchemaDefinition: false });

    const resolverFunctions = { ImageUrl: ImageUrlType, PageSize: PageSizeType, Date: DateResolver, ...SNIP... };

    export const CommonModule = createModule({ id: 'common', typeDefs: typeDefs, resolvers: resolverFunctions, });

    hope it helps you

    https://discord.com/channels/625400653321076807/631489837416841253/832595211166548049