You are designing a GraphQL API. What is an "interface"? Please what is an interface in GraphQL, what problem does it solve, provide an example of defining an interface (e.g., Character) and two types (Human, Droid) that implement it, and show how you can query a field that returns the interface type.
Assistant
We need to produce a response that answers the question: "You are designing a GraphQL API. What is an 'interface'? Please what is an interface in GraphQL, what problem does it solve, provide an example of defining an interface (e.g., Character) and two types (Human, Droid) that implement it, and show how you can query a field that returns the interface type."
We are ChatGPT. The user wants an explanation. They said "Please what is an interface in GraphQL..." likely a typo but they want an explanation. We need to define interface, explain the problem it solves (type abstraction, polymorphism, common fields, etc.), then provide an example: define interface Character with some fields (like id, name). Then two types Human and Droid that implement it and add their own fields. Then show a query that returns a field that returns the interface type, like query { characters { ... on Human { ... } ... on Droid { ... } } } maybe using inline fragments. Also perhaps show how to query fields that are common via the interface.
We need to be thorough but concise. Provide schema definition and query example.
We might also explain that interfaces allow to have multiple object types share common fields, and when a field returns an interface type, the client can ask for fields on the concrete type via type conditions. It solves the problem of representing union-like behavior but with shared fields.
Also note that GraphQL interfaces are defined using the interface keyword, and types implement them using implements.
We should show the schema snippet:
code
interface Character {
id: ID!
name: String!
}
type Human implements Character {
id: ID!
name: String!
homePlanet: String
}
type Droid implements Character {
id: ID!
name: String!
model: String
}
Then a query: maybe we have a field like:
code
type Query {
characters: [Character!]!
}
Then query:
code
query {
characters {
id
name
... on Human {
homePlanet
}
... on Droid {
model
}
}
}
This demonstrates that the result set can contain both Human and Droid objects, and we use inline fragments to request type-specific fields.
We might also mention that the server must include __typename to identify the type (but usually automatically). Also note that we can query common fields directly.
So we need to produce final answer.
But also note that the user might want the answer in a specific format? They just said "Please what is an interface in GraphQL...". So we can produce a clear structured response.
I'll produce an answer with headings or paragraphs: definition, purpose, example.
Also maybe mention that interface can have fields with default resolvers? Not necessary.
I will ensure to explain the problem: For example, in a Star Wars API, both Human and Droid are characters with common fields like id, name. Without interface, you'd have to duplicate fields. With interface, you can have a single field that returns either Human or Droid, and the client can query accordingly.
We might also note that interfaces cannot have arguments on fields? Actually they can have arguments. But for simplicity, keep it simple.
I'll produce final answer.
We need to be careful about GraphQL syntax: In schema, we define:
code
interface Character {
id: ID!
name: String!
}
Then:
code
type Human implements Character {
id: ID!
name: String!
homePlanet: String
}
Similarly for Droid.
Then root query:
code
type Query {
characters: [Character!]!
}
Query:
code
{
characters {
id
name
... on Human {
homePlanet
}
... on Droid {
model
}
}
}
Also mention that you could use a fragment but not required.
We might also mention that interfaces allow for abstract types: you cannot directly return an interface instance; it must be one of the implementing types.
Now produce final answer.
What is an Interface in GraphQL?
An interface in GraphQL is an abstract type that defines a set of fields that other object types can implement. It allows multiple types to share a common set of fields while still maintaining their own specific fields. Interfaces enable polymorphism: a field can return any type that implements the interface, and the client can query both the shared fields and type‑specific fields using inline fragments.
What Problem Does It Solve?
Avoids duplication: Common fields (e.g., id, name) are defined once in the interface instead of being repeated in every type.
Enables union‑like behavior with shared fields: Unlike a union, an interface ensures that all implementing types contain the fields declared in the interface. This lets the client safely query those fields without needing type conditions.
Supports flexible API design: A single field (e.g., characters) can return a heterogeneous list of types (Human, Droid, etc.), allowing the client to request data based on the concrete type.
Example: Defining an Interface and Implementing Types
Imagine a Star Wars API where both Human and Droid are kinds of Character. Here’s how you could define the schema:
graphql
# The interface declares the fields common to all characters.
interface Character {
id: ID!
name: String!
}
# Human implements the Character interface and adds its own field.
type Human implements Character {
id: ID!
name: String!
homePlanet: String
}
# Droid implements the Character interface and adds its own field.
type Droid implements Character {
id: ID!
name: String!
model: String
}
# A root field that returns a list of Character objects (could be Humans, Droids, or any other future Character type).
type Query {
characters: [Character!]!
}
Querying a Field that Returns the Interface Type
When you query the characters field, the response will contain objects of types that implement Character. You can request the shared fields (id, name) directly. To ask for fields that exist only on a specific implementing type, you need to use an inline fragment with a type condition:
graphql
query {
characters {
id
name
... on Human {
homePlanet
}
... on Droid {
model
}
}
}
In this example, the first item is a Human (so homePlanet is present) and the second is a Droid (so model is present). The client uses the fragments to request the appropriate type‑specific fields, while the shared fields are always available. This pattern is fundamental for building flexible, type‑safe GraphQL APIs.