ardatan / ardatan/graphql-tools
Cannot mock a field being undefined or null
- 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**
It seems impossible to simulate a situation where you are returned a partial set of the fields you have requested in a query. This is a very common scenario I want to test. Your UI needs to be able to handle that situation. Seemingly no matter what I try, graphql-mock returns an error when I attempt to do this. I would really appreciate help.
**To Reproduce**
Steps to reproduce the behavior:
```
const typeDefs = `#graphql
type AuthConfig {
twoFactorEnabled: Boolean
}
type OrgConfig {
auth: AuthConfig
defaultSecurityPolicy: String
}
type Query {
getOrgConfig: OrgConfig
}
`;
const query = /* GraphQL */ `
query getOrgConfig {
getOrgConfig {
auth {
twoFactorEnabled
}
defaultSecurityPolicy
}
}
`;
const mockSchema = addMocksToSchema({
schema: makeExecutableSchema({
typeDefs,
}),
mocks: {
AuthConfig: () => undefined,
},
});
graphql(mockSchema, query).then((result) => console.log("Got result", result));
```
**Expected behavior**
I expect a partial return value to be acceptable, as it is in real, not mocked, GQL.
If I try to mock AuthConfig as undefined or null I get the following
```
Got result {
errors: [
Error: Value returned by the mock for AuthConfig is not an object.......
```
This make sense given the following code: https://github.com/ardatan/graphql-tools/blob/0f149dc3ac1195c4c4457f703f303e381e4fb44f/packages/mock/src/MockStore.ts#L433
My main question is how are you supposed to simulate a partial result situation if the Mocking library mandates a value for **every** field.
**Environment:**
- OS: Mac
- ├─ @graphql-tools/delegate@7.0.1
│ └─ @graphql-tools/schema@7.0.0
├─ @graphql-tools/merge@6.2.5
│ └─ @graphql-tools/schema@7.0.0
├─ @graphql-tools/mock@8.7.6
│ └─ @graphql-tools/schema@9.0.4
├─ @graphql-tools/schema@8.3.2
└─ graphql-config@3.0.3
└─ @graphql-tools/schema@7.0.0
- NodeJS: 14.18.1
Contributor guide
Research direction
Start with the reproduction using addMocksToSchema and the AuthConfig mock, then inspect packages/mock/src/MockStore.ts around the linked line where non-object mock values are rejected. Compare the behavior with GraphQL's nullable-field expectations. Done means the undefined/null partial-result case has a clearly defined supported behavior and regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- api, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100