1

According to

http://graphql.org/graphql-js/authentication-and-express-middleware/

To use middleware with a GraphQL resolver, just use the middleware like you would with a normal Express app. The request object is then available as the second argument in any resolver.

However, when I run my resolver

module.exports = {
  Query: {
    books(root, args, context) {
      return books;
    }
  }
};

the second argument is my query arguments. The third argument, however, unless I override the context config property to expressGraphql is indeed my request object.

My full config is

app.use(
  "/graphql",
  expressGraphql({
    schema,
    graphiql: true
  })
);

Are the docs wrong, or am I doing something incorrectly?

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393

1 Answers1

3

Neither :)

Even when building your schema with plain ole' GraphQL.js (as opposed to using graphql-tools or another library), there's a couple of different ways to pass in your resolvers.

You can generate it by creating a GraphQLSchema object, for example, in which case your resolvers are included within the GraphQLObjectTypes you add to it. Here is a repo that does just that.

Alternatively, you can declare a schema using a string with buildSchema. You can see that being done in the docs. If you go this route, the only way to pass in your resolvers is to utilize the rootValue object passed into your endpoint configuration. Normally, the values parameters passed to your resolvers are

  1. root
  2. args
  3. context
  4. info

However, when you go the above route, you lose the first parameter... since I imagine we can't pass root to itself

It's a good illustration of why, if you're going to generate a schema declaratively, graphql-tools is the way to go.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • Oh man thank you so much - this actually a few other points of confusion I had. So I was passing in my resolvers this way, from `graphql-tools` https://github.com/arackaf/booklist/blob/feature/add-graphQL/schema.js#L16 Are you saying there's another route I could take, which does not have my schema as a string, and would affect my resolver args? So if someone goes that route, what _is_ the value of root? I can still specify it, docs mentioned "root resolver" but I didn't quite grok what that meant - I assumed it was for static global helpers or something :-P – Adam Rackis Sep 20 '17 at 17:13
  • 1
    I'm fairly sure the **only** time you'll see that first parameter drop is when you define your resolvers inside the `rootValue`. From what I understand, `rootValue` is a leftover from before `context` was available -- there's limited use for it nowadays... except if you choose to use `buildSchema`, in which case it's the only way you can pass in resolvers. [Example here](http://graphql.org/graphql-js/running-an-express-graphql-server/). If you're already using `graphql-tools`, you probably don't need to mess with rootValue. – Daniel Rearden Sep 20 '17 at 20:12
  • Edited answer with a couple of example links. – Daniel Rearden Sep 20 '17 at 20:14