def to_graphql_argument(param): graphql_type = to_graphql_type(param.type) if param.has_default and isinstance(graphql_type, graphql.GraphQLNonNull): graphql_type = graphql_type.of_type return graphql.GraphQLArgument(type=graphql_type)
def arg_thunk( arg_thunk=self.input_builder.visit(param.type), default=default, out_name=param.name, ) -> graphql.GraphQLArgument: arg_type = exec_thunk(arg_thunk) if (not isinstance(arg_type, graphql.GraphQLNonNull) and default is None): default = graphql.Undefined return graphql.GraphQLArgument(arg_type, default, out_name=out_name)
def arguments(self, definition: Optional[Dict]) -> Optional[GraphQLArgumentMap]: result: GraphQLArgumentMap = dict() if not definition or not isinstance(definition, (list, tuple)): return None for arg in definition: if not isinstance(arg, (Argument, ArgumentList)): continue mapped_type = self.map_input(arg.type) if arg.required: mapped_type = graphql.GraphQLNonNull(mapped_type) arg_name = snake_to_camel(arg.name, False) if self.camelcase else arg.name result[arg_name] = graphql.GraphQLArgument( mapped_type, description=arg.description ) return result
def arg_thunk( arg_factory=arg_factory, default=default, description=description) -> graphql.GraphQLArgument: return graphql.GraphQLArgument(arg_factory.type, default, description)
Person = g.GraphQLObjectType( "Person", lambda: { "name": g.GraphQLField(g.GraphQLNonNull(g.GraphQLString)), "age": g.GraphQLField(g.GraphQLNonNull(g.GraphQLInt)), "nickname": g.GraphQLField(g.GraphQLString), }, ) query_type = g.GraphQLObjectType( "Query", lambda: { "person": g.GraphQLField( Person, args={"name": g.GraphQLArgument(g.GraphQLString)}, ) }, ) Person = t.Dict[str, t.Any] class Root: def __init__(self, data): self.data = data def person(self, info, *, name: t.Optional[str] = None) -> Person: if name is None: return None for row in self.data["people"]:
def arg_from_parameter(mapper, param): default_value = param.default if default_value is Signature.empty: default_value = param.default return graphql.GraphQLArgument(mapper.map(param.annotation), default_value=default_value)
def resolve_counter(root: float, info: graphql.GraphQLResolveInfo, ceil: int): # pylint: disable=W0613, unused-argument return f"{root} :: {info.operation.name.value}" schema = graphql.GraphQLSchema( query=graphql.GraphQLObjectType( name="RootQueryType", fields={ "name": graphql.GraphQLField( graphql.GraphQLString, resolve=resolve_name, args={ "title": graphql.GraphQLArgument( graphql.GraphQLNonNull(graphql.GraphQLString) ) }, ) }, ), subscription=graphql.GraphQLObjectType( name="RootSubscriptionType", fields={ "counter": graphql.GraphQLField( graphql.GraphQLString, resolve=resolve_counter, subscribe=subscribe_counter, args={ "ceil": graphql.GraphQLArgument( graphql.GraphQLNonNull(graphql.GraphQLInt)
def to_arg(t, ctx: TransformContext): params = ("description", "default_value", "out_name") return graphql.GraphQLArgument(t, **{k: ctx[k] for k in params if k in ctx})
Person = g.GraphQLObjectType( "Person", lambda: { "name": g.GraphQLField(g.GraphQLNonNull(g.GraphQLString)), "age": g.GraphQLField(g.GraphQLNonNull(g.GraphQLInt)), "nickname": g.GraphQLField(g.GraphQLString), }, ) query_type = g.GraphQLObjectType( "Query", lambda: { "people": g.GraphQLField( g.GraphQLList(Person), args={"order_by": g.GraphQLArgument(person_order_input_enum)}, ) }, ) class Root: def __init__(self, data): self.data = data def people(self, info, *, order_by): if order_by == "name_DESC": return sorted(self.data["people"], key=lambda d: d["name"], reverse=True) elif order_by == "name_ASC":