Module: GraphQL::Schema::Member::HasFields Private
- Includes:
- EmptyObjects
- Included in:
- Interface::DefinitionMethods, GraphQL::Schema::Mutation, Object, Subscription
- Defined in:
- lib/graphql/schema/member/has_fields.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Shared code for Objects, Interfaces, Mutations, Subscriptions
Defined Under Namespace
Modules: InterfaceMethods, ObjectMethods
Constant Summary collapse
- RUBY_KEYWORDS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
A list of Ruby keywords.
[:class, :module, :def, :undef, :begin, :rescue, :ensure, :end, :if, :unless, :then, :elsif, :else, :case, :when, :while, :until, :for, :break, :next, :redo, :retry, :in, :do, :return, :yield, :super, :self, :nil, :true, :false, :and, :or, :not, :alias, :defined?, :BEGIN, :END, :__LINE__, :__FILE__]
- GRAPHQL_RUBY_KEYWORDS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
A list of GraphQL-Ruby keywords.
[:context, :object, :raw_value]
- CONFLICT_FIELD_NAMES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
A list of field names that we should advise users to pick a different resolve method name.
Set.new(GRAPHQL_RUBY_KEYWORDS + RUBY_KEYWORDS + Object.instance_methods)
Constants included from EmptyObjects
EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH
Instance Method Summary collapse
-
#add_field(field_defn, method_conflict_warning: field_defn.method_conflict_warning?) ⇒ void
private
Register this field with the class, overriding a previous one if needed.
-
#all_field_definitions ⇒ Object
private
-
#field(name_positional = nil, type_positional = nil, desc_positional = nil, **kwargs, &definition_block) {|field| ... } ⇒ GraphQL::Schema::Field
private
Add a field to this object or interface with the given definition.
-
#field_class(new_field_class = nil) ⇒ Class
private
The class to initialize when adding fields to this kind of schema member.
-
#global_id_field(field_name, **kwargs) ⇒ Object
private
-
#has_no_fields(new_has_no_fields) ⇒ void
private
-
#has_no_fields? ⇒ Boolean
private
trueifhas_no_fields(true)was configued. -
#own_fields ⇒ Hash<String => GraphQL::Schema::Field, Array<GraphQL::Schema::Field>>
private
Fields defined on this class specifically, not parent classes.
Instance Method Details
#add_field(field_defn, method_conflict_warning: field_defn.method_conflict_warning?) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Register this field with the class, overriding a previous one if needed.
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 |
# File 'lib/graphql/schema/member/has_fields.rb', line 111 def add_field(field_defn, method_conflict_warning: field_defn.method_conflict_warning?) # Check that `field_defn.original_name` equals `resolver_method` and `method_sym` -- # that shows that no override value was given manually. if method_conflict_warning && CONFLICT_FIELD_NAMES.include?(field_defn.resolver_method) && field_defn.original_name == field_defn.resolver_method && field_defn.original_name == field_defn.method_sym && field_defn.hash_key == NOT_CONFIGURED && field_defn.dig_keys.nil? warn(conflict_field_name_warning(field_defn)) end prev_defn = own_fields[field_defn.name] case prev_defn when nil own_fields[field_defn.name] = field_defn when Array prev_defn << field_defn when GraphQL::Schema::Field own_fields[field_defn.name] = [prev_defn, field_defn] else raise "Invariant: unexpected previous field definition for #{field_defn.name.inspect}: #{prev_defn.inspect}" end nil end |
#all_field_definitions ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/graphql/schema/member/has_fields.rb', line 174 def all_field_definitions all_fields = {} ancestors.reverse_each do |ancestor| if ancestor.respond_to?(:own_fields) all_fields.merge!(ancestor.own_fields) end end all_fields = all_fields.values all_fields.flatten! all_fields end |
#field(name_positional = nil, type_positional = nil, desc_positional = nil, **kwargs, &definition_block) {|field| ... } ⇒ GraphQL::Schema::Field
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add a field to this object or interface with the given definition
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/graphql/schema/member/has_fields.rb', line 56 def field(name_positional = nil, type_positional = nil, desc_positional = nil, **kwargs, &definition_block) resolver = kwargs.delete(:resolver) mutation = kwargs.delete(:mutation) subscription = kwargs.delete(:subscription) if (resolver_class = resolver || mutation || subscription) # Add a reference to that parent class kwargs[:resolver_class] = resolver_class end kwargs[:name] ||= name_positional if !type_positional.nil? if desc_positional if kwargs[:description] raise ArgumentError, "Provide description as a positional argument or `description:` keyword, but not both (#{desc_positional.inspect}, #{kwargs[:description].inspect})" end kwargs[:description] = desc_positional kwargs[:type] = type_positional elsif (resolver || mutation) && type_positional.is_a?(String) # The return type should be copied from the resolver, and the second positional argument is the description kwargs[:description] = type_positional else kwargs[:type] = type_positional end if type_positional.is_a?(Class) && type_positional < GraphQL::Schema::Mutation raise ArgumentError, "Use `field #{name_positional.inspect}, mutation: Mutation, ...` to provide a mutation to this field instead" end end kwargs[:owner] = self field_defn = field_class.new(**kwargs, &definition_block) add_field(field_defn) field_defn end |
#field_class(new_field_class = nil) ⇒ Class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The class to initialize when adding fields to this kind of schema member.
139 140 141 142 143 144 145 146 147 |
# File 'lib/graphql/schema/member/has_fields.rb', line 139 def field_class(new_field_class = nil) if new_field_class @field_class = new_field_class elsif defined?(@field_class) && @field_class @field_class else find_inherited_value(:field_class, GraphQL::Schema::Field) end end |
#global_id_field(field_name, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
149 150 151 152 153 154 155 |
# File 'lib/graphql/schema/member/has_fields.rb', line 149 def global_id_field(field_name, **kwargs) type = self field field_name, "ID", **kwargs, null: false define_method(field_name) do context.schema.id_from_object(object, type, context) end end |
#has_no_fields(new_has_no_fields) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
159 160 161 162 |
# File 'lib/graphql/schema/member/has_fields.rb', line 159 def has_no_fields(new_has_no_fields) @has_no_fields = new_has_no_fields nil end |
#has_no_fields? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true if has_no_fields(true) was configued.
165 166 167 |
# File 'lib/graphql/schema/member/has_fields.rb', line 165 def has_no_fields? @has_no_fields end |
#own_fields ⇒ Hash<String => GraphQL::Schema::Field, Array<GraphQL::Schema::Field>>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Fields defined on this class specifically, not parent classes.
170 171 172 |
# File 'lib/graphql/schema/member/has_fields.rb', line 170 def own_fields @own_fields ||= {} end |