Class: Tina4::GraphQLSchema
- Inherits:
-
Object
- Object
- Tina4::GraphQLSchema
- Defined in:
- lib/tina4/graphql.rb
Overview
─── Schema ───────────────────────────────────────────────────────────
Instance Attribute Summary collapse
-
#mutations ⇒ Object
readonly
Returns the value of attribute mutations.
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#add_mutation(name, args = {}, return_type = nil, resolver = nil, &block) ⇒ Object
Register a mutation field.
-
#add_query(name, args = {}, return_type = nil, resolver = nil, &block) ⇒ Object
Register a query field.
-
#add_type(name_or_type, fields = nil) ⇒ Object
add_type(name, fields) — parity with PHP/Python/Node add_type(type_object) — legacy Ruby form (type_object responds to .name).
-
#from_orm(klass) ⇒ Object
── ORM Auto-Schema ────────────────────────────────────────────────── Generates GraphQL types + CRUD queries/mutations from a Tina4::ORM subclass.
- #get_type(name) ⇒ Object
-
#initialize ⇒ GraphQLSchema
constructor
A new instance of GraphQLSchema.
Constructor Details
#initialize ⇒ GraphQLSchema
Returns a new instance of GraphQLSchema.
64 65 66 67 68 69 |
# File 'lib/tina4/graphql.rb', line 64 def initialize @types = {} @queries = {} # name => { type:, args:, resolve:, description: } @mutations = {} register_scalars end |
Instance Attribute Details
#mutations ⇒ Object (readonly)
Returns the value of attribute mutations.
62 63 64 |
# File 'lib/tina4/graphql.rb', line 62 def mutations @mutations end |
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
62 63 64 |
# File 'lib/tina4/graphql.rb', line 62 def queries @queries end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
62 63 64 |
# File 'lib/tina4/graphql.rb', line 62 def types @types end |
Instance Method Details
#add_mutation(name, args = {}, return_type = nil, resolver = nil, &block) ⇒ Object
Register a mutation field. Cross-framework form: add_mutation(name, args, return_type, resolver) Block form also accepted: add_mutation(name, args, return_type) { |root, args, ctx| … }
98 99 100 101 |
# File 'lib/tina4/graphql.rb', line 98 def add_mutation(name, args = {}, return_type = nil, resolver = nil, &block) resolve = resolver || block @mutations[name] = { type: return_type, args: args, resolve: resolve } end |
#add_query(name, args = {}, return_type = nil, resolver = nil, &block) ⇒ Object
Register a query field. Cross-framework form: add_query(name, args, return_type, resolver) Block form also accepted: add_query(name, args, return_type) { |root, args, ctx| … }
90 91 92 93 |
# File 'lib/tina4/graphql.rb', line 90 def add_query(name, args = {}, return_type = nil, resolver = nil, &block) resolve = resolver || block @queries[name] = { type: return_type, args: args, resolve: resolve } end |
#add_type(name_or_type, fields = nil) ⇒ Object
add_type(name, fields) — parity with PHP/Python/Node add_type(type_object) — legacy Ruby form (type_object responds to .name)
73 74 75 76 77 78 79 80 81 |
# File 'lib/tina4/graphql.rb', line 73 def add_type(name_or_type, fields = nil) if fields # New form: add_type("User", { "id" => "ID", "name" => "String" }) @types[name_or_type] = fields else # Legacy form: add_type(GraphQLType.new(...)) @types[name_or_type.name] = name_or_type end end |
#from_orm(klass) ⇒ Object
── ORM Auto-Schema ──────────────────────────────────────────────────Generates GraphQL types + CRUD queries/mutations from a Tina4::ORM subclass.
schema.from_orm(User)
Creates:
Query: user(id), users(limit, offset)
Mutation: createUser(input), updateUser(id, input), deleteUser(id)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 |
# File 'lib/tina4/graphql.rb', line 111 def from_orm(klass) model_name = klass.name.split("::").last type_name = model_name table_lower = model_name.gsub(/([A-Z])/, '_\1').sub(/\A_/, "").downcase plural = "#{table_lower}s" # Build GraphQL object type from ORM field definitions gql_fields = {} pk_field = nil if klass.respond_to?(:field_definitions) klass.field_definitions.each do |fname, fdef| gql_type = ruby_field_to_gql(fdef[:type] || :string) gql_fields[fname.to_s] = { type: gql_type } pk_field = fname.to_s if fdef[:primary_key] end end pk_field ||= "id" gql_fields[pk_field] ||= { type: "ID" } obj_type = GraphQLType.new(type_name, :object, fields: gql_fields) add_type(obj_type) # Input type for create/update input_fields = gql_fields.reject { |k, _| k == pk_field } input_type = GraphQLType.new("#{type_name}Input", :input_object, fields: input_fields) add_type(input_type) # ── Queries ── # Single record: user(id: ID!): User add_query(table_lower, { pk_field => { type: "ID!" } }, type_name) do |_root, args, _ctx| record = klass.find_by_id(args[pk_field]) record&.to_hash end # List: users(limit: Int, offset: Int): [User] add_query(plural, { "limit" => { type: "Int" }, "offset" => { type: "Int" } }, "[#{type_name}]") do |_root, args, _ctx| limit = args["limit"] || 100 offset = args["offset"] || 0 result = klass.all(limit: limit, offset: offset) result.respond_to?(:to_array) ? result.to_array : Array(result).map { |r| r.respond_to?(:to_hash) ? r.to_hash : r } end # ── Mutations ── # Create add_mutation("create#{model_name}", { "input" => { type: "#{type_name}Input!" } }, type_name) do |_root, args, _ctx| record = klass.create(args["input"] || {}) record.respond_to?(:to_hash) ? record.to_hash : record end # Update add_mutation("update#{model_name}", { pk_field => { type: "ID!" }, "input" => { type: "#{type_name}Input!" } }, type_name) do |_root, args, _ctx| record = klass.find_by_id(args[pk_field]) return nil unless record (args["input"] || {}).each { |k, v| record.send(:"#{k}=", v) if record.respond_to?(:"#{k}=") } record.save record.to_hash end # Delete add_mutation("delete#{model_name}", { pk_field => { type: "ID!" } }, "Boolean") do |_root, args, _ctx| record = klass.find_by_id(args[pk_field]) return false unless record record.delete true end end |
#get_type(name) ⇒ Object
83 84 85 |
# File 'lib/tina4/graphql.rb', line 83 def get_type(name) @types[name] end |