graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

addMocksToSchema uses 2 stores

Open lipsumar opened this issue 3 years ago • 1 comments

Describe the bug

When using addMocksToSchema() and providing a store, I expect this store and this store only to be used. Instead, in some cases the default mockStore created by addMocksToSchema (here) is used instead.

To Reproduce

The issue is reproduced in this repo: https://github.com/lipsumar/repro-graphql-tools-mock-store-issue

Expected behavior

I expect the default mockResolver to always use the same store.

Environment:

  • OS: Mac OS 11.6.2
  • @graphql-tools/mock: 8.5.1,
  • @graphql-tools/schema: 8.3.1
  • graphql: 16.2.0
  • NodeJS: v16.4.2

Additional context

The function addMocksToSchema can be called with an optional store parameter. This user-provided store is named maybeStore. In addition, another store is created, named mockStore.

Right after follows a const store = maybeStore || mockStore;, in order to use the user-provided store or fallback to the mockStore if user didn't provide one.

Next, in the function mockResolver function, all calls to the store are using the variable store, except for this line that's using mockStore instead.

I believe this is not the expected behaviour, as 2 different stores are used instead of one.

This leads to unwanted behaviour as demonstrated in the reproduction repo.

I can provide a PR to fix this, but I first want to confirm the bug.

lipsumar avatar Jan 21 '22 10:01 lipsumar

@ardatan is it worth it for me to make a PR to fix this ?

lipsumar avatar Feb 24 '22 10:02 lipsumar