-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Polymorphic input types #114
Comments
Maybe some way of escaping the type system on input? Accepting a map os string to anything? as far as I can tell, everything in the input has to be type checked, am I missing something? |
I also think that this feature would be a valuable addition to GraphQL. I have similar feature in our API. We model our mutations as mutation UpdateProduct {
updateProduct(id: "1234", version: 1, actions: [
{type: "SetName", newName: "foo"},
{type: "AddPrice", price: 123, currency: "USD"}
])
} It is also important in our case that all these update actions come in one list, since we guarantee that they all would be applied atomically. An ability to define an input There is also a discussion around input |
+1 |
+1 |
This is something we want to consider. Interfaces may be less valuable because it becomes less clear what the semantics should be, though Unions are pretty interesting. This is all interesting area to explore further. |
I would think support for polymorphic types would be desired as a first class feature. I'm guessing from this issue and the comments that it isn't, yet. |
You could think of a union input type as an Input Object type which only allows a single field to be specified per input object value. Unfortunately that would need to be enforced with runtime checks rather than static validation. A union input type could be represented in a similar way, where a possible type is used in place of a field, and static validation can ensure that only a single type is specified. E.g. |
There is another (pretty simple) use case for this feature: a required input parameter that can have one of two possible scalar values. I've seen that the SWAPI-Example suffers from the same issue (with |
Same use case as @bwaidelich for me: I'm retrieving an entity from either one of its alternate identifiers. Currently the But I'd like to use a Current schema ...
product:
type: productType
description: "Retrieves product details, given either a Portfolio or Produit entity id."
args:
eid:
type: new GraphQLNonNull( GraphQLID)
description: "Product entity identifier, or entity identifier of one of its constitutive portfolios." Query: query productByCanonicalEID {
product( eid: "ENTITY-ID-IN-CANONICAL-FORM") { ...ProductFull }
}
query productByAlternateEID {
product( eid: "ENTITY-ID-IN-ALTERNATE-FORM") { ...ProductFull }
} Desired schema ...
ProductEntityIDType = new GraphQLScalarType { ... }
PortfolioEntityIDType = new GraphQLScalarType { ... }
entityIDUnionInputType = new GraphQLUnionInputType {
name: 'EntityID'
types: [ ProductEntityIDType, PortfolioEntityIDType ]
resolveType(value) {
# Lets assume the entity identifiers obey a naming convention,
# they start with specific letters, which we can derive the type from
if value.startsWith( 'PO')
return PortfolioEntityIDType
if value.startsWith( 'PR')
return ProductEntityIDType
}
}
...
product:
type: productType
description: "Retrieves product details, given either a Portfolio or Produit entity id."
args:
eid:
type: new GraphQLNonNull( entityIDUnionInputType) # <– Desired change
description: "Product entity identifier, or entity identifier of one of its constitutive portfolios." Query: the same, but argument |
Also was kinda surprised that it is not available. Though I understand why that is. Unlike with queries, this will probably have to be supported by syntax. Something like (using example above):
As I could not find a ready solution, I made my own with some workarounds using existing syntax: Union Input Type. You could probably also use it in place of an interface. |
Any news on this? It will be really helpful for many people I think. |
Did this ever make it to the roadmap? |
I have a pagination input type:
sortBy can be reference any type of field, thus, the after property must be a value of the referenced field.
I can't set the schema to validate after as a Date type because the sortBy can reference a String field or a number field.
With this, the after field can be validated. |
https://graphql.org/learn/pagination/
So the cursor can be an ID (lets presume some UUID) and an offset (a number). |
It is 2019, we need this... |
Could you see if the solution discussed in #395 (comment) would solve your needs? It's due to be discussed at the next WG. |
@benjie It looks great, I think it would do the job. |
We have begun building a single RFC document for this feature, please see: The goal of this document is to consolidate all related ideas to help move the proposal forward, so we decided to close all overlapping issues including this one. If you want to add something to the discussion please feel free to submit PR against the RFC document. |
We have an interface
Customization
and around 20 types implementing this interface. The number of subtypes ofCustomization
will grow over time, we are even considering adding subtypes dinamically.Each concrete type has it own set of attributes.
On the query side this is easy to represent. The problem is at the mutation side. We want to have a mutation method to add a customization to another object
addCustomizationToNode(nodeId: ID, customizationInput: CustomizationInput)
but this is not currently possible to express since Input Objects cannot have interfaces, we would rather not have one mutation field per subtype, specially since those can change dynamically, potentially.
Do you have any suggestions? Would make sense to have InputObjects take interfaces?
Thank you!
The text was updated successfully, but these errors were encountered: