如何嵌套graphql查询

时间:2017-02-27 04:27:59

标签: graphql graphql-js

我找到的所有示例都有一个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,
});

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

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

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

1 个答案:

答案 0 :(得分:2)

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

提供类型

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

如果每个字段的类型MyFirstGroupQuery必须定义type,例如GraphQLIntGraphQLStringGraphQLID,即使它已经过了自定义类型,如MyFirstGroupQuery

GraphQLSchema构造函数中,您提供的RootQueryQueryType,而GraphQLSchema只接受rootQuery GraphQLObjectType type 1}}其字段必须定义type

GraphQL严格基于类型,您声明的每个字段都必须定义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;

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

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

imageURLs

GraphiQL

enter image description here