Interfaces

Intermediate · 1 min

Use an interface when several object types share fields, query it with inline fragments and __typename, and how the server resolves the concrete type.

Before thisObject types and fieldsNon-null and lists

My Digital Diaries (fka My Sitecore Diaries) · 6 min · from 18:27

Interfaces

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 interface declares fields that several object types must all provide; a field typed as the interface can return any of those types.
  • A query on an interface-typed field can select the shared fields directly, and the type-specific fields only inside an inline fragment such as ... on Book.
  • __typename is available on every object, interface, and union and tells the client which concrete type came back.
  • The server must be able to resolve each returned value to a concrete object type, through resolveType, isTypeOf, or a __typename property on the data.
  • An object type that declares implements Item but omits a field of Item makes the schema invalid, which validateSchema reports.

Different things, same questions

Say your catalogue sells books and albums. Both have an id and a title. A search box returns a mixed list, and the page renders id and title for every result, then adds a page count for books and a track count for albums. How do you type that list?

If the field returns [Book], albums cannot appear. If you invent a Product type that holds every possible field, the types lose their meaning, and most fields are null for most items. An interface solves this: it names the fields every member has, and each object type states that it implements them.

interface Item {
  id: ID!
  title: String!
}

type Book implements Item {
  id: ID!
  title: String!
  pages: Int!
}

type Album implements Item {
  id: ID!
  title: String!
  tracks: Int!
}

type Query {
  items: [Item!]!
  item(id: ID!): Item
}

An implementing type must repeat every interface field with a compatible type. It can add its own. A field typed Item can now hold a Book or an Album.

Querying an interface

Once a field returns Item, you can ask for what Item guarantees:

{ items { __typename id title } }

On graphql-js 17.1.0, with data [{__typename: 'Book', id: '1', title: 'Dune', pages: 412}, {__typename: 'Album', id: '2', title: 'Kind of Blue', tracks: 5}]:

{"data":{"items":[{"__typename":"Book","id":"1","title":"Dune"},{"__typename":"Album","id":"2","title":"Kind of Blue"}]}}

__typename is a meta-field that works on any object, interface or union. It is how a client knows what it got.

You cannot select pages straight off Item, because an album has none:

{ items { pages } }
# Cannot query field "pages" on type "Item". Did you mean to use an inline fragment on "Book"?

Use an inline fragment to say “if it is this type, also give me these”:

{ items { title ... on Book { pages } ... on Album { tracks } } }
{"data":{"items":[{"title":"Dune","pages":412},{"title":"Kind of Blue","tracks":5}]}}

Each result carries the type-specific fields only where they apply. A query with only ... on Book { pages } returns {"pages":412} for the book and {} for the album. Fragments get their own lesson soon (fragments).

How the server knows which type

The executor holds a value and a field type of Item. It needs a concrete object type to decide which fields to resolve, so something has to answer “which one is this?”. graphql-js accepts any of:

  • a resolveType function on the interface,
  • an isTypeOf function on each object type,
  • or, by default, a string __typename property on the data, which is what the data above used.

If none exists you get:

Abstract type "Item" must resolve to an Object type at runtime for field "Query.items".
Either the "Item" type should provide a "resolveType" function or each possible type
should provide an "isTypeOf" function.

That is one of the most common server-side errors when you first return interface or union types. In a database-backed server, resolveType usually looks at a type column or the shape of the row.

Interfaces implementing interfaces

Interfaces can extend other interfaces, and a type must then implement all of them:

interface Node { id: ID! }
interface Item implements Node { id: ID! title: String! }
type Book implements Item & Node { id: ID! title: String! }

I ran { n { id ... on Item { title } } } on this schema and it returned the book’s id and title. This pattern is the base of the Node interface used for global object identification (global object identification).

An invalid schema

If a type claims to implement an interface but leaves out a field, the schema is invalid. buildSchema accepts the SDL, but validateSchema reports: “Interface field Item.id expected but Book does not provide it.” Servers run that validation when you first execute, so you see it right away.

Interface or union?

If the types share fields you read together, use an interface. If they are unrelated, such as a search result that might be a Book, an Author or a Publisher with nothing in common, use a union. A rough test is whether you can write the shared fields in a sentence. “Everything sellable has an id, title, and price.” Interfaces also describe behaviour contracts such as Node or Timestamped.

What the video covers

The embed is a clip of My Digital Diaries’ “GraphQL Schema Explained” (EP03, November 2026), from 18:27 to 24:26 — the chapter its description labels “Unions & Interfaces”. So it covers both topics together, and the next lesson on unions links to a separate video. I judged it from the chapter list only and did not read a transcript. The full episode is linked under deeper. I chose it over a dedicated interfaces video because the other candidates I found were from small channels or for Java or Swift stacks, and I could not verify their quality.

Where this fits

An interface is “all of these types, and they share fields”. A union is its looser cousin. Next: unions.

Check yourself

  1. 01How do you select a field that exists only on one implementing type?

    Wrap it in an inline fragment on that type, such as ... on Book { pages }, because the interface itself only guarantees the shared fields.

  2. 02What is __typename for?

    It returns the name of the concrete object type at runtime, so a client can tell which fragment's fields to expect and read.

  3. 03What error appears if the server cannot tell which type a returned value is?

    Abstract type Item must resolve to an Object type at runtime, which means you need a resolveType function, isTypeOf checks, or a __typename on the data.

  4. 04When would you choose an interface instead of a union?

    When the types share fields that clients commonly read together, such as id and title; a union is for types with nothing in common.

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