1

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.

2
  • 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. Commented Sep 13, 2019 at 18:45
  • 1
    @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! Commented Sep 13, 2019 at 18:51

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

IT WORKS!!!! That is the weirdest way of doing it, yet it makes a lot of sense. Thank you so much for sticking with me! I know it takes a lot to explain concepts that seem trivial, yet it really means a lot to me to finally understand. Thanks!
I have a similar issue - but 'getUser' seems to be limited in that 'chef' cannot be made 'async chef', which would seem to make sense to me when getting data from a database. In this case, 'chef' is just a JSON object that contains a reference to the user object. Or maybe there is some other subtlety that I'm missing here. This also implies that in an array, getUser is called every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.