【问题标题】:The type of Query.user must be Output Type but got: undefinedQuery.user 的类型必须是输出类型但得到:未定义
【发布时间】:2018-11-18 18:39:47
【问题描述】:

http://localhost:4000/ 当我作为输入 { user(id: 1) { firstName } }

我得到输出 { "errors": [ { "message": "The type of Query.user must be Output Type but got: undefined.\n\nThe type of Query.user(id:) must be Input Type but got: undefined." } ] }

我第一次做这个 Graphql,我不确定我在哪里做错了,我看到很多例子,但如果我这样做,我会遇到很多其他错误。我试图从类型更改 InputType,但仍然存在问题。 server.js

var express = require('express'),
app = express(),
port = process.env.PORT || 4000;
var graphQLHTTP = require('express-graphql');
var schema = require('./schema');
app.use(graphQLHTTP({
   schema,
   graphiql:true,}))
app.listen(port);

schema.js 下面

const {
GrpahQLString,
GrpahQLInt,
GraphQLSchema,
GraphQLInputObjectType,
GraphQLObjectType,
GraphQLOutputType
} = require('graphql');
const fetch = require('node-fetch');
const BASE_URL = 'http://localhost:3000';
const UserType = new GraphQLInputObjectType({
    name : 'user',
    description : "...",
    fields: () => ({
    id: {
        inputType : GrpahQLInt,
    //      resolve: (user) => user.id,
    },
    firstName: {
        inputType : GrpahQLString,
//       resolve: (user) => user.firstname,
        },
    lastName: {
        inputType : GrpahQLString,
//       resolve: (user) => user.lastname,
    },
    email: {
        inputType : GrpahQLString,
//       resolve: (user) => user.email,
        }
    })
});
module.exports = new GraphQLSchema({
    query: new GraphQLObjectType({
    name : 'Query',
    fields: () => ({
        user : {
            inputType: UserType,
            args: {
            id: {type: GrpahQLInt}
            },
            resolve : (root ,args) =>
                fetch(`${BASE_URL}/users/${args.id}/`).then(res=>res.json()).then(json=> json.user)
            }
        })
    })
});

【问题讨论】:

    标签: node.js graphql


    【解决方案1】:
    var graphql = require('graphql');
    const fetch = require('node-fetch');
    
    const BASE_URL = 'http://localhost:3000';
    
    
    const userType = new graphql.GraphQLObjectType({
    name : 'user',
    description : "...",
    fields: {
       id: {
        type : graphql.GraphQLInt,
        },
        firstName: {
        type : graphql.GraphQLString,
        resolve: (user) => user.firstname,
        },
        lastName: {
        type : graphql.GraphQLString,
        resolve: (user) => user.lasttname,
        },
        email: {
        type : graphql.GraphQLString,
        resolve: (user) => user.email,
        },
    }
    });
    
    const query1 = new graphql.GraphQLObjectType({
    name : 'Query',
    fields: {
        user : {
          type : userType,
          args : {
              id: { type: graphql.GraphQLInt}
          },
          resolve : (root ,{id}) =>
              fetch(`${BASE_URL}/users/1/`).then(res =>res.json()).then(json=> json.user)
          }
        }
     });
    
    
    module.exports.default = new graphql.GraphQLSchema({
        query : query1
    });
    

    【讨论】:

    • 我对你自己的答案投了反对票,主要是因为你没有解释,甚至一句话都没有解释你的第一个代码有什么问题(即使对于有更多经验的人来说已经足够清楚了)。这样一来,您的回答对任何未来寻找解决自己问题的解决方案的用户都没有帮助,这可能是相似的。这个网站是关于一个开发者互相帮助的社区;我建议您编辑您的答案并解释您自己的代码中有什么问题。很高兴之后重新投票。
    猜你喜欢
    • 2018-06-30
    • 2016-06-29
    • 2017-12-12
    • 2018-12-18
    • 2020-05-31
    • 2019-12-23
    • 2019-09-19
    • 2017-08-16
    • 2017-09-03
    相关资源
    最近更新 更多