Class: GraphQL::Api::Schema
- Inherits:
-
Object
- Object
- GraphQL::Api::Schema
- Defined in:
- lib/graphql/api/schema.rb
Instance Method Summary collapse
- #all_commands ⇒ Object
- #all_models ⇒ Object
- #all_queries ⇒ Object
- #build_model_types ⇒ Object
- #build_mutations ⇒ Object
- #build_object_types ⇒ Object
- #create_command_type(object_type, action) ⇒ Object
- #create_mutation(model_class) ⇒ Object
- #create_type(model_class) ⇒ Object
- #delete_mutation(model_class) ⇒ Object
-
#initialize(commands: [], queries: [], models: []) ⇒ Schema
constructor
A new instance of Schema.
- #mutation(&block) ⇒ Object
- #query(&block) ⇒ Object
- #schema ⇒ Object
- #update_mutation(model_class) ⇒ Object
Constructor Details
#initialize(commands: [], queries: [], models: []) ⇒ Schema
Returns a new instance of Schema.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/graphql/api/schema.rb', line 20 def initialize(commands: [], queries: [], models: []) @types = {} @mutations = {} @load_commands = commands @load_queries = queries @load_models = models build_model_types build_mutations build_object_types end |
Instance Method Details
#all_commands ⇒ Object
41 42 43 |
# File 'lib/graphql/api/schema.rb', line 41 def all_commands @all_commands ||= all_constants('commands') + @load_commands end |
#all_models ⇒ Object
33 34 35 |
# File 'lib/graphql/api/schema.rb', line 33 def all_models @all_models ||= all_constants('models') + @load_models end |
#all_queries ⇒ Object
37 38 39 |
# File 'lib/graphql/api/schema.rb', line 37 def all_queries @all_queries ||= all_constants('queries') + @load_queries end |
#build_model_types ⇒ Object
230 231 232 |
# File 'lib/graphql/api/schema.rb', line 230 def build_model_types all_models.each { |model_class| @types[model_class] = create_type(model_class) } end |
#build_mutations ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/graphql/api/schema.rb', line 238 def build_mutations all_models.each do |model_class| @mutations[model_class] = [ ["create#{model_class.name}", create_mutation(model_class)], ["update#{model_class.name}", update_mutation(model_class)], ["delete#{model_class.name}", delete_mutation(model_class)], ].map { |x| x if x[1] }.compact end all_commands.each do |command| if command.respond_to?(:actions) && command.actions.any? @mutations[command] = [] command.actions.each do |action| @mutations[command] << ["#{action}#{command.name}", create_command_type(command, action)] end else @mutations[command] = [ [command.name.camelize(:lower), create_command_type(command, :perform)] ] end end end |
#build_object_types ⇒ Object
234 235 236 |
# File 'lib/graphql/api/schema.rb', line 234 def build_object_types all_queries.each { |query| @types[query] = nil } end |
#create_command_type(object_type, action) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/graphql/api/schema.rb', line 83 def create_command_type(object_type, action) object_types = @types GraphQL::Relay::Mutation.define do name object_type.name description "Command #{object_type.name} #{action}" object_type.inputs.each do |input, type| input_field input, graphql_type_of(type) end object_type.returns.each do |return_name, return_type| return_field return_name, graphql_type_for_object(return_type, object_types) end resolve Resolvers::CommandMutation.new(object_type, action) end end |
#create_mutation(model_class) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/graphql/api/schema.rb', line 102 def create_mutation(model_class) return nil unless model_class < ActiveRecord::Base object_types = @types GraphQL::Relay::Mutation.define do name "Create#{model_class.name}" description "Create #{model_class.name}" model_class.columns.each do |column| input_field column.name, graphql_type(column) end return_field model_class.name.underscore.to_sym, object_types[model_class] resolve Resolvers::ModelCreateMutation.new(model_class) end end |
#create_type(model_class) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/graphql/api/schema.rb', line 45 def create_type(model_class) object_types = @types GraphQL::ObjectType.define do name model_class.name description "Get #{model_class.name}" if model_class.respond_to?(:columns) model_class.columns.each do |column| field column.name do type graphql_type(column) resolve Resolvers::Field.new(model_class, column.name) end end end if model_class.respond_to?(:fields) model_class.fields.each do |field_name, field_type| field field_name, graphql_type_of(field_type) end end if model_class.respond_to?(:reflections) model_class.reflections.each do |name, association| field name do if association.collection? type types[object_types[association.class_name.constantize]] else type object_types[association.class_name.constantize] end resolve Resolvers::Field.new(model_class, name) end end end end end |
#delete_mutation(model_class) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/graphql/api/schema.rb', line 139 def delete_mutation(model_class) return nil unless model_class < ActiveRecord::Base object_types = @types GraphQL::Relay::Mutation.define do name "Delete#{model_class.name}" description "Delete #{model_class.name}" input_field :id, !types.ID return_field model_class.name.underscore.to_sym, object_types[model_class] resolve Resolvers::ModelDeleteMutation.new(model_class) end end |
#mutation(&block) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/graphql/api/schema.rb', line 211 def mutation(&block) mutations = @mutations GraphQL::ObjectType.define do name 'Mutation' instance_eval(&block) if block mutations.each do |model_class, muts| muts.each do |mutation| field mutation[0], field: mutation[1].field end end end end |
#query(&block) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/graphql/api/schema.rb', line 155 def query(&block) object_types = @types GraphQL::ObjectType.define do name 'Query' description 'The query root for this schema' instance_eval(&block) if block object_types.each do |object_class, graph_type| if object_class < ActiveRecord::Base field(object_class.name.camelize(:lower)) do type graph_type argument :id, types.ID if object_class.respond_to?(:arguments) object_class.arguments.each do |arg| argument arg, graphql_type(object_class.columns.find { |c| c.name.to_sym == arg.to_sym }) end end resolve Resolvers::ModelFindQuery.new(object_class) end field(object_class.name.camelize(:lower).pluralize) do type types[graph_type] argument :limit, types.Int if object_class.respond_to?(:arguments) object_class.arguments.each do |arg| argument arg, graphql_type(object_class.columns.find { |c| c.name.to_sym == arg.to_sym }) end end resolve Resolvers::ModelListQuery.new(object_class) end elsif object_class.respond_to?(:arguments) && object_class.respond_to?(:return_type) field(object_class.name.camelize(:lower)) do type(graphql_type_for_object(object_class.return_type, object_types)) object_class.arguments.each do |argument_name, argument_type| argument argument_name, graphql_type_of(argument_type) end resolve Resolvers::QueryObjectQuery.new(object_class) end end end end end |
#schema ⇒ Object
226 227 228 |
# File 'lib/graphql/api/schema.rb', line 226 def schema @schema ||= GraphQL::Schema.define(query: query, mutation: mutation) end |
#update_mutation(model_class) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/graphql/api/schema.rb', line 120 def update_mutation(model_class) return nil unless model_class < ActiveRecord::Base object_types = @types GraphQL::Relay::Mutation.define do name "Update#{model_class.name}" description "Update #{model_class.name}" input_field :id, !types.ID model_class.columns.each do |column| input_field column.name, graphql_type(column) end return_field model_class.name.underscore.to_sym, object_types[model_class] resolve Resolvers::ModelUpdateMutation.new(model_class) end end |