-
-
Notifications
You must be signed in to change notification settings - Fork 816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot mock a field being undefined or null #4792
Comments
Nice, since 2018 without null mocks.. it's like graphql doesn't have support for null returns! (current mocks are amazing tbh, its just missing this) I made a custom solution which I'm yet to try on a real test codebase: const schema = buildSchema(typeDefs)
const store = createMockStore({schema})
const nullStore = {
_nulls: {} as Record<string, Set<string | undefined>>,
has(key: string, id: string): boolean {
return !!this._nulls[key]?.has(id)
},
add(key: string, id: string) {
if (!this._nulls[key]) {
this._nulls[key] = new Set()
}
this._nulls[key].add(id)
},
delete(key: string, id: string) {
if (!this._nulls[key]) {
return
}
this._nulls[key].delete(id)
},
nullOrGet: (key: string, id: string) => {
return nullStore.has(key, id) ? null : store.get(key, id);
}
}
const schemaWithMocks = addMocksToSchema({
schema,
store,
resolvers: () => ({
Query: {
fooById(_, {id}) { return nullStore.nullOrGet('Foo', id) }
}
})
})
const query = /* GraphQL */ `
query Foo {
foo(id: 1) {
id
}
}
`
nullStore.add('Foo', '1')
const isNull = await graphql({ schema: schemaWithMocks, source: query })
console.log('Got result', isNull);
nullStore.delete('Foo', '1')
const isNotNull = await graphql({ schema: schemaWithMocks, source: query })
console.log('Got result', isNotNull); Essentially, an |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Issue workflow progress
Progress of the issue based on the Contributor Workflow
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:
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
This make sense given the following code:
graphql-tools/packages/mock/src/MockStore.ts
Line 433 in 0f149dc
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
The text was updated successfully, but these errors were encountered: