最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Type RootQueryType must define one or more fields." } - Stack Overflow

programmeradmin0浏览0评论

Trying to get GraphQL to work with JavaScript. Not sure where my mistake is.

My code

const graphql = require('graphql');
const _ = require('lodash');
const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLSchema
} = graphql;
const users = [
  { id: "23", firstName: "Bill", age: 20},
  { id: "47", firstName: "Sam", age: 21}
];
const UserType = new GraphQLObjectType({
  name: 'User',
  fields: {
    id: {type: GraphQLString},
    firstName: {type: GraphQLString},
    age:{type: GraphQLInt}
  }
});
const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    user: {
      type: UserType,
      args: { id: { type: GraphQLString } },
      resolve(parentValue, args) {
         return _.find(users, { id: args.id });
      }
    }
  }
});
module.exports = new GraphQLSchema ({
  query: RootQuery
});

I am getting

{ "errors": [ { "message": "Type RootQueryType must define one or more fields." } ] }

Why is it not working?

Trying to get GraphQL to work with JavaScript. Not sure where my mistake is.

My code

const graphql = require('graphql');
const _ = require('lodash');
const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLSchema
} = graphql;
const users = [
  { id: "23", firstName: "Bill", age: 20},
  { id: "47", firstName: "Sam", age: 21}
];
const UserType = new GraphQLObjectType({
  name: 'User',
  fields: {
    id: {type: GraphQLString},
    firstName: {type: GraphQLString},
    age:{type: GraphQLInt}
  }
});
const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    user: {
      type: UserType,
      args: { id: { type: GraphQLString } },
      resolve(parentValue, args) {
         return _.find(users, { id: args.id });
      }
    }
  }
});
module.exports = new GraphQLSchema ({
  query: RootQuery
});

I am getting

{ "errors": [ { "message": "Type RootQueryType must define one or more fields." } ] }

Why is it not working?

Share Improve this question asked Apr 7, 2018 at 18:40 Nicole MarieNicole Marie 1312 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

you forgot to use an arrow function

const UserType = new GraphQLObjectType({
  name: 'User',
  fields:()=>( {
    id: {type: GraphQLString},
    firstName: {type: GraphQLString},
    age:{type: GraphQLInt}

});

I believe your mistake is simply in your Query. You use the RootQueryType's fields object to make your query endpoints. The fields object in your case only contains one query: user. However you are trying to make a query for User, which is different.

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    // The items listed here are going to be your root query endpoints.
    // Which in this case is only `user`.
    user: {
      type: UserType,
      args: { id: { type: GraphQLString } },
      resolve(parentValue, args) {
         return _.find(users, { id: args.id });
      }
    }
  }
});

So you need to make your query using user.

Additionally, you need to make sure you are making your query correctly. Basic query syntax for what you are trying to achieve looks like this:

{
  user(id: "23") {
    id
    firstName
    age
  }
}

Let me know if this works for you.


Some documentation on queries:

GraphQL Queries

DevHints - GraphQL

发布评论

评论列表(0)

  1. 暂无评论