Nested Resolvers
Learn how to define and implement nested GraphQL resolvers.
We'll cover the following...
Defining our schema
In this lesson, we’ll add a new User type that represents information about a user who has created a post about a particular product. We’ll update our schema so that when a consumer of GraphQL API fetches data about a product, they can also fetch data about the user who posted it.
Each user object in our application will have the following fields:
id
: The unique identifier of a user.userName
: User’s screen name on the website.fullName
: A user’s full name.
To add support for this in our schema, we need to define a new User
type with these three fields:
type User {id: ID!,userName: String!,fullName: String!,}
The only new feature in this type of definition is the ID
type that we’ve used for the id
field. This is another built-in GraphQL type that’s serialized to a string. It signifies that this is a unique identifier.
Now that we have a new User
type, the second thing we need to do is specify that each object of the Product
type has an author
field in the schema.
type Product {name: String!,description: String!,url: String!,numberOfVotes: Int!,publicationAt: String!,# New field that we've addedauthor: User!,}
This should all be familiar by now. We specify that the author
field is of a type User
, which we’ve defined before, and that it should never be null.
When compared to our previous ...