【问题标题】:how to nest graphql queries如何嵌套graphql查询
【发布时间】:2017-07-17 14:24:44
【问题描述】:

我找到的所有示例都有一个 query 顶级对象,然后是一个查询列表,然后返回类型以进行更深入的分析。

由于我有大量查询,我想将它们分组,这是我尝试的:

const AppType = new GraphQLObjectType({
    name: 'App',
    description: 'Generic App Details',

    fields: () => ({
        name: { type: GraphQLString },
        appId: { type: GraphQLInt },
    }),
});

const MyFirstQuery = {
    type: new GraphQLList(AppType),
    args: {
        appId: { type: GraphQLInt },
    },
    resolve: (root, args) => fetchApp(args.appId),
};     

/* snip MySecondQuery, MyThirdQuery, MyFourthQuery */

const MyFirstGroupQuery = new GraphQLObjectType({
    name: 'myFirstGroup',
    description: 'the first group of queries',

    fields: () => ({
        myFirstQuery: MyFirstQuery,
        mySecondQuery: MySecondQuery,
        myThirdQuery: MyThirdQuery,
        myFourthQuery: MyFourthQuery,
    }),
});

/* snip MySecondGroupQuery, MyThirdGroupQuery and their types */    

const QueryType = new GraphQLObjectType({
    name: 'query',
    description: 'read-only query',

    fields: () => ({
        myFirstGroup: MyFirstGroupQuery,
        mySecondGroup: MySecondGroupQuery,
        myThirdGroup: MyThirdGroupQuery,
    }),
});

const Schema = new GraphQLSchema({
    query: QueryType,
});

为什么我不能像 QueryType 那样制作 MyFirstGroupQuery 来制作更多嵌套级别?如果我将所有查询都放在QueryType 中,代码可以正常工作,但我的MyFirstGroupQuery 会产生错误:

Error: query.myFirstGroup field type must be Output Type but got: undefined.

如何实现我想要的?我真的不想只为我的所有查询添加前缀。

【问题讨论】:

  • 你的意思是你在GraphQLSchema中声明了Quertype

标签: graphql graphql-js


【解决方案1】:

错误query.myFirstGroup field type must be Output Type but got: undefined. 表示您没有提供myFirstGroup 的类型 您必须使用 type 字段提供类型

myFirstGroup: { type: MyFirstGroupQuery, resolve: () => MyFirstGroupQuery, },

如果MyFirstGroupQuery 类型每个字段必须定义type,例如GraphQLIntGraphQLStringGraphQLID,即使它是自定义类型,例如MyFirstGroupQuery

GraphQLSchema 构造函数中,您提供RootQuery,即QueryType,这是一个GraphQLSchema,它只接受rootQueryGraphQLObjectType,其字段必须定义type

GraphQL 严格基于类型,您声明的每个字段都必须定义 type

https://github.com/graphql/graphql-js

https://github.com/graphql/graphql-js/blob/master/src/type/schema.js#L32

const {
    GraphQLID,
    GraphQLInt,
    GraphQLString,
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLList,
} = require('graphql');

const AppType = new GraphQLObjectType({
    name: 'App',
    description: 'Generic App Details',

    fields: () => ({
        name: { type: GraphQLString },
        appId: { type: GraphQLInt },
    }),
});

// const MyFirstQuery = {
//     type: new GraphQLList(AppType),
//     args: {
//         appId: { type: GraphQLInt },
//     },
//     resolve: (root, args) => fetchApp(args.appId),
// };

const myFirstQuery = new GraphQLObjectType({
    name: 'First',
    fields: () => ({
        app: {
            type: new GraphQLList(AppType),
            args: {
                appId: { type: GraphQLInt },
            },
            resolve: (root, args) => fetchApp(args.appId),
        },
    }),
});

/* snip MySecondQuery, MyThirdQuery, MyFourthQuery */

const MyFirstGroupQuery = new GraphQLObjectType({
    name: 'myFirstGroup',
    description: 'the first group of queries',
    fields: () => ({
        myFirstQuery: {
            type: myFirstQuery,
            resolve: () => [], // promise
        },
        // mySecondQuery: {
        //     type: MySecondQuery,
        //     resolve: () => //data
        // }
        // myThirdQuery: {
        //     type: MyThirdQuery,
        //     resolve: () => // data
        // }
        // myFourthQuery: {
        //     type: MyFourthQuery,
        //     resolve: () => //data
        // }
    }),
});

/* snip MySecondGroupQuery, MyThirdGroupQuery and their types */

const QueryType = new GraphQLObjectType({
    name: 'query',
    description: 'read-only query',

    fields: () => ({
        myFirstGroup: {
            type: MyFirstGroupQuery,
            resolve: () => MyFirstGroupQuery,
        },
        // mySecondGroup: {
        //     type: MySecondGroupQuery,
        //     resolve: MySecondGroupQuery
        // }
        // myThirdGroup: {
        //     type: MyThirdGroupQuery,
        //     resolve: MyThirdGroupQuery
        // }
    }),
});

const Schema = new GraphQLSchema({
    query: QueryType,
});

module.exports = Schema;

GraphiQL

【讨论】:

  • 谢谢! GraphQLSchema 中省略了该类型的事实让我大吃一惊!
猜你喜欢
  • 2021-01-29
  • 2020-12-02
  • 1970-01-01
  • 2018-06-13
  • 2019-05-30
  • 2021-01-14
  • 2017-06-16
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多