Toptotor
Toptotor

Reputation: 31

GraphQL Input Type as Query parameter

I'm using Apollo Angular and hosting a GraphQL powered server. The request I'm trying to achieve is simple. I need to pass an object as a query parameter of my request. This is my request:

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)) : [])
    );

The query is defined as:

query SpotList($boundaries: Boundaries) {
      spots (boundaries: $boundaries) {
          ...

My schema on the server is defined as:

type Query {
    spot(id: String!): Spot
    spots(boundaries: Boundaries): [Spot!]! 
}

input Boundaries {
    east: Float
    north: Float
    south: Float
    west: Float
}

And when my resolver function

@Query()
    async spots(boundaries) {
      console.log(boundaries) //return undefined

I am wondering if input is allowed as a parameter in a query and if not, what kind of schema or practice should I use.

If possible, I don't want my query to have each arguments explicitly in code.

Thank you !

Edit: Solved, behaviour in resolver function from Nest Query decorator which has a (object, arguments, context, info) pattern to its resolver function.

Upvotes: 2

Views: 1705

Answers (1)

Toptotor
Toptotor

Reputation: 31

As I forgot to mention in my question, I'm using Nest server-side and the Decorator @Query introduced by @nestjs/graphql gives a special signature to resolver functions.

Upvotes: 1

Related Questions