Sha256: 28a1137351064cbc8127dd36878d91f7be914e9a7f86b79fcd5d89d02a9d00e8

Contents?: true

Size: 1.76 KB

Versions: 23

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module GraphQL
  class Schema
    class Field
      class ConnectionExtension < GraphQL::Schema::FieldExtension
        def apply
          field.argument :after, "String", "Returns the elements in the list that come after the specified cursor.", required: false
          field.argument :before, "String", "Returns the elements in the list that come before the specified cursor.", required: false
          field.argument :first, "Int", "Returns the first _n_ elements from the list.", required: false
          field.argument :last, "Int", "Returns the last _n_ elements from the list.", required: false
        end

        # Remove pagination args before passing it to a user method
        def resolve(object:, arguments:, context:)
          next_args = arguments.dup
          next_args.delete(:first)
          next_args.delete(:last)
          next_args.delete(:before)
          next_args.delete(:after)
          yield(object, next_args)
        end

        def after_resolve(value:, object:, arguments:, context:, memo:)
          if value.is_a? GraphQL::ExecutionError
            # This isn't even going to work because context doesn't have ast_node anymore
            context.add_error(value)
            nil
          elsif value.nil?
            nil
          else
            if object.is_a?(GraphQL::Schema::Object)
              object = object.object
            end
            connection_class = GraphQL::Relay::BaseConnection.connection_for_nodes(value)
            connection_class.new(
              value,
              arguments,
              field: field,
              max_page_size: field.max_page_size,
              parent: object,
              context: context,
            )
          end
        end

      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
graphql-1.9.21 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.20 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.19 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.18 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.17 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.16 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.15 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.14 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.13 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.12 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.11 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.10 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.9 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.8 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.7 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.6 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.5 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.4 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.3 lib/graphql/schema/field/connection_extension.rb
graphql-1.9.2 lib/graphql/schema/field/connection_extension.rb