Catch the highlights of GraphQLConf 2023! Click for recordings. Or check out our recap blog post.
Docs
Transforms
Hoist

Hoist Field Transform

The hoist transform allows you to lift a field from one object type to a ‘parent’ root or object type. It is currently only available as a wrap transform.

npm i @graphql-mesh/transform-hoist-field
💡

Underneath it leverages the HoistField transform from the @graphql-tools/wrap package.

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

.meshrc.yaml
transforms:
  - hoist-field:
      - 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

.meshrc.yaml
transforms:
  - hoist-field:
      - typeName: Query
        pathConfig:
          - users
          - results
        newFieldName: users
        filterArgsInPath: true # This flag sets the default for the entire path

Will transform the given schema to:

type Query {
  users: [User!]!
}
 
type User {
  id: ID!
}

Filtering args on specific levels of the path

.meshrc.yaml
transforms:
  - hoist-field:
      - 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!
}

Config API Reference

  • typeName (type: String, required) - Type name that defines where field should be hoisted to
  • pathConfig - - Array of fieldsNames to reach the field to be hoisted (required) Array of:
    • String
    • object:
      • fieldName (type: String, required) - Field name
      • filterArgs (type: Array of String, required) - Match fields based on argument, needs to implement (arg: GraphQLArgument) => boolean;
  • newFieldName (type: String, required) - Name the hoisted field should have when hoisted to the type specified in typeName
  • alias (type: String)
  • filterArgsInPath (type: Boolean) - Defines if args in path are filtered (default = false)