Exemple #1
0
def _transform_class_to_input_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_input_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))

    def out_type(data: dict):
        return cls(**data)

    out_type = ctx.hook__prepare_input_object_type_out_type(cls)

    return graphql.GraphQLInputObjectType(
        name=get_name(cls),
        fields=_lazy_input_fields(ctx, fields),
        description=get_doc(cls),
        out_type=out_type,
    )
    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))
Exemple #3
0
 def factory(
         name: Optional[str],
         description: Optional[str]) -> graphql.GraphQLInputObjectType:
     name = unwrap_name(name, tp)
     if not name.endswith("Input"):
         name += "Input"
     return graphql.GraphQLInputObjectType(
         name,
         lambda: merge_fields(tp, visited_fields, flattened_types),
         description,
     )
Exemple #4
0
 def object(
     self,
     cls: Type,
     fields: Collection[ObjectField],
     merged_types: Mapping[str, Thunk[graphql.GraphQLType]] = None,
 ) -> Thunk[graphql.GraphQLType]:
     name, description = self._ref_and_desc
     name = name if name.endswith("Input") else name + "Input"
     visited_fields = dict(map(self._field, fields))
     return lambda: graphql.GraphQLInputObjectType(
         name,
         lambda: merge_fields(cls, visited_fields, merged_types or {}),
         description,
     )
Exemple #5
0
    def map_input(self, source: Type[Any]) -> GraphQLInputType:
        intersection = self.map_intersection(source, True)
        if intersection:
            return intersection

        name = f"{self.type_name(source)}Input"
        if name in self.mutation_types:
            return self.mutation_types[name]

        load_method = getattr(source, "load", None)
        if load_method:
            load_method = partial(load, callback=load_method, camelcase=self.camelcase)
        elif self.camelcase:
            load_method = to_snake

        helper = Helper(source, self)
        result = graphql.GraphQLInputObjectType(
            name,
            description=source.__doc__,
            fields=helper.input_fields,
            out_type=load_method,
        )
        self.mutation_types[name] = result
        return result
Exemple #6
0
import graphql
import graphql.utilities
import logging
import os.path
import re

from .index import Index
from .query import fetch, fetch_all

LocusInput = graphql.GraphQLInputObjectType(
    'Locus', {
        'gene': graphql.GraphQLInputField(graphql.GraphQLString),
        'chromosome': graphql.GraphQLInputField(graphql.GraphQLString),
        'position': graphql.GraphQLInputField(graphql.GraphQLInt),
        'start': graphql.GraphQLInputField(graphql.GraphQLInt),
        'end': graphql.GraphQLInputField(graphql.GraphQLInt),
    })


def load_schema(config, engine, schema_file):
    """
    Attempt to load a GraphQL schema file from disk.
    """
    if not os.path.isfile(schema_file):
        return None

    # parse the schema
    with open(schema_file) as fp:
        schema = graphql.utilities.build_schema(fp.read())

    # add resolvers for each index