首页 文章

Query.user的类型必须是输出类型,但得到:undefined

提问于
浏览
1

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)
            }
        })
    })
});

1 回答

  • 0
    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
    });
    

相关问题