apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

Apollo 3 typePolicies - pass parent entity inside Context to generate keyArgs based on top of args + entity

Open ovr opened this issue 5 years ago • 0 comments

Hey!

Apollo 3 introduces new mechanics called typePolicies, and it provides an api called keyArgs to generate id based on top of args & context, but sometimes it's needed to get "parent" entity too to generate id.

For example

query GetPodcastWithEpisodesQuery($id: String!) {
    podcast(id: $id) {
        id,
        title,
        episodes(order: { published: DESC }) {
            edges {
                node {
                    id,
                    title,
                    pid
                }
            },
            pageInfo {
                endCursor,
                hasNextPage,
            },
            aggregate {
                count,
            }
        }
    }
}

Problem

I am interested to create calculate keyArgs for episodes based on top of ${podcast.id}-${args.order.published}, but I am not able to do it, because keyArgs didnt pass "parent type"

return new InMemoryCache({
    typePolicies: {
        podcast: {
            fields: {
                episodes: {
                    keyArgs: (args: any, context) => {
                        // I am not able to get parent entity
                        const podcast = { id: 5 };

                        return podcast.id + buildQueryCacheFromObject(args, ['order']);
                    },
                }
            }
        }
    },
});

Thanks

ovr avatar Jul 17 '20 11:07 ovr