graphql-typed-client icon indicating copy to clipboard operation
graphql-typed-client copied to clipboard

GraphQL Aliases

Open travis-r6s opened this issue 4 years ago • 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..?

travis-r6s avatar Nov 01 '19 14:11 travis-r6s

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

helios1138 avatar Jan 23 '20 11:01 helios1138

this looks reasonable to me, and also it would be easy to preserve typing and intellisense

client.query({
  originalQueryName: {
    __alias: {
      newQueryName: {
        field: true,
      },
    },
  },
})

pierissimo avatar Apr 28 '20 08:04 pierissimo

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 Qs.

Gozala avatar May 20 '21 18:05 Gozala

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 🙂

Nevvulo avatar Jul 15 '21 06:07 Nevvulo