Graphql cache: Specify a merge function to a query which is nested under a common type

Graphql cache: Specify a merge function to a query which is nested under a common type


0

Lets say graphql scema looks like this

type query {
    foodInformation: Foodinformation

     # some other query types
    carInformation: CarInformation
}

type Foodinformation {
  getDishes(): Dishes!
  getFilteredDishes(input: filterInput): Dishes!

  # some other quiries
}

type PageInformation {
 pageNumber: int
 pageSize: int
 totalPages: int
 totalItems: int
}

input FilterInput {
 name: string
 id: int
 comment: string
 pageNumber: int
 pageSize: int
}

type Dishes {
 dish: [Dish]
 pageInformation: PageInformation
}

type Dish {
 recipes: [Recipe]
}

type Recipe {
 id: int
 name: string
 amoun: int
 comment: string
}

Now lets say I call getDishes() using useQuery with filterInput for params.
I’m populating a table based on the result I get from the response / Dishes object.
Now the question is how can i define the cache policy to merge the array objects for pagination

const cache = new InMemoryCache({
  typePolicies: {
    Foodinformation: {
      fields: {
        getDishes: {
          keyArgs:["dish", ["id"]],
          merge(existing = { dishes: [] }, incoming) {
            const { dishes: existingDishes } = existing;
            const { dishes: incomingDishes, pageInformation } = incoming;

            const mergedPageInfo = pageInformation || existing.pageInformation;

            const mergedDishes = [...existingDishes, ...incomingDishes];

            return {
              dishes: mergedDishes,
              pageInformation: mergedPageInfo,
            };
          },
        },
      },
    },
  },
});

merging list is not working.

I’ve tried ommiting the Foodinformation in the typePolicy config, but then graphql complains that Foodinformation will override other objects since there’s no identifier for it.

I’ve other cases where the query is directly under root query type and they work fine. But we have this category graphqltypes as FoodInformation, CarInformation YadayadaInformation which we need to have to structure up all our queries, now I don’t realy know how I can merge arrays that are basically 3 levels deep

any suggestions?


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *