Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not working with root resolvers #1

Open
mcollina opened this issue Nov 5, 2018 · 4 comments
Open

not working with root resolvers #1

mcollina opened this issue Nov 5, 2018 · 4 comments

Comments

@mcollina
Copy link

mcollina commented Nov 5, 2018

I'm currently trying to adjust https://github.com/mcollina/fastify-gql to use this.

However, I'm getting stuck on this problem:

{
  "data": {
    "add": null
  },
  "errors": [
    {
      "message": "Int cannot represent non-integer value: [function add]",
      "locations": [
        {
          "line": 1,
          "column": 2
        }
      ],
      "path": [
        "add"
      ]
    }
  ]
}

This is our source code:

'use strict'

const Fastify = require('fastify')
const GQL = require('.')

const app = Fastify()

const schema = `
  type Query {
    add(x: Int, y: Int): Int
  }
`

const resolvers = {
  add: async ({ x, y }) => x + y
}

app.register(GQL, {
  schema,
  resolvers,
  graphiql: true
})

app.get('/', async function (req, reply) {
  const query = '{ add(x: 2, y: 2) }'
  return reply.graphql(query)
})

app.listen(3000)

Note that attaching add to the query object works.

'use strict'

const Fastify = require('fastify')
const GQL = require('.')

const app = Fastify()

const schema = `
  type Query {
    add(x: Int, y: Int): Int
  }
`

const resolvers = {
  Query: {
    add: async (_, obj) => {
      const { x, y } = obj
      return x + y
    }
  }
}

app.register(GQL, {
  schema,
  resolvers,
  graphiql: true
})

app.get('/', async function (req, reply) {
  const query = '{ add(x: 2, y: 2) }'
  return reply.graphql(query)
})

app.listen(3000)

Our integration lives here: https://github.com/mcollina/fastify-gql/tree/jit.

@ruiaraujo
Copy link
Collaborator

@mcollina The first option is expected not to work since we follow https://www.apollographql.com/docs/graphql-tools/resolvers.html.

However there should have been a compiling error message.
So that is one improvement.

Another QoS improvement we could do, is a normalization for a Query only schema where the root resolvers are wrapped in the Query object. Does that sound good?

@mcollina
Copy link
Author

@ruiaraujo the first option is still valid usage of GraphQL.js - the GraphQL engine supports that. Is the goal of this library only to be compatible with Apollo? Note that most of the examples in https://graphql.org/graphql-js/basic-types/ use async functions in the root value.

IMHO giving a clear validation error when something is not supported would be of great help, however in this case I think this feature is valid GraphQL and should be supported. i would not normalize, as it might have some side effects.

@ruiaraujo
Copy link
Collaborator

That usage is not a resolver, it is using the root object and the default field resolver will call those functions. This does not work with the compiler since the compiler relies on the resolver presence to compile the function.

@ruiaraujo
Copy link
Collaborator

I will fix the compiler error in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants