Class: GraphitiGraphQL::Schema

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

Defined Under Namespace

Modules: BaseInterface Classes: BaseField, BaseObject, PageInfoType, PageType, SortDirType

Constant Summary collapse

GQL_TYPE_MAP =
{
  integer_id: String,
  string: String,
  uuid: String,
  integer: Integer,
  float: Float,
  boolean: GraphQL::Schema::Member::GraphQLTypeNames::Boolean,
  date: GraphQL::Types::ISO8601Date,
  datetime: GraphQL::Types::ISO8601DateTime,
  hash: GraphQL::Types::JSON,
  array: [GraphQL::Types::JSON],
  array_of_strings: [String],
  array_of_integers: [Integer],
  array_of_floats: [Float],
  array_of_dates: [GraphQL::Types::ISO8601Date],
  array_of_datetimes: [GraphQL::Types::ISO8601DateTime]
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



74
75
76
77
# File 'lib/graphiti_graphql/schema.rb', line 74

def initialize
  @type_registry = {}
  @query_fields = {}
end

Class Attribute Details

.base_fieldObject



43
44
45
# File 'lib/graphiti_graphql/schema.rb', line 43

def self.base_field
  @base_field || BaseField
end

.base_interfaceObject



51
52
53
# File 'lib/graphiti_graphql/schema.rb', line 51

def self.base_interface
  @base_interface || BaseInterface
end

.base_objectObject



47
48
49
# File 'lib/graphiti_graphql/schema.rb', line 47

def self.base_object
  @base_object || BaseObject
end

.entrypointsObject

Returns the value of attribute entrypoints.



32
33
34
# File 'lib/graphiti_graphql/schema.rb', line 32

def entrypoints
  @entrypoints
end

.federationObject

Returns the value of attribute federation.



32
33
34
# File 'lib/graphiti_graphql/schema.rb', line 32

def federation
  @federation
end

Instance Attribute Details

#graphiti_schemaObject

Returns the value of attribute graphiti_schema.



36
37
38
# File 'lib/graphiti_graphql/schema.rb', line 36

def graphiti_schema
  @graphiti_schema
end

#query_fieldsObject (readonly)

Returns the value of attribute query_fields.



37
38
39
# File 'lib/graphiti_graphql/schema.rb', line 37

def query_fields
  @query_fields
end

#schemaObject

Returns the value of attribute schema.



36
37
38
# File 'lib/graphiti_graphql/schema.rb', line 36

def schema
  @schema
end

#type_registryObject

Returns the value of attribute type_registry.



36
37
38
# File 'lib/graphiti_graphql/schema.rb', line 36

def type_registry
  @type_registry
end

Class Method Details

.federation?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/graphiti_graphql/schema.rb', line 39

def self.federation?
  !!@federation
end

.generate(resources: nil, entrypoints: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/graphiti_graphql/schema.rb', line 55

def self.generate(resources: nil, entrypoints: nil)
  instance = new
  schema = Class.new(::GraphitiGraphQL.schema_class || GraphQL::Schema)
  graphiti_schema = GraphitiGraphQL::GraphitiSchema::Wrapper
    .new(Graphiti::Schema.generate)
  instance.graphiti_schema = graphiti_schema

  resources ||= graphiti_schema.resources
  entrypoints ||= self.entrypoints || resources

  instance.schema = schema
  instance.apply_query(resources, entrypoints)
  instance
end

.resource_class_for_type(type_name) ⇒ Object



70
71
72
# File 'lib/graphiti_graphql/schema.rb', line 70

def self.resource_class_for_type(type_name)
  const_get "#{type_name}Resource".gsub("__", "")
end

Instance Method Details

#apply_query(resources, entrypoints) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/graphiti_graphql/schema.rb', line 79

def apply_query(resources, entrypoints)
  query_type = generate_schema_query(resources, entrypoints)
  Federation::SchemaDecorator.decorate(self) if self.class.federation?

  # NB - don't call .query here of federation will break things
  if schema.instance_variable_get(:@query_object)
    schema.instance_variable_set(:@query_object, nil)
    schema.instance_variable_set(:@federation_query_object, nil)
  end
  schema.orphan_types(orphans(schema))
  schema.query(query_type)
  schema.query # Actually fires the federation code
end

#generate_schema_query(resources, entrypoints) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/graphiti_graphql/schema.rb', line 93

def generate_schema_query(resources, entrypoints)
  existing_query = schema.instance_variable_get(:@query)
  existing_query ||= schema.send(:find_inherited_value, :query)
  # NB - don't call graphql_schema.query here of federation will break things
  query_class = Class.new(existing_query || self.class.base_object)
  # NB MUST be Query or federation-ruby will break things
  query_class.graphql_name "Query"
  query_class.field_class BaseField

  # Ensure all types are generated, even if they aren't in a top-level Query
  # We might now want to expose the query, but do want to federate the Type
  get_schema_resources(resources).each do |resource|
    generate_type(resource)
  end

  get_schema_resources(entrypoints).each do |resource|
    add_index(query_class, resource)
    add_show(query_class, resource)
  end
  query_class
end

#orphans(graphql_schema) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/graphiti_graphql/schema.rb', line 115

def orphans(graphql_schema)
  [].tap do |orphans|
    type_registry.keys.each do |type_name|
      unless graphql_schema.types.has_key?(type_name)
        klass = type_registry[type_name][:type]
        orphans << klass if klass.is_a?(Class)
      end
    end
  end
end

#query_field?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def query_field?(name)
  @query_fields.include?(name.underscore.to_sym)
end

#resource_for_query_field(name) ⇒ Object

We can’t just constantize the name from the schema Because classes can be reopened and modified in tests (or elsewhere, in theory)



136
137
138
139
# File 'lib/graphiti_graphql/schema.rb', line 136

def resource_for_query_field(name)
  schema_resource = @query_fields[name.underscore.to_sym]
  Graphiti.resources.find { |r| r.name == schema_resource.name }
end

#schema_resource_for_query_field(name) ⇒ Object



126
127
128
# File 'lib/graphiti_graphql/schema.rb', line 126

def schema_resource_for_query_field(name)
  @query_fields[name.underscore.to_sym]
end