Class: GraphQL::Schema::Object
- Extended by:
- Member::AcceptsDefinition, Member::HasFields
- Defined in:
- lib/graphql/schema/object.rb
Direct Known Subclasses
Constant Summary
Constants included from Member::GraphQLTypeNames
Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int
Instance Attribute Summary collapse
-
#context ⇒ GraphQL::Query::Context
readonly
The context instance for this query.
-
#object ⇒ Object
readonly
The application object this type is wrapping.
Class Method Summary collapse
-
.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.
-
.fields ⇒ Object
Include legacy-style interfaces, too.
- .implements(*new_interfaces) ⇒ Object
- .interfaces ⇒ Object
- .kind ⇒ Object
- .own_interfaces ⇒ Object
- .to_graphql ⇒ GraphQL::ObjectType
Instance Method Summary collapse
-
#initialize(object, context) ⇒ Object
constructor
A new instance of Object.
Methods included from Member::HasFields
add_default_resolve_module, add_field, extended, field, field_class, fields, global_id_field, included, inherited, own_fields
Methods included from Member::CachedGraphQLDefinition
#graphql_definition, #initialize_copy
Methods included from Relay::TypeExtensions
#connection_type, #define_connection, #define_edge, #edge_type
Methods included from Member::BaseDSLMethods
#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #mutation, #name, #overridden_graphql_name, #to_graphql, #visible?
Methods included from Member::TypeSystemHelpers
#kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature
Methods included from Member::Scoped
Methods included from Member::RelayShortcuts
#connection_type, #connection_type_class, #edge_type, #edge_type_class
Methods included from Member::HasPath
Constructor Details
#initialize(object, context) ⇒ Object
Returns a new instance of Object.
54 55 56 57 |
# File 'lib/graphql/schema/object.rb', line 54 def initialize(object, context) @object = object @context = context end |
Instance Attribute Details
#context ⇒ GraphQL::Query::Context (readonly)
Returns the context instance for this query.
13 14 15 |
# File 'lib/graphql/schema/object.rb', line 13 def context @context end |
#object ⇒ Object (readonly)
Returns the application object this type is wrapping.
10 11 12 |
# File 'lib/graphql/schema/object.rb', line 10 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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graphql/schema/object.rb', line 37 def (object, context) context.schema.after_lazy((object, context)) do || if 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 new_obj = context.schema.(err) if new_obj self.new(new_obj, context) end end end end |
.fields ⇒ Object
Include legacy-style interfaces, too
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/graphql/schema/object.rb', line 85 def fields all_fields = super interfaces.each do |int| if int.is_a?(GraphQL::InterfaceType) int_f = {} int.fields.each do |name, legacy_field| int_f[name] = field_class.(name, field: legacy_field) end all_fields = int_f.merge(all_fields) end end all_fields end |
.implements(*new_interfaces) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/graphql/schema/object.rb', line 60 def implements(*new_interfaces) new_interfaces.each do |int| if int.is_a?(Module) unless int.include?(GraphQL::Schema::Interface) raise "#{int} cannot be implemented since it's not a GraphQL Interface. Use `include` for plain Ruby modules." end # Include the methods here, # `.fields` will use the inheritance chain # to find inherited fields include(int) end end own_interfaces.concat(new_interfaces) end |
.interfaces ⇒ Object
76 77 78 |
# File 'lib/graphql/schema/object.rb', line 76 def interfaces own_interfaces + (superclass <= GraphQL::Schema::Object ? superclass.interfaces : []) end |
.kind ⇒ Object
117 118 119 |
# File 'lib/graphql/schema/object.rb', line 117 def kind GraphQL::TypeKinds::OBJECT end |
.own_interfaces ⇒ Object
80 81 82 |
# File 'lib/graphql/schema/object.rb', line 80 def own_interfaces @own_interfaces ||= [] end |
.to_graphql ⇒ GraphQL::ObjectType
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/graphql/schema/object.rb', line 100 def to_graphql obj_type = GraphQL::ObjectType.new obj_type.name = graphql_name obj_type.description = description obj_type.interfaces = interfaces obj_type.introspection = introspection obj_type.mutation = mutation fields.each do |field_name, field_inst| field_defn = field_inst.to_graphql obj_type.fields[field_defn.name] = field_defn end obj_type.[:type_class] = self obj_type end |