Object types and fields

Beginner · 1 min

Object types and their fields are the core of a GraphQL schema, including nesting, fields with arguments, computed fields, and how a field maps to a resolver.

Before thisSchema definition languageScalar types and custom scalars

My Digital Diaries (fka My Sitecore Diaries) · 3 min · from 5:59

Object types and fields

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by My Digital Diaries (fka My Sitecore Diaries)Report a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An object type is a named set of fields; each field has a name, a type, and optionally arguments.
  • Fields can point at other object types, which is what makes the schema a graph and lets one query nest as deep as the schema allows.
  • Every field is backed by a resolver; if you write none, graphql-js reads the property of the same name from the parent object, calling it if it is a function.
  • A field does not need to exist as stored data: fullName can be computed from first and last without any client knowing.
  • A client can only select fields that exist, and must select subfields on an object type, or validation fails before execution.

What an object type is

When you ask a database for a user, you get a row of columns. When you ask a GraphQL API for a user, you get what the schema calls an object type: a named collection of fields. It is the main building block. Almost everything you design in a GraphQL schema is an object type or something that refers to one.

type User {
  first: String!
  last: String!
  fullName: String!
  friends(first: Int = 2): [User!]!
}

type Query {
  me: User
}

A field has a name, a type, and optional arguments. first and last are plain scalars. fullName looks like a scalar too, but it does not have to be stored anywhere. friends returns a list of other Users, and takes an argument with a default.

That last one is the interesting part. A field that returns another object type is a link, and links are what make the schema a graph. A Book can have an author, and an Author can have books, and a client can bounce between them in one query for as long as it likes. You are not describing tables. You are describing what can be reached from what.

Run it

This is runnable on graphql-js 17.1.0. The root value stands in for a database:

import { buildSchema, graphql } from 'graphql'

const schema = buildSchema(`
  type Query { me: User }
  type User {
    first: String! last: String! fullName: String!
    friends(first: Int = 2): [User!]!
  }
`)

const user = (f, l) => ({
  first: f,
  last: l,
  fullName() { return `${this.first} ${this.last}` },
  friends: ({ first }) => [user('A', 'B'), user('C', 'D'), user('E', 'F')].slice(0, first),
})

const run = (source) => graphql({ schema, source, rootValue: { me: user('Ada', 'Lovelace') } })

First query, with no arguments:

{ me { fullName friends { first } } }
{"data":{"me":{"fullName":"Ada Lovelace","friends":[{"first":"A"},{"first":"C"}]}}}

Two things happened that you did not write code for. first was never resolved explicitly; graphql-js looked up first on the parent object. And friends got two entries because the schema default first: 2 was applied. Now pass the argument:

{ me { friends(first: 1) { fullName } } }
{"data":{"me":{"friends":[{"fullName":"A B"}]}}}

Fields run as functions

Behind every field is a resolver, even when you do not write it. The default resolver does this: take the parent object, read the property named like the field, and if that property is a function, call it with the field’s arguments. That is why fullName() above worked without any setup and why friends received { first: 1 }.

The consequence for design is that a field is a contract, not a column. fullName can be computed. friends can run a database query only when someone asks for it. A field the client never selects costs the server nothing. That last point is the reason GraphQL lets you hang expensive fields off a type safely. Resolvers covers writing them explicitly.

What the validator enforces

Two mistakes you will make in your first hour, both caught before any data is fetched:

{ me { age } }
# Cannot query field "age" on type "User".

{ me }
# Field "me" of type "User" must have a selection of subfields. Did you mean "me { ... }"?

The first is a field that does not exist. The second is the rule that an object type needs a selection set, which the fields and selection sets lesson comes back to.

Naming and design hints

  • Type names are PascalCase (BlogPost), field names are camelCase (publishedAt). Those are conventions, not rules, but tooling assumes them.
  • Do not mirror your database one-to-one. Model what a client wants to talk about. A User might combine columns from three tables.
  • Avoid putting a field on a type just because you have the data. Anything you add becomes something clients depend on and you must maintain. Evolving a schema later is easier if the schema starts small (evolving a schema without versions).
  • A field’s return type decides whether null is allowed. That is the next lesson, non-null and lists.

What the video covers

This is a clip of a longer episode: My Digital Diaries’ “GraphQL Schema Explained” (GraphQL Fundamentals EP03, published November 2026, 27 minutes). The embed runs from 05:59 to 09:20, which its chapter list labels “Types (Object Types)”. The channel’s page calls itself “My Digital Diaries (English)”, which is why this one passed my English-audio screen. I judged the clip from its chapters and description only; I did not read a transcript. The rest of the episode (scalars, enums, unions, interfaces, non-null) is the basis for the following lessons. The runner-up is This Dot Media’s video with Eve Porcello, which centres on federation entities and so is a different slice of the topic.

Where this fits

An object type says which fields exist. Whether each can be null, and whether it holds one value or many, is controlled by two small modifiers. Next: non-null and lists.

Check yourself

  1. 01How does a field get its value when you have not written a resolver for it?

    The default resolver reads the property with the field's name from the parent object, and if that property is a function it calls it with the field's arguments.

  2. 02Why must you write a selection set for a field that returns an object type?

    An object has many fields and the server cannot guess which you want, so a query like { me } is rejected and you must write { me { first } }.

  3. 03Can a field have arguments, and where are they declared?

    Yes, in parentheses after the field name in SDL, such as friends(first: Int = 2): [User!]!.

  4. 04What makes a schema a graph rather than a list of tables?

    Fields whose types are other object types, so Book has an author and Author has books, and queries can follow those links.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark