ashkan18 / graphlient

Ruby GraphQL Client
MIT License
251 stars 43 forks source link

Mutation #61

Closed leoGalani closed 5 years ago

leoGalani commented 5 years ago

Hi!

I'm trying to set a mutation as a 'string' but I'm getting NameError: wrong constant name createSomething

Here is an example of my code:

@client.parse <<-'GRAPHQL'
  mutation createSomething($name: String!, $filter: createSomethingFilterInput!, $frequency: SomethingFrequency!) {
     createSomething(name: $name, filter: $filter, frequency: $frequency) {
     success
     message
     __typename
     }
}
GRAPHQL

But the same mutation works using graphql playground.

  mutation createSomething($name: String!, $filter: createSomethingFilterInput!, $frequency: SomethingFrequency!) {
     createSomething(name: $name, filter: $filter, frequency: $frequency) {
     success
     message
     __typename
     }
}

I'm trying to avoid the use of Blocks as I want to maintain a standard on my code.

yuki24 commented 5 years ago

I'm not sure what you refer to by "as a string", but parsed results can only be set to constants, which in Ruby must start with a capitalized letter. Have you tried renaming mutation to Mutation?

ashkan18 commented 5 years ago

this looks ok to me, the only thing I can think of is ' around GRAPHQL, can you try removing that? or switch to something like

%(
mutation createSomething($name: String!, $filter: createSomethingFilterInput!, $frequency: SomethingFrequency!) {
     createSomething(name: $name, filter: $filter, frequency: $frequency) {
     success
     message
     __typename
     }
})
yuki24 commented 5 years ago

I should've been more clear on what I was really referring to by mutation, but I was referring to the local variable mutation in Ruby, not the one in the actual GraphQL query:

-mutation = @client.parse <<-'GRAPHQL'
+Mutation = @client.parse <<-'GRAPHQL'

The single quotes around GRAPHQL is valid Ruby syntax:

foo = <<-'GRAPHQL'
  text
GRAPHQL
# => "  text\n"
leoGalani commented 5 years ago

Hello! Thanks for the support. I'm actually using a lambda function to define my constant.

I was talking with a friend and he gave me some light on how operations work and seem like if I define a name for my mutation operation, it doesn't work.

mutation createSomething($name: String!, $filter: createSomethingFilterInput!, $frequency: SomethingFrequency!) { ... throws constant name error.

mutation CreateSomething($name: String!, $filter: createSomethingFilterInput!, $frequency: SomethingFrequency!) { .... create a module class, not a #<GraphQL::Client::OperationDefinition:...>

mutation($name: String!, $filter: createSomethingFilterInput!, $frequency: SomethingFrequency!) { ... Works just fine.

ashkan18 commented 5 years ago

interesting! so parse in Graphlient underneath uses graphql-client to parse the query, if you pass in a block, it will first try to create query string from it and otherwise it will just pass it to the underneath parse method. Looking at their examples i don't see them using mutation or query alias.

On the other hand, i don't think you'd need that mutation name necessarily, that only changes how you access the results of that mutation and adds a createSomething to response path. so instead of response.data.data... you'd access thing using response.data.create_someting. Unless you are planning to have multiple mutation per one request, i don't think you'd need that alias. Hope this helps 💚

leoGalani commented 5 years ago

My team uses mutation names to track down possible issues @ datadog... if the operation doesn't have a name, it's a bit harder to debug* :)

Thanks again for the support!

ashkan18 commented 5 years ago

@leoGalani , Sorry haven't had a chance to look at this yet, did you figure out how to fix it? I was hoping to create a failing spec and go from there.

leoGalani commented 5 years ago

I didn't have time to stop and look at how the parsing goes. Once I finish my mid tasks, I will fork your repo and try some stuff out and then come with a PR :)

For now, I don't mind not having an operation name 😅

Hareramrai commented 12 months ago

We have an around for this by using https://github.com/tools-aoeur/graphlient/pull/1.

trevorh commented 7 months ago

We have an around for this by using tools-aoeur#1.

@ashkan18 - this fork works perfectly for me. I was encountering the same issue in trying to use query/mutation aliases and not getting the correct object type back (orphaned Module instance).

I'm guessing that graphql-client doesn't permit them as they expect the constant returned from parsing to function as the alias. However, as others have mentioned, for logging/debugging/observability, the alias needs to be preserved in whatever is sent for execution.

dblock commented 7 months ago

Can someone PR the fix (and other changes on the fork) here?