Beispiel #1
0
def create_input_field(field_name,
                       field_type,
                       required=False) -> GraphQLInputField:
    if not required:
        return GraphQLInputField(field_type, out_name=field_name)
    else:
        return GraphQLInputField(GraphQLNonNull(field_type),
                                 out_name=field_name)
Beispiel #2
0
    def get_fields() -> GraphQLInputFieldMap:
        fields = {}

        for column in get_table(model).columns:
            fields[column.name] = GraphQLInputField(ORDER_BY_ENUM)

        for name, relationship in get_relationships(model):
            fields[name] = GraphQLInputField(
                inputs[get_model_order_by_input_name(
                    relationship.mapper.entity)])

        return fields
    def get_fields() -> GraphQLInputFieldMap:
        """ initial field population """
        input_field1 = {
            "where": {
                "_and": GraphQLInputField(GraphQLList(inputs[type_name])),
                "_or": GraphQLInputField(GraphQLList(inputs[type_name])),
                "_not": GraphQLInputField(inputs[type_name]),
            },
            "on_conflict": {
                "merge": GraphQLInputField(GraphQLNonNull(GraphQLBoolean)),
            },
        }

        if input_type in input_field1.keys():
            fields = input_field1[input_type]
        else:
            fields = get_empty_dict()
        """ per column population """
        for column in get_table(model).columns:
            graphql_type = get_graphql_type_from_column(column.type)
            column_type = GraphQLInputField(graphql_type)

            input_field2 = {
                "where":
                GraphQLInputField(
                    get_type_comparison_fields(
                        graphql_type, inputs,
                        get_field_name(graphql_type, "comparison"))),
                "order_by":
                GraphQLInputField(ORDER_BY_ENUM),
                "insert_input":
                column_type,
                "inc_input":
                column_type if isinstance(column.type,
                                          (Integer, Float)) else None,
                "set_input":
                column_type,
            }

            if input_type in input_field2.keys() and input_field2[input_type]:
                fields[column.name] = cast(GraphQLInputField,
                                           input_field2[input_type])
        """ relationship population """
        for name, relationship in get_relationships(model):
            input_field3 = {
                "where":
                GraphQLInputField(inputs[get_field_name(
                    relationship.mapper.entity, "where")]),
                "order_by":
                GraphQLInputField(inputs[get_field_name(
                    relationship.mapper.entity, "order_by")]),
            }

            if input_type in input_field3.keys():
                fields[name] = input_field3[input_type]

        return fields
Beispiel #4
0
def get_conflict_type(model: DeclarativeMeta, inputs: Inputs) -> GraphQLInputObjectType:
    type_name = get_field_name(model, "on_conflict")
    if type_name in inputs:
        return inputs[type_name]

    fields = {
        "constraint": GraphQLInputField(GraphQLNonNull(get_constraint_enum(model))),
        "update_columns": GraphQLInputField(GraphQLNonNull(GraphQLList(GraphQLNonNull(get_update_column_enums(model))))),
        "where": GraphQLInputField(get_input_type(model, inputs, "where")),
    }

    input_type = GraphQLInputObjectType(type_name, fields)
    inputs[type_name] = input_type
    return input_type
Beispiel #5
0
def get_base_comparison_fields(graphql_type: Union[GraphQLScalarType, GraphQLList]) -> Dict[str, GraphQLInputField]:
    return {
        "_eq": GraphQLInputField(graphql_type),
        "_neq": GraphQLInputField(graphql_type),
        "_in": GraphQLInputField(GraphQLList(GraphQLNonNull(graphql_type))),
        "_nin": GraphQLInputField(GraphQLList(GraphQLNonNull(graphql_type))),
        "_lt": GraphQLInputField(graphql_type),
        "_gt": GraphQLInputField(graphql_type),
        "_gte": GraphQLInputField(graphql_type),
        "_lte": GraphQLInputField(graphql_type),
        "_is_null": GraphQLInputField(GraphQLBoolean),
    }
