Open jmtimko5 opened 2 years ago
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 if should be null ? then null
in each Query/Mutation field resolver that I can control almost like the mock store
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: 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