-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypePolicy on a request level #6949
Comments
I have the exact same set-up/use case. I wish to avoid adding identical merge type policies for every kind of type that uses my pagination helper. I was giving this some thought and came up with the following idea: All of my types that can be paginated via my helper implement a GQL
|
@vigie I agree that being able to define policies for abstract interface/union types (which could then be inherited by subtypes) would be a great improvement. Do you think that would cover all of your needs, or do you have use cases where you need even more flexibility than inheritance would provide? For example, the @tomprats Does this inheritance idea make sense for your case? Happy to explain in more depth if you're not sure what this would look like. |
@benjamn I think it definitely helps. I'd still prefer being able to do something on the request level, but this would definitely be the next best thing. |
Hi @benjamn I also have the problems described in #6808 but I think I'd rather address them in a more declarative way, allowing types (of all flavors - user defined, abstract and even custom scalar) to declare policies that include the ability to declare default merge (and other) behavior. For example, we currently make quite heavy use of the custom JSONObject scalar (this allows for a nice shorthand allowing us to work with data coming back from a legacy REST service before we are ready to fully formalize that data into the schema). In order to upgrade without warnings, I need to go through my entire schema and declare a field policy for all usages of this scalar to declare "replace, don't merge". For example, for a type
Instead, I would like to be able to attach default merge policies directly to types:
This idea also corrects a mistake I make in my pagination use case I sketched above. There, it's not that I want to attach fieldPolicies to an abstract type (although that will definitely be useful in other scenarios and should be allowed), it's that again, I want to attach a default merge policy directly to the type:
As I'm sure you know, the interaction choices between a merge policy on a type and a field are:
I'm not sure which would be most appropriate, interested to hear your thoughts. If we had this ability then, at least as of today, I don't think I would also need the dynamic |
Motivating discussion: #6949 (comment) JavaScript developers will be familiar with the idea of inheritance from the `extends` clause of `class` declarations, or possibly from dealing with prototype chains created by `Object.create`. In a more general sense, inheritance is a powerful code-sharing tool, and it works well for Apollo Client for several reasons: 1. InMemoryCache already knows about the supertype-subtype relationships (interfaces and unions) in your schema, thanks to possibleTypes, so no additional configuration is necessary to provide that information. 2. Inheritance allows a supertype to provide default configuration values to all its subtypes, including keyFields and individual field policies, which can be selectively overridden by subtypes that want something different. 3. A single subtype can have multiple supertypes in a GraphQL schema, which is difficult to model using the single inheritance model of classes or prototypes. In other words, supporting multiple inheritance in JavaScript requires building a system something like this one, rather than just reusing built-in language features. 4. Developers can add their own client-only supertypes to the possibleTypes map, as a way of reusing behavior across types, even if their schema knows nothing about those made-up supertypes. Inheritance is a step back (in terms of freedom/power) from the completely dynamic policyForType and policyForField functions I proposed in #6808 (comment), but I think inheritable typePolicies will go along way towards addressing the concerns raised in #6808.
@vigie @tomprats See #7065 for the inheritance implementation. @vigie I have another PR in the works that I think does basically what you're asking, by allowing you to mark any type as |
Thanks for working on this. I would specifically need the ability to set mergeable to false and also to a custom function - will that also be handled in your pending PR? |
I don't really know if this is about solving pagination when you have nested connections that you want to paginate? Here is an example, let's say this is my schema:
the data structure will then be in an ideal world I would like to specify:
and call How can I solve this? |
@pontusab , I have exactly the same usecase your defining with relayBased nested connections.
Just as you , i had hoped to specify the typePolicy as followed:
But that's unfortunately not possible at the moment. How did you end up solving this? |
@michel Yeah I solved my issue by:
|
I have a similar issue on migrating from v2 to v3 on cache updating. I used the same query while with different view types in my chat application. Here is the sample query query Chatmessages(...) {
chatmessages(filter: $filter, limit: $limit, offset: $offset, order: $order) {
...Chatmessage
}
} The main part displays all messages in an infinite-scrolling method, where the following policy works great.
While the other part displays logs of messages in a pages, where only
As updateQuery is deprecated, is there a workaround for making both work now in v3? |
The original issue here was solved by #7070. Please open subsequent issues separately. Thanks! |
I haven't worked with graphql a lot so I'm a bit out of my element. In my project, all the list queries are specific types like In comparison are yours of type Connection? So it looks like I'd still have to specify the type policy on each of these individual connections. Just wanted to double check if I'm missing something |
With the deprecation of
updateQuery
on afetchMore
, there doesn't seem to be a way to specify its replacement, a TypePolicy, in the same place. I understand that you can specify it on specific entities as seen in the docs and below:But in my use-case, I have a helper method used by various entities with the list query passed in:
I'm not sure how I can convert this method to using type policies instead. There are a couple of ways to avoid this issue:
It'd make more sense to me if
useQuery
orfetchMore
accepted atypePolicy
, similar to how it accepts afetchPolicy
. That may be a naive statement, but there just seems like there should be an equivalent way of handling the issue ifupdateQuery
is going away.The text was updated successfully, but these errors were encountered: