The goal of GraphQL is to provide users with a comprehensive view of data stored within an API, the ability to only receive relevant data and an architecture that makes APIs easier to scale and adapt over time. In a broad sense, GraphQL is
In order to build APIs using GraphQL, a GraphQL server, that hosts the API, and a client, that connects to an endpoint, is required. GraphQL APIs are composed of three characteristics: the schema, queries
- Schema- Is the
typesystem used to define an API within a server implementation. All capabilities and functionalities are defined within the schema.
- Query- The request, or instruction, for output that is made. New queries are declared with a keyword and can support
nested fields, arrays and
- Resolver- This tells GraphQL how and where data can be found in relation to the given fields. Without this, the GraphQL server would not know how to handle a query.
GraphQL helps to create flexible APIs that can be adjusted with changing needs and provides uniformity across applications. When applications need to be adjusted to add new or remove unused features, those corresponding fields in the query can simply be adjusted and executed with minimal effort and disruption.
Benefits of GraphQL
- Organizes code in a hierarchical nature to reduce complexity and display relationships between objects
- Has strongly-typed fields that alert developers of error messages before running a query.
- Returns predictable results from complex queries.
- Outputs only relevant, desired data.
- Allows applications to run faster and more stable.
- Takes advantage of existing code and data sources, eliminating redundant work.
- Is version free to support evolution and future
GraphQL vs REST
Facebook developed GraphQL to simplify the endpoints of REST. Instead of having multiple endpoints with small amounts of disjointed data, GraphQL has a single endpoint that inputs complex queries and outputs only relevant information.
Using a metaphor, REST runs APIs as a sequential to-do list. It first runs errands, then picks up the mail and then makes a dentist appointment- all components are separate. With GraphQL, there is an added step before tasks are completed where the desired outcome is requested (in this case, the desired outcome is that the errands are completed, the mail is obtained and the appointment is scheduled). Then, GraphQL steps in and does all the tasks simultaneously and only returns the outcome, similar to a personal assistant.
REST has become the standard tool for developing web APIs but has shown to be too inflexible to keep up with rapid updates and susceptible to problems of over- and