Beispiel #6
0
    def get_fields() -> GraphQLInputFieldMap:
        fields = {
            "_and": GraphQLInputField(GraphQLList(inputs[type_name])),
            "_or": GraphQLInputField(GraphQLList(inputs[type_name])),
            "_not": GraphQLInputField(inputs[type_name]),
        }

        for column in get_table(model).columns:
            fields[column.name] = GraphQLInputField(
                get_comparison_input_type(column, inputs))

        for name, relationship in get_relationships(model):
            fields[name] = GraphQLInputField(inputs[get_model_where_input_name(
                relationship.mapper.entity)])

        return fields
Beispiel #7
0
 def supports_thunks_as_input_and_output_fields():
     some_mutation = mutation_with_client_mutation_id(
         "SomeMutation",
         {"inputData": GraphQLInputField(GraphQLInt)},
         {"result": GraphQLField(GraphQLInt)},
         dummy_resolve,
     )
     schema = wrap_in_schema({"someMutation": some_mutation})
     source = """
         mutation {
           someMutation(input: {inputData: 1234, clientMutationId: "abc"}) {
             result
             clientMutationId
           }
         }
         """
     assert graphql_sync(schema, source) == (
         {
             "someMutation": {
                 "result": 1234,
                 "clientMutationId": "abc",
             }
         },
         None,
     )
Beispiel #8
0
def make_model_fields_input_type(model: DeclarativeMeta,
                                 type_name: str) -> GraphQLInputObjectType:
    fields = {}
    for column in get_table(model).columns:
        fields[column.name] = GraphQLInputField(
            get_graphql_type_from_column(column.type))

    return GraphQLInputObjectType(type_name, fields)
Beispiel #9
0
 def compile_input_field(self, field: InputField) -> GraphQLInputField:
     assert isinstance(field, InputField)
     return GraphQLInputField(
         type_=self.get_graphql_type(field.type),
         default_value=field.default_value,
         description=field.description,
         out_name=field.out_name,
     )
Beispiel #10
0
 def graphql_type(self):
     type_ = self.type_()
     if self.many:
         type_ = GraphQLList(type_)
     if self.required:
         type_ = GraphQLNonNull(type_)
     return GraphQLInputField(type_=type_,
                              default_value=self.default_value,
                              description=self.description)
Beispiel #11
0
def get_pk_columns_input(model: DeclarativeMeta) -> GraphQLInputObjectType:
    type_name = get_model_pk_columns_input_type_name(model)
    primary_key = get_table(model).primary_key

    fields = {}
    for column in primary_key.columns:
        fields[column.name] = GraphQLInputField(
            GraphQLNonNull(get_graphql_type_from_column(column.type)))

    return GraphQLInputObjectType(type_name, fields)
Beispiel #12
0
    def from_input_field(self, field: FieldDefinition) -> GraphQLInputField:
        if field.default_value in [undefined, UNSET]:
            default_value = Undefined
        else:
            default_value = field.default_value

        field_type = self.get_graphql_type_field(field)
        field_type = cast(GraphQLInputType, field_type)

        return GraphQLInputField(type_=field_type,
                                 default_value=default_value,
                                 description=field.description)
Beispiel #13
0
    def create_fields_for_type(self, graphene_type, is_input_type=False):
        create_graphql_type = self.add_type

        fields = {}
        for name, field in graphene_type._meta.fields.items():
            if isinstance(field, Dynamic):
                field = get_field_as(field.get_type(self), _as=Field)
                if not field:
                    continue
            field_type = create_graphql_type(field.type)
            if is_input_type:
                _field = GraphQLInputField(
                    field_type,
                    default_value=field.default_value,
                    out_name=name,
                    description=field.description,
                )
            else:
                args = {}
                for arg_name, arg in field.args.items():
                    arg_type = create_graphql_type(arg.type)
                    processed_arg_name = arg.name or self.get_name(arg_name)
                    args[processed_arg_name] = GraphQLArgument(
                        arg_type,
                        out_name=arg_name,
                        description=arg.description,
                        default_value=Undefined
                        if isinstance(arg.type, NonNull)
                        else arg.default_value,
                    )
                _field = GraphQLField(
                    field_type,
                    args=args,
                    resolve=field.get_resolver(
                        self.get_resolver_for_type(
                            graphene_type, f"resolve_{name}", name, field.default_value
                        )
                    ),
                    subscribe=field.get_resolver(
                        self.get_resolver_for_type(
                            graphene_type,
                            f"subscribe_{name}",
                            name,
                            field.default_value,
                        )
                    ),
                    deprecation_reason=field.deprecation_reason,
                    description=field.description,
                )
            field_name = field.name or self.get_name(name)
            fields[field_name] = _field
        return fields
