Khan / genqlient

a truly type-safe Go GraphQL client
MIT License
1.07k stars 107 forks source link

Cannot use Go keywords as variable names in mutation. #286

Closed mcncl closed 1 year ago

mcncl commented 1 year ago

Describe the bug It's not possible to use Go keywords as a variable in a GraphQL query. This means I cannot have a variable type, for example in my mutation.

To Reproduce Using the following in my sso.graphql file:

mutation SSOProviderCreate(
  $type: SSOProviderTypes!
  $note: String
) {
  ssoProviderCreate(
    input: {
      type: $type
      note: $note
    }
  ) {
    ssoProviderEdge {
      node {
        id
      }
    }
  }
}

I am unable to successfully run the generate command as I get the following error:

go run github.com/Khan/genqlient
app/graphql/sso.graphql:2: variable name must not be a go keyword
exit status 1
make: *** [generate] Error 1

If I remove the type then I do not receive this error. I believe this is due to a change made in https://github.com/Khan/genqlient/pull/195#issuecomment-1641313276, though I agree with the change, it might be sensible to offer a means of mapping to the name on the GraphQL server rather than forcing folks to not use a plethora of reserved keywords.

Obviously, in a Query I can use ailasing if need be with providerType: type, but that's not possible with a Mutation.

Expected behavior I expect that the generate command will complete.

genqlient version github.com/Khan/genqlient v0.6.0

Additional context I tried the examples mentioned in the PR already and the initial comment on that PR was made as a result of an internal escalation I raised due to this issue questioning what the impact would be on changing our type to be a non-Go keyword such as providerType, but this seems like more of an oversight with Genqlient as we cannot be the only team using a reserved keyword of some kind in our API.

benjaminjkraft commented 1 year ago

You can just change the variable name!

mutation SSOProviderCreate(
  $typ: SSOProviderTypes! # <--
  $note: String
) {
  ssoProviderCreate(
    input: {
      type: $typ # <--
      note: $note
    }
  ) {
    ssoProviderEdge {
      node {
        id
      }
    }
  }
}

or to $type_, $ssoType, etc. -- point is that's something that you set, not something that has to match the schema.