How to create Apollo Client read function to read a field from a nested object

673 Views Asked by At

I need to distinguish between two queries with the same __typename (Union) to get Apollo Client typePolicies cache to work properly.

So RelatedNodes is a Union and I don't get a unique identifier field from the server.

The nodes are differentiated by a field called type. See the query:

query GetNodesTypeOne($limit: Int, $offset: Int,) {
  getNodesTypeOne(limit: $limit, offset: $offset) {
    __typename
    nodes {
      uuid
      type
      title
    }
  }
}

I want to use that field nodes.type to create a unique identifier, which I can use in the keyFields property (like keyFields: ['type']).

The Apollo Client typePolicies configured like:

typePolicies: {
    RelatedNodes: {
      keyFields: [],
      fields: {
        nodes: offsetLimitPagination(),
      },
    },
  },

What I am trying:

Adding a local only field to my query:

   query GetNodesTypeOne($limit: Int, $offset: Int,) {
      getNodesTypeOne(limit: $limit, offset: $offset) {
        type @client // the field a want to use in the typePolicies
        nodes {
          uuid
          type
          title
        }
      }
    }

Then with Apollo Client read function, I want to create a type field which gets it 's value from nodes.type?

Is that possible?

0

There are 0 best solutions below