Sha256: 72dfa1af3457c9702d661a094f0a7edcdf75a4d480c680639abece717178b54d

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Relay
    # Helpers for working with Relay-specific Node objects.
    module Node
      # @return [GraphQL::Field] a field for finding objects by their global ID.
      def self.field
        # We have to define it fresh each time because
        # its name will be modified and its description
        # _may_ be modified.
        GraphQL::Field.define do
          type(GraphQL::Relay::Node.interface)
          description("Fetches an object given its ID.")
          argument(:id, !types.ID, "ID of the object.")
          resolve(GraphQL::Relay::Node::FindNode)
          relay_node_field(true)
        end
      end

      def self.plural_field
        GraphQL::Field.define do
          type(!types[GraphQL::Relay::Node.interface])
          description("Fetches a list of objects given a list of IDs.")
          argument(:ids, !types[!types.ID], "IDs of the objects.")
          resolve(GraphQL::Relay::Node::FindNodes)
          relay_nodes_field(true)
        end
      end

      # @return [GraphQL::InterfaceType] The interface which all Relay types must implement
      def self.interface
        @interface ||= GraphQL::InterfaceType.define do
          name("Node")
          description("An object with an ID.")
          field(:id, !types.ID, "ID of the object.")
          default_relay(true)
        end
      end

      # A field resolve for finding objects by IDs
      module FindNodes
        def self.call(obj, args, ctx)
          args[:ids].map { |id| ctx.query.schema.object_from_id(id, ctx) }
        end
      end

      # A field resolve for finding an object by ID
      module FindNode
        def self.call(obj, args, ctx)
          ctx.query.schema.object_from_id(args[:id], ctx )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-1.4.3 lib/graphql/relay/node.rb