APIs are on the rise, mainly in companies that are not digital in their nature of business. This context includes banks, financial companies, insurance companies, payment media companies, retail companies, etc. Companies are seeking to make the experience of their consumers more digital and with fewer human interactions, and APIs are a great enabler for digital strategies.
But, after all, what are APIs?
An API (Application Programming Interface) is a set of routines and standards established by software for the use of its feature by applications that do not aim to be involved in implementation details, but only use their features.
Examples include RMI, JMS, and positional file exchange.
Web API is one of the possible ways to build APIs, in which the exchange of information between applications is carried out through the Internet (more specifically, through HTTP).
Examples include WebService SOAP, RESTful, and GraphQL.
Before talking about GraphQL, we will talk address difficulties encountered in the RESTful standard, which have caused the great ascent of the use of GraphQL for building APIs.
In RESTful, the features of APIs are accessed from a URI, which interacts with Resources and Elements through the HTTP Methods.
Imagine that we need to build an application in which all our Facebook friends who “liked” Sérgio Moro’s page (the judge responsible for one of the largest anti-corruption proceedings in Brazil) are obtained.
How would that query be made if Facebook had only one API in the RESTful standard:
GET: http://api.facebook.com/me/friends
2. Now, for each of the “N” returned friends, a request should be made at:
GET: http://api.facebook.com/me/friends/{friend_id}/pages?page_name=Sérgio Moro
Depending on the number of friends you have, it may be necessary to make the request hundreds or thousands of times.
When the APIs are intended for internal use, it is possible to perform a workaround (as in the previous example) to solve certain problems.
That, however, is not scalable!
In an Open API strategy, as is the case with Facebook APIs, it is impossible to predict all the needs that application developers will have regarding the APIs. Therefore, it is important that application developers have the autonomy and freedom to use the APIs in a much more flexible way than enabled by the RESTful standard.
Normally, it only uses one endpoint / graphqlFacebook mobile applications have used the GraphQL API since 2012. Since becoming Open Source in 2015, many other devices of all sizes are also using GraphQL:
And many others…In a simple and summarized way, to perform a “hello world” in GraphQL, the following is necessary:
Describe your data
type Project {
name: String
tagline: Stringcontributors: [User]
}
Inform what you need (request)
{
project(name: “GraphQL”) {
tagline}
}
Obtain the result (response)
{
“project”: {
“tagline”: “A query language for APIs”
}
}
GraphQL-Server is where the GraphQL API implementation is performed. At the run time, GraphQL-Server has the following responsibilities:
The image below shows what the use of a GraphQL API is like.
Note that in each request to a GraphQL API:
In GraphQL, it is possible to perform data Queries and Mutations (data alterations).Queries – Analog to Restful’s GET operation.
The client informs the data it needs, and the answer is a mirror of the request.
GraphQL-Server executes the queries in parallel to optimize the request.
Avoiding conflicts between different instances of the same object.
Each attribute and nested object can have its own set of arguments.
Situations in which you do not know the type of return and you need to determine somehow how to process that data in the client.
It is analogous to the POST / PUT / DELETE operations in Restful.
While the Queries are executed in parallel, the mutations are executed in series, one after the other. This means that, when sending two “Credit Implement” mutations, the first one will end before the second one begins.
If the mutation operation returns an object, you can request specific attributes and nested objects.
Attention points
Suggestions and best practices
GraphQL Clients
Server Libraries
IDE to Document and Explore GraphQL APIs
GraphiQL – https://github.com/graphql/graphiql