ashkan18 / graphlient

Ruby GraphQL Client
MIT License
251 stars 44 forks source link

Camelcasing => ClientError between 0.3.3 & 0.3.7 #76

Open ATMartin opened 4 years ago

ATMartin commented 4 years ago

When updating from graphlient v0.3.3 to v0.3.7, I'm seeing an odd error:

Graphlient::Errors::ClientError: Field 'image_raw' doesn't exist on type 'Resource'

Here's a rough cut of the code resulting in this error:

def query_for(user)
    @client = Graphlient::Client.new(my_cool_service_url)
    @client.query(my_query, { email: user.email })
end

def my_query
  <<~'GRAPHQL'
      query($email: String!) {
        user(email: $email) {
          registrations(first: 10) {
            edges {
              node {
                resource {
                  name
                  slug
                  image_raw
                }
                path {
                  progress
                  points {
                    total
                    earned
                  }
                }
              }
            }
          }
        }
      }
  GRAPHQL
end

Nothing's changed in our API & I can duplicate this error by changing nothing but the gem version.

I poked around a bit in IRB and found that I get back a generic Class from @client.schema in 0.3.7, whereas I see a much richer Graphlient::Schema object in 0.3.3. Has something changed in schema parsing that might cause this error? Am I missing a syntactic change I need to make? I did see the new schema_path option, but that appears to be optional.

Interestingly, if I switch to the image property (which returns a URL to a compressed version of image_raw), everything works fine. Maybe this property name is breaking a new rule? Our implementation includes a handful of other queries with similar failures in 0.3.7 that work on 0.3.3 & when directly posted via Insomnia.

I looked through the diff between these versions, as well as related issues/PRs, and didn't see anything right away that would cause this. Since most of it is delegated, I also browsed through graphql-clients changes with no luck. That said, I'm frustrated so my comprehension may be compromised.

Regardless, thanks for this client!

ATMartin commented 4 years ago

Fresh eyes this morning have led me down the "camelCase" path. In 0.3.7, my schema resolves to looking for an imageRaw field - which our API rejects as a non-existent field. This doesn't happen in 0.3.3.

It looks like this may be caused by things down in graphql itself, in which case we're pinning graphlient versions until I can convince the API team to do some work! Is there any way to disable this auto-camelcasing behavior at the Graphlient level?

dblock commented 4 years ago

It looks like this may be caused by things down in graphql itself, in which case we're pinning graphlient versions until I can convince the API team to do some work! Is there any way to disable this auto-camelcasing behavior at the Graphlient level?

AFAIK no, if you find a way to do this in the graphql-ruby client we should be able to expose it.

ashkan18 commented 4 years ago

Graphql Ruby is camelCaseing by default to match GraphQL recommended naming approach, in our case, when you get response back it should already be snake_cased for you to access it in the response. So I would change image_raw in your query to imageRaw but when reading it from response you would still use image_raw.

ATMartin commented 4 years ago

@ashkan18 I appreciate the suggestion! I believe the issue is that the API we're hitting does not camelCase their fields, so imageRaw is rejected by the server as an invalid field.

I'd love to submit a PR for this! I've got finding a fix in graphql-ruby (or graphql-client) on my TODO list.