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

[mock][store] Storing an object that its id is different with the key

Open omid-ebrahimi opened this issue 2 years ago • 1 comments
trafficstars

I want to store a customer by its token. This is a sample code:

store.set('Customer', token, customer);

But I get this error:

Field id is a key field of Customer and you are trying to set it to `<customer-id>` while the key is `<token>`

It would be great if I can store objects with any arbitrary key.

Why do we need this feature? Using a token as a key, we can have multiple versions of any mocked customer. Then the mock server will be able to handle multiple e2e tests that are running for the same customerId in different sessions.

omid-ebrahimi avatar Aug 10 '23 14:08 omid-ebrahimi

Would changing the typePolicy help you in this case?

Note that, by default, the id or _id field will be used as a storage key and the store will make sure the storage key and the field value are equal. You can change the key field using the option typePolicies.

const store = createMockStore({
    schema,
    typePolicies: {
      Customer: {
        keyFieldName: 'token'
      }
    }
  });

esquevin avatar Aug 24 '23 15:08 esquevin