apollographql / apollographql/federation
Computed field on base entity
- Dominant language
- TypeScript
- Stars
- 725
- Forks
- 276
- Avg merge
- 1h 47m
- Merged PRs (30d)
- 1
Description
Hi,
I'm putting together a POC for federation, and finding it amazing so far (good work!) However, I have one particular use case that seems like federation should be able to solve, but it currently doesn't work.
I have 3 services: User, Product, Basket.
Basket extends User (by adding a 'basket' field) and contains a field 'products: [product]'
What I want to do is have a subtotal field on basket, that takes the array of products as an input, adds up all the product totals and then shows the total rather than relying on the client to do it (also, I might have promotional rules or whatever serverside)
I thought I'd be able to do this:
```
type Basket @key(fields: ""id""){
id: ID!
user: User
products: [Product]
subtotal: String @requires(fields: ""products"")
}
```
But of course that throws an error ('Found extraneous @requires directive. @requires cannot be used on base types.') because requires can only be used in an extending service.
Contributor guide
Research direction
Reproduce the Basket schema with a computed subtotal and the reported extraneous @requires error. Start by tracing how federation validates @requires on base types, then determine whether the requested behavior can be supported without an extending service. Done means the schema composes successfully and subtotal can be resolved from products for the stated use case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql
- Domain
- api, distributed-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100