首页 > 解决方案 > GraphQL NodeJS 错误:提供的用于构建模式的类型之一缺少名称

问题描述

我正在使用 NodeJS 学习 GraphQL,但出现错误。我已经开发了一个用于理解和学习目的的 mongoDB 和 NodeJS GraphQL REST API

在此处输入图像描述

这是我的代码:

const graphql = require('graphql');
const user = require('../models/User');
const post = require('../models/Post');
const { GraphQLObjectType, GraphQLID, GraphQLString } = graphql;

const UserType = new GraphQLObjectType({
    name: 'User',
    fields: () => ({
        id: { type: GraphQLID },
        fname: { type: GraphQLString },
        lname: { type: GraphQLString },
        email: { type: GraphQLString },
        posts: {
            type: PostType,
            resolve(parent, args) {
                return 1;
            }
        }
    })
});

const PostType = new GraphQLObjectType({
    name: 'Post',
    fields: () => ({
        id: { type: GraphQLID },
        Userid: { type: GraphQLID },
        title: { type: GraphQLString },
        date: { type: GraphQLString },
    })
});

const RootQueryType = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        user: {
            type: UserType,
            args: { id: { type: GraphQLID } },
            resolve(parent, args) {
                return user.findById({ id: args.id });
            }
        },
        post: {
            type: PostType,
            args: { id: { type: GraphQLID } },
            resolve(parent, args) {
                return post.findById({ id: args.id });
            }
        }
    }
});

module.exports = new graphql.GraphQLSchema({
    query: RootQueryType,
});

请帮助我找到解决方案。回答我以解决此错误。

标签: javascriptnode.jsmongodbgraphqlgraphql-schema

解决方案


推荐阅读