Dmitry Klochkov
Dmitry Klochkov

Reputation: 2635

Remove custom directives from schema before loading

I am using graphql-codegen with typescript-mongodb plugin to generate database model files from the qraphql schema. And therefore my schema contains custom directive from the typescript-mongodb like @entity and @column. The codegen works fine but then when I load the schema using the graphql-tools loadSchemaSync function I get a schema validation error which complains about unknown directives.

Probably the most simple solution would be to add the mongo plugin directive definitions to the schema(which I could not yet get working either). But there is not actually any reason to have those directives in the schema after the graphql-codegen generated the configured files.

So I wonder is there some standard way to remove the mongo-related directives from the schema as some intermediate step before loading the schema files into the executable schema?

Or is there a way to tell the loadSchemaSync function to ignore the "unknown directives" error?

Here the my current code to load the schema files:

import { join } from "path";
import {loadSchemaSync, GraphQLFileLoader} from "graphql-tools"

const schema = loadSchemaSync(join(__dirname, '../src/graphql/schemas/**/*.graphql'), {
  loaders: [
    new GraphQLFileLoader()
  ]
})

Upvotes: 1

Views: 1220

Answers (1)

Phil
Phil

Reputation: 2313

It sounds like you haven't imported the mongo codegen directives in to your schema.

You should know that graphql-tools is deprecated and wont receive anymore updates. You should use the appropriate scoped packages such as: https://www.npmjs.com/package/@graphql-codegen/typescript-mongodb

Check out the usage example in the documentation.

https://www.graphql-code-generator.com/docs/plugins/typescript-mongodb#usage-example

import { makeExecutableSchema } from '@graphql-tools/schema';
import { DIRECTIVES } from '@graphql-codegen/typescript-mongodb';

const schema = makeExecutableSchema({
  typeDefs: [
    DIRECTIVES,
    // the rest of your GraphQL types
  ],
  resolvers,
});

Upvotes: 1

Related Questions