使用merge-graphql-schemas 進行graphql schema 以及resovler 合並


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 的一個工具也是不錯的

參考資料

https://github.com/Urigo/merge-graphql-schemas


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM