Class: Tina4::GraphQLSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/graphql.rb

Overview

─── Schema ───────────────────────────────────────────────────────────

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphQLSchema

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

#mutationsObject (readonly)

Returns the value of attribute mutations.



62
63
64
# File 'lib/tina4/graphql.rb', line 62

def mutations
  @mutations
end

#queriesObject (readonly)

Returns the value of attribute queries.



62
63
64
# File 'lib/tina4/graphql.rb', line 62

def queries
  @queries
end

#typesObject (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, type:, args: {}, description: nil, &resolve) ⇒ Object

Register a mutation field



86
87
88
# File 'lib/tina4/graphql.rb', line 86

def add_mutation(name, type:, args: {}, description: nil, &resolve)
  @mutations[name] = { type: type, args: args, resolve: resolve, description: description }
end

#add_query(name, type:, args: {}, description: nil, &resolve) ⇒ Object

Register a query field

schema.add_query("user", type: "User", args: { id: { type: "ID!" } }) { |root, args, ctx| ... }


81
82
83
# File 'lib/tina4/graphql.rb', line 81

def add_query(name, type:, args: {}, description: nil, &resolve)
  @queries[name] = { type: type, args: args, resolve: resolve, description: description }
end

#add_type(type) ⇒ Object



71
72
73
# File 'lib/tina4/graphql.rb', line 71

def add_type(type)
  @types[type.name] = type
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)


98
99
100
101
102
103
104
105
106
107
108
109
110
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
# File 'lib/tina4/graphql.rb', line 98

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, type: type_name,
            args: { pk_field => { type: "ID!" } },
            description: "Fetch a single #{model_name} by #{pk_field}") 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, type: "[#{type_name}]",
            args: { "limit" => { type: "Int" }, "offset" => { type: "Int" } },
            description: "Fetch a list of #{model_name} records") 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}", type: type_name,
               args: { "input" => { type: "#{type_name}Input!" } },
               description: "Create a new #{model_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}", type: type_name,
               args: { pk_field => { type: "ID!" }, "input" => { type: "#{type_name}Input!" } },
               description: "Update an existing #{model_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}", type: "Boolean",
               args: { pk_field => { type: "ID!" } },
               description: "Delete a #{model_name} by #{pk_field}") 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



75
76
77
# File 'lib/tina4/graphql.rb', line 75

def get_type(name)
  @types[name]
end