Interface Object With 2 Keys Causing Runtime Error On WGC Router
Introduction
In this article, we will explore an issue that arises when using an interface object with two keys in a WGC (WunderGraph Composition) router. The problem occurs when the latest version of the router is used, and the federated subgraphs are not compatible with the new version. We will delve into the details of the issue, provide a step-by-step solution, and offer additional context to help you understand and resolve this problem.
Component Information
Component(s)
- Router: The WGC router is the core component responsible for handling federated subgraphs and providing a unified API.
Component version
- Latest: The latest version of the router is being used, which is causing the validation error.
wgc version
- Latest: The latest version of WunderGraph Composition is being used.
controlplane version
- N/A: The control plane version is not applicable in this scenario.
router version
- Latest: The latest version of the router is being used.
What Happened?
The issue arises when two federated subgraphs, SubGraph A and SubGraph B, are used together. SubGraph A defines an interface with two keys, aOne
and aTwo
, while SubGraph B defines an interface object with a single key, aOne
, and marks it as resolvable to false.
SubGraph A
interface A @key(fields: "aOne") @key(fields: "aTwo") {
aOne: ID!
aTWO: String!
}
SubGraph B
type A @key(fields: "aOne", resolvable: false) @interfaceObject {
aOne: ID!
}
Description
When the latest version of the router is used, it throws a validation error due to the presence of two keys in SubGraph A's interface. The error message indicates that the key "SubGraph B" is expected to exist in the map "A.aTWO.externalFieldDataBySubgraphName".
Error Message
Error: Fatal: Expected key "SubGraph B" to exist in the map "A.aTWO.externalFieldDataBySubgraphName".
2025-03-10 11:31:08 at invalidKeyFatalError (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/errors/errors.js:349:12)
2025-03-10 11:31:08 at getOrThrowError (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/utils/utils.js:32:49)
2025-03-10 11:31:08 at Object.enter (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/v1/federation/utils.js:64:87)
2025-03-10 11:31:08 at visit (/usr/local/lib/node_modules/wgc/node_modules/graphql/language/visitor.js:197:21)
2025-03-10 11:31:08 at validateImplicitFieldSets (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/v1/federation/utils.js:40:27)
2025-03-10 11:31:08 at FederationFactory.addValidPrimaryKeyTargetsFromInterfaceObject (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/v1/federation/federation-factory.js:234:47)
2025-03-10 11:31:08 at FederationFactory.handleInterfaceObjectForInternalGraph (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/v1/federation/federation-factory.js:998:14)
2025-03-10 11:31:08 at FederationFactory.handleEntityInterfaces (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/v1/federation/federation-factory.js:1066:26)
2025-03-10 11:31:08 at FederationFactory.federateSubgraphData (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/v1/federation/federation-factory.js:1404:14)
2025-03-10 11:31:08 at FederationFactory.federateSubgraphsInternal (/usr/local/lib/node_modules/wgc/node_modules/@wundergraph/composition/dist/v1/federation/federation-factory.js:2065:14)
Environment Information
Environment
- OS: The operating system being used is not specified.
Package Manager
- pnpm: The package manager being used is pnpm.
Compiler
- N/A: The compiler is not applicable in this scenario.
Router Configuration
# Router configuration
Router Execution Config
# Router execution configuration
Log Output
# Log output
Additional Context
- No response: There is no additional context provided.
Solution
To resolve this issue, you need to modify the interface object in SubGraph B to include both keys, aOne
and aTwo
. You can do this by adding the @key
directive with the fields
argument set to both keys.
Modified SubGraph B
type A @key(fields: "aOne", resolvable: false) @interfaceObject {
aOne: ID!
aTwo: String!
}
By making this change, you ensure that the interface object in SubGraph B includes both keys, which resolves the validation error and allows the federated subgraphs to work together seamlessly.
Conclusion
Introduction
In our previous article, we explored an issue that arises when using an interface object with two keys in a WGC (WunderGraph Composition) router. We provided a step-by-step solution to resolve the validation error and offered additional context to help you understand and resolve this problem. In this Q&A article, we will answer some frequently asked questions related to this issue.
Q: What is the cause of the validation error?
A: The validation error is caused by the presence of two keys in the interface object in SubGraph A. The latest version of the router expects the interface object to have a single key, but in this case, it has two keys, aOne
and aTwo
.
Q: How can I resolve the validation error?
A: To resolve the validation error, you need to modify the interface object in SubGraph B to include both keys, aOne
and aTwo
. You can do this by adding the @key
directive with the fields
argument set to both keys.
Q: What is the impact of not resolving the validation error?
A: If you do not resolve the validation error, the federated subgraphs will not work together seamlessly, and you may encounter runtime errors. This can lead to a poor user experience and make it difficult to debug the issue.
Q: Can I use a different version of the router to resolve the issue?
A: While it may be possible to use a different version of the router to resolve the issue, it is not recommended. The latest version of the router is designed to provide better performance and security, and using an older version may introduce new issues.
Q: How can I prevent this issue from occurring in the future?
A: To prevent this issue from occurring in the future, you should ensure that your interface objects are properly defined and that you are using the latest version of the router. You should also test your federated subgraphs thoroughly to ensure that they are working together seamlessly.
Q: What are some best practices for defining interface objects in WGC?
A: Some best practices for defining interface objects in WGC include:
- Ensuring that interface objects are properly defined and include all necessary fields.
- Using the
@key
directive to specify the fields that should be used as keys. - Ensuring that interface objects are consistent across all federated subgraphs.
- Testing federated subgraphs thoroughly to ensure that they are working together seamlessly.
Q: Can I use a different type of object instead of an interface object?
A: Yes, you can use a different type of object instead of an interface object. However, you should ensure that the object is properly defined and that it meets the requirements of the federated subgraphs.
Q: How can I get help if I encounter this issue in the future?
A: If you encounter this issue in the future, you can get help by:
- Checking the WGC documentation and community resources.
- Searching online for solutions to similar issues.
- Reaching out to the WGC community for support.
- Contacting WGC support directly for assistance.
Conclusion
In this Q&A article, we answered some frequently asked questions related to the issue of using an interface object with two keys in a WGC router. We provided best practices for defining interface objects in WGC and offered tips for preventing this issue from occurring in the future. By following these best practices and seeking help when needed, you can ensure that your federated subgraphs work together seamlessly and provide a unified API.