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

Federation Subgraph Transform

This transform allows to create subgraphs from your regular sources which don’t have any federation annotations. This transform uses graphql-transform-federation package.

npm i @graphql-mesh/transform-federation

How to use?

Add the following configuration to your Mesh config file:

.meshrc.yaml
transforms:
  - federation:
      types:
        # Ensure the root queries of this schema show up the combined schema
        - name: Query
          config:
            extends: true
        - name: Product
          config:
            # extend Product {
            extend: true
            # Product @key(fields: "id") {
            key:
              fields:
                - id
            fields:
              # id: Int! @external
              - name: id
                config:
                  external: true
            resolveReference:
              queryFieldName: user

Add Reference Resolver as a Code File

To add more complex business logic, you can point to a code file that exports a resolver function.

.meshrc.yaml
resolveReference: ./userResolveReference.ts
userResolveReference.ts
// So we can point to an existing query field to resolve that entity
export default function (root, context, info) {
  return context.accounts.Query.user({ root, args: { id: root.id }, context, info })
}
πŸ’‘
You can check out our example that uses Federation as a merging strategy.
  • types (type: Array of Object, required):
    • name (type: String, required)
    • config (type: Object):
      • key (type: Array of Object):
        • fields (type: String)
      • shareable (type: Boolean)
      • extends (type: Boolean)
      • fields (type: Array of Object, required):
        • name (type: String, required)
        • config (type: Object, required):
          • external (type: Boolean)
          • provides (type: Object):
            • fields (type: String)
          • requires (type: Object):
            • fields (type: String)
          • tag (type: Object):
            • name (type: String)
          • inaccessible (type: Boolean)
          • override (type: Object):
            • from (type: String)
      • resolveReference - One of:
        • String
        • object:
          • queryFieldName (type: String, required) - Name of root field name that resolves the reference
          • args (type: JSON) - You need configure the arguments for that field;
args:
  someArg: "{root.someKeyValue}"
  • version (type: String) - Version of the federation spec Default: v2.0