Beispiel #14
0
def get_inc_input_type(model: DeclarativeMeta,
                       inputs: Inputs) -> GraphQLInputObjectType:
    type_name = get_model_inc_input_type_name(model)
    if type_name in inputs:
        return inputs[type_name]

    fields = {}
    for column in get_table(model).columns:
        if isinstance(column.type, (Integer, Float)):
            fields[column.name] = GraphQLInputField(
                get_graphql_type_from_column(column.type))

    inputs[type_name] = GraphQLInputObjectType(type_name, fields)
    return inputs[type_name]
Beispiel #15
0
 def convert(self, type_map: t.Mapping[str, GraphQLType]) -> GraphQLInputField:
     if self.type_name in type_map:
         field_type = type_map[self.type_name]
     else:
         field_type = self.type_name.convert(type_map)
     return GraphQLInputField(
         t.cast(
             t.Union[
                 GraphQLScalarType,
                 GraphQLEnumType,
                 GraphQLInputObjectType,
                 GraphQLWrappingType,
             ],
             field_type,
         )
     )
Beispiel #16
0
    def input_fields(self, source: Type[Any]) -> GraphQLInputFieldMap:
        result: GraphQLInputFieldMap = {}

        for build_type in self.build_type(source):
            if build_type.metadata.get("readonly") is True:
                continue

            field_name = self.field_name(build_type.field)
            description = build_type.metadata.get("description", "")
            mapped_type = self.map_input(build_type.source)

            if is_required(build_type.field):
                mapped_type = GraphQLNonNull(mapped_type)
            result[field_name] = GraphQLInputField(mapped_type,
                                                   description=description)
        return result
Beispiel #17
0
    def from_input_field(self, field: StrawberryField) -> GraphQLInputField:
        field_type: GraphQLType

        if isinstance(field.type, StrawberryOptional):
            field_type = self.from_optional(field.type)
        else:
            field_type = self.from_non_optional(field.type)

        default_value: object

        if is_unset(field.default_value):
            default_value = Undefined
        else:
            default_value = field.default_value

        return GraphQLInputField(
            type_=field_type,
            default_value=default_value,
            description=field.description,
        )
Beispiel #18
0
 def construct_fields_for_type(self, map_, type_, is_input_type=False):
     fields = {}
     for name, field in type_._meta.fields.items():
         if isinstance(field, Dynamic):
             field = get_field_as(field.get_type(self), _as=Field)
             if not field:
                 continue
         map_ = self.type_map_reducer(map_, field.type)
         field_type = self.get_field_type(map_, field.type)
         if is_input_type:
             _field = GraphQLInputField(
                 field_type,
                 default_value=field.default_value,
                 out_name=name,
                 description=field.description,
             )
         else:
             args = {}
             for arg_name, arg in field.args.items():
                 map_ = self.type_map_reducer(map_, arg.type)
                 arg_type = self.get_field_type(map_, arg.type)
                 processed_arg_name = arg.name or self.get_name(arg_name)
                 args[processed_arg_name] = GraphQLArgument(
                     arg_type,
                     out_name=arg_name,
                     description=arg.description,
                     default_value=INVALID if isinstance(arg.type, NonNull)
                     else arg.default_value,
                 )
             _field = GraphQLField(
                 field_type,
                 args=args,
                 resolve=field.get_resolver(
                     self.get_resolver_for_type(type_, name,
                                                field.default_value)),
                 deprecation_reason=field.deprecation_reason,
                 description=field.description,
             )
         field_name = field.name or self.get_name(name)
         fields[field_name] = _field
     return fields
