GraphQL 输入类型作为查询参数
GraphQL Input Type as Query parameter
我正在使用 Apollo Angular 并托管一个由 GraphQL 驱动的服务器。
我要实现的要求很简单。我需要传递一个对象作为请求的查询参数。这是我的要求:
return this.apollo
.watchQuery({
query: this.ListQuery,
variables: {
boundaries: {
east: boundaries.east,
west: boundaries.west,
north: boundaries.north,
south: boundaries.south,
}
},
fetchPolicy: 'no-cache',
})
.valueChanges.pipe(
map(({data}: any) => data.spots ? data.spots.map((spot) => new Spot(spot)) : [])
);
查询定义为:
query SpotList($boundaries: Boundaries) {
spots (boundaries: $boundaries) {
...
我在服务器上的架构定义为:
type Query {
spot(id: String!): Spot
spots(boundaries: Boundaries): [Spot!]!
}
input Boundaries {
east: Float
north: Float
south: Float
west: Float
}
并且当我的解析器函数
@Query()
async spots(boundaries) {
console.log(boundaries) //return undefined
我想知道是否允许输入作为查询中的参数,如果不允许,我应该使用哪种模式或做法。
如果可能的话,我不希望我的 query
在代码中明确地包含每个参数。
谢谢!
Edit: Solved, behaviour in resolver function from Nest Query decorator which has a (object, arguments, context, info) pattern to its resolver function.
正如我在问题中忘记提及的那样,我正在使用 Nest 服务器端和 @nestjs/graphql 引入的 Decorator @Query 为解析器函数提供特殊签名。
我正在使用 Apollo Angular 并托管一个由 GraphQL 驱动的服务器。 我要实现的要求很简单。我需要传递一个对象作为请求的查询参数。这是我的要求:
return this.apollo
.watchQuery({
query: this.ListQuery,
variables: {
boundaries: {
east: boundaries.east,
west: boundaries.west,
north: boundaries.north,
south: boundaries.south,
}
},
fetchPolicy: 'no-cache',
})
.valueChanges.pipe(
map(({data}: any) => data.spots ? data.spots.map((spot) => new Spot(spot)) : [])
);
查询定义为:
query SpotList($boundaries: Boundaries) {
spots (boundaries: $boundaries) {
...
我在服务器上的架构定义为:
type Query {
spot(id: String!): Spot
spots(boundaries: Boundaries): [Spot!]!
}
input Boundaries {
east: Float
north: Float
south: Float
west: Float
}
并且当我的解析器函数
@Query()
async spots(boundaries) {
console.log(boundaries) //return undefined
我想知道是否允许输入作为查询中的参数,如果不允许,我应该使用哪种模式或做法。
如果可能的话,我不希望我的 query
在代码中明确地包含每个参数。
谢谢!
Edit: Solved, behaviour in resolver function from Nest Query decorator which has a (object, arguments, context, info) pattern to its resolver function.
正如我在问题中忘记提及的那样,我正在使用 Nest 服务器端和 @nestjs/graphql 引入的 Decorator @Query 为解析器函数提供特殊签名。