apollographql / apollographql/graphql-testing-library

Undefined Resolvers Have a Default?

Open
#160 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
61
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Editing this, I was confused about getting mock responses for queries I haven't mocked. This should be clearly communicated and it should be intuitive to opt out.

https://github.com/apollographql/graphql-testing-library/blob/05fed23d8414abef1cb00f70ec2cbee121f477c6/src/handlers.ts#L69

Before - getting unexpected responses:
```ts
import { createHandler } from '@apollo/graphql-testing-library'
import { TypeDefs } from '@/data/graphql/generated/typedefs'
import { Resolvers } from '@/data/graphql/generated/resolvers'

const mockRequestHandler = createHandler({
typeDefs: TypeDefs,
resolvers: {},
delay: 300
})

export default mockRequestHandler

```

After:
```ts
import { createHandlerFromSchema } from '@apollo/graphql-testing-library'
import { TypeDefs as typeDefs } from '@/data/graphql/generated/typedefs'
import { Resolvers } from '@/data/graphql/generated/resolvers'
import { makeExecutableSchema } from '@graphql-tools/schema'

const mockRequestHandler = createHandlerFromSchema({
schema: makeExecutableSchema({ typeDefs }),
delay: 300
})

export default mockRequestHandler
```

The second issue that is not expected either, that now I don't get the default mocks but I get null responses instead. My expectation is that this would be considered unhandled by MSW and I could clearly see that with the default `onUnhandledRequest` option.

I understand that in terms of HTTP this is the same route but I'm sure it can be conditional, right?

results in
```json
{
"errors": [
{
"message": "Cannot return null for non-nullable field Query.myQuery.",
"path": [
"myQuery"
]
}
],
"data": null
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at src/handlers.ts around line 69 and trace how createHandler handles an empty resolvers object versus createHandlerFromSchema. Review the MSW onUnhandledRequest behavior described in the issue and determine the expected distinction between default mocks, null responses, and unhandled requests. Done should make undefined resolvers behavior explicit and provide an intuitive opt-out.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.