From 37772980c522937f4eeb3e7d196b204f31b15bfa Mon Sep 17 00:00:00 2001 From: Greg MacWilliam Date: Fri, 17 Feb 2023 21:05:06 -0500 Subject: [PATCH 1/3] add leaf distinction to type kinds. --- lib/graphql/type_kinds.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/graphql/type_kinds.rb b/lib/graphql/type_kinds.rb index ea62a3e637..61a80b3b76 100644 --- a/lib/graphql/type_kinds.rb +++ b/lib/graphql/type_kinds.rb @@ -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 @@ -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 @@ -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.'), From 192e30535befaa9753334c6f20b76946c7181d6d Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Mon, 20 Feb 2023 13:08:34 -0500 Subject: [PATCH 2/3] Add TypeKinds spec --- spec/graphql/type_kinds/type_kind_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 spec/graphql/type_kinds/type_kind_spec.rb diff --git a/spec/graphql/type_kinds/type_kind_spec.rb b/spec/graphql/type_kinds/type_kind_spec.rb new file mode 100644 index 0000000000..71b66c8592 --- /dev/null +++ b/spec/graphql/type_kinds/type_kind_spec.rb @@ -0,0 +1,14 @@ +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 From e44723e14300c856ef44f42e4da82edbdc525e7a Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Mon, 20 Feb 2023 13:10:26 -0500 Subject: [PATCH 3/3] Fix lint error --- spec/graphql/type_kinds/type_kind_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/graphql/type_kinds/type_kind_spec.rb b/spec/graphql/type_kinds/type_kind_spec.rb index 71b66c8592..e32dd5aed5 100644 --- a/spec/graphql/type_kinds/type_kind_spec.rb +++ b/spec/graphql/type_kinds/type_kind_spec.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require "spec_helper" describe GraphQL::TypeKinds::TypeKind do