introduction
Federation
What is GraphQL
DocsGraphQL Basics - Types, Unions, InterfacesWhat is GraphQL

GraphQL Basics: An Exploration of Types, Interfaces, and Unions

In the modern era of web development, GraphQL has managed to make a distinctive place due to its efficient and powerful features. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need, and helps in accumulating multiple resources in a single request. The important aspects contributing to its overwhelming adoption include GraphQL Type, GraphQL Interface, and GraphQL Union.

Understanding GraphQL Type

GraphQL Type is one of the most important concepts in GraphQL. It's a way of defining the shape and nature of the data that can be requested and sent back from the server. It outlines the type of data that can be requested like String, Integer, custom defined types, and more. GraphQL Type is really where GraphQL's power shines - it offers developers a predictable data fetching experience that can be orchestrated based on the application needs.

type President{
  country: String!
  firstName: String!
  lastName: String!
  hairColor: String
}

Exploring GraphQL Interface

The GraphQL Interface is a powerful abstract type that allows different types to include certain fields. These are particularly useful when you have a bunch of types that have some fields in common. Interfaces provide a way for the fields to be reused so that they don't need to be rearranged in the types that implement them. This increases efficiency, reduces potential points of failure, and encourages a more structured and organized codebase.

interface Person{
  firstName: String!
  lastName: String!
}

type President implements Person{
  country: String!
  firstName: String!
  lastName: String!
  hairColor: String
}

In GraphQL in opposition to other programming languages - you need to copy fields to the actual type implementing the interface.

Interfaces implementing other interface

This is also possible in GraphQL. To do it we also have to copy the fields all the time. We also need to remember about listing all the interfaces on the actual type.

interface Person{
  firstName: String!
  lastName: String!
}

type Politican implements Person{
  country: String!
  firstName: String!
  lastName: String!
}

type President implements Politican & Person{
  country: String!
  firstName: String!
  lastName: String!
  hairColor: String
}

Delving into GraphQL Union

The GraphQL Union is slightly different from Interfaces. They constitute an abstract type that enables one of several types to be included, but they don't specify any common fields between the types. The GraphQL Union provides a layer of flexibility in the system to maintain types with possible variations in fields but under a singular reference. This flexibility creates space for growth and changes in your application and API design.

Here is an example of GraphQL union:

interface Person{
  firstName: String!
  lastName: String!
}

type President implements Person{
  country: String!
  firstName: String!
  lastName: String!
  hairColor: String
}

type PrimeMinister implements Person{
  country: String!
  firstName: String!
  lastName: String!
  age: Int!
}

union HeadOfState = President | PrimeMinister

By understanding these primary aspects - GraphQL type, GraphQL interface, and GraphQL union, you can harness GraphQL's strengths and build robust, adaptable, and efficient APIs. This fundamental knowledge will pave the way for a more advanced understanding of GraphQL and its potential.