【发布时间】: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)
}
})
})
});
【问题讨论】: