Class: GraphQL::Schema::Object

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

Constant Summary

Constants included from Member::HasFields

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

Constants included from Member::HasDirectives

Member::HasDirectives::NO_DIRECTIVES

Constants included from Member::GraphQLTypeNames

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Member::HasFields

add_field, all_field_definitions, field, field_class, fields, get_field, global_id_field, own_fields

Methods included from Member::HasInterfaces

implements, interface_type_memberships, interfaces, own_interface_type_memberships

Methods included from Member::BaseDSLMethods

#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #introspection?, #mutation, #name, #overridden_graphql_name, #visible?

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

#scope_items

Methods included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Methods included from Member::HasPath

#path

Methods included from Member::HasAstNode

#ast_node

Methods included from Member::HasDirectives

#directive, #directives, #remove_directive

Constructor Details

#initialize(object, context) ⇒ Object

Returns a new instance of Object.



93
94
95
96
# File 'lib/graphql/schema/object.rb', line 93

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

Instance Attribute Details

#contextGraphQL::Query::Context (readonly)

Returns the context instance for this query.

Returns:



15
16
17
# File 'lib/graphql/schema/object.rb', line 15

def context
  @context
end

#objectObject (readonly)

Returns the application object this type is wrapping.

Returns:

  • (Object)

    the application object this type is wrapping



12
13
14
# File 'lib/graphql/schema/object.rb', line 12

def object
  @object
end

Class Method Details

.authorized_new(object, context) ⇒ GraphQL::Schema::Object, GraphQL::Execution::Lazy

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 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 GraphQL::Schema::Object instance. So, hopefully this wrapper method will do the job.

Parameters:

Returns:

Raises:



50
51
52
53
54
55
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/object.rb', line 50

def authorized_new(object, context)
  trace_payload = { context: context, type: self, object: object, path: context[:current_path] }

  maybe_lazy_auth_val = context.query.trace("authorized", trace_payload) do
    begin
      authorized?(object, context)
    rescue GraphQL::UnauthorizedError => err
      context.schema.unauthorized_object(err)
    rescue StandardError => err
      context.query.handle_or_reraise(err)
    end
  end

  auth_val = if context.schema.lazy?(maybe_lazy_auth_val)
    GraphQL::Execution::Lazy.new do
      context.query.trace("authorized_lazy", trace_payload) do
        context.schema.sync_lazy(maybe_lazy_auth_val)
      end
    end
  else
    maybe_lazy_auth_val
  end

  context.schema.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 = 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.



101
102
103
104
105
106
107
108
109
# File 'lib/graphql/schema/object.rb', line 101

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

.kindObject



111
112
113
# File 'lib/graphql/schema/object.rb', line 111

def kind
  GraphQL::TypeKinds::OBJECT
end

Instance Method Details

#dataloaderGraphQL::Dataloader

Returns:



18
19
20
# File 'lib/graphql/schema/object.rb', line 18

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.



24
25
26
# File 'lib/graphql/schema/object.rb', line 24

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