gpt4 book ai didi

node.js - 循环模式给出错误为 "Error: Schema must contain unique named types but contains multiple types named "Node"."

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:48 27 4
gpt4 key购买 nike

我是使用 Nodejs 的“GraphQL”新手。我陷入了双向模式映射。帖子 <-> 作者。使用graphqlgraphql-relay模块。

以下是我们正在使用的两个架构。

--posts.js // here we are requiring authors.js 
const {
AuthorType,
schema: AuthorSchema,
AuthorsConnection
} = require('./authors');

class Post {}

const {
nodeInterface,
nodeField
} = nodeDefinitions(
globalId => {
const {
type,
id
} = fromGlobalId(globalId);
// return based on the id
return DataSource['posts'][0];
},
obj => {
console.log(" : PostType : ", PostType);
// type to be return
return Post;
}
);

const PostType = new GraphQLObjectType({
"name": "PostType",
"description": "Posts type and it's relevant fields",
"fields": () => ({
"id": globalIdField('Post'),
"title": {
"type": GraphQLString
},
"body": {
"type": GraphQLString
},
"author": {
"type": AuthorsConnection,
"resolve": (parent, argument, root, currentSdl) => {
console.log("v1, v2, v3, v4 :", parent);
if (parent.author)
return connectionFromArray(DataSource['authors'], {})
return [];
}
}
}),
isTypeOf: Post,
interfaces: [nodeInterface]
});

const {
connectionType: PostsConnection,
edgeType: GQLPostEdge
} = connectionDefinitions({
name: "Post",
nodeType: PostType
});
module.exports = exports = {
PostType,
PostsConnection,
schema: {
post: nodeField,
posts: {
type: PostsConnection,
resolve: (root, v2, v3) => {
return connectionFromArray(DataSource['posts'], {});
}
}
}
};

--authors.js // here we have required posts.js

const {
PostType,
PostsConnection
} = require('./posts');

class Author {}

const {
nodeInterface,
nodeField
} = nodeDefinitions(
globalId => {
const {
type,
id
} = fromGlobalId(globalId);
// return based on the id
return DataSource['authors'][0];
},
obj => {
console.log(" : Authorype : ", Authorype);
// type to be return
return Author;
}
);

const AuthorType = new GraphQLObjectType({
"name": "AuthorType",
"description": "Author type and it's relevant fields",
"fields": () => ({
"id": globalIdField('Author'),
"firstName": {
"type": GraphQLString
},
"lastName": {
"type": GraphQLString
},
authorPosts: {
type: PostsConnection,
resolve: (parent, args, root, context) => {
return connectionFromArray(DataSource['posts'], {});
}
}
}),
isTypeOf: null,
interfaces: [nodeInterface]
});

const {
connectionType: AuthorsConnection,
edgeType: GQLAuthorEdge
} = connectionDefinitions({
name: "Author",
nodeType: AuthorType
});

module.exports = exports = {
AuthorType,
AuthorsConnection,
schema: {
author: nodeField,
authors: {
type: AuthorsConnection,
resolve: (root, v2, v3) => {
return connectionFromArray(DataSource['authors'], {});
}
}
}
};

一旦我合并了 GraphQL 的上述架构,我就会收到以下错误。

Error: Schema must contain unique named types but contains multiple types named "Node".

我尝试调试这个问题,以下是我观察到的情况。

  • 一旦我将“作者”字段从帖子模式更改为其他模式“AuthorsConnection”开始工作。
  • 或者如果删除“作者”字段从帖子架构开始工作。

请让我知道这里出了什么问题,它与 nodeDefinitions 函数相关吗?

最佳答案

它确实与nodeDefinitions函数有关。来自 graphql-relay 文档:

nodeDefinitions returns the Node interface that objects can implement, and returns the node root field to include on the query type. To implement this, it takes a function to resolve an ID to an object, and to determine the type of a given object.

您调用了两次,这导致 Node 类型被定义两次,并且您引用了其中的一个:

schema: {
post: nodeField,

// ...

schema: {
author: nodeField,

这导致了错误 - 现在有两个无效的 Node 独立实例。

解决方案是只调用一次nodeDefinitions,然后将生成的nodeFieldnodeInterface的引用传递到相关地方。然后,您的 globalId => {...} 函数将需要查看 type 以确定如何获取相关记录,无论是作者还是帖子。

关于node.js - 循环模式给出错误为 "Error: Schema must contain unique named types but contains multiple types named "Node".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52931324/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com