SurveyMonkey / graphql-ergonomock

Automatic mocking of GraphQL queries
MIT License
19 stars 5 forks source link

test: added test cases for addTypename #108

Closed victor-guoyu closed 3 years ago

victor-guoyu commented 3 years ago

issue: https://github.com/SurveyMonkey/graphql-ergonomock/issues/104

Problem: ergonomock() only includes __typename if the query does

For the fix, I borrowed the addTypenameToDocument helper function from @apollo/client. As the result, every query will have __typename auto-appended if applicable.

That being said, I don't like that we introduced vendor specific dependency in the mock function. However, I also don't want to re-invent the wheel...

Please let me know if you know a better way to solve this.

cc @joual

joual commented 3 years ago

Yeah I think using addTypenameToDocument from Apollo makes a lot of sense in this case. As per the inline comment I would suggest moving it up one level to the MockLink to stay in line with Apollo's implementation.

victor-guoyu commented 3 years ago

@joual alright, sounds good! Just one question tho, do you foresee a case where we might want to add addTypename option directly on the ergonomock function?

I'm thinking about the users that using ergonomock directly instead of the MockedProvider

victor-guoyu commented 3 years ago

Actually I did more diggings on this one, if we use ErgonoMockedProvider directly, __typename will be automatically added, since InMemoryCache is calling the addTypenameToDocument for us.

I added couple test cases in the latest commit for this very scenario.

Here are the relevant code traces if you want to know more details about this:

Passing through addTypename to InMemoryCache https://github.com/SurveyMonkey/graphql-ergonomock/blob/master/src/apollo/ErgonoMockedProvider.tsx#L41

QueryManager.fetchQueryObservable trigging the document transform https://github.com/apollographql/apollo-client/blob/834c6ff65f520744f95094bc5f805b3bf558ea25/src/core/QueryManager.ts#L906-L914

QueryManger calling the transform function on cache https://github.com/apollographql/apollo-client/blob/834c6ff65f520744f95094bc5f805b3bf558ea25/src/core/QueryManager.ts#L464-L468

InMemoryCache calling the addTypenameToDocument function https://github.com/apollographql/apollo-client/blob/main/src/cache/inmemory/inMemoryCache.ts#L350-L353

I think @jfulse wants to have the addTypename option added directly to the ergonomock function. From the issue description, seems like he is using the MockedProvider from apollo rather than ErgonoMockedProvider.

cc @joual

jfulse commented 3 years ago

Apologies, I have not noticed this PR!

I think @jfulse wants to have the addTypename option added directly to the ergonomock function. From the issue description, seems like he is using the MockedProvider from apollo rather than ErgonoMockedProvider.

That is correct, that was my original request, and I was indeed using apollo's MockedProvider, not realising that my issue with ErgonoMockedProvider was that I had custom scalars. Thanks to your great work it seems that might soon no longer be an issue though!

victor-guoyu commented 3 years ago

I'm merging this PR since it's only containing the test cases for addTypename. I believe this is useful for us to prevent regressions in the future.

The fix for the actual issue (custom scalar type) is tracked under a seperate PR https://github.com/SurveyMonkey/graphql-ergonomock/pull/113

joual commented 3 years ago

:tada: This PR is included in version 1.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: