GraphQL Aliases
travis-r6s opened this issue · 4 comments
Is there anyway to specify an alias in the query object?
My use case - I have a list of product ID's that I want to fetch from shopify. I want to fetch the products all at once, so would usually programatically create a gql string like so:
query products {
[id]: product (...) {..}
[id]: product (...) {..}
[id]: product (...) {..}
[id]: product (...) {..}
...
}
I can create an object similar to the above to pass into query, but obviously can't have multiple product: [{}]
as they are duplicate keys. Or perhaps there is a way to create a chain with a number of queries?
Hope that makes sense..?
Aliases are not supported right now, because I couldn't think of a nice syntax that would allow describing aliases in TS, while also preserving auto-completion support :)
Suggestions are welcome
this looks reasonable to me, and also it would be easy to preserve typing and intellisense
client.query({
originalQueryName: {
__alias: {
newQueryName: {
field: true,
},
},
},
})
Maybe something along the lines of:
client.aliased.query({
first: {
product: [input, {
}]
},
second: {
product: [input, {
}]
}
})
I think at type level it should be possible to turn QR
into union of records each containing a single field providing type for the aliased field. Return also could be type mapped to corresponding Q
s.
I've just ran into a situation where I need to use aliases in my query and I would love to see this added. I would've expected something similar to how on_[Model]
works at the moment, for example the key would be: alias_[aliased name]_[real field name]
(in this case alias_specificProduct_product
) and the value would be a normal query object. I understand there's no alias
keyword in GraphQL so it may not read like the original but feels intuitive enough to fit into an existing query. But I'm not fussed and I think both suggestions above are good as well 🙂