Mosius
Mosius

Reputation: 1682

Graphql: How can get field arguments of a type in resolver?

I use graphql-tools library and makeExecutableSchema function to make my schema by passing schema and resolver to it

here is my schema:

type Trip {
  code: String!
  driver: User!
  vehicle: Vehicle!
  destination: Location!
  passengers(page: Int, count: Int): [User!]!
}
type Query {
  trip(id: String!): Trip
}

and here is my resolver:

// some imports...
export default {
  Query: {
    async trip(_, { id }, ctx, info) {
      const trip = await Trip.findById(id);
      // const page = ???, count = ???
      // work on fetch data...
      return result;
    },
};

how can I get page and count which are defined as nested argument for passengers?

Upvotes: 0

Views: 1933

Answers (1)

zetavg
zetavg

Reputation: 318

You should define a resolver for the type Trip, such as:

export default {
  Query: {
    async trip(_, { id }, ctx, info) {
      const trip = await Trip.findById(id);
      // const page = ???, count = ???
      // work on fetch data...
      return result;
    },
  Trip: {
    async passengers(trip, { page, count }, ctx, info) {
      ...
    },
  }
};

In GraphQL, it's not the concept of "nested fields of a type", but just combinations of "the type of a field". The trip field of type Query has the Trip type, so when you want to work with the passengers field, it should be considered as a field under the Trip type, not a nested field of the Query type.

Upvotes: 1

Related Questions