What is GraphQL? Is a query language and it’s focused on data manipulation of APIs, like an abstraction layer that lets us have many sources of data (Relational Database, APIs, documents databases, key-values databases) attached to one entrypoint, that let us filter fields from the client and take only the attributes/columns/data that we need and get in the response only what we defined as needed data.
Why use GraphQL in this process? because it is designed to make APIs fast, flexible, and developer-friendly. It can even be deployed within an integrated development environment (IDE) known as GraphiQL. As an alternative to REST, GraphQL lets developers construct requests that pull data from multiple data sources in a single API call. GraphQL gives API maintainers the flexibility to add or deprecate fields without impacting existing queries, that’s a plus to avoid errors from versioned API and have a lower impact on production changes.
Things to take into account when we iterate over a product API:
Schema first design: The first thing you must do is agree on a schema with the team who will be building the backend. This will save a lot of refactoring later on, and if all goes according to plan you will be able to swap out the MockedProvider for the real one and your application should work seamlessly.
Design and implement with the user in mind.
Remember API retirement and change Management.
Common GraphQL terms.
Graphql is a strongly typed language[1] this means that Graphql defines various data types to define the schema in this way is a fixed contract between client and server, so we communicate in the same way with each request, and allows us to talk about GraphQL schemas in a language-agnostic way (we don’t care if we are using javaScript, java or golang).
The most basic components of a GraphQL schema are object types, which just represent a kind of object you can fetch from your service, and what fields it has.
Some of Type System’s schema are:
Object Type
Fields
Query
Mutations
Array
List
And a set of default scalar types out of the box: Int (a signed 32 bits), float (A signed double precision), String (UTF-8 character sequence), Boolean (true or false), ID (The ID scalar type represents a unique identifier).
After we did a resume about the type system of Graphql, we could use several tools to mock data into this services, one of them is:
- Apollo graphql (server/client)
- mock.io/graphql ( it’s no a free, you’ll have to pay to used it)
- hasura.io[2] (server, works with apollo client or any client for graphql) is an open source service that gives you production-grade APIs on your data without having to build, operate & scale a GraphQL server. This has a developer tier free. You could try it.
Advantages and Drawbacks of GraphQL.
Advantages.
Reducing the amount of data returned between your frontend and the backend. Solves the issue of over-fetching or under-fetching data. Over-fetching is fetching all the data from an endpoint, but using only a portion of that data for, say, a mobile application. Under-fetching is fetching all the data from an endpoint, but not having enough data. This usually requires a call to a second endpoint.
Developers can build APIs with whatever methods they prefer, and the GraphQL specification will ensure they function in predictable ways to clients without impacting existing queries.
Aggregate that data together into a single meaningful response. GraphQL can be set up to call multiple backend services as it assembles its query response.
GraphQL is a way to simplify the interactions with your backend data, by using an interface whose schemas describe the system behavior so you can get efficient data from your APIs, bringing you flexibility in retrieving information. has general usefulness for simplifying the use of complex APIs.
Can be scalable using Apollo Federation[3], in this way you have one entrypoint (GraphQL Schema Server) and many implementations of resolvers behind this schema. Apollo Federation encourages a design principle called separation of concerns. This enables different teams to work on different products and features within a single data graph, without interfering with each other. This promised the benefits of a unified schema with distributed ownership and implementation.
Drawbacks.
The basic framework cannot be avoided. You have to do things according to the GraphQL way.
GraphQL presents a learning curve for developers familiar with REST APIs.
GraphQL shifts much of the work of a data query to the server side, which adds complexity for server developers.
API maintainers have the additional task of writing maintainable GraphQL schema.
Tools on GraphQL.
strapi, allow you to auto generate documentation from GraphQL schemas. https://strapi.io/documentation
hotchocolate, .Net Platform to implement a GraphQL server (C#).
https://github.com/ChilliCream/hotchocolate
Offix, an offline client that allows GraphQL mutations and queries to execute even when an application is unreachable.
Graphback, a command line-client for generating GraphQL-enabled Node.js servers.
OpenAPI-to-GraphQL, a command-line interface and library for translating APIs described by OpenAPI Specifications or Swagger into GraphQL.
https://github.com/IBM/openapi-to-graphql
Links referenced.
[1] https://graphql.org/learn/schema/
[3] https://www.apollographql.com/docs/federation/
See Also:
- https://www.redhat.com/architect/apis-soap-rest-graphql-grpc
- https://www.apollographql.com/docs/federation/
- https://opensourcelibs.com/libs/graphql-schema
- https://www.howtographql.com/
- https://graphql.org/
- https://graphql.org/swapi-graphql/
No comments:
Post a Comment