(Apollo) GraphQL 合并模式
(Apollo) GraphQL Merging schema
我正在使用 Apollo 提供的 GraphQL tools/libraries。
是否可以将远程 GraphQL 模式合并到嵌套结构中?
假设我有 n
个远程模式,我想将来自不同模式的 Query
、Mutation
和 Subscription
合并到一个模式中,除了,每个远程模式都放在自己的 type
.
下
假设我们有一个名为 MenuX
的远程模式,其中:
type Item {
id: Int!
description: String
cost: Int
}
type Query {
items: [Item]
}
和一个名为 MenuY
的远程架构,其中:
type Item {
id: Int!
name: String
cost: Int
}
type Query {
items: [Item]
}
我想将模式合并为一个模式,但在它们自己的类型下。一个人为的例子:
type MenuXItem {
id: Int!
description: String
cost: Int
}
type MenuYItem {
id: Int!
name: String
cost: Int
}
type MenuXQuery {
items: [MenuXItem]
}
type MenuYQuery {
items: [MenuYItem]
}
type Query {
MenuX: MenuXItem
MenuY: MenuYItem
}
我们可以看到,在 Query
类型下,它包含两个新类型,其中包含来自远程模式的查询类型。来自架构 MenuX
的 Item
已通过使用来自 graphql-tools
的转换器重命名,类似地,来自架构 MenuY
的 Item
也已被转换。
但是也可以转换结构吗?
对于实际的远程模式,我们正在查看每个模式的数百种类型,理想情况下,我不想污染 GraphiQL 中的根类型和自省文档。
Apollo 的 graphql-tools 包含 transform schemas 的模块。您应该能够重命名 MenuX
中的所有内容,例如
import {
makeRemoteExecutableSchema,
transformSchema,
RenameTypes
} from 'graphql-tools';
const schema = makeRemoteExecutableSchema(...);
const menuXSchema = transformSchema(schema, [
RenameTypes((name) => `MenuX${name}`)
]);
然后你可以use the transformed schema作为mergeSchemas
的输入。
请注意,顶级 Query
和 Mutation
类型有些特殊,您可能想尝试更直接地合并这些类型而不重命名它们,尤其是在它们不冲突的情况下。
可以实现模式,其中 Query
类型具有根字段作为源模式的入口点,但仅适用于 Query
类型,因为 Mutation
类型不支持突变嵌套在名字下。
因此,为名称添加前缀是模式拼接的首选解决方案。
Gatsby 有一个插件,其中包含一个转换器,可以满足您的需求:https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/transforms.js
它为现有 GraphQL 模式的类型命名空间,因此您最终得到:
type Namespace1Item {
...
}
type Namespace2Item {
...
}
type Namespace1Query {
items: [Namespace1Item]
}
type Namespace2Query {
items: [Namespace2Item]
}
type Query {
namespace1: Namespace1Query
namespace2: Namespace2Query
}
所以,如果你转换你的模式并将它们合并,你应该很好。
我正在使用 Apollo 提供的 GraphQL tools/libraries。
是否可以将远程 GraphQL 模式合并到嵌套结构中?
假设我有 n
个远程模式,我想将来自不同模式的 Query
、Mutation
和 Subscription
合并到一个模式中,除了,每个远程模式都放在自己的 type
.
假设我们有一个名为 MenuX
的远程模式,其中:
type Item {
id: Int!
description: String
cost: Int
}
type Query {
items: [Item]
}
和一个名为 MenuY
的远程架构,其中:
type Item {
id: Int!
name: String
cost: Int
}
type Query {
items: [Item]
}
我想将模式合并为一个模式,但在它们自己的类型下。一个人为的例子:
type MenuXItem {
id: Int!
description: String
cost: Int
}
type MenuYItem {
id: Int!
name: String
cost: Int
}
type MenuXQuery {
items: [MenuXItem]
}
type MenuYQuery {
items: [MenuYItem]
}
type Query {
MenuX: MenuXItem
MenuY: MenuYItem
}
我们可以看到,在 Query
类型下,它包含两个新类型,其中包含来自远程模式的查询类型。来自架构 MenuX
的 Item
已通过使用来自 graphql-tools
的转换器重命名,类似地,来自架构 MenuY
的 Item
也已被转换。
但是也可以转换结构吗?
对于实际的远程模式,我们正在查看每个模式的数百种类型,理想情况下,我不想污染 GraphiQL 中的根类型和自省文档。
Apollo 的 graphql-tools 包含 transform schemas 的模块。您应该能够重命名 MenuX
中的所有内容,例如
import {
makeRemoteExecutableSchema,
transformSchema,
RenameTypes
} from 'graphql-tools';
const schema = makeRemoteExecutableSchema(...);
const menuXSchema = transformSchema(schema, [
RenameTypes((name) => `MenuX${name}`)
]);
然后你可以use the transformed schema作为mergeSchemas
的输入。
请注意,顶级 Query
和 Mutation
类型有些特殊,您可能想尝试更直接地合并这些类型而不重命名它们,尤其是在它们不冲突的情况下。
可以实现模式,其中 Query
类型具有根字段作为源模式的入口点,但仅适用于 Query
类型,因为 Mutation
类型不支持突变嵌套在名字下。
因此,为名称添加前缀是模式拼接的首选解决方案。
Gatsby 有一个插件,其中包含一个转换器,可以满足您的需求:https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/transforms.js
它为现有 GraphQL 模式的类型命名空间,因此您最终得到:
type Namespace1Item {
...
}
type Namespace2Item {
...
}
type Namespace1Query {
items: [Namespace1Item]
}
type Namespace2Query {
items: [Namespace2Item]
}
type Query {
namespace1: Namespace1Query
namespace2: Namespace2Query
}
所以,如果你转换你的模式并将它们合并,你应该很好。