1
votes

I am trying to use a GraphQL nested query (I am 80% sure this is a nested query?) to get information on the listing and the chef (author) of the listing. I can get the listing info just fine, but I am unable to get the chef info.

I was under the impression that the default resolver (user) would fire when getListing(args) returned without a valid User object for the chef. But the default resolver does not appear to be firing.

How do I properly get the nested information?

For example, my query is:

query getListing($listingID: String!) {
  getListing(listingID: $listingID) {
    name
    chef {
      firstName
    }
  }
}

The query returns:

{
  "data": {
    "getListing": {
      "name": "Test",
      "chef": {
        "firstName": null
      }
    }
  }
}

The function getListing(args) queries the DB and returns:

{
  name: 'Test',
  chef: 'testUsername',
  listingID: 'testListingID'
}

My Schema is:

type Listing {
  uuid: String!
  name: String!
  chef: User!
}

type User {
  username: String
  firstName: String
}

type Query {
  getUser(jwt: String!): User
  getListing(listingID: String): Listing
}

And my resolvers are:

const resolvers = {
  Query: {
    getListing: async (parent, args, context, info) => {
      console.log('GET_LISTING');
      return getListing(args);
    },
    getUser: async (parent, args, context, info) => {
      console.log('GET_USER');
      return getUser(args);
    },
  },

  User: async (parent, args) => {
    console.log('USER RESOLVER');
    return getUser(args);
  },
};

Other Info: I am using Apollo Server running on AWS Lambda integrating with DynamoDB on the backend.

1
Resolvers exist only at the field level. You can't resolve a type (i.e. User). You can only resolve a field that has that type.Daniel Rearden
@DanielRearden I thought that, but when I put in a resolver for chef (similar to the User resolver) GraphQL didnt like that at all. How would you properly resolve the chef field? Thanks!Reid

1 Answers

3
votes

Resolvers exist only at the field level. You can't resolve a type (i.e. User). You can only resolve a field that has that type (i.e. chef).

const resolvers = {
  // ...
  Listing: {
    chef: (parent, args) => {
      return getUser()
    }, 
  },
}

It's unclear what sort of parameters getUser accepts, so you'll need to modify the above example accordingly. You won't use args unless you actually specify arguments for the field being resolved in your schema. It looks like the returning listing has a chef property that's the name of the user, so you can access that value with parent.chef.