Overview of Apollo GraphQL Features and Tools

October 01, 2018 

Overview of Apollo GraphQL Features and Tools

What is Apollo GraphQL?

GraphQL is a query based language for REST APIs used to retrieve the exact data needed. GraphQL query always returns predictable results. GraphQL is fast and stable because GraphQL fetches the data clients needs in a single API request while typical REST APIs loads from multiple URLs. It returns the data quickly even on slow network connections. GraphQL APIs built regarding types and fields, not endpoints. It uses types to ensure the clients only ask for what’s feasible. If API doesn't have the data of that type, GraphQL returns clear and helpful errors. It is handy in error handling. Apollo GraphQL is GraphQL for React, JavaScript, and native platforms to connect data to UI.

Apollo GraphQL can be used with any JavaScript front-end language. Write the schema query in apps for fetching the data from GraphQL server, and rest Apollo client will do for him. It will return the exact data with types and fields what client API requested.


Features of Apollo GraphQL

Apollo GraphQL has the following features -

  • Incrementally Adoptable - User can implement it into any of existing apps today.
  • Universally Compatible - Apollo is compatible with any build setup, any GraphQL server, and any GraphQL schema.
  • Easy to get started with - User can start using GraphQL with small queries for fetching data right away and learn about the advanced features a bit later.
  • Inspectable and Understandable - Any user can easily understand the flow what is precisely happening in his application.
  • Built for interactive apps - Gives a swift response. Changes reflected immediately on apps.
  • Small and flexible - User will receive the data from APIs what exactly requested.

Why Adopting Apollo GraphQL Matters?

Most of the apps use the tree structure schema in APIs. For example, users details database in apps, its Rest API architecture seeded with a key or slug for some domain object. Basis of designing apps driven by URL-based routing which has been still for the past decade. So the user has to hit multiple APIs to get the details of few details for a single person. First, fetch the all users list, to get the user_id, then hit next API, but then again it will return the whole details of that user in a big tree-like structure. It will waste time and as well as data. Its a straightforward linkage between those requests.

For today's broadband world, bandwidth and latency matter, especially for mobile users. So here Apollo GraphQL is the solution to avoid the multiple requests and to save the time and bandwidth. It only returns the data that user needs for with just a single API request. It improves the productivity of using the GraphQL schema in the app.


How to Adopt Apollo GraphQL?

If any user is using the GraphQl API for the first time in his product, it will be critical adoption and engagement of users. To get people to start first time engagement with the API, make it understandable and useful to begin working within the context of the project quickly. It is an enormous and tricky task.

The API documentation Paradox

Usually, understanding the API starts with documentation. The paradox is that most of the developers don’t like writing docs making it hard for both developer and users to create a good experience for the first-time engagement with API. What most developerelopers look for when approaching a new API are examples which they can use in their project and easily modify.

The problem is, cases can be hard to change in the user’s project.Write easy and understandable code for users to start implementing in the project, quickly move to actual engagement with the API.


Apollo GraphQL Best Practices

A strongly typed GraphQL schema is required.

  • Designing for client needs
  • Style conventions
  • Utilizing interfaces
  • Designing for client needs

Designing for Client Needs

GraphQL schemas designed around the needs of client applications. While designing GraphQL schema, create literal mappings on top of existing database tables using CRUD-like root fields. If a database has fields which are not required , don’t include them in the schema up front. GraphQL allows creating a relation between his data that don’t exist in the latest data, enabling to move complex data manipulation logic out of clients.

Style Conventions

The GraphQL blueprint is flexible and doesn’t require specific naming guidelines. However, to expedite development and continuity across GraphQL deployments, it’s useful to have a standard set of conventions.

  • Fields - Name defined in camelCase since the majority of consumers will be client applications are written in Kotlin, JavaScript, Java, or Swift, all of which recommend for camelCase variable names.
  • Types - Should be PascalCase, to match how functions and classes defined in the above languages.
  • Enums - Should have defined their type name in PascalCase, and their value names in ALL_CAPS since they are similar to constants. If conventions applied above, no need to define any extra logic in his clients to switch the current conventions of these languages.

Utilizing Interfaces

Interfaces are a dominant way to build and use GraphQL schemas by the use of abstract types. Abstract types cannot be used directly in the schema but used as growing blocks for creating explicit types.

Consider an example where different types of plants share a standard set of attributes, such as indoor plants and outdoor plants. A simple foundation for these plants represented as the following interface -


interface Plant {
 name: String
 season: Season
            }

This interface will not be used to query for a plant directly, but to implement particular types. Imagine a screen within an app which needs to display a feed of all plants, without regard to their type. To create such functionality, we could define the following -


type IndoorPlant implements Plant {
  name: String
  season: Season
  height: [Height]
}

type OutdoorPlant implements Plant {
  name: String
  season: Season
  colors: [Color]
}

type Query {
  plants: [Plant]
}

Plant interface as the foundation for the IndoorPlant and OutdoorPlant types. Then, a plants field expresses that it returns a list of plants (i.e. [Plant]). Implementing the plant feed example removes the return of kind of plants. A query against this schema, return indoor plants and outdoor plants, might look like -


query GetPlants {
  plants {
      Name
     season
  }
}

Helpful for feeds of general content, user role systems, and more! Furthermore, if we need to return specific fields which are only provided by either IndoorPlants or OutdoorPlants (not both), we can request scrapped data from the abstract types in the query. Those scrapped data filled in only as appropriate; in the case of the example, only outdoor plants returned with colors, and only indoor plants will have height -


query GetPlants {
  plants {
    name
    ... on IndoorPlant {
      height {
        length
      }
    }
    ... on OutdoorPlant {
      colors {
        name
      }
    }
  }
}

Apollo GraphQL Tools

Apollo GraphQL divided into three types of tools -

  • Apollo GraphQL Client - Connect data to the UI.
  • Apollo GraphQL Engine - For Infrastructure and Tooling.
  • ApolloGraphQL Server - Translate the REST API and backends into a GraphQL schema.

Apollo GraphQL Client

Apollo GraphQL Client is an ultra-flexible and community-driven GraphQL client for React, JavaScript, and native platforms, which is used to bind data to the UI. It is the best GraphQL client because it supports advanced data loading: caching, mutations, optimistic UI, subscriptions, pagination, server-side rendering, prefetching, and more. Built for the modern applications. It is compatible with Redux, React Router, Recompose, Expo, Next.js and everything else in the React.

Apollo GraphQL Engine

Apollo GraphQL Engine is a cloud service that provides deep insights into the GraphQL layer, to run in production. It offers essential features including caching, performance tracking, and error tracking with alerts and regular reports. Apollo Engine is compatible with any GraphQL server. It's easy to deploy Apollo Server with Engine to a platform-as-a-service like Heroku. Apollo Engine runs anywhere, Docker container with just a few configuration options. Drop it into existing setup.

Apollo GraphQL Server

Apollo GraphQL Server is an open-source solution with all the features the user needs to take his GraphQL API from prototype to production. It helps to build a GraphQL layer over his existing REST APIs and databases. Describe the data, services have in a schema and fill in business logic with resolver functions. Add Event-Based Data Streaming to API with GraphQL subscriptions, enabling Real-Time features. Mock GraphQL schema with one line of code to develop UI in parallel with the API.