Configuring keyArgs for types rather than fields?
See original GitHub issueHey,
I’ve seen that we can specify merge
function for types rather than fields. But since typePolicies.Type.keyArgs
does not exist - which seems normal since we are talking about a type, not a query field - am I forced to use Query.fields
for pagination?
To be more precise: I have an album containing multiple tracks with this schema:
type Album {
id: ID!
tracks(first: Int = 20, after: String): AlbumTracksConnection!
}
type AlbumTracksConnection {
edges: [AlbumTracksEdge!]!
pageInfo: PageInfo!
}
type AlbumTracksEdge {
cursor: String!
node: Track
}
When I want to paginate, I could use the following query:
query Album($albumId: String!, $nb: Int, $after: String) {
album(albumId: $albumId) {
id
tracks(first: $nb, after: $after) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
id
title
}
}
}
}
}
with this fetchMore
:
fetchMore({
variables: {
albumId: "13",
nb: 3,
after: data.album.tracks.pageInfo.endCursor,
},
})
and this typePolicies
:
typePolicies: {
Album: {
fields: {
tracks: relayStylePagination(),
},
},
}
Everything works perfectly.
But then, if I want to configure the typePolicies
for type over fields, since AlbumTracksConnection
is always paginated, I’d try:
typePolicies: {
AlbumTracksConnection: relayStylePagination(),
}
which does not work as the Album.fields.tracks
policy keeps its original keyArgs
value.
I would then need to specify Album.fields.tracks.keyArgs
to false in the typePolicies for the pagination to work. But then I would loose the benefit of being able to target types over fields name.
So the question is:
Do we want to be able to handle pagination/keyArgs via type, like we are able to handle merge
? It would ease the pagination for types.
Or am I doing something wrong with the pagination?
Probably related: https://github.com/apollographql/apollo-client/pull/7070#issuecomment-736799353
PS: In real life, I’d use a Paginable
type like so:
new InMemoryCache({
typePolicies: {
Paginable: relayStylePagination(),
},
possibleTypes: {
Paginable: ["AlbumTracksConnection", "AnythingConnection", ...],
},
});
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Having worked on pagination in the past few days, I’m not sure how your scenario would work.
relayStylePagination()
needs access toargs.after
andargs.before
to work. These are not defined at theAlbumTracksConnection
level, they are defined at the field level, in this case,tracks(first: $nb, after: $after)
, that’s whyrelayStylePagination()
is associated to thetracks
field. Case in point, you could have two different fields returning anAlbumTracksConnection
, but with different arguments for each fields.Type Policy
https://www.apollographql.com/docs/react/caching/cache-configuration/#disabling-normalization
Item.children.edges[0].node.children(where: {active: false}).edges
Item.children.edges[0].node.children(where: {active: true}).edges
active: false
queries will interfere with each otherItem.children(where: {active: false}).edges
Item.children(where: {active: true}).edges.node.items(where: {active: false}).edges
Ideas
@sebastienbarre it should be the type of the field that would be the referring type, which at that point you could supply the args for normalisation / manipulation.