apollographql / apollographql/federation
Documentation for using directives with a federated service does not work
- Dominant language
- TypeScript
- Stars
- 725
- Forks
- 276
- Avg merge
- 1h 47m
- Merged PRs (30d)
- 1
Description
I'm running into some problems getting a gateway working with a federated service that defines some directives. The [docs suggest this is possible](https://www.apollographql.com/docs/federation/implementing-services/#defining-custom-directives) with some slightly different setup to a non-federated service, but the gateway never seems to have knowledge of the effects the directives are intended to have on the fields they are applied to.
Here is [a repo](https://github.com/shackpank/apollo-federation-directives-issue-example) with a minimal example of the issue.
Reading through what's going on internally, it looks to me like [this](https://github.com/apollographql/federation/blob/b499c0bb68b921ae7cb1152cfa9498303702ed83/federation-js/src/service/buildFederatedSchema.ts#L72) is the cause - the first time `buildFederatedSchema` is called, it creates an SDL string representation of the initially provided schema, which is then reused [here](https://github.com/apollographql/federation/blob/b499c0bb68b921ae7cb1152cfa9498303702ed83/federation-js/src/service/buildFederatedSchema.ts#L108) at the point the gateway is composing schemas together.
If I replace the second line linked above with another call to `printSchema` it all works as expected and the SDL contains the modifications made by the later call to `visitSchemaDirectives`, however it also contains a few fields the comments are suggesting have been intentionally not included through the current placement of `printSchema` (such as the `__entities` field).
Any advice appreciated - I was initially going to open a PR with that local patch to stop "memoizing" the SDL and regen it every time, but having read the comments I'm not sure that's the right way to go. Maybe `buildFederatedSchema` could be altered somehow to accept the results of `makeExecutableSchema`, with directives already baked in, so client code would look like this?
```javascript
const schema = makeExecutableSchema({ typeDefs, resolvers })
SchemaDirectiveVisitor.visitSchemaDirectives(schema, directives)
const federatedSchema = buildFederatedSchema([schema])
const server = new ApolloServer({
schema: federatedSchema
})
```
Contributor guide
Research direction
Start with the minimal reproduction repository and federation-js/src/service/buildFederatedSchema.ts, especially the linked printSchema and visitSchemaDirectives paths. Reproduce the gateway composition failure, then verify that directive effects survive federation without unintentionally including fields such as __entities.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100