Exploring GraphQL Essentials: Understanding Schema and Object Types

April 29, 2024Author: Fabio J Raminhuk
graphql-essentials-understanding-schema-and-object-types.jpg

If you've been following our previous discussions on GraphQL, you're likely familiar with its powerful capabilities. In earlier articles, we delved into creating a GraphQL API using Node.js and explored its integration with React. Today, let's shift our focus to essential concepts within GraphQL—the GraphQL schema and its object types.

 

As a quick reminder, GraphQL serves as a query language for interacting with APIs. Unlike traditional REST APIs, where the server determines the structure of the response, GraphQL empowers clients to specify the exact data they need, leading to more efficient data retrieval. For instance, consider a query to fetch specific fields like title and price from a listing object:

 
graphqlCopy code
{
  listing {
    title
    price
  }
}

 

Executing this query could yield results such as:

 
jsonCopy code
{
  "data": {
    "listing": {
      "title": "Beachfront condo...",
      "price": 50
    }
  }
}

However, one might wonder: How do we know which properties are available within the listing object? Does it offer additional fields beyond title and price? This is where the GraphQL Schema comes into play.

 

At the heart of every GraphQL API lies its schema, serving as a comprehensive blueprint of all possible data requests. When a query is received, it undergoes validation against this schema, ensuring adherence to predefined rules and structures.

 

Central to the GraphQL schema are object types, defining the entities we can retrieve from the service along with their associated fields.

 

Object Types

Let's revisit our earlier example featuring a listing object type containing title and price fields. Here's how we might define this object within a GraphQL schema:

 
graphqlCopy code
type Listing {
  id: ID!
  title: String!
  address: String!
  price: Int!
}

 

In this schema snippet:

  • We declare a GraphQL object type named Listing.
  • Within Listing, we specify properties such as id, title, address, and price, each accompanied by its respective data type.

    This schema definition adheres to the GraphQL Schema Language syntax, providing a clear representation of the available data structure.

     

    Within a GraphQL schema, it's common to establish connections between different object types. For instance, a Listing object might reference a tenant, represented by the User object type:

     
    graphqlCopy code
    type Listing {
      ...
      tenant: User!
    }
    
    
     

    Conversely, the User object type could feature a listing field:

    graphqlCopy code
    type User {
      ...
      listing: Listing!
    }
    
    

    These one-to-one connections illustrate the relationships between various entities within the schema.

     

    Query and Mutation

    In addition to object types, GraphQL schemas include two primary object types: Query and Mutation. While a Query type is mandatory, a Mutation type is optional.

     

    The Query type serves as the entry point for data retrieval, outlining available pathways to fetch information. On the other hand, the Mutation type facilitates data modification operations.

     

    Consider the following examples:

    graphqlCopy code
    type Query {
      listings: [Listing!]!
    }
    
    type Mutation {
      deleteListing(id: ID!): Listing!
    }
    
    

    Here, listings represents a query to fetch a list of listings, while deleteListing defines a mutation to delete a listing based on its ID.

     

    Scalar Types

     

    In GraphQL, every attribute of an object ultimately resolves to a fundamental value known as a scalar type. These scalar types include:

     
    • Boolean: representing true or false values.
    • Int: denoting signed 32-bit whole numbers.
    • Float: representing signed double-precision floating-point numbers.
    • String: representing sequences of UTF-8 characters.
    • ID: serving as unique identifiers for objects.
     

    Enumeration Types

    Enumeration types, or enums, restrict scalar types to predefined sets of values. For instance:

    graphqlCopy code
    enum ListingType {
      HOUSE
      APARTMENT
    }
    
    
     

    Lists and Non-Null Fields

    In GraphQL, square brackets denote lists, allowing fields to return multiple instances of a specified type. Additionally, the ! symbol designates fields as non-nullable, ensuring they always resolve to a valid value.

    graphqlCopy code
    type Listing {
      ...
      bookings: [Booking]  // nullable list
      users: [User!]!      // non-null list
    }
    
    
     

    Arguments and Input Types

    GraphQL fields can accept arguments, similar to functions in traditional programming languages. Input object types simplify the management of complex argument structures.

    graphqlCopy code
    input CreateListingInput {
      id: ID!
      title: String!
      address: String!
      price: Int!
    }
    
    type Mutation {
      createListing(input: CreateListingInput!): Listing!
    }
    
    
     

    Conclusion

    GraphQL's schema-driven approach empowers clients to request and manipulate data with precision. Object types lie at the core of this schema, defining data entities and their relationships. In upcoming articles, we'll explore additional GraphQL concepts such as resolver functions, pagination, and caching.

    Tags:
    GraphQLSchemaAPIFrontEnd