0

在我将资源部分添加到代码中之前,此代码一直有效。它与其他两个相似,所以我不确定为什么它不起作用。

提前致谢

更新: - 使用调试器后,我开始明白问题出在 RootQueryType.js 和与之关联的文件中,因为当我尝试导出模式时弹出错误并且恰好在 query:RootQueryType 位置但仍然可以't 指出错误。

更新:-我也将 schema.js 文件放在最后

资源类型.js

const graphql = require("graphql");
const UserType = require("./userType");
const User = require("../models/User");
const Project = require("../models/Project");
const Resource = require("../models/Resource");
const ProjectType = require("./projectType");

const {
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
  } = graphql;


  const ResourceType = new GraphQLObjectType({
    name: "ResourceType",
    fields: () => ({
      id: { type: GraphQLString },
      title: { type: GraphQLString },
      url: { type: GraphQLString },
      project:{
        type:ProjectType,
        resolve(parentValues,args){
          return Project.findById(parentValues.id).populate("resources")
        }
      }
    })
  });

  module.exports=ResourceType;

RootQueryType.js

    const mongoose = require('mongoose');
const graphql = require('graphql');
const { GraphQLObjectType, GraphQLList, GraphQLID, GraphQLNonNull } = graphql;

const ProjectType  = require('./../types/projectType');
const UserType  = require('./../types/userType');
const Project=require("../models/Project");
const User=require("../models/User");

 const RootQuery=new GraphQLObjectType({
    name:"RootQueryType",
    fields: () => ({
        projects:{
            type:new GraphQLList(ProjectType),
            resolve(parentValues,args,request){
                return Project.find().populate("contributors").populate("resources");
            }
        },
        project:{
            type:ProjectType,
            args:{id:{type:new GraphQLNonNull(GraphQLID)}},
            resolve(parentValue,args,request){
                return Project.findById(args.id).populate("contributors").populate("resources");
            }
        },
        users:{
            type:new GraphQLList(UserType),
            resolve(parentValues,args,request){
                return User.find().populate("projects");
            }
        },
        user:{
            type:UserType,
            args:{id:{type:new GraphQLNonNull(GraphQLID)}},
            resolve(parentValue,args,request){
                return User.findById(args.id).populate("projects")
            }
        },

    })
})

module.exports = RootQuery;

突变.js

    const mongoose = require("mongoose");
const ProjectType = require("./../types/projectType");
const UserType = require("./../types/userType");
const graphql = require("graphql");
const {
  GraphQLObjectType,
  GraphQLList,
  GraphQLID,
  GraphQLNonNull,
  GraphQLString
} = graphql;

const Project = require("../models/Project");
const User = require("../models/User");

const mutation = new GraphQLObjectType({
  name: "mutation",
  fields: () => ({
    addUser: {
      type: UserType,
      args: {
        name: { type: new GraphQLNonNull(GraphQLString) },
        username: { type: new GraphQLNonNull(GraphQLString) },
        password: { type: new GraphQLNonNull(GraphQLString) },
        email: { type: new GraphQLNonNull(GraphQLString) },
        githubProfile: { type: new GraphQLNonNull(GraphQLString) }
      },
      resolve(parentValues, args, request) {
        return User.create(args);
      }
    },
    addProject: {
      type: ProjectType,
      args: {
        name: { type: new GraphQLNonNull(GraphQLString) },
        description: { type: new GraphQLNonNull(GraphQLString) },
        image: { type:GraphQLString },
        // contributor:{ type: new GraphQLNonNull(new GraphQLList(GraphQLID))
        contributor:{ type: new GraphQLNonNull(GraphQLID)
         },
      },
      resolve(parentValues, args, request) {
        return Project.create(args);
      }
    }
  })
});

module.exports = mutation;

我很肯定这不是其他类型的问题,因为它们工作得更早,我只将 .populate("resources") 属性添加到它们的解析函数中。但以防万一我也为他们添加代码。

用户类型.js

