Тип Query.user должен быть Тип вывода, но получил: undefined - PullRequest
0 голосов
/ 08 июня 2018

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 голосов
/ 13 июня 2018
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
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...