Pete
Pete

Reputation: 3451

With GraphQL Apollo Client, How To set @client fields to store in Cache object?

I have an Apollo Query set as I have below.

query { 
  speakers {
    id
    name
    checkBoxField @client
  }
}

I have a field policy set as follows:

export const genericBoolean = cache.makeVar(false);

typePolicies: {
   Speaker: {
      fields: {
        checkBoxField: {
           read(checkBoxField = false) {
             return genericBoolean === true ? "true" : "false";
           ...

I have a button on my page where I use useQuery defined and that button executes the code:

onClick : genericBoolean(true)

When I dump (JSON.stringify) the data returned from useQuery, the checkBoxField does change to true for all rows as expected, but in my Apollo Cache (as seen from chrome extension), there is no field stored for checkBoxField. I want that there so I can iterate through my cache and get the set valued for each row.

Upvotes: 0

Views: 474

Answers (1)

mer10z_tech
mer10z_tech

Reputation: 695

According to the Apollo docs, you can use reactive variables OR the Apollo client cache to store local state: https://www.apollographql.com/docs/react/local-state/managing-state-with-field-policies/#storing

Your example above is using reactive variables. To store your data in the cache you would need to use writeQuery or writeFragment: https://www.apollographql.com/docs/react/local-state/managing-state-with-field-policies/#storing-local-state-in-the-cache

Upvotes: 1

Related Questions