未调用嵌套的GraphQL解析器

未调用嵌套的GraphQL解析器,graphql,sequelize.js,Graphql,Sequelize.js,我的GraphQL/Sequelize源代码还有一个问题。似乎在运行时没有使用我的解析器。当前,似乎为顶级输出类型(第页)调用了冲突解决程序,但似乎没有为第二个输出类型(内容)调用冲突解决程序 前端发送的查询: query { pages (hidden: false) { // this resolver is called (Page) id label paragraphs (hidden: false) { // but not t

我的GraphQL/Sequelize源代码还有一个问题。似乎在运行时没有使用我的解析器。当前,似乎为顶级输出类型(第页)调用了冲突解决程序,但似乎没有为第二个输出类型(内容)调用冲突解决程序

前端发送的查询:

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
        };
    }
});
页面字段文件:

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;
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;
注意:将调用此解析器,GraphiQL工具和终端都会显示SELECT查询

contentFields文件:

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;
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;
。。。
//这是页面表的Sequelize模型定义(输出类型)。。。
常量页面=新的GraphQLObjectType({
名称:'第页',
description:'这表示一个页面',
字段:()=>{
返回{
身份证:{
类型:GraphQLInt,
解析(第页){
返回page.id;
}
},
...
段落:{

args:{/如果
pageFields
resolver带有
nested:true
返回
段落
,则此属性已解析

不需要单独调用它,它的解析器被跳过,而不是调用

这种解析器行为用于优化,当调用多个子(一个接一个地使用单独的DB请求)时,在父级中进行过度抓取是无效的


删除
nested:true
或基于顶层
hidden
arg(如果在第页隐藏==在段落隐藏)执行附加筛选。通常比较复杂(生成)在顶级父查询级别生成/使用的筛选器可以声明嵌套子项的条件。

这取决于您如何为
页面上的
段落
字段实现解析器,但上面的代码中没有显示这一点。@DanielRearden:您指的是输入类型PageParagage(输入/内容)的导入吗?此问题现已解决…由于Daniel Rearden的评论,我终于找到了放置解析程序的位置(我也不知道需要的解析程序…)…谢谢!谢谢@xadm的澄清!那么,如果我有嵌套类型的参数,为什么在创建SELECT查询时不考虑这个参数呢?我几乎按照预期从数据库获得嵌套查询的响应,我只是得到了所有的响应,而不是在嵌套的第2、3等级别上通过查询的参数进行过滤。。。知道原因吗?只能使用当前级别的解析器args…使用它们来构造更详细的[和更手动的]
'where':
条件…
'where sth args and parages.sth2 args.hidden'
再次感谢@xadm,我不知道!我现在将研究如何解决此问题。Thx!可能类似于
where:{…args,段落:{hidden:true}}
…但这是一个与续集相关的问题,搜索文档或单独询问如何为关系构造条件我知道你在做什么,但我希望使用GraphQL查询来确定参数。。。