Search code examples
javascriptnode.jsgraphqlexpress-graphql

How to attach variables to the Graphql context?


I am using express-graphql. I want to attach some params to the context, but they don't attach.

Middlware:

  app.use(graphqlHTTP({
  schema: schema,
  rootValue: resolver,
  graphiql: true,
  context: {someValue: 100}
}))

Schema:

type Query {
  get_number: Int!
}

Resolver:

get_number(_, __, context) {
 console.log(context);
 return context.someValue;
}

When I try to call get_number it says:

    {
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Query.get_number.",
      "locations": [
        {
          "line": 2,
          "column": 2
        }
      ],
      "path": [
        "get_number"
      ]
    }
  ],
  "data": null
}

Here's what console.log(context) says:

{
  fieldName: 'get_number',
  fieldNodes: [
    {
      kind: 'Field',
      alias: undefined,
      name: [Object],
      arguments: [],
      directives: [],
      selectionSet: undefined,
      loc: [Object]
    }
  ],
  returnType: Int!,
  parentType: Query,
  path: { prev: undefined, key: 'get_number' },
  schema: GraphQLSchema {
    __validationErrors: [],
    __allowedLegacyNames: [],
    _queryType: Query,
    _mutationType: Mutation,
    _subscriptionType: undefined,
    _directives: [ @skip, @include, @deprecated ],
    astNode: undefined,
    extensionASTNodes: undefined,
    _typeMap: [Object: null prototype] {
      Query: Query,
      TestType: TestType,
      Int: Int,
      User: User,
      String: String,
      Float: Float,
      ClassMatein: ClassMatein,
      ClassMate: ClassMate,
      Mutation: Mutation,
      UserInput: UserInput,
      __Schema: __Schema,
      __Type: __Type,
      __TypeKind: __TypeKind,
      Boolean: Boolean,
      __Field: __Field,
      __InputValue: __InputValue,
      __EnumValue: __EnumValue,
      __Directive: __Directive,
      __DirectiveLocation: __DirectiveLocation
    },
    _possibleTypeMap: [Object: null prototype] {},
    _implementations: [Object: null prototype] {}
  },
  fragments: [Object: null prototype] {},
  rootValue: {
    test: [Function: test],
    random: [Function: random],
    addTestUser: [Function: addTestUser],
    get_mate: [Function: get_mate],
    get_number: [Function: get_number]
  },
  operation: {
    kind: 'OperationDefinition',
    operation: 'query',
    name: undefined,
    variableDefinitions: [],
    directives: [],
    selectionSet: { kind: 'SelectionSet', selections: [Array], loc: [Object] },
    loc: { start: 0, end: 21 }
  },
  variableValues: {}
}

My schema:

const {
  buildSchema
} = require('graphql')
const gql = require('graphql-tag');

module.exports = buildSchema(`
  type User {
    name: String!
    email: String!
    age: Int!
  }

I guess my someValue should be in variableValues, but it's not there:( What should I do to attach this variable(someValue) to the context? Thanks in advance.


Solution

  • The problem is that you are passing your resolvers object as the rootValue. Instead, you should put them in your schema, e.g. using makeExecutableSchema.

    A resolver takes four arguments: parent object, field arguments, context, and resolve info.

    If there is no resolver for a field in the schema, GraphQL falls back to simply accessing the property with the field name on the parent object (which is the rootValue for fields on the root Query type). If the property is a method, it invokes the method (i.e. this = parent object) with three arguments: field arguments, context , and resolve info.