def make_update_args(model: DeclarativeMeta, inputs: Inputs) -> GraphQLArgumentMap: return { "_inc": GraphQLArgument(get_inc_input_type(model, inputs)), "_set": GraphQLArgument(get_set_input_type(model, inputs)), "where": GraphQLArgument(get_where_input_type(model, inputs)), }
def test_field_args_is_union_of_original_field_args(self): self._assert_merge( [ GraphQLObjectType("Object", fields={ "id": GraphQLField( type=GraphQLInt, args={"id": GraphQLArgument(type=GraphQLInt)}, ), }), GraphQLObjectType("Object", fields={ "id": GraphQLField( type=GraphQLInt, args={ "id": GraphQLArgument(type=GraphQLInt), "name": GraphQLArgument(type=GraphQLString), }, ), }), ], is_object_type(fields=has_entries({ "id": is_field(args=has_entries({ "id": is_arg(type=is_int), "name": is_arg(type=is_string), })), })), )
def make_mutation_args(model: DeclarativeMeta, inputs: Inputs, mutation_type: str) -> GraphQLArgumentMap: args = { "insert": { "objects": GraphQLArgument(GraphQLNonNull(GraphQLList(GraphQLNonNull(get_input_type(model, inputs, "insert_input"))))), "on_conflict": GraphQLArgument(get_input_type(model, inputs, "on_conflict")), }, "insert_one": { "object": GraphQLArgument(get_input_type(model, inputs, "insert_input")), "on_conflict": GraphQLArgument(get_input_type(model, inputs, "on_conflict")), }, "update": { **({"_inc": GraphQLArgument(get_input_type(model, inputs, "inc_input"))} if has_int(model) else {}), "_set": GraphQLArgument(get_input_type(model, inputs, "set_input")), "where": GraphQLArgument(get_input_type(model, inputs, "where")), }, "update_by_pk": { **({"_inc": GraphQLArgument(get_input_type(model, inputs, "inc_input"))} if has_int(model) else {}), "_set": GraphQLArgument(get_input_type(model, inputs, "set_input")), **make_pk_args(model), }, "delete": { "where": GraphQLArgument(get_input_type(model, inputs, "where")), }, "delete_by_pk": { **make_pk_args(model), }, } return args[mutation_type]
def make_query_args(model: DeclarativeMeta, inputs: Inputs) -> GraphQLArgumentMap: args = { "order": GraphQLArgument(GraphQLList(GraphQLNonNull(get_input_type(model, inputs, "order_by")))), "where": GraphQLArgument(get_input_type(model, inputs, "where")), **PAGINATION_ARGS, } return args
def make_update_by_pk_args(model: DeclarativeMeta, inputs: Inputs) -> GraphQLArgumentMap: return { "_inc": GraphQLArgument(get_inc_input_type(model, inputs)), "_set": GraphQLArgument(get_set_input_type(model, inputs)), "pk_columns": GraphQLArgument(GraphQLNonNull(get_pk_columns_input(model))), }
def get_args(): return { "width": GraphQLArgument( GraphQLNonNull(GraphQLInt), description="Value to returned str lenght" ), "fillchar": GraphQLArgument( GraphQLString, description="Value to fill the returned str" ), }
def make_insert_args(model: DeclarativeMeta, inputs: Inputs) -> GraphQLArgumentMap: return { "objects": GraphQLArgument( GraphQLNonNull( GraphQLList( GraphQLNonNull(get_insert_input_type(model, inputs))))), "on_conflict": GraphQLArgument(ON_CONFLICT_INPUT), }
def node_definitions( fetch_by_id: Callable[[str, GraphQLResolveInfo], Any], type_resolver: Optional[GraphQLTypeResolver] = None, ) -> GraphQLNodeDefinitions: """ Given a function to map from an ID to an underlying object, and a function to map from an underlying object to the concrete GraphQLObjectType it corresponds to, constructs a `Node` interface that objects can implement, and a field object to be used as a `node` root field. If the type_resolver is omitted, object resolution on the interface will be handled with the `is_type_of` method on object types, as with any GraphQL interface without a provided `resolve_type` method. """ node_interface = GraphQLInterfaceType( "Node", description="An object with an ID", fields=lambda: { "id": GraphQLField(GraphQLNonNull(GraphQLID), description="The id of the object.") }, resolve_type=type_resolver, ) # noinspection PyShadowingBuiltins node_field = GraphQLField( node_interface, description="Fetches an object given its ID", args={ "id": GraphQLArgument(GraphQLNonNull(GraphQLID), description="The ID of an object") }, resolve=lambda _obj, info, id: fetch_by_id(id, info), ) nodes_field = GraphQLField( GraphQLNonNull(GraphQLList(node_interface)), description="Fetches objects given their IDs", args={ "ids": GraphQLArgument( GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLID))), description="The IDs of objects", ) }, resolve=lambda _obj, info, ids: [fetch_by_id(id_, info) for id_ in ids], ) return GraphQLNodeDefinitions(node_interface, node_field, nodes_field)
def create_type(): return GraphQLObjectType( name='Report', fields=lambda: { 'id': GraphQLField( type=req(GraphQLID), resolver=lambda obj, args, *_: obj.obj_id(*args), ), 'provider': GraphQLField(type=GraphQLProvider.type()), 'reportRecordNumber': GraphQLField( type=req(GraphQLInt), resolver=lambda obj, args, *_: obj.report_record_number( *args), ), 'providerNumber': GraphQLField( type=req(GraphQLInt), resolver=lambda obj, args, *_: obj.provider_number(*args), ), 'fiscalIntermediaryNumber': GraphQLField( type=req(GraphQLInt), resolver=lambda obj, args, *_: obj. fiscal_intermediary_number(*args), ), 'processDate': GraphQLField( type=req(GraphQLDate.type()), resolver=lambda obj, args, *_: obj.process_date(*args), ), 'medicareUtilizationLevel': GraphQLField( type=req(GraphQLMedicareUtilizationLevel.type()), resolver=lambda obj, args, *_: obj. medicare_utilization_level(*args).name if obj.medicare_utilization_level(*args) else None, ), 'worksheetInstances': GraphQLField( type=req(list_of(req(GraphQLWorksheetInstance.type()))), args={ 'after': GraphQLArgument(type=GraphQLID), 'first': GraphQLArgument(type=GraphQLInt), }, resolver=lambda obj, args, *_: obj.worksheet_instances( *args), ), }, )
def get_args(): return { 'width': GraphQLArgument( type=GraphQLNonNull(GraphQLInt), description='Value to returned str lenght', ), 'fillchar': GraphQLArgument( type=GraphQLString, description='Value to fill the returned str', ), }
def fields(): return { "books": many(book_join_type, lambda *_: all_books), "book": single_or_null(book_join_type, book_query, args={"id": GraphQLArgument(type=GraphQLInt)}), "author": single_or_null(author_join_type, author_query, args={"id": GraphQLArgument(type=GraphQLInt)}), }
def make_args(model: DeclarativeMeta, inputs: Inputs) -> GraphQLArgumentMap: args = { "order": GraphQLArgument( GraphQLList(GraphQLNonNull(get_order_input_type(model, inputs)))), "where": GraphQLArgument(get_where_input_type(model, inputs)), } for name, field in PAGINATION_ARGS.items(): args[name] = GraphQLArgument(field) return args
def fields(): return { "books": many(book_join_type, lambda *_: Query([]).select_from(Book)), "book": single_or_null(book_join_type, book_query, args={"id": GraphQLArgument(type=GraphQLInt)}), "author": single_or_null(author_join_type, author_query, args={"id": GraphQLArgument(type=GraphQLInt)}), }
def get_args(): return { "old": GraphQLArgument( type=GraphQLNonNull(GraphQLString), description="Value of old character to replace", ), "new": GraphQLArgument( type=GraphQLNonNull(GraphQLString), description="Value of new character to replace", ), "count": GraphQLArgument( type=GraphQLInt, description="Value to returned str lenght" ), }
def test_when_field_has_arg_of_subtype_then_is_not_subtype(self): assert not is_subtype( GraphQLObjectType("Object", { "value": GraphQLField( type=GraphQLNonNull(GraphQLInt), args={"id": GraphQLArgument(type=GraphQLNonNull(GraphQLInt))}, ) }), GraphQLObjectType("Object", { "value": GraphQLField( type=GraphQLNonNull(GraphQLInt), args={"id": GraphQLArgument(type=GraphQLInt)}, ) }), )
def convert( self, type_map: t.Mapping[ str, t.Union[ GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLWrappingType, ], ], ) -> GraphQLArgument: if self.type_ in type_map: converted_type = type_map[self.type_] else: converted_type = self.type_.convert(type_map) return GraphQLArgument( t.cast( t.Union[ GraphQLScalarType, GraphQLEnumType, GraphQLInputObjectType, GraphQLWrappingType, ], converted_type, ), self.default_value, )
def build_federated_schema(query=None, mutation=None, types=None): """Create GraphQL schema that supports Apollo Federation.""" schema = graphene.Schema( query=query, mutation=mutation, types=list(types) + [_Any, _Entity, _Service], ) entity_type = schema.get_type("_Entity") entity_type.resolve_type = create_entity_type_resolver(schema) query_type = schema.get_type("Query") query_type.fields["_entities"] = GraphQLField( GraphQLList(entity_type), args={ "representations": GraphQLArgument(GraphQLList(schema.get_type("_Any")), ), }, resolver=resolve_entities, ) query_type.fields["_service"] = GraphQLField( schema.get_type("_Service"), resolver=create_service_sdl_resolver(schema), ) return schema
def plural_identifying_root_field( arg_name: str, input_type: GraphQLInputType, output_type: GraphQLOutputType, resolve_single_input: Callable[[GraphQLResolveInfo, str], Any], description: Optional[str] = None, ) -> GraphQLField: def resolve(_obj: Any, info: GraphQLResolveInfo, **args: Any) -> List: inputs = args[arg_name] return [resolve_single_input(info, input_) for input_ in inputs] return GraphQLField( GraphQLList(output_type), description=description, args={ arg_name: GraphQLArgument( GraphQLNonNull( GraphQLList( GraphQLNonNull( get_nullable_type(input_type)) # type: ignore ))) }, resolve=resolve, )
def get_args(): return { "op": GraphQLArgument( type=GraphQLString, description='Action to perform: "encode" or "decode"', ) }
def get_args(): return { "chars": GraphQLArgument( type=GraphQLString, description="Value to specify the set of characters to be removed", ) }
def get_args(): return { 'format': GraphQLArgument( type=GraphQLString, description='A format given by dateutil module', ), }
def fields(): return { "books": many( book_join_type, books_query, args={"genre": GraphQLArgument(type=GraphQLString)} ) }
def get_args(): return { "format": GraphQLArgument( type_=GraphQLNonNull(GraphQLString), description="Format Date or DateTime value in this format.", ) }
def get_args(): return { 'symbol': GraphQLArgument( type=GraphQLString, description='Currency symbol (default: $)', ), }
def _common_subarg(left, right): if left is None: return right elif right is None: return left else: type_ = _common_supertype(left.type, right.type) return GraphQLArgument(type=type_)
def get_args(): return { 'as': GraphQLArgument( type=GraphQLNonNull(GraphQLString), description='Value to default to', ), }
def test_batches_correctly(executor): Business = GraphQLObjectType( 'Business', lambda: { 'id': GraphQLField(GraphQLID, resolver=lambda root, info, **args: root), }) Query = GraphQLObjectType( 'Query', lambda: { 'getBusiness': GraphQLField(Business, args={ 'id': GraphQLArgument(GraphQLNonNull(GraphQLID)), }, resolver=lambda root, info, **args: info.context. business_data_loader.load(args.get('id'))), }) schema = GraphQLSchema(query=Query) doc = ''' { business1: getBusiness(id: "1") { id } business2: getBusiness(id: "2") { id } } ''' doc_ast = parse(doc) load_calls = [] class BusinessDataLoader(DataLoader): def batch_load_fn(self, keys): load_calls.append(keys) return Promise.resolve(keys) class Context(object): business_data_loader = BusinessDataLoader() result = execute(schema, doc_ast, None, context_value=Context(), executor=executor) assert not result.errors assert result.data == { 'business1': { 'id': '1' }, 'business2': { 'id': '2' }, } assert load_calls == [['1', '2']]
def __init__(self): super().__init__() self.mode_data = {} mavros.set_namespace("mavros") # register mode control self.mode_control = rospy.ServiceProxy(mavros.get_topic("set_mode"), SetMode) self.mode_control_type = GraphQLObjectType( "Mode", lambda: { "uuid": GraphQLField(GraphQLString, description="The uuid of the vehicle"), "modeString": GraphQLField(GraphQLString, description=""), "baseMode": GraphQLField(GraphQLInt, description=""), "customMode": GraphQLField(GraphQLInt, description=""), "updateTime": GraphQLField(GraphQLInt, description=""), "returncode": GraphQLField(GraphQLInt, description=""), }, description="Mode control", ) self.q = { "Mode": GraphQLField( self.mode_control_type, args={ "uuid": GraphQLArgument( GraphQLNonNull(GraphQLString), description="The uuid of the target vehicle", ) }, resolve=self.get_mode, ) } self.m = { "Mode": GraphQLField( self.mode_control_type, args=self.get_mutation_args(self.mode_control_type), resolve=self.update_mode, ) } self.s = { "Mode": GraphQLField(self.mode_control_type, subscribe=self.sub_mode, resolve=None) }
def _get_insert_args(self, typename): return { 'data': GraphQLArgument( GraphQLNonNull( GraphQLList( GraphQLNonNull( self._gql_inobjtypes[f'Insert{typename}'])))), }
def test_batches_correctly(executor): # type: (SyncExecutor) -> None Business = GraphQLObjectType( "Business", lambda: { "id": GraphQLField(GraphQLID, resolver=lambda root, info, **args: root) }, ) Query = GraphQLObjectType( "Query", lambda: { "getBusiness": GraphQLField( Business, args={"id": GraphQLArgument(GraphQLNonNull(GraphQLID))}, resolver=lambda root, info, **args: info.context. business_data_loader.load(args.get("id")), ) }, ) schema = GraphQLSchema(query=Query) doc = """ { business1: getBusiness(id: "1") { id } business2: getBusiness(id: "2") { id } } """ doc_ast = parse(doc) load_calls = [] class BusinessDataLoader(DataLoader): def batch_load_fn(self, keys): # type: (List[str]) -> Promise load_calls.append(keys) return Promise.resolve(keys) class Context(object): business_data_loader = BusinessDataLoader() result = execute(schema, doc_ast, None, context_value=Context(), executor=executor) assert not result.errors assert result.data == {"business1": {"id": "1"}, "business2": {"id": "2"}} assert load_calls == [["1", "2"]]