【问题标题】:GraphQL, incorrect syntax for query using GraphiQLGraphQL,使用 GraphiQL 的查询语法不正确
【发布时间】:2018-05-25 12:32:11
【问题描述】:

GraphQL 新手,我在通过 GraphiQL 发送查询时遇到问题。

这是我的 schema.js

const graphql = require('graphql');
const _ = require('lodash');
const{
    GraphQLObjectType,
    GraphQLInt,
    GraphQLString,
    GraphQLSchema
} = graphql;

const users = [
    {id:'23', firstName:'Bill', age:20},
    {id:'47', firstName:'Samantha', 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){ // move the resolve function to here
                return _.find(users, {id: args.id});
            }
        },

    }
});

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

当我在 graphiql 上运行以下查询时:

query {
  user {
    id: "23"
  }
}

我收到“语法错误 GraphQL 请求 (3:9) 预期名称,找到字符串”,但我希望得到 id 为“23”的用户。

我错过了什么?

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    你将你的参数值放在你的选择上,它应该看起来像

    query {
      user(id: “23”) {
        id
        firstName
        age
      }
    }
    

    请查看教程http://graphql.org/graphql-js/passing-arguments/的参数部分

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2021-11-18
      • 2020-06-09
      • 2017-03-19
      • 2021-12-09
      • 1970-01-01
      • 2022-12-30
      • 2018-06-28
      • 1970-01-01
      相关资源
      最近更新 更多