如何嵌套 graphql 查询
how to nest graphql queries
我找到的所有示例都有一个 query
顶级对象,然后是一个查询列表,然后 return 键入更深入的内容。
由于我有大量查询,我想将它们分组,这是我尝试过的:
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.
如何完成我想要的?我真的不想只为所有查询添加前缀。
错误 query.myFirstGroup field type must be Output Type but got: undefined.
表示您没有提供 myFirstGroup
的类型
您必须使用 type
字段
提供类型
myFirstGroup: {
type: MyFirstGroupQuery,
resolve: () => MyFirstGroupQuery,
},
并且如果类型 MyFirstGroupQuery
每个字段必须具有 type
定义,例如 GraphQLInt
、GraphQLString
、GraphQLID
,即使它是自定义类型MyFirstGroupQuery
在 GraphQLSchema
构造函数中,您提供 RootQuery
即 QueryType
,这是一个 GraphQLSchema
它只接受 rootQuery
和 GraphQLObjectType
其字段必须定义 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
我找到的所有示例都有一个 query
顶级对象,然后是一个查询列表,然后 return 键入更深入的内容。
由于我有大量查询,我想将它们分组,这是我尝试过的:
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.
如何完成我想要的?我真的不想只为所有查询添加前缀。
错误 query.myFirstGroup field type must be Output Type but got: undefined.
表示您没有提供 myFirstGroup
的类型
您必须使用 type
字段
myFirstGroup: {
type: MyFirstGroupQuery,
resolve: () => MyFirstGroupQuery,
},
并且如果类型 MyFirstGroupQuery
每个字段必须具有 type
定义,例如 GraphQLInt
、GraphQLString
、GraphQLID
,即使它是自定义类型MyFirstGroupQuery
在 GraphQLSchema
构造函数中,您提供 RootQuery
即 QueryType
,这是一个 GraphQLSchema
它只接受 rootQuery
和 GraphQLObjectType
其字段必须定义 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