On this page

Federation Transform

Convert non federation subgraphs to federation subgraphs

If the GraphQL source is not a Federated Subgraph, you may use this Federation Transform to provide necessary annotations and directive to convert the GraphQL source to a Federated Subgraph conforming to the Federation specification.

How to use?

Let’s say you have the following subgraph;

type Product {
  id: ID!
  name: String!
  price: Float!
}

type Query {
  productById(id: ID!): Product
}

And you want to add @key directive to the Product type. Then you can add the following configuration to your mesh.config.ts file. But you need resolveReference to be able to resolve the reference, which is equivalent of __resolveReference resolver in regular Federation.

mesh.config.ts
import {
  createFederationTransform,
  defineConfig,
  loadGraphQLHTTPSubgraph
} from '@graphql-mesh/compose-cli'

export const composeConfig = defineConfig({
  subgraphs: [
    {
      sourceHandler: loadGraphQLHTTPSubgraph('myApi', {
        endpoint: 'http://localhost:4001/my-api'
      }),
      transforms: [
        createFederationTransform({
          // Schema Coordinates
          Product: {
            // Directive name
            key: {
              // Directive arguments
              fields: 'id',
              // Reference resolver (only for @key directive)
              resolveReference: {
                fieldName: 'productById'
              }
            }
          },
          // You can also add other directives
          Query: {
            extends: true
          }
        })
      ]
    }
  ]
})