错误:预期[object Object]为GraphQL类型

时间:2018-06-18 08:12:23

标签: graphql express-graphql

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

提前致谢

更新: - 在使用调试器之后,我开始明白问题出在RootQueryType.js和与之关联的文件中,因为当我尝试导出模式并且正好在查询时弹出错误:RootQueryType的地方但仍然无法查明在错误。

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

resourceType.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;

Mutation.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”)属性添加到它们的解析函数中。但是,以防万一我也为他们添加代码。

userType.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;

,另一个是

projectType.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引起的,我仍然没有解决错误但删除它似乎使错误消失,不明白为什么。

3 个答案:

答案 0 :(得分:3)

最终解决了这个问题,就像@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");

需要底部的文件

答案 1 :(得分:1)

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

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

顺便说一句,创建newGraphQLList包装器时不需要GraphQLNonNull。但这不是为什么会出现错误。

答案 2 :(得分:0)

另一种可供选择的方法是:

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

      resolve(parentValues, args, request) {
        return Project.findResources(parentValues.id);
      }
    }