A
A
Andrey Pike2020-09-04 19:06:19
JavaScript
Andrey Pike, 2020-09-04 19:06:19

GraphQL: Cause of "Query fields must be an object..." error?

Started learning GraphQL.
But either a lot of time has passed since May 24, 2019, or ... I don’t know.
The author's example is reprinted one to one, but he transpiles without problems, and I have a complete plug.

The swearing is: Query fields must be an object with field names as keys or a function which returns such an object. And everything works for the author of the video .

app.js:

const express = require('express');
const { graphqlHTTP } = require('express-graphql'); // кстати, тут в варианте автора тоже не работает, т.к. сейчас импорт из express-graphql надо декомпозировать, сейчас там экспортируется не функция, а объект
const schema = require('../schema/schema');

const app = express();
const PORT = 3005;

app.use('/graphql', graphqlHTTP({
  schema,
  graphiql: true,
}));

app.listen(PORT, err => {
  err ? console.log(err) : console.log('Server started!');
});


schema.js:
const { GraphQLObjectType, GraphQLString, GraphQLSchema } = require('graphql');

const MovieType = new GraphQLObjectType({
  name: 'Movie',
  fields: () => ({
    id: { type: GraphQLString },
    name: { type: GraphQLString },
    genre: { type: GraphQLString },
  }),
});

const Query = new GraphQLObjectType({
  name: 'Query',
  movie: {
    type: MovieType,
    args: { id: { type: GraphQLString } },
    resolve(parent, args) {
    },
  },
});

module.exports = new GraphQLSchema({
  query: Query,
});

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
carter234523, 2021-01-09
@carter234523

Write like this, it should work.

const Query = new GraphQLObjectType({
  name: 'Query',
  fields: {
    movie: {
      type: MovieType,
      args: {
        id: { type: GraphQLString },
      },
      resolve(parent, args) {},
    },
  },
});

S
Sergey, 2021-02-09
@SergeyKA007

just watch the lesson a little further)

R
romy4, 2020-09-10
@romy4

"Query fields must be an object with field name". So you don't have a fields property in the Query object. What is movie it does not understand and is unlikely to understand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question