urql icon indicating copy to clipboard operation
urql copied to clipboard

RFC: export the getOperationName utility function

Open giacomocerquone opened this issue 6 months ago • 2 comments

Summary

When logging out errors with a map exchange is useful to get the operation name. So whether we're doing a query or a mutation, like the following:

query empireHero {
  hero(episode: EMPIRE) {
    name
  }
}

I'm saying I'd like to have empireHero.

I've noticed you can grab it from the ast that the graphql parser generates and you already have a function called getOperationName to extract it correctly.

Proposed Solution

Either:

  • Export the getOperationName function from the codebase (like apollo does https://github.com/apollographql/apollo-client/blob/main/src/utilities/graphql/getFromAST.ts#L55).
  • Include it among the operation fields. This one would be quite invasive in terms of code changes and also a bit out of scope for the Operation type.

giacomocerquone avatar Jun 19 '25 22:06 giacomocerquone

Just to clarify, where query { empireHero: hero }, "empireHero" is the alias name, not the operation name. The operation name would be "empireHero" for query empireHero { ... }.

I'm not generally opposed to exposing the utility for this, although it's really quite simple, so it'd only really be for convenience

kitten avatar Jun 20 '25 10:06 kitten

Yes you're right @kitten I took the code example from the graphql documentation website and forgot to edit it properly! Going to edit it.

Simple or not, it's there! I mean, it's generally safer for devs, especially in typescript codebases, to use this instead of going scavenging for the right fields with the right types.

Furthermore, if it was for me, I would even avoid exporting a function but give it in the mapExchange in a field within the operation object. But as I've written, this might be a bit invasive and I'd avoid that.

giacomocerquone avatar Jun 20 '25 15:06 giacomocerquone