Beispiel #19
0
 def __init__(self, session):
     self.type_map = {
         "Test":
         GraphQLObjectType("Test",
                           GraphQLField(GraphQLList(GraphQLString))),
         "TestEmptyObject":
         GraphQLObjectType("TestEmptyObject", {}),
         "TestNestedObjects":
         GraphQLObjectType(
             "TestNestedObjects",
             {
                 "EnumField":
                 GraphQLField(
                     GraphQLEnumType("TestEnum", {
                         "RED": 0,
                         "GREEN": 1,
                         "BLUE": 2
                     })),
                 "InputField":
                 GraphQLInputField(GraphQLNonNull(GraphQLInt)),
                 "List":
                 GraphQLList(GraphQLString),
                 "InputObjectType":
                 GraphQLInputObjectType(
                     "TestInputObject",
                     GraphQLNonNull(
                         GraphQLUnionType("TestUnion",
                                          [GraphQLString, GraphQLID])),
                 ),
                 "ArgumentType":
                 GraphQLArgument(GraphQLBoolean),
                 "Float":
                 GraphQLFloat,
             },
         ),
     }
     self.context = session
Beispiel #20
0
def get_string_comparison_fields() -> Dict[str, GraphQLInputField]:
    return {"_like": GraphQLInputField(GraphQLString), "_nlike": GraphQLInputField(GraphQLString)}
    def __init__(self):
        super(ParamSchema, self).__init__(self)

        self.parameter_data = {}
        self.parameter_meta = {}

        # the trick with parameters is that they are either floats or ints
        # a custom type is required...
        GraphQLParamValue = GraphQLScalarType(
            name="ParamValue",
            description="",
            serialize=serialize_param_value,
            parse_value=coerce_param_value,
            parse_literal=parse_param_value_literal,
        )

        self.parameter_meta_input = GraphQLInputObjectType(
            "MetaInput",
            lambda: {
                "humanName": GraphQLInputField(GraphQLString),
                "humanGroup": GraphQLInputField(GraphQLString),
                "documentation": GraphQLInputField(GraphQLString),
                "group": GraphQLInputField(GraphQLString),
                "increment": GraphQLInputField(GraphQLParamValue),
                "min": GraphQLInputField(GraphQLParamValue),
                "max": GraphQLInputField(GraphQLParamValue),
                "decimal": GraphQLInputField(GraphQLParamValue),
                "rebootRequired": GraphQLInputField(GraphQLBoolean),
                "unitText": GraphQLInputField(GraphQLString),
                "units": GraphQLInputField(GraphQLString),
                "bitmask": GraphQLInputField(GraphQLString),
                "values": GraphQLInputField(GraphQLString),
                "type": GraphQLInputField(GraphQLString),
            },
        )
        self.parameter_meta_type = GraphQLObjectType(
            "Meta",
            lambda: {
                "humanName": GraphQLField(GraphQLString, description=""),
                "humanGroup": GraphQLField(GraphQLString, description=""),
                "documentation": GraphQLField(GraphQLString, description=""),
                "group": GraphQLField(GraphQLString, description=""),
                "increment": GraphQLField(GraphQLParamValue, description=""),
                "min": GraphQLField(GraphQLParamValue, description=""),
                "max": GraphQLField(GraphQLParamValue, description=""),
                "decimal": GraphQLField(GraphQLParamValue, description=""),
                "rebootRequired": GraphQLField(GraphQLBoolean, description=""),
                "unitText": GraphQLField(GraphQLString, description=""),
                "units": GraphQLField(GraphQLString, description=""),
                "bitmask": GraphQLField(GraphQLString, description=""),
                "values": GraphQLField(GraphQLString, description=""),
                "type": GraphQLField(GraphQLString, description=""),
            },
        )

        self.parameter_type = GraphQLObjectType(
            "Parameter",
            lambda: {
                "id":
                GraphQLField(GraphQLString,
                             description="The id of the parameter"),
                "value":
                GraphQLField(GraphQLParamValue,
                             description="The value of the parameter"),
                "meta":
                GraphQLField(self.parameter_meta_type),
            },
            description="Parameter item",
        )

        self.parameter_list_type = GraphQLObjectType(
            "ParameterList",
            lambda:
            {"parameters": GraphQLField(GraphQLList(self.parameter_type))},
        )

        self.parameter_input_type = GraphQLInputObjectType(
            "ParameterInput",
            {
                "id": GraphQLInputField(GraphQLNonNull(GraphQLString)),
                "value": GraphQLInputField(GraphQLNonNull(GraphQLParamValue)),
            },
        )

        self.q = {
            "Parameter":
            GraphQLField(
                self.parameter_type,
                args={
                    "id":
                    GraphQLArgument(
                        GraphQLNonNull(GraphQLString),
                        description="The id of the desired parameter.",
                    )
                },
                resolve=self.get_parameter,
            ),
            "ParameterList":
            GraphQLField(
                self.parameter_list_type,
                args={
                    "query":
                    GraphQLArgument(
                        GraphQLNonNull(GraphQLString),
                        description=
                        "The query used to match desired parameters. * can be used as a wildcard.",
                    )
                },
                resolve=self.get_parameter_list,
            ),
        }

        self.m = {
            "Parameter":
            GraphQLField(
                self.parameter_type,
                args={
                    "id": GraphQLArgument(GraphQLNonNull(GraphQLString)),
                    "value":
                    GraphQLArgument(GraphQLNonNull(GraphQLParamValue)),
                    "meta": GraphQLArgument(self.parameter_meta_input),
                },
                resolve=self.update_parameter,
            ),
            "ParameterList":
            GraphQLField(
                self.parameter_list_type,
                args={
                    "parameters":
                    GraphQLArgument(
                        GraphQLNonNull(GraphQLList(self.parameter_input_type)))
                },
                resolve=self.update_parameter_list,
            ),
        }

        self.s = {
            "Parameter":
            GraphQLField(self.parameter_type,
                         subscribe=self.sub_parameter,
                         resolve=None)
        }
