How do I distinguish between two queries with the same __typename to get Apollo Client typePolicies cache to work properly

Viewed 284

I have two GraphQL queries from our GraphQL server (see below), but they both have the same __typename: "__typename": "RelatedNodes",.

I am building a Carousel with pagination using Apollo Client fetchMore function. When I use just one query all works fine.

But when I use both queries, Apollo Client cache gets confused.

The Apollo Client typePolicies configured like:

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

Query one:

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

Which returns:

{
  "data": {
    "getNodesTypeOne": {
      "__typename": "RelatedNodes",
      "nodes": [
        {
          "uuid": "ac6ea",
          "type": "TypeOne",
          "title": "Foo"
        },
        {
          "uuid": "7768",
          "type": "TypeOne",
          "title": "Foo bar"
        },
        {
          "uuid": "1e07e",
          "type": "TypeOne",
          "title": "Foooo"
        },
      ]
    }
  }
}

And query two:

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

Which returns:

{
  "data": {
    "getNodesTypeTwo": {
      "__typename": "RelatedNodes",
      "nodes": [
        {
          "uuid": "76676",
          "type": "TypeTwo",
          "title": "Foo"
        },
        {
          "uuid": "89896",
          "type": "TypeTwo",
          "title": "Foo bar"
        },
        {
          "uuid": "45215",
          "type": "TypeTwo",
          "title": "Foooo"
        },
      ]
    }
  }
}

How do I have to set up my typePolicies so that the cache can distinguish between the two queries? Or do I have to change the GraphQL server types?

0 Answers
Related