v1TransformsEncapsulate

Encapsulate Transform

The encapsulate transform allows to easily encapsulate a specific schema into a single field under the root type.

For instance, if your handler created a schema like this, named mySchema:

type Query {
  something: String
}
 
type Mutation {
  doSomething: String
}

The encapsulate transform will change your schema to this:

type Query {
  mySchema: mySchemaQuery!
}
 
type Mutation {
  mySchema: mySchemaMutation!
}
 
type mySchemaQuery {
  something: String
}
 
type mySchemaMutation {
  doSomething: String
}

This transformer is useful when you have multiple APIs in your Mesh Gateway and you wish to have it wrapped with a name to better understand where each field is coming from.

How to use?

mesh.config.ts
import {
  createEncapsulateTransform,
  defineConfig,
  loadGraphQLHTTPSubgraph
} from '@graphql-mesh/compose-cli'
 
export const composeConfig = defineConfig({
  subgraphs: [
    {
      sourceHandler: loadGraphQLHTTPSubgraph('mySchema', {
        endpoint: 'http://localhost:4001/my-schema'
      }),
      transforms: [
        createEncapsulateTransform({
          applyTo: {
            // This will apply the encapsulate transform only to query fields
            query: true,
            mutation: false,
            subscription: false
          }
        })
      ]
    }
  ]
})