def generate_graphql_type(graph_type): if graph_type == schema.Boolean: return graphql.GraphQLNonNull(graphql.GraphQLBoolean) elif graph_type == schema.Float: return graphql.GraphQLNonNull(graphql.GraphQLFloat) elif graph_type == schema.Int: return graphql.GraphQLNonNull(graphql.GraphQLInt) elif graph_type == schema.String: return graphql.GraphQLNonNull(graphql.GraphQLString) elif isinstance(graph_type, schema.EnumType): # TODO: should enums map names or values? values = iterables.to_dict( (member.value, graphql.GraphQLEnumValue(member.value)) for member in graph_type.enum) graphql_type = graphql.GraphQLEnumType(graph_type.name, values=values) return graphql.GraphQLNonNull(graphql_type) elif isinstance(graph_type, schema.InputObjectType): return graphql.GraphQLNonNull( graphql.GraphQLInputObjectType( name=graph_type.name, fields=lambda: iterables.to_dict( (snake_case_to_camel_case(field.name), to_graphql_input_field(field)) for field in graph_type.fields), )) elif isinstance(graph_type, schema.InterfaceType): return graphql.GraphQLNonNull( graphql.GraphQLInterfaceType( name=graph_type.name, fields=to_graphql_fields(graph_type.fields), resolve_type=lambda: None, )) elif isinstance(graph_type, schema.ListType): return graphql.GraphQLNonNull( graphql.GraphQLList(to_graphql_type(graph_type.element_type))) elif isinstance(graph_type, schema.NullableType): return to_graphql_type(graph_type.element_type).of_type elif isinstance(graph_type, schema.ObjectType): return graphql.GraphQLNonNull( graphql.GraphQLObjectType( name=graph_type.name, fields=to_graphql_fields(graph_type.fields), interfaces=tuple( to_graphql_type(interface).of_type for interface in graph_type.interfaces), )) else: raise ValueError("unsupported type: {}".format(graph_type))
def _transform_class_to_output_type(cls, ctx: TransformContext): fields = [] dc_fields = {} if is_dataclass(cls): dc_fields = {f.name: f for f in dataclass_fields(cls)} for name, definition, field_kw in iterate_class_attributes_for_output_type( cls, ctx): type_ = definition.type_ dataclass_field = dc_fields.get(name) if dataclass_field: field_kw.update(dataclass_field.metadata) fields.append((name, type_, field_kw)) if not fields: raise TypeError( f"Please define proper attributes on {cls.__qualname__}") if is_interface(cls): resolve_type = ctx.get("resolve_type", getattr(cls, "resolve_type", None)) if resolve_type is None: resolve_type = ctx.hook__prepare_default_interface_type_resolver( cls) else: resolve_type = ctx.hook__prepare_interface_type_resolver( resolve_type, cls) return graphql.GraphQLInterfaceType( name=get_name(cls), fields=_lazy_fields(ctx, fields), description=get_doc(cls), resolve_type=resolve_type, ) interfaces = lambda: [ _transform_class_to_output_type(interface_cls, ctx) for interface_cls in get_interfaces(cls) ] return graphql.GraphQLObjectType( name=get_name(cls), fields=_lazy_fields(ctx, fields), description=get_doc(cls), interfaces=interfaces, )
def factory( name: Optional[str], description: Optional[str] ) -> Union[graphql.GraphQLObjectType, graphql.GraphQLInterfaceType]: name = unwrap_name(name, cls) if is_interface(cls): return graphql.GraphQLInterfaceType(name, field_thunk, interface_thunk, description=description) else: return graphql.GraphQLObjectType( name, field_thunk, interface_thunk, is_type_of=lambda obj, _: isinstance(obj, cls), description=description, )
def object( self, cls: Type, fields: Collection[ObjectField], merged_types: Mapping[str, Thunk[graphql.GraphQLType]] = None, ) -> Thunk[graphql.GraphQLType]: fields_and_resolvers = list(fields) try: name, description = self._ref_and_desc except MissingRef: if cls.__name__ not in ("Query", "Mutation", "Subscription"): raise name, description = cls.__name__, self._description for resolver_name, resolver in get_resolvers(cls).items(): resolve = resolver_resolve(resolver, self.aliaser, error_as_null=self.error_as_null) resolver_field = ObjectField( resolver_name, wrap_return_type(resolver.return_type, self.error_as_null), conversions=resolver.conversions, parameters=field_parameters(resolver), resolve=resolve, ) fields_and_resolvers.append(resolver_field) visited_fields = dict(map(self._field, fields_and_resolvers)) def field_thunk() -> graphql.GraphQLFieldMap: return merge_fields( cls, visited_fields, merged_types or {}, deref_merged_field, ) interfaces = list(map(self.visit, get_interfaces(cls))) interface_thunk = None if interfaces: def interface_thunk() -> Collection[graphql.GraphQLInterfaceType]: result = {exec_thunk(i, non_null=False) for i in interfaces} for merged_thunk in (merged_types or {}).values(): merged = cast( Union[graphql.GraphQLObjectType, graphql.GraphQLInterfaceType], exec_thunk(merged_thunk, non_null=False), ) result.update(merged.interfaces) return sorted(result, key=lambda i: i.name) if is_interface(cls): return lambda: graphql.GraphQLInterfaceType( name, field_thunk, interface_thunk, description=description, ) else: return lambda: graphql.GraphQLObjectType( name, field_thunk, interface_thunk, is_type_of=lambda obj, _: isinstance(obj, cls), description=description, )