apollographql / apollographql/federation

subgraph-js: directive can't be used on custom scalar definition

Open
#2,882 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
725
Forks
276
Avg merge
1h 47m
Merged PRs (30d)
1

Description

We need to add `@tag` directive on custom scalar in our subgraph schema to control contracts, unfortunately it disappears during the subgraph schema generation (we call `buildSubgraphSchema()` with `typeDefs` and `resolvers` provided). It happens only if resolver for that custom scalar type is also added. Without the resolver directive is present in the final schema.

Whole `astNode` looks to be removed from the custom scalar type during schema build.
I trace problem down to this code in the `addResolversToSchema` method.

https://github.com/apollographql/federation/blob/160299f5db19003514ea60a2ffcb8c0b3b001076/subgraph-js/src/schema-helper/buildSchemaFromSDL.ts#L135

I'm not fully sure what is this code doing/copiing, but what helped me is to prevent `astNode` to be copied if it is empty:

```
if (isScalarType(type)) {
for (const fn in fieldConfigs) {
// CUSTOMIZATION: skip astNode copying if empty to keep directives etc on the scalar type definition
if (fn != "astNode" || (fieldConfigs as any)[fn])
(type as any)[fn] = (fieldConfigs as any)[fn];
}
}
```

Contributor guide

Open the contributing guide

Research direction

Read subgraph-js/src/schema-helper/buildSchemaFromSDL.ts at addResolversToSchema, especially the scalar handling around the linked line. Reproduce schema generation with a custom scalar resolver and an @tag directive, then verify that the generated scalar retains its astNode and directive; the issue provides no test file to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.