Skip to content

Commit

Permalink
Merge pull request rmosolgo#4352 from gmac/gmac/add_leaf_distinction
Browse files Browse the repository at this point in the history
Add `leaf?` distinction to TypeKind
  • Loading branch information
rmosolgo authored Feb 20, 2023
2 parents 532bd2f + e44723e commit d265f4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/graphql/type_kinds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ module TypeKinds
# These objects are singletons, eg `GraphQL::TypeKinds::UNION`, `GraphQL::TypeKinds::SCALAR`.
class TypeKind
attr_reader :name, :description
def initialize(name, abstract: false, fields: false, wraps: false, input: false, description: nil)
def initialize(name, abstract: false, leaf: false, fields: false, wraps: false, input: false, description: nil)
@name = name
@abstract = abstract
@fields = fields
@wraps = wraps
@input = input
@leaf = leaf
@composite = fields? || abstract?
@description = description
end
Expand All @@ -27,6 +28,8 @@ def wraps?; @wraps; end
# Is this TypeKind a valid query input?
def input?; @input; end
def to_s; @name; end
# Is this TypeKind a primitive value?
def leaf?; @leaf; end
# Is this TypeKind composed of many values?
def composite?; @composite; end

Expand Down Expand Up @@ -64,11 +67,11 @@ def non_null?
end

TYPE_KINDS = [
SCALAR = TypeKind.new("SCALAR", input: true, description: 'Indicates this type is a scalar.'),
SCALAR = TypeKind.new("SCALAR", input: true, leaf: true, description: 'Indicates this type is a scalar.'),
OBJECT = TypeKind.new("OBJECT", fields: true, description: 'Indicates this type is an object. `fields` and `interfaces` are valid fields.'),
INTERFACE = TypeKind.new("INTERFACE", abstract: true, fields: true, description: 'Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.'),
UNION = TypeKind.new("UNION", abstract: true, description: 'Indicates this type is a union. `possibleTypes` is a valid field.'),
ENUM = TypeKind.new("ENUM", input: true, description: 'Indicates this type is an enum. `enumValues` is a valid field.'),
ENUM = TypeKind.new("ENUM", input: true, leaf: true, description: 'Indicates this type is an enum. `enumValues` is a valid field.'),
INPUT_OBJECT = TypeKind.new("INPUT_OBJECT", input: true, description: 'Indicates this type is an input object. `inputFields` is a valid field.'),
LIST = TypeKind.new("LIST", wraps: true, description: 'Indicates this type is a list. `ofType` is a valid field.'),
NON_NULL = TypeKind.new("NON_NULL", wraps: true, description: 'Indicates this type is a non-null. `ofType` is a valid field.'),
Expand Down
15 changes: 15 additions & 0 deletions spec/graphql/type_kinds/type_kind_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::TypeKinds::TypeKind do
describe ".leaf?" do
it "is true for enums and scalars, but false for others" do
assert GraphQL::Schema::Scalar.kind.leaf?
assert GraphQL::Schema::Enum.kind.leaf?
refute GraphQL::Schema::Object.kind.leaf?
refute GraphQL::Schema::Interface.kind.leaf?
refute GraphQL::Schema::Union.kind.leaf?
refute GraphQL::Schema::InputObject.kind.leaf?
end
end
end

0 comments on commit d265f4a

Please sign in to comment.