Skip to content
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

Add support for named GraphQL operations #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mislav
Copy link

@mislav mislav commented Jul 28, 2020

For purposes of both server-side metrics and stubbing of HTTP requests in client-side tests, we need all GraphQL operations to be named; e.g.

query FindUser{...}
mutation AddReaction{...}

This library did not support any facilities to name queries. Furthermore, adding an extra name argument to current methods would compromise backwards compatibility. #12

This proposes a declarative, backwards-compatible interface for naming queries:

// old, unnamed queries
client.Query(context.Background(), &q, vars)

// named queries
client.Query(context.Background(), graphql.NamedOperation("FindUser", &q), vars)
client.Mutate(context.Background(), graphql.NamedOperation("AddReaction", &m), vars)

@mislav mislav force-pushed the named-queries-declarative branch from 291e061 to a4a48d3 Compare July 28, 2020 18:34
@dmitshur
Copy link
Member

dmitshur commented Aug 5, 2020

Thanks for the PR. I'll try to get to reviewing it, but it may not happen right away.

I hope you don't mind continuing to use your fork in the meantime, and please let me know if you learn anything more about this API and approach as you use it.

@mislav
Copy link
Author

mislav commented Aug 6, 2020

Sure, take your time! We are already using a fork in GitHub CLI.

@anagrius
Copy link

LGTM

@jdoss
Copy link

jdoss commented Nov 6, 2020

Can we perchance get this merged? GitHub CLI is using a fork of this and it is making things difficult w/r/t packaging the GitHub CLI in Fedora.

Copy link

@parkr parkr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @dmitshur, are you happy with this PR?

@bdentino
Copy link

I'm having a hard time getting this to work, but I am a golang novice so I'm not sure if it's something I'm doing wrong. It seems that client.Query(ctx, &q, vars) is still trying to unmarshal the GraphQL response data directly into the 2nd function argument, which doesn't work when you pass in a NamedOperation. Am I missing something?

@mislav
Copy link
Author

mislav commented Apr 28, 2021

@bdentino Apologies; the deserialization was never actually tested. I've mostly submitted it as a suggestion of a potential approach.

I've just pushed a commit that deserializes to the correct struct. Try it out!

@obukhov
Copy link

obukhov commented Jun 23, 2021

This one would be nice to have merged. I think it is general GraphQL best practice to have operations named.

@rocktavious
Copy link

+1 on merging this - i need this functionality

@m3libea
Copy link

m3libea commented Jun 8, 2022

+1 on merging this!

@fillup
Copy link

fillup commented Dec 1, 2022

@dmitshur any chance this could be merged soon? Atlassian will soon be requiring named operations for calls to their GraphQL APIs which will break our integrations and force us to maintain/use a fork of this or do something else. It sounds like from the comments here that this is well tested and ready to go.

All responses from Atlassian's GraphQL API right now include:

{
  "errors": [
    {
      "message": "Warning : You MUST provide a query operation name.  In the future operations without names will be banned.",
      "extensions": {
        "statusCode": 400,
        "classification": "ExecutionAborted"
      }
    }
  ]
}

Fortunately they still return the response data I'm asking for so I'm able to inspect the error and ignore it in this case, but that's an ugly band aid that won't last for very long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.