Class: Parse::GraphQL::TypeGenerator
- Inherits:
-
Object
- Object
- Parse::GraphQL::TypeGenerator
- Defined in:
- lib/parse/graphql/type_generator.rb
Overview
Generates GraphQL::Schema::Object subclasses from Parse::Object
subclasses by reading the class-level property and association
registries: fields, field_map, references (belongs_to),
has_one_associations, has_many_associations, and relations.
v1 scope: type shape only. Default graphql-ruby field resolution
invokes the same-named method on the underlying Ruby object, and
Parse::Object subclasses already expose typed accessors
(song.album, band.fans, etc.) — so no resolvers are emitted.
Pagination arguments, Loaders, and Relay connections are deferred
until query/mutation passthrough lands (TODO §7).
== Schema shape, not an authorization layer
The generated types describe the shape of a Parse class. They do not authorize anything. Default graphql-ruby resolution reads whatever is on the object handed to the resolver, so the resolvers (or the objects passed to them) must enforce ACL, CLP, and protectedFields for the requesting user. The usual way is to resolve through queries run with that user's session token, so Parse Server applies its own checks. Omitting a field from a type does not stop a resolver from loading or returning it through another path.
== Field policy
Output is deterministic: the same model and the same options always
produce the same fields. Without protected_fields: :fetch, no CLP
cache or ambient state is consulted.
- Credential denylist (always on). Session tokens, auth data, passwords, device tokens, and every column in PipelineSecurity::INTERNAL_FIELDS_DENYLIST (see SENSITIVE_FIELD_NAMES) are dropped, matched by local accessor or wire name. The denylist only keeps known credential columns out; it is not a substitute for protectedFields.
exclude_fields:drops caller-listed fields.protected_fields:applies the class's public ("*") protectedFields, only when supplied or explicitly fetched. With the default (nil) no protectedFields lookup happens at all, so a field such as_User.emailis emitted unless it is excluded or supplied here. Pass a Hash ({ "ClassName" => [:field, ...] }) to supply the lists, or:fetchto load each class's CLP from the server throughParse::CLPScope.protected_fields_for(a schema request per class, which includesParse::CLPScope.default_protected_fields). A failed fetch raises ProtectedFieldsUnavailable.include_sensitive:emits a field the denylist or protectedFields would drop.exclude_fields:still wins over it.
has_many fields are emitted as plain unpaginated lists ([Type]), and
nested selections are not bounded here. A schema built from these
types should set max_depth and max_complexity (graphql-ruby schema
class methods) so a single query cannot walk an unbounded object graph.
Cross-class references (pointers, has_many) require all referenced
model types to be in the registry BEFORE field emission. Use
generate_all to codegen a list of models in one call (two-pass:
stub classes first, then add fields), or generate with an
explicit registry: hash if you want incremental control.
Defined Under Namespace
Classes: ProtectedFieldsUnavailable
Constant Summary collapse
- SCALAR_TYPE_MAP =
Maps Parse property
:typesymbols to graphql-ruby built-ins. Cross-class references (:pointer,:relation) are NOT in this map — they need the target class and are handled per-field. Nil mappings fall through to the JSON scalar with a warning. { string: ::GraphQL::Types::String, integer: ::GraphQL::Types::Int, float: ::GraphQL::Types::Float, # A :number property may hold fractional values, which GraphQL's # 32-bit Int cannot represent, so it is exposed as Float. number: ::GraphQL::Types::Float, boolean: ::GraphQL::Types::Boolean, date: ::GraphQL::Types::ISO8601DateTime, timezone: ::GraphQL::Types::String, phone: ::GraphQL::Types::String, email: ::GraphQL::Types::String, # Parse Bytes is a `{__type: Bytes, base64: ...}` wrapper, not a # bare string — the accessor returns the wrapped object, so # falling through to the JSON scalar (with a warn) is safer than # ::String.to_s on the hash. Subscribers needing structured byte # access should declare a `:string` property containing the base64. bytes: nil, polygon: nil, # JSON fallback (GeoJSON-shaped, multi-ring) # Parse vector columns are bounded-length Float arrays # (embeddings). Emit as a typed list of Floats, not JSON. vector: [::GraphQL::Types::Float], array: nil, # JSON fallback (no element type known) object: nil, # JSON fallback }.freeze
- OMITTED_TYPES =
Property types that are deliberately omitted. ACL is internal authz metadata; exposing it via GraphQL would leak authorization shape into clients. The objectId is exposed under the canonical
id: IDfield separately. %i[acl].freeze
- SENSITIVE_FIELD_NAMES =
Credential field names that are never emitted, whatever their declared type. Compared against both the local accessor and the Parse wire name. Columns starting with
_auth_data_are also dropped. ( %w[session_token sessionToken auth_data authData password device_token deviceToken] + Parse::PipelineSecurity::INTERNAL_FIELDS_DENYLIST ).to_set.freeze
- SENSITIVE_FIELD_PREFIXES =
Prefixes of credential columns that are never emitted.
Parse::PipelineSecurity::INTERNAL_FIELDS_PREFIX_DENYLIST
Class Method Summary collapse
- .build_stub(model_class) ⇒ Object
-
.detect_name_collisions!(registry) ⇒ Object
graphql-ruby requires unique
graphql_nameacross the schema. -
.generate(model_class, registry: nil, include_sensitive: [], exclude_fields: [], protected_fields: nil) ⇒ Class
Generate a single type.
-
.generate_all(model_classes, include_sensitive: [], exclude_fields: [], protected_fields: nil) ⇒ Hash{String => Class}
Generate types for a list of models in one call.
-
.per_class_value(map, model_class) ⇒ Object
Look up a per-class option keyed by Parse class name (String or Symbol) or by the model class itself.
- .validate!(model_class) ⇒ Object
- .validate_protected_fields_option!(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(model_class, registry:, _prebuilt:, include_sensitive: [], exclude_fields: [], protected_fields: nil) ⇒ TypeGenerator
constructor
A new instance of TypeGenerator.
- #populate_fields ⇒ Object
Constructor Details
#initialize(model_class, registry:, _prebuilt:, include_sensitive: [], exclude_fields: [], protected_fields: nil) ⇒ TypeGenerator
Returns a new instance of TypeGenerator.
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/parse/graphql/type_generator.rb', line 244 def initialize(model_class, registry:, _prebuilt:, include_sensitive: [], exclude_fields: [], protected_fields: nil) @model_class = model_class @registry = registry @klass = _prebuilt @include_sensitive = Array(include_sensitive).map(&:to_s).to_set excluded = exclude_fields.is_a?(Hash) ? self.class.per_class_value(exclude_fields, model_class) : exclude_fields @exclude_fields = Array(excluded).map(&:to_s).to_set @protected_fields_option = protected_fields end |
Class Method Details
.build_stub(model_class) ⇒ Object
235 236 237 238 239 240 241 242 |
# File 'lib/parse/graphql/type_generator.rb', line 235 def self.build_stub(model_class) type_name = model_class.parse_class.tr("_", "") description_text = "Generated GraphQL type for Parse class #{model_class.parse_class}." Class.new(::GraphQL::Schema::Object) do graphql_name type_name description description_text end end |
.detect_name_collisions!(registry) ⇒ Object
graphql-ruby requires unique graphql_name across the schema.
build_stub strips underscores so _User and User collapse
to the same name. Raise a clear error rather than letting
graphql-ruby's DuplicateNamesError surface at schema-build
time, which doesn't say which Parse classes collided.
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/parse/graphql/type_generator.rb', line 172 def self.detect_name_collisions!(registry) by_gql_name = registry.each_with_object({}) do |(parse_name, type), acc| (acc[type.graphql_name] ||= []) << parse_name end collisions = by_gql_name.select { |_, names| names.size > 1 } return if collisions.empty? details = collisions.map { |gql, parse| "#{gql} ← #{parse.join(", ")}" }.join("; ") raise "Parse::GraphQL::TypeGenerator: graphql_name collisions: #{details}. " \ "Parse class names that differ only by underscores collapse to the same " \ "GraphQL type name. Rename or generate the conflicting classes separately." end |
.generate(model_class, registry: nil, include_sensitive: [], exclude_fields: [], protected_fields: nil) ⇒ Class
Generate a single type. If the model has belongs_to / has_many
references to other Parse classes, those targets must already be
in the registry — otherwise an error is raised at field-emit
time. Prefer generate_all when you have a graph of models.
The result is a schema shape, not an authorization layer; see generate_all.
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/parse/graphql/type_generator.rb', line 199 def self.generate(model_class, registry: nil, include_sensitive: [], exclude_fields: [], protected_fields: nil) validate!(model_class) validate_protected_fields_option!(protected_fields) registry ||= {} stub = registry[model_class.parse_class] ||= build_stub(model_class) new(model_class, registry: registry, _prebuilt: stub, include_sensitive: include_sensitive, exclude_fields: exclude_fields, protected_fields: protected_fields).populate_fields stub end |
.generate_all(model_classes, include_sensitive: [], exclude_fields: [], protected_fields: nil) ⇒ Hash{String => Class}
Generate types for a list of models in one call. Two-pass: creates empty stub classes for every model first so cross-class references resolve regardless of declaration order.
The result is a schema shape, not an authorization layer: resolvers
must enforce ACL, CLP, and protectedFields for the requesting user
(for example by resolving through queries run with that user's
session token). has_many fields are unpaginated lists, so set
max_depth and max_complexity on the schema.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/parse/graphql/type_generator.rb', line 148 def self.generate_all(model_classes, include_sensitive: [], exclude_fields: [], protected_fields: nil) validate_protected_fields_option!(protected_fields) registry = {} # Pass 1: stub classes registered by parse_class name. model_classes.each do |model| validate!(model) registry[model.parse_class] = build_stub(model) end # Pass 2: populate fields. Cross-references now resolve. model_classes.each do |model| new(model, registry: registry, _prebuilt: registry[model.parse_class], include_sensitive: include_sensitive, exclude_fields: exclude_fields, protected_fields: protected_fields).populate_fields end detect_name_collisions!(registry) registry end |
.per_class_value(map, model_class) ⇒ Object
Look up a per-class option keyed by Parse class name (String or Symbol) or by the model class itself.
220 221 222 223 224 225 226 |
# File 'lib/parse/graphql/type_generator.rb', line 220 def self.per_class_value(map, model_class) name = model_class.parse_class [name, name.to_sym, model_class].each do |key| return map[key] if map.key?(key) end nil end |
.validate!(model_class) ⇒ Object
228 229 230 231 232 233 |
# File 'lib/parse/graphql/type_generator.rb', line 228 def self.validate!(model_class) unless model_class.is_a?(Class) && model_class < Parse::Object raise ArgumentError, "Parse::GraphQL::TypeGenerator requires a Parse::Object subclass, got #{model_class.inspect}" end end |
.validate_protected_fields_option!(value) ⇒ Object
211 212 213 214 215 216 |
# File 'lib/parse/graphql/type_generator.rb', line 211 def self.validate_protected_fields_option!(value) return if value.nil? || value == :fetch || value.is_a?(Hash) raise ArgumentError, "Parse::GraphQL::TypeGenerator: protected_fields: must be nil, :fetch, " \ "or a Hash of class name => field list, got #{value.inspect}" end |
Instance Method Details
#populate_fields ⇒ Object
255 256 257 258 259 260 261 |
# File 'lib/parse/graphql/type_generator.rb', line 255 def populate_fields emit_scalar_fields emit_belongs_to_fields emit_has_one_fields emit_has_many_fields @klass end |