Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apollo graphql pass arguments to resolve function

I have a schema with associated categories and markers

type Category {
    id: Int
    name: String
    parent: Category
    children: [Category]
    markers: [Marker]
}

type Marker {
    id: Int
    name: String
    categories: [Category]
}

type Query {
  category(limit: Int, offset: Int): [Category]
  marker(limit: Int, offset: Int): [Marker]
}

schema {
  query: Query
}

I can now write a query like this:

query{
  category(limit: 3){
    name
    markers{
      name

    }
  }
}

How can I define possible arguments for the markers as well? -->

  query{
      category(limit: 3){
        name
        markers(limit: 3){
          name

        }
      }
    }

so I can use the argument in the resolvers -->

        export const resolvers = {
            Query: {
                async category(root, args, context) {
                    return Categories.findAll({limit: args.limit, offset: args.offset});
                },
                async marker(root, args, context) {
                    return Markers.findAll({limit: args.limit, offset: args.offset});
                },
            },
            Category: {
                async markers(category){
                    return category.getMarkers();
                }
            }
    }

I.e. I can pass it into the query resolvers since it is defined in the schema, but I cannot apply it to the markers resolver within the category type

like image 884
Chris Avatar asked Oct 30 '22 22:10

Chris


1 Answers

You can put query variables anywhere in your schema, not just at the top level:

https://learngraphql.com/basics/query-variables/3

like image 122
Loren Avatar answered Nov 11 '22 15:11

Loren