Class: GraphitiGraphQL::Schema
- Inherits:
-
Object
- Object
- GraphitiGraphQL::Schema
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
#initialize ⇒ Schema
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_field ⇒ Object
43
44
45
|
# File 'lib/graphiti_graphql/schema.rb', line 43
def self.base_field
@base_field || BaseField
end
|
.base_interface ⇒ Object
51
52
53
|
# File 'lib/graphiti_graphql/schema.rb', line 51
def self.base_interface
@base_interface || BaseInterface
end
|
.base_object ⇒ Object
47
48
49
|
# File 'lib/graphiti_graphql/schema.rb', line 47
def self.base_object
@base_object || BaseObject
end
|
.entrypoints ⇒ Object
Returns the value of attribute entrypoints.
32
33
34
|
# File 'lib/graphiti_graphql/schema.rb', line 32
def entrypoints
@entrypoints
end
|
.federation ⇒ Object
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_schema ⇒ Object
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_fields ⇒ Object
Returns the value of attribute query_fields.
37
38
39
|
# File 'lib/graphiti_graphql/schema.rb', line 37
def query_fields
@query_fields
end
|
#schema ⇒ Object
Returns the value of attribute schema.
36
37
38
|
# File 'lib/graphiti_graphql/schema.rb', line 36
def schema
@schema
end
|
#type_registry ⇒ Object
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
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?
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 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)
query_class = Class.new(existing_query || self.class.base_object)
query_class.graphql_name "Query"
query_class.field_class BaseField
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
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
|