gpt4 book ai didi

javascript - 如何在模式中嵌套两个 graphQL 查询?

转载 作者:搜寻专家 更新时间:2023-11-01 04:35:06 24 4
gpt4 key购买 nike

我创建了一个包含两个字段的 GraphQLSchema,这两个字段都使用 resolve() 从 mongoDB 获取数据。

这样,查询...

{
article(id: "Dn59y87PGhkJXpaiZ") {
title
},
articleContent(id: "Dn59y87PGhkJXpaiZ") {
_id,
content(language: "en"),
type
}
}

...结果:

{
"data": {
"article": {
"title": "Sample Article"
},
"articleContent": [
{
"_id": "Kho2N8yip3uWj7Cib",
"content": "group",
"type": "group"
},
{
"_id": "mFopAj4jQQuGAJoAH",
"content": "paragraph",
"type": null
}
]
}
}

但我需要这样的结果结构(内容应该在文章对象内):

预期结果

{
"data": {
"article": {
"title": "Sample Article",
"content": [
{
"_id": "Kho2N8yip3uWj7Cib",
"content": "group",
"type": "group"
},
{
"_id": "mFopAj4jQQuGAJoAH",
"content": "paragraph",
"type": null
}
]
},
}
}

对我来说,问题是异步 mongoDB 在我的模式中解析:

export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {

article: {
type: new GraphQLObjectType({
name: 'article',
fields: {
title: {
type: GraphQLString,
resolve (parent) {
return parent.title
}
}
}
}),
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
async resolve ({ db }, { id }) {
return db.collection('content').findOne({ _id: id })
}
},

articleContent: {
type: new GraphQLList(new GraphQLObjectType({
name: 'articleContent',
fields: {
_id: { type: GraphQLID },
type: { type: GraphQLString },
content: {
type: GraphQLString,
args: {
language: { type: new GraphQLNonNull(GraphQLString) }
},
resolve (parent, { language }, context) {
return parent.content[language][0].content
}
}
}
})),
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
async resolve ({ db }, { id }) {
return db.collection('content').find({ main: id }).toArray()
}
}
}
})
})

更新

如果我将内容嵌套在文章中,我会得到错误 Cannot read property 'collection' of undefined

export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {

article: {
type: new GraphQLObjectType({
name: 'article',
fields: {
title: {
type: GraphQLString,
resolve (parent) {
return parent.title
}
},
articleContent: {
type: new GraphQLList(new GraphQLObjectType({
name: 'articleContent',
fields: {
_id: { type: GraphQLID },
type: { type: GraphQLString },
content: {
type: GraphQLString,
args: {
language: { type: new GraphQLNonNull(GraphQLString) }
},
resolve (parent, { language }, context) {
return parent.content[language][0].content
}
}
}
})),
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
async resolve ({ db }, { id }) { // db is undefined here!!
return db.collection('content').find({ main: id }).toArray()
}
}
}
}),
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
async resolve ({ db }, { id }) {
return db.collection('content').findOne({ _id: id })
}
}
}
})
})

最佳答案

首先,让我们分析一下解析器的签名。

function resolve(root, args, context)

root 是父解析器返回的值。这就是您得到 Cannot read property 'collection' of undefined 的原因,因为父解析器没有返回具有 db 属性的对象。

args 是传递给字段的参数,如下所示:article(id:'someid') 在编写查询时。

context 是传递给每个解析器的参数,主要用于制作可访问的 API 范围的实用程序,例如 db 连接。

要在您的上下文中设置 db,您可以使用它来初始化您的 GraphQL 服务器。

app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
context: {
db: db
},
graphiql: true,
}));

关于现在的嵌套,你可以有这样的东西。

export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
article: {
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
resolve (_, { id }) {
return id; // will make it accessible to children resolvers
}
type: new GraphQLObjectType({
name: 'article',
fields: {
title: {
async resolve (id /* resolved by article */, _, { db } /* db from context */) {
const article = await db.collection('content').findOne({ _id: id });
return article.title;
}
type: GraphQLString,
},
content: {
async resolve (id /* resolved by article */, _, { db } /* db from context */) {
const contents = await db.collection('content').find({ main: id }).toArray();
return contents;
}
type: new GraphQLList(new GraphQLObjectType({
name: 'articleContent',
fields: {
_id: { type: GraphQLID },
type: { type: GraphQLString },
content: {
args: {
language: { type: new GraphQLNonNull(GraphQLString) }
},
aync resolve (parent /* resolved in content */, { language }) {
return parent.content[language][0].content
}
type: GraphQLString,
}
}
})),
}
}
}),
}
}
})
})

按顺序,这将发生:

  • article 获取其参数 id 并将其返回,将其提供给子解析器。

  • title 和 outer content 将同时触发它们的请求,访问 context 中的 db

  • 当外部内容从数据库返回时,每个元素的内部内容字段将使用它们的参数 language 返回正确的结果。

关于javascript - 如何在模式中嵌套两个 graphQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46929327/

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