我的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查询...
contentFields文件:
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
答案 0 :(得分:1)
如果带有pageFields
的{{1}}解析器返回nested:true
,则此属性已被解析。
无需单独调用它,它的解析器被跳过,而不被调用。
这种解析器行为用于优化,当调用多个子对象(一个单独的DB请求一个一个地调用)时,父对象中的过度获取将无效。
删除paragraphs
或基于顶级nested:true
arg实施附加过滤(如果隐藏在page == hidden中)。通常,在顶级查询级别构建/使用的复杂(生成的)过滤器可以声明嵌套子项的条件。