merge-graphql-schemas 是一個方便的工具,可以進行schema 以及resovler 的合並處理
一個schema 合並參考demo
- schema 定義
// ./graphql/types/clientType.js
export default `
type Client {
id: ID!
name: String
age: Int
products: [Product]
}
type Query {
clients: [Client]
client(id: ID!): Client
}
type Mutation {
addClient(name: String!, age: Int!): Client
}
`;
// ./graphql/types/productType.js
export default `
type Product {
id: ID!
description: String
price: Int
client: Client
}
type Query {
products: [Product]
product(id: ID!): Product
}
`;
- 合並
// ./graphql/types/index.js
import { mergeTypes } from 'merge-graphql-schemas';
import clientType from './clientType';
import productType from './productType';
const types = [
clientType,
productType,
];
// NOTE: 2nd param is optional, and defaults to false
// Only use if you have defined the same type multiple times in
// different files and wish to attempt merging them together.
export default mergeTypes(types, { all: true });
說明
如果在早期項目規划schema 約定還是比較好的時候merge-graphql-schemas
還是一個不錯的方案
同時已經好好多解決方案了,apollo 團隊的聯邦還是很不錯的,使用此功能,我們可以方便的進行graphql
api 聚合,如果對於后端約定比較好的,做為graphql gateway 的一個工具也是不錯的