def get_graphql_type(self, pytype): """Resolve a Python type to equivalent GraphQL type""" if isinstance(pytype, Object): return self.compile_object(pytype) if isinstance(pytype, Interface): return self.compile_interface(pytype) if isinstance(pytype, InputObject): return self.compile_input_object(pytype) if isinstance(pytype, Union): return self.compile_union(pytype) if isinstance(pytype, NonNull): return GraphQLNonNull(self.get_graphql_type(pytype.subtype)) if isinstance(pytype, List): return GraphQLList(self.get_graphql_type(pytype.subtype)) # If it's a GraphQL type already, just let it through # TODO: is this a good thing? W/O a good usecase, remove this if graphql.is_type(pytype): return pytype # Simple scalar types try: return self._type_map[pytype] except KeyError: pass # ---------------------------------------------------------------- # Lists, defined using ``typing.List[subtype]`` # # In Python 3.5 and 3.6, we could use issubclass(List[str], List). # In Python 3.7 this will fail, as "Subscribed generics cannot be # used with class and instance checks". # # As a fallback, we're checking the __origin__ attribute, but this # feels quite sub-optimal. # # Also, on Python 3.5 / 3.6 ``List[str].__origin__ is List``, # while on Python 3.7 ``List[str].__origin__ is list``. # # Search for a better alternative is under way. # ---------------------------------------------------------------- pytype_origin = getattr(pytype, "__origin__", None) if pytype_origin is list or pytype_origin is typing.List: (arg, ) = pytype.__args__ return GraphQLList(self.get_graphql_type(arg)) # TODO: support typing.Union type too! # Convert Python enum to graphql if isinstance(pytype, type) and issubclass(pytype, Enum): return self.compile_enum(pytype) raise TypeError("Unable to map Python type to GraphQL: %s", repr(pytype))
def transform(self, t, *, allow_null=MISSING, **kw): logger.debug( "[TRANSFORM] Try to transform %r with (allow_null=%s, %s)", t, allow_null, kw, ) t = resolve_thunk(t) # Unwrap type container if isinstance(t, T): allow_null = t.allow_null if t.allow_null is not MISSING else allow_null kw = {**t.graphql_kw, **kw} return self.transform(t.type_, allow_null=allow_null, **kw) ctx = self.ctx(origin_type=t, allow_null=allow_null, **kw) extra_options = get_extra_schema_options(t) if extra_options: ctx = ctx(**extra_options) gql_t = self._transform(t, ctx=ctx) # The case with unwrapping types # (they don't need nullability handling) if isinstance(gql_t, UnwrappedType): return gql_t.final_type # Catch if `t` was not converted if not graphql.is_type(gql_t): raise TypeError(f"Type {t!r} cannot be converted to graphql type.") return self._handle_nullability(gql_t, allow_null)
def get_graphql_type(self, _type): if not _type: return _type if is_type(_type): return _type if is_graphene_type(_type): graphql_type = self.get_type(_type._meta.name) assert graphql_type, "Type {} not found in this schema.".format(_type._meta.name) assert graphql_type.graphene_type == _type return graphql_type raise Exception("{} is not a valid GraphQL type.".format(_type))
def __eq__(self, other: Any) -> bool: """Return True if the CompilerEntity objects are equal, and False otherwise.""" if type(self) != type(other): return False if len(self._print_args) != len(other._print_args): return False # The args sometimes contain GraphQL type objects, which unfortunately do not define "==". # We have to split them out and compare them using "is_same_type()" instead. for self_arg, other_arg in six.moves.zip(self._print_args, other._print_args): if is_type(self_arg): if not is_same_type(self_arg, other_arg): return False else: if self_arg != other_arg: return False return self._print_kwargs == other._print_kwargs
def is_graphql_type(graphql_type): """Return True if the argument is a GraphQL type object, and False otherwise.""" # Helper function to work around the fact that "is_type" is a poorly-named function. return is_type(graphql_type)
def transform(t, ctx): if graphql.is_type(t): return t return None
def can_transform(t, ctx): return graphql.is_type(t)
def _matches(self, item): return graphql.is_type(item) and str(item) == str(self.type_)