Skip to main content
Version: v3.x

Subgraph GraphQL Root Field and Type Name Prefixing

Subgraphs are namespaced and metadata object names are independent from one another and cannot conflict. However, the GraphQL API is where the subgraphs meet, potentially leading to naming collisions.

To avoid collisions between GraphQL root fields and type names across when federating subgraphs, you can optionally customize the prefixes for each.

For example, if two subgraphs both have a Users type, you can apply different prefixes to distinguish one from the other. This ensures that each subgraph remains unique and prevents any naming conflicts.

You can make these modifications in the subgraph.yaml file for a subgraph.

Add the highlighted lines:
kind: Subgraph
version: v2
definition:
name: my_subgraph
generator:
rootPath: .
graphqlRootFieldPrefix: my_subgraph_
graphqlTypeNamePrefix: My_subgraph_

By default, the subgraph.yaml file is generated without any prefixes. You can read more about these fields here.

Renaming GraphQL root fields and GraphQL type name prefixes

This codemod will rename prefixes in already generated metadata. It can also be used to add or remove prefixes altogether.

The --from-graphql-root-field prefix will be stripped if provided, and the new prefix, --graphql-root-field, will be added. If the new prefix is already present, it will not be reapplied.

Examples:

# Add root field and type name prefixes to the subgraph set in the context
ddn codemod rename-graphql-prefixes --graphql-root-field 'app_' --graphql-type-name 'App_'

# Change the root field prefix for the specified subgraph
ddn codemod rename-graphql-prefixes --subgraph app/subgraph.yaml --from-graphql-root-field 'app_' --graphql-root-field 'new_'