我对javascript相当陌生,目前正在学习使用Node.js在MongoDB后端实现graphQL API。我遇到了两种类型之间循环依赖的问题。
基本上,我有一个经典的博客帖子/博客作者情况。帖子只有一位作者,因此 Mongoose 模式拥有对该作者的引用。
在我的graphQL类型“作者”中,我想添加一个字段“帖子”,该字段使我可以从作者导航到他们撰写的所有帖子。该引用未在数据库模型中编码,而是通过 Controller 进行检索。这是我的博客邮政编码。
var graphql = require("graphql");
var AuthorResolvers = require("../resolvers/author");
var PostResolvers = require("../resolvers/post");
var AuthorType = require("./author").AuthorType;
var PostType = new graphql.GraphQLObjectType({
name: "PostType",
fields: {
_id: {
type: graphql.GraphQLID
},
title: {
type: graphql.GraphQLString
},
author: {
type: AuthorType,
description: "Author of this post",
resolve: (post) => AuthorResolvers.retwArgs(post)
}
}
});
module.exports = {PostType};
resolvers.js文件仅导出函数以寻址 Controller 。
我的作者类型定义如下:
var graphql = require ("graphql");
var AuthorResolvers = require("../resolvers/author");
var PostResolvers = require("../resolvers/post");
var AuthorType = new graphql.GraphQLObjectType({
name: "AuthorType",
fields: () => {
var PostType = require("./post");
return {
_id: {
type: graphql.GraphQLID
},
name: {
type: graphql.GraphQLString
},
posts: {
type: new graphql.GraphQLList(PostType),
description: "Posts written by this author",
resolve: (author) => PostResolvers.retwArgs(author)
}
}
}
});
我已经在这里尝试了两件事:
当我尝试运行此服务器示例时,出现以下错误:
Can only create List of a GraphQLType but got: [object Object].
指向那条线
type: new graphql.GraphQLList(PostType)
在作者中。
包含上面发布的类型定义的文件也将导出查询,如下所示:
var PostQuery = {
posts: {
type: new graphql.GraphQLList(PostType),
description: "Posts of this Blog",
resolve: PostResolvers.ret
}
};
对于帖子,像这样
var AuthorQuery = {
authors: {
type: new graphql.GraphQLList(AuthorType),
description: "The authors working on this Blog",
resolve: AuthorResolvers.ret
}
};
分别为作者。一切都聚集在一个Schema文件中,如下所示:
var graphql = require("graphql");
var Author = require("./types/author").AuthorQuery;
var Post = require("./types/post").PostQuery;
var root_query = new graphql.GraphQLObjectType({
name: "root_query",
fields: {
posts: Post.posts,
authors: Author.authors
}
});
module.exports = new graphql.GraphQLSchema({
query: root_query
});
最后是服务器:
var graphql = require ('graphql').graphql
var express = require('express')
var graphQLHTTP = require('express-graphql')
var Schema = require('./api/schema')
var app = express()
.use("/", graphQLHTTP(
{
schema: Schema,
pretty: true,
graphiql: true,
}
))
.listen(4000, function(err) {
console.log('Running a GraphQL API server at localhost:4000');
})
我真的看不到任何解决这种循环依赖的方法。如果我只是在AuthorType定义中注释掉对PostType的引用,则服务器将启动而不会出现问题。在这里的任何帮助将不胜感激。
也许是为了更好的理解。目录结构如下所示:
│ package.json
│ server.js
│
├───api
│ │ schema.js
│ │
│ ├───controllers
│ │ author.js
│ │ post.js
│ │
│ ├───models
│ │ author.js
│ │ post.js
│ │
│ ├───resolvers
│ │ author.js
│ │ post.js
│ │
│ └───types
│ author.js
│ post.js
│
└───config
db.js
最佳答案
这是模块循环依赖的典型问题-您可以引用How to deal with cyclic dependencies in Node.js这个问题。在这种情况下,它与GraphQL无关。
而且,您执行module.exports = { PostType }
,但是在AuthorType
中执行var PostType = require('./post')
,不应该是var PostType = require('./post').PostType
吗?我想这是导致以下错误的原因:Can only create List of a GraphQLType but got: [object Object].
因为您的PostType
现在是{ PostType: ... }
,而不是GraphQLObjectType
实例。