const graphql = require("graphql");
const ProjectType = require("./projectType");

const { GraphQLObjectType, GraphQLString, GraphQLList } = graphql;

const UserType = new GraphQLObjectType({
  name: "User",
  fields: () => ({
    id: { type: GraphQLString },
    name: { type: GraphQLString },
    githubProfile: { type: GraphQLString },
    username: { type: GraphQLString },
    password: { type: GraphQLString },
    email: { type: GraphQLString },
    projects: {
      type: new GraphQLList(ProjectType),
      resolve(parentValues, args) {
        return User.findById(parentValues.id).populate("projects");
      }
    }
  })
});

module.exports = UserType;

另一个是

项目类型.js

const graphql = require("graphql");
const UserType = require("./userType");
const ResourceType = require("./resourceType");
const User = require("../models/User");
const Project = require("../models/Project");

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLList,
} = graphql;

const ProjectType = new GraphQLObjectType({
  name: "ProjectType",
  fields: () => ({
    id: { type: GraphQLString },
    name: { type: GraphQLString },
    description: { type: GraphQLString },
    image: { type: GraphQLString },
    contributors: {
      type: new GraphQLList(UserType),
      resolve(parentValues, args, request) {
        return Project.findContributors(parentValues.id);
      }
    },
    resources:{
      type: new GraphQLList(ResourceType),
      resolve(parentValues, args, request) {
        return Project.findResources(parentValues.id);
      }
    }
  })
});

module.exports=ProjectType;

schema.js

const graphql = require("graphql");
const RootQuery = require("./RootQueryType");
const Mutation = require("./Mutation");

const { GraphQLSchema } = graphql;

console.log(RootQuery,Mutation);

module.exports = new GraphQLSchema({
  query: RootQuery,
  mutation: Mutation
});

更新:-我已删除资源文件,但仍然收到相同的错误,因此它必须在用户和项目类型之间

更新:-我终于找到了项目和用户类型的类型文件中的错误,它是由新的 GraphQLList 引起的,我仍然没有解决错误,但删除它似乎使错误消失,不知道为什么。

4

3 回答 3

6

终于解决了这个问题,正如@cito所说,由于循环依赖是导致错误的原因,也就是说我的 UserType 依赖于 ProjectType 并且同样地,因此我得到了这个错误,这被解决了

const graphql = require("graphql");

const User = require("../models/User");
const Project = require("../models/Project");

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLList,
} = graphql;

const ProjectType = new GraphQLObjectType({
  name: "ProjectType",
  fields: () => ({
    id: { type: GraphQLString },
    name: { type: GraphQLString },
    description: { type: GraphQLString },
    image: { type: GraphQLString },
    contributors: {
      type: new GraphQLList(UserType),
      resolve(parentValues, args, request) {
        return Project.findContributors(parentValues.id);
      }
    },
    resources:{
      type: new GraphQLList(ResourceType),
      resolve(parentValues, args, request) {
        return Project.findResources(parentValues.id);
      }
    }
  })
});

module.exports=ProjectType;



// This is here to prevent circular dependencies problem which will lead to the formation of infinite loop

const UserType = require("./userType");
const ResourceType = require("./resourceType");

这需要底部的文件

于 2018-06-23T13:50:19.137 回答
2

另一种方法是:

resources:{
      // require directly without creating a new variable
      type: new GraphQLList(require("./resourceType")),

      resolve(parentValues, args, request) {
        return Project.findResources(parentValues.id);
      }
    }
于 2019-04-11T05:38:09.113 回答
2

您的问题是userTypeandprojectType模块之间的循环依赖关系。因此,当传递给 const 值时,它仍然是一个空对象GraphQLList

作为一种解决方案,您可以将所有类型移动到一个模块中。或者,在导出类时,将它们设置为module.exports. 这将起作用,因为您将字段正确定义为 thunk。

new顺便说一句,创建GraphQLListGraphQLNonNull包装器时不需要。但这不是您收到错误的原因。

于 2018-06-23T13:21:12.807 回答