def _get_parent_type(self, node: NodeField) -> Union[str, "GraphQLType"]: try: return reduce_type(node.field_executor.schema_field.gql_type) except (AttributeError, TypeError): pass return self.schema.find_type( self.schema.get_operation_type(self._internal_ctx.operation.type))
def _get_coercer(field: "GraphQLField") -> Optional[Callable]: if not field.schema: return None field_type = field.gql_type reduced_type = reduce_type(field_type) is_union = _is_union(reduced_type, field.schema) if reduced_type == "__Type": # preserve None coercer = partial( _built_in_coercer, partial(_object_coercer, reduced_type if not is_union else None), ) else: try: coercer = _is_an_enum(reduced_type, field.schema) if not coercer: coercer = _is_a_scalar(reduced_type, field.schema) if not coercer: # per default you're an object coercer = partial( _object_coercer, reduced_type if not is_union else None ) except AttributeError: coercer = partial( _object_coercer, reduced_type if not is_union else None ) # Manage List and NonNull return _list_and_null_coercer(field_type, coercer)
async def argument_coercer(argument_definition, args, ctx, info): value = UNDEFINED_VALUE try: value = args[argument_definition.name] except KeyError: pass if value is UNDEFINED_VALUE and argument_definition.default_value: value = argument_definition.default_value if value is UNDEFINED_VALUE: return value try: value = value.value except AttributeError: pass schema_type = argument_definition.schema.find_type( reduce_type(argument_definition.gql_type)) if not isinstance(schema_type, GraphQLInputObjectType): return value return await coerce_arguments(schema_type.arguments, value, ctx, info)
def get_coercer( field: Union["GraphQLField", "GraphQLArgument"], schema=None, way=CoercerWay.OUTPUT, ) -> Optional[Callable]: schema = schema or field.schema if not schema: return None field_type = field.gql_type reduced_type = reduce_type(field_type) is_union = _is_union(reduced_type, schema) default_coercer = partial(_object_coercer, reduced_type if not is_union else None) # TODO We can do better here. try: coercer = _is_an_enum(reduced_type, schema, way) if not coercer: coercer = _is_a_scalar(reduced_type, schema, way) if not coercer: coercer = _is_an_input_object(reduced_type, schema) if not coercer: # per default you're an object coercer = default_coercer except AttributeError: coercer = default_coercer # Manage List and NonNull coercer = _list_and_null_coercer(field_type, coercer) # Manage directives return _add_directive_runner_partial(coercer, reduced_type, schema, way)
def _on_field_in(self, element: _VisitorElement): self.field_path.append(element.name) self._depth = self._depth + 1 try: parent_type = self._current_node.gql_type except AttributeError: parent_type = self._schema.types[self._schema.query_type] field = self._schema.get_field_by_name(parent_type.name + '.' + element.name) try: gql_type = reduce_type(field.gql_type) except AttributeError: self.continue_child = 0 self.exception = TartifletteException("No field %s in %s" % (element.name, parent_type)) return resolver = getattr(field, 'resolver', _default_resolver) if resolver is None: resolver = wrap_resolver(self._schema, field, _default_resolver) node = NodeField(resolver, element.get_location(), self.field_path[:], element.name, self._current_type_condition, gql_type, field) node.parent = self._current_node self._current_node = node self._add_node(node)
def _validate_type_is_an_input_types(self, obj, message_prefix) -> List[str]: errors = [] rtype = reduce_type(obj.gql_type) if not rtype in self._input_types: errors.append(f"{message_prefix} is of type " f"< {rtype} > which is not a Scalar, " f"an Enum or an InputObject") return errors
def _validate_schema_named_types(self) -> bool: for type_name, gql_type in self._gql_types.items(): try: for field in gql_type.fields: reduced_type = reduce_type(field.gql_type) if str(reduced_type) not in self._gql_types: raise GraphQLSchemaError( "field `{}` in GraphQL type `{}` is invalid, " "the given type `{}` does not exist!".format( field.name, type_name, reduced_type)) except AttributeError: pass return True
def _validate_schema_named_types(self) -> List[str]: errors = [] for type_name, gql_type in self._gql_types.items(): try: for field in gql_type.fields: reduced_type = reduce_type(field.gql_type) if str(reduced_type) not in self._gql_types: errors.append( f"Field < {type_name}.{field.name} > is Invalid: " f"the given Type < {reduced_type} > does not exist!" ) except AttributeError: pass return errors
async def argument_coercer( argument_definition, args, ctx, info, input_coercer=None ): value = UNDEFINED_VALUE try: value = args[argument_definition.name] except KeyError: pass if value is UNDEFINED_VALUE and argument_definition.default_value: value = argument_definition.default_value if value is UNDEFINED_VALUE: return value try: value = ( value.value if not input_coercer else input_coercer(value.value, info) ) except AttributeError: pass if value is None: return None schema_type = argument_definition.schema.find_type( reduce_type(argument_definition.gql_type) ) if ( not isinstance(schema_type, GraphQLInputObjectType) or argument_definition.is_list_type ): return value if ( not isinstance(argument_definition.gql_type, str) and argument_definition.is_list_type ): return await asyncio.gather( *[ coerce_arguments(schema_type.arguments, x, ctx, info) for x in value ] ) return await coerce_arguments(schema_type.arguments, value, ctx, info)
def get_resolver(self, field_path: str) -> Optional[callable]: if not field_path: return None field_path_pieces = field_path.split('.') if not self._gql_types.get(field_path_pieces[0]): field_path_pieces.insert(0, self._query_type) field = self.get_field_by_name('.'.join(field_path_pieces[0:2])) while field_path_pieces[-1] != getattr(field, 'name', None): if not field: break field_path_pieces = [reduce_type(field.gql_type)] + field_path_pieces[2:] field = self.get_field_by_name(".".join(field_path_pieces[0:2])) if field: return field.resolver return None
def _compute_is_leaf(self) -> bool: rtype = reduce_type(self.gql_type) try: if self._schema.find_scalar(rtype): return True except KeyError: pass try: if self._schema.find_enum(rtype): return True except KeyError: pass return False
def bake( self, schema: "GraphQLSchema", parent_type: Any, custom_default_resolver: Optional[Callable], ) -> None: self._schema = schema self._reduced_type_name = reduce_type(self.gql_type) self._reduced_type = self._schema.find_type(self._reduced_type_name) self._directives_implementations = get_directive_instances( self._directives, self._schema) self._introspection_directives = wraps_with_directives( directives_definition=self._directives_implementations, directive_hook="on_introspection", ) self.parent_type = parent_type self._is_leaf = self._compute_is_leaf() for arg in self.arguments.values(): arg.bake(self._schema) self.resolver.bake(custom_default_resolver)
def test_reduce_type(gql_type, expected): reduced_gql_type = reduce_type(gql_type) assert reduced_gql_type == expected