Beispiel #22
0
from .names import (
    get_graphql_type_comparison_name,
    get_model_inc_input_type_name,
    get_model_insert_input_name,
    get_model_order_by_input_name,
    get_model_pk_columns_input_type_name,
    get_model_set_input_type_name,
    get_model_where_input_name,
)
from .types import Inputs

ORDER_BY_ENUM = GraphQLEnumType("order_by", {"desc": "desc", "asc": "asc"})
ON_CONFLICT_INPUT = GraphQLInputObjectType(
    "on_conflict_input",
    {
        "merge": GraphQLInputField(GraphQLNonNull(GraphQLBoolean)),
    },
)


def get_comparison_input_type(column: Column,
                              inputs: Inputs) -> GraphQLInputObjectType:
    graphql_type = get_graphql_type_from_column(column.type)
    type_name = get_graphql_type_comparison_name(graphql_type)

    if type_name in inputs:
        return inputs[type_name]

    fields = get_base_comparison_fields(graphql_type)

    if graphql_type == GraphQLString:
user_type = GraphQLObjectType(
    "UserType",
    {
        "id": GraphQLField(GraphQLNonNull(GraphQLID)),
        "firstName": GraphQLField(GraphQLNonNull(GraphQLString)),
        "lastName": GraphQLField(GraphQLNonNull(GraphQLString)),
        "tweets": GraphQLField(GraphQLInt),
        "verified": GraphQLField(GraphQLNonNull(GraphQLBoolean)),
    },
)

user_input_type = GraphQLInputObjectType(
    "UserInputType",
    {
        "firstName": GraphQLInputField(GraphQLNonNull(GraphQLString)),
        "lastName": GraphQLInputField(GraphQLNonNull(GraphQLString)),
        "tweets": GraphQLInputField(GraphQLInt),
        "verified": GraphQLInputField(GraphQLBoolean),
    },
)

subscription_user_type = GraphQLObjectType(
    "SubscriptionUserType",
    {
        "mutation": GraphQLField(mutation_type),
        "user": GraphQLField(user_type)
    },
)

