Class: GraphQL::Schema::Object

Inherits:
Member
  • Object
show all
Extended by:
Member::HasAuthorization, Member::HasFields, Member::HasInterfaces
Includes:
Member::HasDataloader
Defined in:
lib/graphql/schema/object.rb

Defined Under Namespace

Classes: FieldsAreRequiredError

Constant Summary

Constants included from Member::HasFields

Member::HasFields::CONFLICT_FIELD_NAMES, Member::HasFields::GRAPHQL_RUBY_KEYWORDS, Member::HasFields::RUBY_KEYWORDS

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Constants included from Member::GraphQLTypeNames

Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int

Instance Attribute Summary collapse

Attributes included from Member::BaseDSLMethods

#default_graphql_name, #graphql_name

Attributes included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Attributes included from Member::HasAstNode

#ast_node

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Member::HasAuthorization

authorized?, extended, included

Methods included from Member::HasFields

add_field, all_field_definitions, extended, field, field_class, global_id_field, has_no_fields, has_no_fields?, included, own_fields

Methods included from Member::HasInterfaces

extended, implements, interface_type_memberships, interfaces, own_interface_type_memberships

Methods included from Member::HasDataloader

#dataload, #dataload_all, #dataload_all_associations, #dataload_all_records, #dataload_association, #dataload_record

Methods inherited from Member

authorizes?

Methods included from Member::BaseDSLMethods

#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation, #visible?

Methods included from FindInheritedValue

extended, included

Methods included from Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Member::TypeSystemHelpers

#kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Member::Scoped

#inherited, #reauthorize_scoped_objects, #scope_items

Methods included from Member::HasPath

#path

Methods included from Member::HasAstNode

extended, #inherited

Methods included from Member::HasDirectives

add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive

Constructor Details

#initialize(object, context) ⇒ Object

Returns a new instance of Object.



152
153
154
155
# File 'lib/graphql/schema/object.rb', line 152

def initialize(object, context)
  @object = object
  @context = context
end

Instance Attribute Details

#context ⇒ Object (readonly)

Returns

  • GraphQL::Query::Context — the context instance for this query


31
32
33
# File 'lib/graphql/schema/object.rb', line 31

def context
  @context
end

#object ⇒ Object (readonly)

Returns

  • Object — the application object this type is wrapping


26
27
28
# File 'lib/graphql/schema/object.rb', line 26

def object
  @object
end

Class Method Details

.authorized_new(object, context) ⇒ Object

Make a new instance of this type if the auth check passes, otherwise, raise an error.

Probably only the framework should call this method.

This might return a GraphQL::Execution::Lazy if the user-provided .authorized? hook returns some lazy value (like a Promise).

The reason that the auth check is in this wrapper method instead of .new is because of how it might return a Promise. It would be weird if .new returned a promise; It would be a headache to try to maintain Promise-y state inside a Schema::Object instance. So, hopefully this wrapper method will do the job.

Parameters

  • object (Object) — The thing wrapped by this object
  • context (GraphQL::Query::Context)

Returns

  • GraphQL::Schema::Object, GraphQL::Execution::Lazy

Raises

  • GraphQL::UnauthorizedError — if the user-provided hook returns false

:call-seq:

authorized_new(Object object, GraphQL::Query::Context context) -> GraphQL::Schema::Object | GraphQL::Execution::Lazy | GraphQL::UnauthorizedError


91
92
93
94
95
96
97
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
# File 'lib/graphql/schema/object.rb', line 91

def authorized_new(object, context)
  context.query.current_trace.begin_authorized(self, object, context)
  raised_auth_err = nil #: GraphQL::UnauthorizedError?
  begin
    maybe_lazy_auth_val = context.query.current_trace.authorized(query: context.query, type: self, object: object) do
      begin
        authorized?(object, context)
      rescue GraphQL::UnauthorizedError => err
        # Raising is equivalent to returning `false`; `unauthorized_object` is called below
        raised_auth_err = err
        false
      rescue StandardError => err
        context.query.handle_or_reraise(err)
      end
    end
  ensure
    context.query.current_trace.end_authorized(self, object, context, maybe_lazy_auth_val)
  end

  auth_val = if context.schema.lazy?(maybe_lazy_auth_val)
    GraphQL::Execution::Lazy.new do
      context.query.current_trace.begin_authorized(self, object, context)
      context.query.current_trace.authorized_lazy(query: context.query, type: self, object: object) do
        res = begin
          context.schema.sync_lazy(maybe_lazy_auth_val)
        rescue GraphQL::UnauthorizedError => err
          raised_auth_err = err
          false
        end
        context.query.current_trace.end_authorized(self, object, context, res)
        res
      end
    end
  else
    maybe_lazy_auth_val
  end

  context.query.after_lazy(auth_val) do |is_authorized|
    if is_authorized
      self.new(object, context)
    else
      # It failed the authorization check, so go to the schema's authorized object hook
      err = raised_auth_err || GraphQL::UnauthorizedError.new(object: object, type: self, context: context)
      # If a new value was returned, wrap that instead of the original value
      begin
        new_obj = context.schema.unauthorized_object(err)
        if new_obj
          self.new(new_obj, context)
        else
          nil
        end
      end
    end
  end
end

.const_missing(name) ⇒ Object

Set up a type-specific invalid null error to use when this object's non-null fields wrongly return nil. It should help with debugging and bug tracker integrations.



160
161
162
163
164
165
166
167
168
# File 'lib/graphql/schema/object.rb', line 160

def const_missing(name)
  if name == :InvalidNullError
    custom_err_class = GraphQL::InvalidNullError.subclass_for(self)
    const_set(:InvalidNullError, custom_err_class)
    custom_err_class
  else
    super
  end
end

.kind ⇒ Object



170
171
172
# File 'lib/graphql/schema/object.rb', line 170

def kind
  GraphQL::TypeKinds::OBJECT
end

.scoped_new(object, context) ⇒ Object



147
148
149
# File 'lib/graphql/schema/object.rb', line 147

def scoped_new(object, context)
  self.new(object, context)
end

.wrap(object, context) ⇒ Object

This is called by the runtime to return an object to call methods on.



59
60
61
# File 'lib/graphql/schema/object.rb', line 59

def wrap(object, context)
  authorized_new(object, context)
end

.wrap_scoped(object, context) ⇒ Object



54
55
56
# File 'lib/graphql/schema/object.rb', line 54

def wrap_scoped(object, context)
  scoped_new(object, context)
end

Instance Method Details

#dataloader ⇒ Object

Returns

  • GraphQL::Dataloader

:call-seq:

dataloader() -> GraphQL::Dataloader


39
40
41
# File 'lib/graphql/schema/object.rb', line 39

def dataloader
  context.dataloader
end

#raw_value(obj) ⇒ Object

Call this in a field method to return a value that should be returned to the client without any further handling by GraphQL.



45
46
47
# File 'lib/graphql/schema/object.rb', line 45

def raw_value(obj)
  GraphQL::Execution::Interpreter::RawValue.new(obj)
end