ardatan / ardatan/graphql-tools

On mutation of stitched schema, executed query on merged type

Open
#4,508 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
5.4k
Forks
830
Avg merge
10h 59m
Merged PRs (30d)
45

Description

### Issue workflow progress

_Progress of the issue based on the [Contributor Workflow](https://github.com/the-guild-org/Stack/blob/master/CONTRIBUTING.md#a-typical-contributor-workflow)_

- [ ] 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox
> Make sure to fork this template and run `yarn generate` in the terminal.
>
> Please make sure the GraphQL Tools package versions under `package.json` matches yours.
- [ ] 2. A failing test has been provided
- [ ] 3. A local solution has been provided
- [ ] 4. A pull request is pending review

---

**Describe the bug**

If a graphql query is `mutation`, at step 4 onward, `query` is being executed instead of `mutation`

![image](https://user-images.githubusercontent.com/20808792/172062871-14db57ce-df19-4c73-bed7-76a716ec7aa0.png)
https://www.graphql-tools.com/docs/schema-stitching/stitch-type-merging#merging-flow

If such field name is not defined in Query of stitched schema, the merged resolver fails silently returning null result.

**To Reproduce**

```bash
cd gateway
npm i
pip install awscli aws-sam-cli
sam local start-api -t ./local.yml -l message.log --host 0.0.0.0 -p 3000
```

Example 1: Has `Mutation.*User`, not `Query.*User`

https://github.com/flyingmilktea/graphql-bug-reproduce/tree/mutation-fwd-query-v1

```graphql
mutation {
# or oneUser, which zeroValue becomes null
zeroUser(id: 1) {
__typename
id
zeroValue
oneValue
user {
__typename
id
zeroValue
oneValue
}
}
}
```

Result:
```json
{
"data": {
"zeroUser": {
"__typename": "User",
"id": "1",
"zeroValue": "0: User: mutation",
"oneValue": null,
"user": {
"__typename": "User",
"id": "1",
"zeroValue": "0: User: mutation",
"oneValue": null
}
}
}
}
```

Example 2: Added `Query.*User` to both server

https://github.com/flyingmilktea/graphql-bug-reproduce/tree/mutation-fwd-query-v2

```graphql
mutation {
# or oneUser, which zeroValue becomes query
zeroUser(id: 1) {
__typename
id
zeroValue
oneValue
user {
__typename
id
zeroValue
oneValue
}
}
}
```

```json
{
"data": {
"zeroUser": {
"__typename": "User",
"id": "1",
"zeroValue": "0: User: mutation",
"oneValue": "1: User: query",
"user": {
"__typename": "User",
"id": "1",
"zeroValue": "0: User: mutation",
"oneValue": "1: User: query"
}
}
}
}
```

**Expected behavior**

```json
{
"data": {
"zeroUser": {
"__typename": "User",
"id": "1",
"zeroValue": "0: User: mutation",
"oneValue": "1: User: mutation",
"user": {
"__typename": "User",
"id": "1",
"zeroValue": "0: User: mutation",
"oneValue": "1: User: mutation"
}
}
}
}
```

**Environment:**

- OS: Linux 4.18.0-372.9.1.el8.x86_64
- "@graphql-tools/graphql-file-loader": "^7.3.14",
- "@graphql-tools/load": "^7.5.13",
- "@graphql-tools/schema": "^8.3.13",
- "@graphql-tools/stitch": "^8.6.12",
- "@graphql-yoga/node": "^2.8.0",
- "@vendia/serverless-express": "^4.8.0",
- NodeJS: v14.19.3

**Additional context**

https://github.com/ardatan/graphql-tools/blob/bccf6591cbedd69084d5abb8e35ec07f2a4267b3/packages/stitch/src/createMergedTypeResolver.ts#L5-L61

The problem likely caused by `operation: "query"` being hardcoded into default `createMergedTypeResolver`, actually changing to `operation: info.operation.operation` on both occurrence will fix it.

Still I do not understand the origin that, no error is thrown for the example of `v1` even it is a validation error at stitched schema (Attempt to run Query.fieldName while only Mutation.fieldName exists).
* For v1, In the case of using a remote executor at step 4, no query is received on the remote server.

Thanks @Abbywpy for help on testing the problem

Contributor guide

Open the contributing guide

Research direction

Start with packages/stitch/src/createMergedTypeResolver.ts at the referenced lines and reproduce the mutation cases from the linked repository using the gateway commands in the issue. Verify that merged type resolution preserves the mutation operation at each step, including when the field exists only on Mutation, and add coverage showing the expected mutation results.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.