Example 1: Merging schemas without naming conflicts

In its simplest form, schema stitching might simply mean to merge two schemas that don't have any naming conflicts. Consider the following two schemas, A and B:

Schema A:

const typeDefs = `
  type Query {
    hello: String
  }`

const resolvers = {
  Query: {
    hello: () => 'Hello'
  }
}

const schemaA = makeExecutableSchema({ typeDefs, resolvers })

Schema B:

const typeDefs = `
  type Query {
    goodbye: String
  }

  type Mutation {
    launchMissiles: Boolean
  }`

const resolvers = {
  Query: {
    goodbye: () => 'Goodbye'
  },
  Mutation: {
    launchMissiles: () => Math.random() >= 0.5
  }
}

const schemaB = makeExecutableSchema({ typeDefs, resolvers })

Here is what the merge process looks like in code, using mergeSchemas:

const { mergeSchemas } = require('graphql-tools')
const { schemaA, schemaB } = require('./schemas')

const mergedSchema = mergeSchemas({
  schemas: [schemaA, schemaB]
})

Note that schemaA and schemaB as well as the result mergedSchema are executable schemas.

After merging the two schemas, the result will look as follows:

type Query {
  hello: String   # originates from schema A
  goodbye: String # originates from schema B
}

type Mutation {
  launchMissiles: Boolean # originates from schema B
}

All the fields on Query and Mutation have exactly the same behaviour as in their respective original schemas.

results matching ""

    No results matching ""