Apollo-client: InMemoryCache.writeData doesn't respect custom scalar data

Created on 8 Mar 2019  ·  2Comments  ·  Source: apollographql/apollo-client

Intended outcome:

My GraphQL schema has scalar JSON.
and, It contains object and array.
My application has a below steps.

  1. user choose data (it contains schema-less JSON data) from the list.
  2. user will modify the data and submit it to server.

I made reproduction code. https://codesandbox.io/s/o4r2lk7nmz

I'm reading https://www.apollographql.com/docs/graphql-tools/scalars.html .
It explains server side implementations, but client side is not.

Actual outcome:

  1. user choose data (it contains schema-less JSON data) from the list.
  2. user will modify the data and submit it to server.

I implement step 1 by GraphQL query (fetch list from server) and mutation (update local state by @client mutation).
I used cache.writeData to hold data that is selected by user.
but cache.writeData can't handling scalar JSON data.

Uncaught (in promise) Invariant Violation: Missing selection set for object of type undefined returned for query field data
    at new InvariantError (https://o4r2lk7nmz.codesandbox.io/node_modules/ts-invariant/lib/invariant.js:16:28)
    at assertSelectionSetForIdValue (https://o4r2lk7nmz.codesandbox.io/node_modules/apollo-cache-inmemory/lib/bundle.cjs.js:463:90)
    at StoreReader.executeField (https://o4r2lk7nmz.codesandbox.io/node_modules/apollo-cache-inmemory/lib/bundle.cjs.js:401:13)
    at eval (https://o4r2lk7nmz.codesandbox.io/node_modules/apollo-cache-inmemory/lib/bundle.cjs.js:350:55)

I found some suspicious parts.

queryFromPojo doesn't respect for custom scalar type.

I tried use cache.writeData with id, but this code can't get result. because I want to write data to it, but this code runs before write.
and this code overwrite my __typename by __ClientData always.
cache.writeData ignore my request silently.

How to reproduce the issue:

https://codesandbox.io/s/o4r2lk7nmz
↑ PRESS EXECUTE MUTATION button

Versions

  System:
    OS: macOS 10.14.3
  Binaries:
    Node: 11.10.1 - ~/.nodebrew/current/bin/node
    Yarn: 1.13.0 - ~/.nodebrew/current/bin/yarn
    npm: 6.7.0 - ~/.nodebrew/current/bin/npm
  Browsers:
    Chrome: 72.0.3626.121
    Firefox: 62.0
    Safari: 12.0.3
  npmPackages:
    apollo-cache-inmemory: ^1.5.1 => 1.5.1
    apollo-client: ^2.5.1 => 2.5.1
    apollo-link: ^1.2.8 => 1.2.9
    react-apollo: ^2.5.1 => 2.5.2

Most helpful comment

I found the work around.

I read this article carefully again.
https://www.apollographql.com/docs/react/advanced/caching.html#writequery-and-writefragment
https://www.apollographql.com/docs/react/advanced/caching.html#cacheRedirect
I avoid to use cache.writeData and use cache.writeQuery, cache.writeFragment, cacheRedirects option.

This issue still remains.

All 2 comments

I found the work around.

I read this article carefully again.
https://www.apollographql.com/docs/react/advanced/caching.html#writequery-and-writefragment
https://www.apollographql.com/docs/react/advanced/caching.html#cacheRedirect
I avoid to use cache.writeData and use cache.writeQuery, cache.writeFragment, cacheRedirects option.

This issue still remains.

writeData has been removed from Apollo Client 3. For the full details, see: https://github.com/apollographql/apollo-client/pull/5923

Was this page helpful?
0 / 5 - 0 ratings