Beispiel #24
0
    def create_fields_for_type(self, graphene_type, is_input_type=False):
        create_graphql_type = self.add_type

        fields = {}
        for name, field in graphene_type._meta.fields.items():
            if isinstance(field, Dynamic):
                field = get_field_as(field.get_type(self), _as=Field)
                if not field:
                    continue
            field_type = create_graphql_type(field.type)
            if is_input_type:
                _field = GraphQLInputField(
                    field_type,
                    default_value=field.default_value,
                    out_name=name,
                    description=field.description,
                )
            else:
                args = {}
                for arg_name, arg in field.args.items():
                    arg_type = create_graphql_type(arg.type)
                    processed_arg_name = arg.name or self.get_name(arg_name)
                    args[processed_arg_name] = GraphQLArgument(
                        arg_type,
                        out_name=arg_name,
                        description=arg.description,
                        default_value=arg.default_value,
                    )
                subscribe = field.wrap_subscribe(
                    self.get_function_for_type(graphene_type,
                                               f"subscribe_{name}", name,
                                               field.default_value))

                # If we are in a subscription, we use (by default) an
                # identity-based resolver for the root, rather than the
                # default resolver for objects/dicts.
                if subscribe:
                    field_default_resolver = identity_resolve
                elif issubclass(graphene_type, ObjectType):
                    default_resolver = (graphene_type._meta.default_resolver
                                        or get_default_resolver())
                    field_default_resolver = partial(default_resolver, name,
                                                     field.default_value)
                else:
                    field_default_resolver = None

                resolve = field.wrap_resolve(
                    self.get_function_for_type(
                        graphene_type, f"resolve_{name}", name,
                        field.default_value) or field_default_resolver)

                _field = GraphQLField(
                    field_type,
                    args=args,
                    resolve=resolve,
                    subscribe=subscribe,
                    deprecation_reason=field.deprecation_reason,
                    description=field.description,
                )
            field_name = field.name or self.get_name(name)
            fields[field_name] = _field
        return fields
Beispiel #25
0
        "episode": GraphQLField(episode_enum, description="The movie"),
        "stars": GraphQLField(
            GraphQLNonNull(GraphQLInt),
            description="The number of stars this review gave, 1-5",
        ),
        "commentary": GraphQLField(
            GraphQLString, description="Comment about the movie"
        ),
    },
    description="Represents a review for a movie",
)

review_input_type = GraphQLInputObjectType(
    "ReviewInput",
    lambda: {
        "stars": GraphQLInputField(GraphQLInt, description="0-5 stars"),
        "commentary": GraphQLInputField(
            GraphQLString, description="Comment about the movie, optional"
        ),
    },
    description="The input object sent when someone is creating a new review",
)

