smooth-code / graphql-directive

Use custom directives in your GraphQL schema and queries 🎩
MIT License
152 stars 14 forks source link

Cannot read property 'directives' of undefined at createFieldExecutionResolver #8

Open ticdenis opened 6 years ago

ticdenis commented 6 years ago

Hi, it throws me the following error when trying to add directive resolve functions to the schema.

I have checked with forEachField and astNode is always undefined, is it my configuration or is it a bug?

const schema = new GraphQLSchema({
  directives: [
    new GraphQLDirective({
      name: 'isAuth',
      locations: [ DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.FIELD ]
    })
  ],
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      user: {
        type: new GraphQLObjectType({
          name: 'User',
          fields: () => ({
            id: { type: GraphQLNonNull(GraphQLInt) },
            email: { type: GraphQLNonNull(GraphQLString) }
          })
        }),
        args: { id: { type: GraphQLInt } },
        resolve: (parent, args, context, info) => {
          return { id: 1, email: 'foo@gmail.com' };
        }
      }
    }
  })
});

const directiveResolvers = {
  isAuth(next, source, args, context, info) {
    throw Error('Ups!');
  }
};

const { addDirectiveResolveFunctionsToSchema } = require('graphql-directive');
addDirectiveResolveFunctionsToSchema(schema, directiveResolvers);
gregberge commented 6 years ago

Hello what GraphQL version do you use?

ticdenis commented 6 years ago

0.13.1 version

gregberge commented 6 years ago

I just ran test with all up to date and it works, it is weird.

ticdenis commented 6 years ago

Is for use ApolloServer?

package.json

// ...
"apollo-server-express": "^1.3.2",
"graphql": "^0.13.1",
"graphql-date": "^1.0.3",
"graphql-directive": "^0.2.1",
"graphql-fields": "^1.0.2",
"graphql-tools": "^2.21.0",
// ...

server

   this.app = express();
   const {
      graphqlExpress,
      graphiqlExpress
    } = require('apollo-server-express');
    // ...
    this.app.use(compression());
    this.app.use('/graphql', bodyParser.json(), graphqlExpress({
      schema: require('./schema'),
      context: {
        db: require('./config/knex')
      },
      debug: this.isDevelopment,
      tracing: true,
      cacheControl: true
    }));

    if (this.isDevelopment) {
      this.app.use('/graphiql', graphiqlExpress({
        endpointURL: '/graphql'
      }));
    }
Yunoo commented 6 years ago

I am facing the same problem while using graphql 0.13.2 with apollo-server-koa 1.3.4

krishesk commented 5 years ago

Hi there,

have you figured out why yet?