资料内容:
1.2 GraphQL
At its core, GraphQL is a specification for building APIs. The GraphQL specification
describes an API query language and a way of fulfilling those requests. When building
a GraphQL API, we describe the data available using a strict type system. These type
definitions become the specification for the API, and the client is free to request the
data it requires based on these type definitions, which also define the entry points for
the API.
GraphQL is typically framed as an alternative to REST, which is the API paradigm
you are mostly likely to be familiar with. This can be true in some cases; however,
GraphQL can also wrap existing REST APIs or other data sources. This is due to the
benefit of GraphQL being data-layer-agnostic, meaning we can use GraphQL with any
data source.
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your
existing data. 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 nothing more, makes
it easier to evolve APIs over time, and enables powerful developer tools.
1.2.1 GraphQL type definitions
Rather than being organized around endpoints that map to resources (as with REST),
GraphQL APIs are centered around type definitions that define the data types, fields,
and how they are connected in the API. These type definitions become the schema of
the API, which is served from a single endpoint.
Since GraphQL services can be implemented in any language, a language-agnostic
GraphQL Schema Definition Language (SDL) is used to define GraphQL types. Let’s
look at an example in figure 1.2, motivated by considering a simple movie application.
Imagine you’ve been hired to create a website that allows users to search a movie cata
log for movie details, such as title, actors, and description, as well as show recommen
dations for similar movies the user may find interesting.