Graphql-ruby: GraphQL Pro stable cursor makes AR fail with only count

Created on 5 Nov 2017  Â·  4Comments  Â·  Source: rmosolgo/graphql-ruby

Hey! I'm in the process of upgrading our app from [email protected] to current and [email protected] to current.

I notice some of our tests are failing with this error:

     Failure/Error: obj.nodes.count

     ActiveRecord::StatementInvalid:
       PG::SyntaxError: ERROR:  syntax error at or near "AS"
       LINE 1: SELECT COUNT(stores.*, stores.id AS cursor_0) FROM "stores" ...
                                                ^
       : SELECT COUNT(stores.*, stores.id AS cursor_0) FROM "stores" INNER JOIN "pokemon_display_definitions_authorized_stores" ON "stores"."id" = "pokemon_display_definitions_authorized_stores"."store_id" WHERE "pokemon_display_definitions_authorized_stores"."display_definition_id" = $1
     # /bundle/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:637:in `prepare'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:637:in `prepare_statement'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:596:in `exec_cache'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/connection_adapters/abstract/database_statements.rb:356:in `select'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/relation/calculations.rb:270:in `execute_simple_calculation'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/relation/calculations.rb:227:in `perform_calculation'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/relation/calculations.rb:133:in `calculate'
     # /bundle/gems/activerecord-4.2.9/lib/active_record/relation/calculations.rb:48:in `count'
     # ./app/graph/types/hermes/display_definition_authorized_stores_connection_type.rb:12:in `block (3 levels) in <top (required)>'

this use care is a little bit specific, here is display_definition_authorized_stores_connection_type.rb:

Hermes::DisplayDefinitionAuthorizedStoresConnectionType = Hermes::StoreType.define_connection(
  edge_class: Hermes::DisplayDefinitionAuthorizedStoreEdge,
  edge_type: Hermes::DisplayDefinitionAuthorizedStoreEdgeType
) do
  name 'DisplayDefinitionAuthorizedStoresConnection'
  description 'The list of stores where and when a Display Definition is authorized at'

  field :totalCount do
    type !types.Int
    resolve ->(obj, _args, _ctx) {
      obj.nodes.count
    }
  end
end

and this failing test if performing this precise query:

      mutation destroyAuthorizedStoresMutation($input: DestroyAuthorizedStoresInput!) {
        destroyAuthorizedStores(input: $input) {
          success
          displayDefinition {
            _id
            authorizedStores {
              totalCount
            }
          }
          errors {
            field
            message
          }
        }
      }

As you can see we are only requiring the totalCount on this query and nothing about the edges — which makes me think that maybe graphql-pro shouldn't be adding what's it's adding the AR query to get a stable cursor?

I hope I'm making sense and providing enough details, if not let me know and I'll hop on Slack or provide more details here

Most helpful comment

This is a great bug report, thanks for all the details!

I'm sure this bug was introduced in 1.5.3, as part of the fix for joined tables with columns of the same name:

https://github.com/rmosolgo/graphql-ruby/blob/caeb995da53fb71cecaef22ca5c96cfbdbb35247/CHANGELOG-pro.md#153-4-sept-2017

So, I will take a look tomorrow and get back to you!

All 4 comments

This is a great bug report, thanks for all the details!

I'm sure this bug was introduced in 1.5.3, as part of the fix for joined tables with columns of the same name:

https://github.com/rmosolgo/graphql-ruby/blob/caeb995da53fb71cecaef22ca5c96cfbdbb35247/CHANGELOG-pro.md#153-4-sept-2017

So, I will take a look tomorrow and get back to you!

I tried and tried to replicate in the test suite ... then looked at my implementation of totalCount:

      resolve ->(obj, args, ctx) {
        obj.nodes.size
      }

😅 It used .size.

I figured that was it: since the new connection implementation adds to the select columns, you have to use size (where Rails does magic) or _manually_ specify COUNT(*) with .count(:all). See for example: https://github.com/rails/rails/issues/29686#issuecomment-313185266

I can't think of another way to beat this one, can you modify your totalCount field to work that way? If it works for you, I'll fix the docs!

Oh well,

I can definitely do that! I’ll try that tomorrow morning and update you. Thanks for looking it!

Loïc


From: Robert Mosolgo notifications@github.com
Sent: Monday, November 6, 2017 5:50:55 PM
To: rmosolgo/graphql-ruby
Cc: Loïc CHOLLIER; Author
Subject: Re: [rmosolgo/graphql-ruby] GraphQL Pro stable cursor makes AR fail with only count (#1076)

I tried and tried to replicate in the test suite ... then looked at my implementation of totalCount:

  resolve ->(obj, args, ctx) {
    obj.nodes.size
  }

😅 It used .size.

I figured that was it: since the new connection implementation adds to the select columns, you have to use size (where Rails does magic) or manually specify COUNT(*) with .count(:all). See for example: rails/rails#29686 (comment)https://github.com/rails/rails/issues/29686#issuecomment-313185266

I can't think of another way to beat this one, can you modify your totalCount field to work that way? If it works for you, I'll fix the docs!

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/rmosolgo/graphql-ruby/issues/1076#issuecomment-342349500, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAAHTSQmSWE9A_pXuxW_IYdVdji7-Z3aks5sz7d-gaJpZM4QSP4j.

This solved it thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ecomuere picture ecomuere  Â·  3Comments

KevinColemanInc picture KevinColemanInc  Â·  3Comments

peterphan1996 picture peterphan1996  Â·  3Comments

amozoss picture amozoss  Â·  3Comments

jesster2k10 picture jesster2k10  Â·  3Comments