v1TransformsHoist Field

Hoist Field Transform

The hoist field transform allows you to lift a field from one object type to a ‘parent’ root or object type.

How to use?

Given the following schema:

type Query {
  users(limit: Int!, page: Int): UserSearchResult
}
 
type UserSearchResult {
  page: Int!
  results: [User!]!
}
 
type User {
  id: ID!
}

Simple hoisting

mesh.config.ts
import {
  createHoistFieldTransform,
  defineConfig,
  loadGraphQLHTTPSubgraph
} from '@graphql-mesh/compose-cli'
 
export const composeConfig = defineConfig({
  subgraphs: [
    {
      sourceHandler: loadGraphQLHTTPSubgraph('Users', {
        endpoint: 'http://localhost:4001/users'
      }),
      transforms: [
        createHoistFieldTransform({
          mapping: [
            {
              typeName: 'Query',
              pathConfig: ['users', 'results'],
              newFieldName: 'users'
            }
          ]
        })
      ]
    }
  ]
})

Will transform the given schema to:

type Query {
  users(limit: Int!, page: Int): [User!]!
}
 
type User {
  id: ID!
}

Filtering args via a default for the entire path

mesh-config.ts
import {
  createHoistFieldTransform,
  defineConfig,
  loadGraphQLHTTPSubgraph
} from '@graphql-mesh/compose-cli'
 
export const composeConfig = defineConfig({
  subgraphs: [
    {
      sourceHandler: loadGraphQLHTTPSubgraph('Users', {
        endpoint: 'http://localhost:4001/users'
      }),
      transforms: [
        createHoistFieldTransform({
          mapping: [
            {
              typeName: 'Query',
              pathConfig: [
                {
                  fieldName: 'users',
                  filterArgs: ['limit']
                },
                'results'
              ],
              newFieldName: 'users'
            }
          ]
        })
      ]
    }
  ]
})

Will transform the given schema to:

type Query {
  users(page: Int): [User!]!
}
 
type User {
  id: ID!
}