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

Serialize times with milliseconds #7

Open
josuemontano opened this issue Nov 10, 2021 · 1 comment
Open

Serialize times with milliseconds #7

josuemontano opened this issue Nov 10, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@josuemontano
Copy link

josuemontano commented Nov 10, 2021

Describe the bug

Timestamps cannot be used to generate stable cursors since they are serialized with precision of seconds

https://github.com/bibendi/graphql-connections/blob/c979406fc407216384fbe80351c1cf749794eae8/lib/graphql/connections/base.rb#L42

To Reproduce

  1. Install the graphql gem.
  2. Create a User ActiveRecord model with timestamps, such as created_at.
  3. Create a type for the User model
  4. Create a query to fetch users which returns a stable connection from this gem
class User < ApplicationRecord
end

module Types
  class UserType < Types::BaseObject
    field :id, ID, null: false
  end
end

module Types
  class QueryType < Types::BaseObject
    field :users, Types::UserType.connection_type, null: false
    def users
      GraphQL::Connections::Stable.new(User.all, primary_key: :created_at)
    end
  end
end

The generated SQL looks like this:

SELECT "users".* FROM "users"
WHERE "users"."created_at" > '2021-11-10 14:26:19'
ORDER BY "users"."created_at" ASC LIMIT 10

Expected behavior
The SQL query should include milliseconds to avoid skipping records that were created in the same second:

SELECT "users".* FROM "users"
WHERE "users"."created_at" > '2021-11-10 14:26:19.152283'
ORDER BY "users"."created_at" ASC LIMIT 10

To achieve it, serialize times with higher precision

# lib/graphql/connections/base.rb
def serialize(cursor)
  case cursor
  when Time, DateTime, Date
    cursor.iso8601(9)
  else
    cursor.to_s
  end
end

Context (please complete the following information):

  • Ruby: 2.7.4
  • graphql gem: 1.14
  • rails: 6.1
@bibendi
Copy link
Collaborator

bibendi commented Nov 11, 2021

Hi!
Makes sense!

@bibendi bibendi added the enhancement New feature or request label Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants