简体   繁体   中英

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();
                }
            }
    }

Ie 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM