Interfaces in GraphQL allow you to define common fields and their types that multiple object types can implement. This allows you to create more flexible and modular schemas. For example, consider a schema for a social media platform. You could define an interface called Post
with common fields like id
, content
, author
, and createdAt
. Then, you can have object types like TextPost
, ImagePost
, and VideoPost
that all implement this interface. This way, you can query for a list of posts without caring about their specific types.
Here's an example of how to define an interface in GraphQL:
interface Post {
id: ID!
content: String!
author: User!
createdAt: DateTime!
}