Class: Graphlyte::Schema

Inherits:
Data
  • Object
show all
Defined in:
lib/graphlyte/schema.rb

Overview

Represents a schema definition, containing all type definitions available in a server Reflects the response to a [schema query](./schema_query.rb)

Defined Under Namespace

Classes: Directive, Enum, Field, InputValue, Type, TypeRef

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Data

#==, attr_accessor, attr_reader, attributes, #dup, #eql?, #hash, #inspect

Constructor Details

#initializeSchema

Returns a new instance of Schema.



133
134
135
136
137
138
# File 'lib/graphlyte/schema.rb', line 133

def initialize(**)
  super

  @types ||= {}
  @directives ||= {}
end

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



131
132
133
# File 'lib/graphlyte/schema.rb', line 131

def directives
  @directives
end

#mutation_typeObject

Returns the value of attribute mutation_type.



130
131
132
# File 'lib/graphlyte/schema.rb', line 130

def mutation_type
  @mutation_type
end

#query_typeObject

Returns the value of attribute query_type.



130
131
132
# File 'lib/graphlyte/schema.rb', line 130

def query_type
  @query_type
end

#subscription_typeObject

Returns the value of attribute subscription_type.



130
131
132
# File 'lib/graphlyte/schema.rb', line 130

def subscription_type
  @subscription_type
end

#typesObject (readonly)

Returns the value of attribute types.



131
132
133
# File 'lib/graphlyte/schema.rb', line 131

def types
  @types
end

Class Method Details

.entity_list(entity, resp) ⇒ Object



153
154
155
156
157
# File 'lib/graphlyte/schema.rb', line 153

def self.entity_list(entity, resp)
  return unless resp

  resp.map { entity.from_schema_response(_1) }
end

.entity_map(entity, resp) ⇒ Object



159
160
161
162
163
# File 'lib/graphlyte/schema.rb', line 159

def self.entity_map(entity, resp)
  return unless resp

  resp.to_h { |entry| [entry['name'], entity.from_schema_response(entry)] }
end

.from_schema_response(response) ⇒ Object

Raises:

  • (Argument)


140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/graphlyte/schema.rb', line 140

def self.from_schema_response(response)
  data = response.dig('data', '__schema')
  raise Argument, 'No data' unless data

  new(
    query_type: data.dig('queryType', 'name'),
    mutation_type: data.dig('queryType', 'name'),
    subscription_type: data.dig('subscriptionType', 'name'),
    types: entity_map(Type, data['types']),
    directives: entity_map(Directive, data['directives'])
  )
end