Created
September 6, 2024 14:35
-
-
Save rmosolgo/428a8812953645873487b46c90a707d7 to your computer and use it in GitHub Desktop.
GraphQL-Ruby underscore-cased page info field for connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile do | |
gem "graphql" | |
end | |
class MySchema < GraphQL::Schema | |
class BaseField < GraphQL::Schema::Field | |
def initialize(*args, camelize: false, **kwargs, &block) | |
super | |
end | |
end | |
class BaseObject < GraphQL::Schema::Object | |
field_class(BaseField) | |
end | |
class PageInfo < BaseObject | |
include GraphQL::Types::Relay::PageInfoBehaviors | |
end | |
class BaseConnection < BaseObject | |
include GraphQL::Types::Relay::ConnectionBehaviors | |
# Add this field again so that it uses our custom PageInfo class | |
# instead of GraphQL-Ruby's built-in one (which has camelized field names) | |
own_fields.delete("page_info") | |
field :page_info, PageInfo, null: false, description: "Information to aid in pagination." | |
end | |
BaseObject.connection_type_class(BaseConnection) | |
class Thing < BaseObject | |
field :name, String | |
end | |
class Query < BaseObject | |
field :things, Thing.connection_type | |
end | |
query(Query) | |
end | |
puts MySchema.to_definition | |
# """ | |
# Information about pagination in a connection. | |
# """ | |
# type PageInfo { | |
# """ | |
# When paginating forwards, the cursor to continue. | |
# """ | |
# end_cursor: String | |
# """ | |
# When paginating forwards, are there more items? | |
# """ | |
# has_next_page: Boolean! | |
# """ | |
# When paginating backwards, are there more items? | |
# """ | |
# has_previous_page: Boolean! | |
# """ | |
# When paginating backwards, the cursor to continue. | |
# """ | |
# start_cursor: String | |
# } | |
# type Query { | |
# things( | |
# """ | |
# Returns the elements in the list that come after the specified cursor. | |
# """ | |
# after: String | |
# """ | |
# Returns the elements in the list that come before the specified cursor. | |
# """ | |
# before: String | |
# """ | |
# Returns the first _n_ elements from the list. | |
# """ | |
# first: Int | |
# """ | |
# Returns the last _n_ elements from the list. | |
# """ | |
# last: Int | |
# ): ThingConnection | |
# } | |
# type Thing { | |
# name: String | |
# } | |
# """ | |
# The connection type for Thing. | |
# """ | |
# type ThingConnection { | |
# """ | |
# A list of edges. | |
# """ | |
# edges: [ThingEdge] | |
# """ | |
# A list of nodes. | |
# """ | |
# nodes: [Thing] | |
# """ | |
# Information to aid in pagination. | |
# """ | |
# page_info: PageInfo! | |
# } | |
# """ | |
# An edge in a connection. | |
# """ | |
# type ThingEdge { | |
# """ | |
# A cursor for use in pagination. | |
# """ | |
# cursor: String! | |
# """ | |
# The item at the end of the edge. | |
# """ | |
# node: Thing | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment