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

Trying to get GraphQL to work with JavaScript.Not sure where my mistake is.My codeconst graphql = req

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745336886a4623145.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信