Class: GraphQL::Relay::GlobalIdField

Inherits:
Field
  • Object
show all
Defined in:
lib/graphql/relay/global_id_field.rb

Overview

Examples:

Create a field that returns the global ID for an object

RestaurantType = GraphQL::ObjectType.define do
  name "Restaurant"
  field :id, field: GraphQL::Relay::GlobalIdField.new("Restaurant")
end

Instance Method Summary collapse

Constructor Details

#initialize(type_name, property: :id) ⇒ GlobalIdField

Returns a new instance of GlobalIdField.



9
10
11
12
13
14
15
# File 'lib/graphql/relay/global_id_field.rb', line 9

def initialize(type_name, property: :id)
  self.arguments = {}
  self.type = !GraphQL::ID_TYPE
  self.resolve = -> (obj, args, ctx) {
    ctx.query.schema.node_identification.to_global_id(type_name, obj.public_send(property))
  }
end