apollographql / apollographql/federation

printSubgraphSchema fails if using a schema transformer and extending an external type

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

Description

### Issue Description

If your subgraph has a schema that references an external type, e.g.
```gql
type SubgraphType {
id: ID!
external: ExternalType
}

extend type ExternalType @key(fields: "id") {
id: ID! @external
}
```

and you have a transformer applied to the schema, e.g.
```ts
const schemaText = fs.readFileSync(path.join(__dirname, 'schema.graphql')).toString()
const typeDefs = gql(schemaText)

let schema = buildSubgraphSchema([
{ typeDefs }
])

schema = basicSchemaTransformer(schema)
```

Then `printSubgraphSchema` will fail with the error
```
GraphQLError: Type ExternalType must define one or more fields.
at Object.err (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/federation-internals/src/error.ts:36:60)
at Validator.addError (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/federation-internals/src/validate.ts:138:45)
at Validator.validateObjectOrInterfaceType (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/federation-internals/src/validate.ts:168:12)
at Validator.validate (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/federation-internals/src/validate.ts:90:16)
at validateSchema (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/federation-internals/src/validate.ts:30:32)
at Schema.validate (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/federation-internals/src/definitions.ts:1606:42)
at Subgraph.validate (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/federation-internals/src/federation.ts:1766:19)
at buildSubgraph (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/federation-internals/src/federation.ts:1329:19)
at printSubgraphSchema (/Users/kgrant/git/print-subgraph-schema-error/node_modules/@apollo/subgraph/src/printSubgraphSchema.ts:16:33)
at Object. (/Users/kgrant/git/print-subgraph-schema-error/src/print-schema.ts:4:32) {
message: '[] Type ExternalType must define one or more fields.',
path: undefined,
locations: undefined,
extensions: { code: 'INVALID_GRAPHQL' }
}
```

### Link to Reproduction

https://github.com/kal4l/print-subgraph-schema-error

### Reproduction Steps

* See reproduction repo

### Version

* `"@apollo/subgraph": "2.5.7"`

Contributor guide

Open the contributing guide

Research direction

Start by running the linked reproduction repo with the schema transformer and the external type example. Inspect @apollo/subgraph/src/printSubgraphSchema.ts and the federation validation path shown in the stack trace. Done means printSubgraphSchema succeeds for this transformed schema and a regression test covers the case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.