Build the mental model
A field on a GraphQL object type can be another object type rather than a scalar — this models a relational database's foreign-key relationships directly in the schema. When executing a query, GraphQL calls a separate resolver function for each field; resolving `Tutorial.author` reads `authorId` off the parent tutorial object and fetches the author data.
Connect it to a real scenario
Declare an `Author` type with `id`, `name`, and `tutorials: [Tutorial!]!`, and add an `author: Author!` field on `Tutorial`. A single query now fetches tutorial fields and the author's name in one round trip — where the REST version needed two endpoints.
Try the working example
type Author {
id: ID!
name: String!
tutorials: [Tutorial!]!
}
type Tutorial {
id: ID!
title: String!
author: Author!
}
type Query {
tutorial(id: ID!): Tutorial
}You can write nested object types and model a relationship field in the schema.5-minute try-it
Add a `Comment` type (`id`, `body`, `author: Author!`) and give `Tutorial` a `comments: [Comment!]!` field.
One important caution
Marking a relationship non-null (`Author!`) while your data contains orphaned tutorials (a missing author) will crash the resolver — think about data integrity alongside schema constraints.
GraphQL — Schemas and Types — GraphQL