website/src/docs/fusion/v14/node.md
By leveraging the global object identification pattern, also known as the node pattern, Fusion can work seamlessly with source systems that implement this specification. This integration enables a zero-configuration setup for your distributed GraphQL services and allows you to re-expose fields like node through the gateway.
The node pattern is a part of the Relay GraphQL server specifications that provides a way to globally identify objects within a GraphQL schema. It introduces a universal Node interface that any object can implement, ensuring each object has a unique identifier. This pattern is particularly beneficial for client applications using Relay, as it allows for efficient data (re-)fetching and caching.
To take advantage of the node pattern in Fusion, your source systems need to implement the Node interface as defined by the Relay specifications.
This involves adding the global object identification pattern to your schema.
Here's how you can implement the node pattern in HotChocolate:
builder.Services
.AddGraphQLServer()
.AddTypes() // source generated by HotChocolate.Types.Analyzer
.AddGlobalObjectIdentification(); // Adds the Node interface and node field
Now you need to add a node resolver to your schema:
[QueryType]
public static class Query
{
[NodeResolver]
public static Product GetProductById(int id, IProductService userService)
{
return userService.GetProductById(id);
}
}
By specifying the NodeResolver attribute on the GetProductById method, you indicate that this method resolves nodes based on their global ID. As Product defines a node resolver, HotChocolate will automatically expose the node field in the schema, let Product implement the Node interface, and also infer the id field as the global identifier of type ID!.
Fusion can automatically detect source systems that implement the node pattern, allowing for a zero-configuration setup. By leveraging the global object identification, Fusion can optimize data fetching across subgraphs without additional configuration.
Export Schemas: Export the schemas from your subgraphs without additional configuration.
dotnet run --project quick-start.Products -- schema export --output schema.graphql
Pack Subgraphs: Pack the subgraph configurations.
fusion subgraph pack --name Products --schema schema.graphql --output products.fsp
Compose the Gateway: Use Fusion to compose the gateway configuration.
fusion compose -p gateway.fgp -s ../quick-start.Products --enable-nodes
Clients can now query for nodes directly via the gateway:
{
node(id: "UHJvZHVjdDoy") {
... on Product {
id
name
}
}
}
This query fetches a Product node with a specific ID.
A practical example demonstrating this integration is available in the HotChocolate Examples Repository.
This example also showcases how you can integrate --enable-nodes directly with the Aspire Integration to simplify the composition process further.