query_type = GraphQLObjectType(
    "Query",
    lambda: {
        "hero": GraphQLField(
            character_interface,
            args={
                "episode": GraphQLArgument(
                    episode_enum,
    def __init__(self):
        super().__init__()
        # create a mission database dir
        self.mission_database_dir = Path(options.datadir).joinpath("missions")
        # make the path if it does not exist
        mkdirs(self.mission_database_dir)

        self.mission_data = {}
        self.mission_meta = {
            "meta": {
                "total": 0,
                "updateTime": int(time.time())
            }
        }

        self.mission_type = GraphQLObjectType(
            "Mission",
            lambda: {
                "seq":
                GraphQLField(GraphQLInt,
                             description=
                             "The sequence number of the mission item."),
                "isCurrent":
                GraphQLField(
                    GraphQLBoolean,
                    description=
                    "True if this mission item is the active target",
                ),
                "autocontinue":
                GraphQLField(
                    GraphQLBoolean,
                    description="Continue mission after this mission item",
                ),
                "frame":
                GraphQLField(GraphQLInt, description=""),
                "command":
                GraphQLField(GraphQLInt, description=""),
                "param1":
                GraphQLField(GraphQLFloat, description=""),
                "param2":
                GraphQLField(GraphQLFloat, description=""),
                "param3":
                GraphQLField(GraphQLFloat, description=""),
                "param4":
                GraphQLField(GraphQLFloat, description=""),
                "latitude":
                GraphQLField(GraphQLFloat, description=""),
                "longitude":
                GraphQLField(GraphQLFloat, description=""),
                "altitude":
                GraphQLField(GraphQLFloat, description=""),
            },
            description="Mission item",
        )

        self.mission_input_type = GraphQLInputObjectType(
            "MissionInput",
            {
                "seq":
                GraphQLInputField(
                    GraphQLNonNull(GraphQLInt),
                    description="The sequence number of the mission item.",
                ),
                "isCurrent":
                GraphQLInputField(
                    GraphQLNonNull(GraphQLBoolean),
                    description=
                    "True if this mission item is the active target",
                ),
                "autocontinue":
                GraphQLInputField(
                    GraphQLNonNull(GraphQLBoolean),
                    description="Continue mission after this mission item",
                ),
                "frame":
                GraphQLInputField(GraphQLNonNull(GraphQLInt), description=""),
                "command":
                GraphQLInputField(GraphQLNonNull(GraphQLInt), description=""),
                "param1":
                GraphQLInputField(GraphQLNonNull(GraphQLFloat),
                                  description=""),
                "param2":
                GraphQLInputField(GraphQLNonNull(GraphQLFloat),
                                  description=""),
                "param3":
                GraphQLInputField(GraphQLNonNull(GraphQLFloat),
                                  description=""),
                "param4":
                GraphQLInputField(GraphQLNonNull(GraphQLFloat),
                                  description=""),
                "latitude":
                GraphQLInputField(GraphQLNonNull(GraphQLFloat),
                                  description=""),
                "longitude":
                GraphQLInputField(GraphQLNonNull(GraphQLFloat),
                                  description=""),
                "altitude":
                GraphQLInputField(GraphQLNonNull(GraphQLFloat),
                                  description=""),
            },
        )

        self.mission_list_type = GraphQLObjectType(
            "MissionList",
            lambda: {
                "id":
                GraphQLField(GraphQLString,
                             description="The id of the mission."),
                "mission":
                GraphQLField(GraphQLList(self.mission_type)),
                "total":
                GraphQLField(GraphQLInt,
                             description="Total number of mission items"),
                "updateTime":
                GraphQLField(GraphQLInt, description=""),
            },
            description="Mission",
        )

        self.mission_database_type = GraphQLObjectType(
            "MissionDatabase",
            lambda: {
                "id":
                GraphQLField(
                    GraphQLString,
                    description=
                    "The id of the database use the wildcard '*' to access all avalable databases.",
                ),
                "missions":
                GraphQLField(GraphQLList(self.mission_list_type)),
                "total":
                GraphQLField(GraphQLInt,
                             description="Total number of missions in database"
                             ),
                "updateTime":
                GraphQLField(GraphQLInt, description=""),
            },
            description="Collection of missions",
        )

        self.q = {
            "Mission":
            GraphQLField(
                self.mission_type,
                args={
                    "seq":
                    GraphQLArgument(
                        GraphQLNonNull(GraphQLInt),
                        description=
                        "The sequence number of desired mission item",
                    )
                },
                resolve=self.get_mission,
            ),
            "MissionList":
            GraphQLField(
                self.mission_list_type,
                args={
                    "id":
                    GraphQLArgument(
                        GraphQLNonNull(GraphQLString),
                        description="The id of the desired mission",
                    )
                },
                resolve=self.get_mission_list,
            ),
            "MissionDatabase":
            GraphQLField(
                self.mission_database_type,
                args={
                    "id":
                    GraphQLArgument(
                        GraphQLNonNull(GraphQLString),
                        description="The id of the database",
                    )
                },
                resolve=self.get_mission_database,
            ),
        }

        self.m = {
            "Mission":
            GraphQLField(
                self.mission_type,
                args=self.get_mutation_args(self.mission_type),
                resolve=self.update_mission,
            ),
            "MissionList":
            GraphQLField(
                self.mission_list_type,
                args={
                    "id":
                    GraphQLArgument(GraphQLNonNull(GraphQLString)),
                    "mission":
                    GraphQLArgument(
                        GraphQLNonNull(GraphQLList(self.mission_input_type))),
                },
                resolve=self.update_mission_list,
            ),
        }

        self.s = {
            "Mission":
            GraphQLField(self.mission_type,
                         subscribe=self.sub_mission,
                         resolve=None),
            "MissionList":
            GraphQLField(self.mission_list_type,
                         subscribe=self.sub_mission_list,
                         resolve=None),
            "MissionDatabase":
            GraphQLField(
                self.mission_database_type,
                subscribe=self.sub_mission_database,
                resolve=None,
            ),
        }
Beispiel #27
0
from graphql import (
    GraphQLArgument,
    GraphQLField,
    GraphQLInputField,
    GraphQLInputObjectType,
    GraphQLInt,
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLString,
)

nestedInput = GraphQLInputObjectType(
    "Nested",
    description="The input object that has a field pointing to itself",
    fields={"foo": GraphQLInputField(GraphQLInt, description="foo")},
)

nestedInput.fields["child"] = GraphQLInputField(nestedInput, description="child")

queryType = GraphQLObjectType(
    "Query",
    fields=lambda: {
        "echo": GraphQLField(
            args={"nested": GraphQLArgument(type_=nestedInput)},
            resolve=lambda *args, **kwargs: json.dumps(kwargs["nested"]),
            type_=GraphQLString,
        ),
    },
)
Beispiel #28
0
from graphql import GraphQLObjectType, GraphQLField, GraphQLString, GraphQLInt, GraphQLList, GraphQLScalarType, \
    GraphQLNonNull, GraphQLInputObjectType, GraphQLInputField

from .query.service import ServiceType, resolve_services

PortsType = GraphQLObjectType(name="Ports",
                              fields={
                                  "internal":
                                  GraphQLField(GraphQLString,
                                               resolve=lambda x, i: x[1]),
                                  "external":
                                  GraphQLField(GraphQLString,
                                               resolve=lambda x, i: x[0])
                              })

VarsType = GraphQLScalarType(name="Vars", serialize=lambda x: x)

VarInputType = GraphQLInputObjectType(name="StackVar",
                                      fields={
                                          "name":
                                          GraphQLInputField(
                                              GraphQLNonNull(GraphQLString)),
                                          "value":
                                          GraphQLInputField(GraphQLString),
                                      })
    def __init__(self, shipId, factionId, clientMutationId=None):
        self.shipId = shipId
        self.factionId = factionId
        self.clientMutationId = clientMutationId


# noinspection PyPep8Naming
def mutate_and_get_payload(_info, shipName, factionId, **_input):
    new_ship = create_ship(shipName, factionId)
    return IntroduceShipMutation(shipId=new_ship.id, factionId=factionId)


shipMutation = mutation_with_client_mutation_id(
    "IntroduceShip",
    input_fields={
        "shipName": GraphQLInputField(GraphQLNonNull(GraphQLString)),
        "factionId": GraphQLInputField(GraphQLNonNull(GraphQLID)),
    },
    output_fields={
        "ship": GraphQLField(
            shipType, resolve=lambda payload, _info: get_ship(payload.shipId)
        ),
        "faction": GraphQLField(
            factionType, resolve=lambda payload, _info: get_faction(payload.factionId)
        ),
    },
    mutate_and_get_payload=mutate_and_get_payload,
)

# This is the type that will be the root of our mutations, and the
# entry point into performing writes in our schema.
def get_type_comparison_fields(graphql_type: Union[GraphQLScalarType,
                                                   GraphQLList],
                               inputs: Inputs,
                               type_name: str) -> GraphQLInputObjectType:
    if type_name in inputs:
        return inputs[type_name]

    fields = {
        "_eq": GraphQLInputField(graphql_type),
        "_neq": GraphQLInputField(graphql_type),
        "_in": GraphQLInputField(GraphQLList(GraphQLNonNull(graphql_type))),
        "_nin": GraphQLInputField(GraphQLList(GraphQLNonNull(graphql_type))),
        "_lt": GraphQLInputField(graphql_type),
        "_gt": GraphQLInputField(graphql_type),
        "_gte": GraphQLInputField(graphql_type),
        "_lte": GraphQLInputField(graphql_type),
        "_is_null": GraphQLInputField(GraphQLBoolean),
    }

    fields_string = {
        "_like": GraphQLInputField(GraphQLString),
        "_nlike": GraphQLInputField(GraphQLString),
    }

    if graphql_type == GraphQLString:
        fields.update(fields_string)

    inputs[type_name] = GraphQLInputObjectType(type_name, fields)
    return inputs[type_name]