gpt4 book ai didi

graphql - 未调用嵌套的 GraphQL 解析器

转载 作者:行者123 更新时间:2023-12-03 22:25:23 24 4
gpt4 key购买 nike

我的 GraphQL/Sequelize 源代码还有一个问题。似乎我的解析器在运行时没有被使用。目前,似乎为顶级输出类型 (Page) 调用了解析器,但似乎没有为第二个输出类型 (Content) 调用解析器。
前端发送的查询:

query {
pages (hidden: false) { // this resolver is called (Page)
id
label
paragraphs (hidden: false) { // but not this one... (Content)
id
heading
text
}
}
}
查询包定义:
import pageFields from './fields/page';
import contentFields from './fields/content';

const QueryBundle = new GraphQLObjectType({
name: 'Query',
description: 'This is the root query',
fields: () => {
return {
pages: pageFields,
paragraphs: contentFields
};
}
});
pageFields 文件:
import Page from '../types/page';
import PageParagraph from '../inputs/content';
import db from '../db';

// This is the Page's fields for the QueryBundle definition...
const pageFields = {
type: new GraphQLList(Page),
args: {
id: {
type: GraphQLInt
},
label: {
type: GraphQLString
},
hidden: {
type: GraphQLBoolean
},
paragraphs: {
type: new GraphQLList(PageParagraph)
}
},
async resolve(parent, args) {
return await db.models.page.findAll({
include: [{
all: true,
nested: true
}],
where: args
});
}
};

export default pageFields;
注意:这个解析器将被调用,GraphiQL 工具和终端都会显示一个 SELECT 查询...
内容字段文件:
import Content from '../types/content';
import db from '../db';

// This is the Content's fields for the QueryBundle definition...
const contentFields = {
type: new GraphQLList(Content),
args: {
id: {
type: GraphQLInt
},
heading: {
type: GraphQLString
},
text: {
type: GraphQLString
},
hidden: {
type: GraphQLBoolean
}
},
async resolve(parent, args) {
return await db.models.content.findAll({
include: [{
all: true,
nested: true
}],
where: args
});
}
};

export default contentFields;
注意:但是这个从来没有被调用过,怎么来的?我在查询中使用的任何参数都将被忽略,因为它永远不会到达这一点......
解决方案:
...

// This is the Sequelize model definition (output type) of the Page table...
const Page = new GraphQLObjectType({
name: 'Page',
description: 'This represents a Page',
fields: () => {
return {
id: {
type: GraphQLInt,
resolve(page) {
return page.id;
}
},
...
paragraphs: {
args: {// <== This is new! Here we add the arguments...
hidden: {
type: GraphQLBoolean
},
box: {
type: GraphQLBoolean
}
},
type: new GraphQLList(Paragraph), // <== Unchanged
resolve(parent, args, {pagesParagraphsLoader}, info) {// <== This is new! Here we needed a resolver for the OUTPUT type... Same goes for every nested type...
const data = {
id: parent.id,
args: args
};
return pagesParagraphsLoader.load(data);
}
}
};
}
});

export default Page;
Github 包含所有工作更改: goldenmaza's Github

最佳答案

如果带有 pageFieldsnested:true 解析器返回 paragraphs 则该属性已经被解析。
不需要单独调用它,它的解析器被跳过,不调用。
这种解析器行为用于优化,在父级中过度获取,当调用多个子级时(一个接一个单独的数据库请求)将无效。
删除 nested:true 或基于顶级 hidden arg 实现额外过滤(如果隐藏在页面上==隐藏在段落处)。通常在父级查询级别构建/使用的复杂(生成)过滤器可以声明嵌套子级的条件。

关于graphql - 未调用嵌套的 GraphQL 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63255839/

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