graphql-tools-merge-typedefs
This library is used to merge GraphQL query, mutation, and subscription type definitions that are specified in separate files.
Installation
npm install graphql-tools-merge-typedefs
or
yarn add graphql-tools-merge-typedefs
Usage
Imagine the following schema definition file named customer-schema.js
:
const gql = ;const customersData = ; exportstypeDefs = gql` type Customer { id: ID! firstName: String! lastName: String! } type Query { customer(id: ID!): Customer }`; exportsresolvers = Query: customersData ;
And another schema definition file named order-schema.js
:
const gql = ;const ordersData = ; const order = gql` type Order { id: ID! product: String! quantity: Int! }`;const query = gql` type Query { order(id: ID!): Order }`;// you can also export an array of querysexportstypeDefs = order query exportsresolvers = Query: ordersData ;
Merging these two files using the graphql-tools makeExecutableSchema
function would result in a merge conflict since both customer-schema.js
and order-schema.js
files include a "Query" definition. However, using graphql-tools-merge-typedefs
, these definitions can be merged as follows:
const ApolloServer = ;const makeExecutableSchema = ;const mergeTypeDefs = ;const customerSchema = ;const orderSchema = ; // merge conflicting "Query", "Mutation", and "Subscription" definitionsconst typeDefs = ; const resolvers = customerSchemaresolvers orderSchemaresolvers; const schema = ; const server = schema ; server;
If you need to merge typeDefs other than Query
, Mutation
, and Subscription
definitions something you can pass in your own custom type(s) by passing in a string or an array of strings
const gql = const mergeTypeDefs = ; const typeDefs = ;...