Use a type known in only one subgraph
pweemeeuw opened this issue · comments
Hi, in the product and review subgraphs Spring example (https://github.com/apollographql/federation-jvm-spring-example), the parent type (product) is known in both subgraphs. But what is the recommended approach if you do not want to expose the parent type in the other subgraph?
We found a an explication at the specification but no example for the implementation with Federation JVM.
https://www.apollographql.com/docs/federation/
“You'll notice some exceptions to this, such as Review.product (which is still defined in the Reviews subgraph even though it requires data from the Products subgraph). Exceptions like this improve data encapsulation (the Products subgraph doesn't really need to know about the Review type), and we handle them with powerful federated types called entities.”
Suppose you have a Meeting (only present in subgraph 1), with a location ID, and you want to provide the location details in subgraph 2, but don't want to define the Meeting type:
For subgraph 1:
type Query {
version:String
meeting(id: ID!): Meeting
}
type Meeting {
id: ID!
subject: String!
location: Location!
}
type Location @key(fields: "id") {
id: ID! @external
}
@Controller
class MeetingController {
@QueryMapping
fun meeting(@Argument id: Long): Meeting {
return Meeting(id, "test", Location(1))
}
@SchemaMapping(typeName = "Meeting", field = "location")
fun location(meeting: Meeting): Location {
return meeting.location
}
}
For subgraph 2:
type Location @key(fields: "id") {
id: ID!
city: String!
street: String!
}
What is the recommended approach in subgraph 2 in Spring GraphQL, should we provide a DataFetcher for Location?
Maybe an example for this scenario could be useful?
Hello 👋
Generally we recommend to use our Community Forums for general questions about the federation.
In Federation v2 you can reference entities without contributing fields. You should define location as not resolvable in your subgraph # 1, i.e.
type Location @key(fields: "id", resolvable: false) {
id: ID!
}
This will direct the gateway to always resolve the location through the other subgraph. Since Location
type is an entity, your subgraph # 2 just have to implement the entity data fetcher and entity type resolver (see: GraphQLConfiguration.java#L22). There is no Location
specific data fetcher anymore.
Closing due to inactivity. For general questions about Apollo Federation please use our Community Forums.