dotansimha / dotansimha/graphql-code-generator

schema-ast incorrectly removes the extend keyword when using federation

Open
#7,643 1 comment 4 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**Describe the bug**
Apollo Federation allows subgraphs to `extend` types in other graphs. The `schema-ast` plugin strips the extend keyword off all types when it generates the local combined schema file. It should keep the extend keyword when extending a type that was not defined else ware in the same subgraph/project.

**To Reproduce**
Steps to reproduce the behavior:

1. My GraphQL schema:

```graphql
scalar Date

schema {
query: Query
}

type Query {
me: User!
user(id: ID!): User
allUsers: [User]
}

type User {
id: ID!
username: String!
email: String!
profile: Profile @requires(fields: "id")
}

extend type Profile @key(fields: id) {
id: ID! @external
}

extend type User {
alias: String
}
```

2. My GraphQL operations:

```graphql
# n/a
```

3. My `codegen.yml` config file:

```yml
generates:
schema.graphql:
plugins:
- schema-ast
config:
federation: true
includeDirectives: true
```

**Expected behavior**
* Types that are only extended, should keep the extend prefix
* Types that are have there original definition locally, should be merged, removing the extend prefix (current behavior)

The above code generated these types.
```graphql
type User {
id: ID!
username: String!
email: String!
profile: Profile @requires(fields: "id")
alias: String
}

type Profile @key(fields: id) {
id: ID! @external
}
```

It should have kept the `extend` prefix on the `Profile` type, because it is extending a type that is defined in another subgraph
```graphql
type User {
id: ID!
username: String!
email: String!
profile: Profile @requires(fields: "id")
alias: String
}

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

**Environment:**

- OS: Mac 11.4
- NodeJS: 14.17.6
```
"@graphql-codegen/add": "^3.1.1",
"@graphql-codegen/cli": "^2.6.2",
"@graphql-codegen/introspection": "^2.1.1",
"@graphql-codegen/typescript": "^2.4.7",
"@graphql-codegen/typescript-document-nodes": "^2.2.7",
"@graphql-codegen/typescript-graphql-request": "^4.4.2",
"@graphql-codegen/typescript-operations": "^2.3.4",
"@graphql-codegen/typescript-resolvers": "^2.5.4",
```

**Additional context**
* I have tested the above sample in the live demo
* https://www.apollographql.com/docs/federation/entities/#extending-entities

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.