DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • DGS GraphQL and Spring Boot
  • Optimizing Natural Language Queries for Multi-Service Information Retrieval
  • Mastering Async Context Manager Mocking in Python Tests
  • Building a Rust Command Line Interface to Chat With Llama 3.2

Trending

  • Efficient API Communication With Spring WebClient
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  1. DZone
  2. Data Engineering
  3. Databases
  4. A Beginner’s Guide to GraphQL Interfaces and Unions

A Beginner’s Guide to GraphQL Interfaces and Unions

Learn about Interfaces and Unions in GraphQL, key concepts that simplify API development by allowing flexible data querying and improving query efficiency.

By 
Sugandha Singh user avatar
Sugandha Singh
·
Nov. 25, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
1.5K Views

Join the DZone community and get the full member experience.

Join For Free

What is GraphQL?

GraphQL is an open-source query language for APIs, initially developed by Facebook in 2012 and released to the public in 2015. It offers a flexible and efficient alternative to traditional REST APIs by allowing clients to request only the specific data they need, solving issues of over-fetching and under-fetching that often occur with REST APIs.

One of the reasons for GraphQL’s growing popularity is its client-driven nature. This makes it particularly well-suited for modern applications where performance, scalability, and seamless user experiences are critical. GraphQL enables clients to combine multiple resources into a single request, reducing network traffic and making it an excellent solution for mobile apps with limited bandwidth or complex front-end needs.

Major companies like GitHub, Twitter, Indeed, and Shopify have embraced GraphQL, highlighting its potential to streamline API development and improve client-server interactions.

GraphQL Interface

In GraphQL, an Interface functions similarly to interfaces in object-oriented programming. It is an abstract type that defines a set of common fields that multiple object types can implement. This ensures that the client can confidently query these common fields across different types. 

Markdown
 
type Query {
  findVehicles(): [Vehicle!]!
}

interface Vehicle {
   id: ID!
   name: String!
   model: String
}

type Car implements Vehicle {
   id: ID!
   name: String!
   model: String

   # Specific to Car
   fuelType: String!
}

type Bicycle implements Vehicle {
   id: ID!
   name: String!
   model: String

   # Specific to Bicycle
   gearCount: Int!
   isElectric: Boolean!
}


GraphQL client query:

Markdown
 
query findVehicles() {
 findVehicles() {
   vehicle {
       id,
       name,
       model,
       ... on Car {
           fuelType
       }
       ... on Bicycle {
           gearCount,
           isElectric
       }
   }
 }
}


In this example, the common fields like id, name, and model are available across all object types that implement the Vehicle interface. However, the type-specific fields like fuelType for Car and gearCount and isElectric for Bicycle can be queried using fragments.

If the client only needs common fields, they can omit the fragments:

Markdown
 
query findCars() {
 findCars() {
   car {
       id,
       name,
       model
   }
 }
}


Benefits of Using Interfaces

  1. Code Reusability: Common fields can be defined in the interface and shared across multiple types, reducing redundancy.
  2. Simplified Client-Side Logic: Clients don’t need conditional checks for object types. They can request exact types and confidently handle responses.
  3. Schema Extensibility: Adding a new common field becomes easier since it only needs to be defined in the interface.
  4. Enforced Structure: Interfaces enforce a shared structure across all implementing types, ensuring consistency (e.g., all vehicles must have an id, name, and model).
  5. Unified Querying: Instead of querying different types individually, clients can query a single interface to retrieve data from all implementing types.
  6. Improved Documentation: By defining common behavior through interfaces, it becomes easier for API consumers to understand and work with related types.

GraphQL Union

A Union in GraphQL is an abstract type that allows clients to query multiple object types that are related in some aspect through a single field. Unlike interfaces, unions do not require the member types to share common fields. This makes unions ideal for handling cases where related types have different structures but need to be queried together. 

Markdown
 
type Query {
   getPurchaseItems(): [PurchaseItem!]!
}

union PurchaseItem = Product | Service | Subscription

type Product {
   id: ID!
   productName: String!
   price: Float!
}

type Service {
   id: ID!
   serviceName: String
   duration: Float
}

type Subscription {
   id: ID!
   planName: String
   billingCycle: String
}


GraphQL client query:

Markdown
 
query getPurchaseItems() {
   getPurchaseItems() {
       purchaseItems {
           ... on Product {
               id
               productName
               price
           }
           ... on Service {
               id
               serviceName
               duration
           }
           ... on Subscription {
               id
               planName
               billingCycle
           }
       }
   }
}


In this example, Product, Service, and Subscription are all distinct types that belong to the PurchaseItem union. The client can query all three types using fragments in a single query, even though they don’t share any fields.

Important note about unions, the member types of a Union type must all be Object base types; Scalar, Interface, and Union types must not be member types of a Union. Similarly, wrapping types must not be member types of a Union.

Benefits of Using Unions

  • Flexible Grouping: Unions allow for querying related types that don’t share any common fields, which would be cumbersome to handle otherwise.
  • Simplified Error Handling: Clients can use fragments to query specific types, reducing the need for complex conditional logic on the client side.
  • Heterogeneous Data Handling: A single query can retrieve data from multiple types, simplifying the handling of varied data structures.
  • Useful for Error Responses: Unions are particularly useful when you want to combine success responses and error details into a single response type, allowing clients to handle them efficiently.

In Summary

GraphQL Interfaces and Unions offer powerful ways to structure your GraphQL schema, enabling flexibility and simplifying client-server interactions. Interfaces allow shared fields across types, promoting code reusability and easier schema extensions. Unions, on the other hand, offer a way to query disparate types together without requiring common fields.

GraphQL Interface (computing)

Opinions expressed by DZone contributors are their own.

Related

  • DGS GraphQL and Spring Boot
  • Optimizing Natural Language Queries for Multi-Service Information Retrieval
  • Mastering Async Context Manager Mocking in Python Tests
  • Building a Rust Command Line Interface to Chat With Llama 3.2

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!