GraphSQL - Sharing Data in A Microservices Architecture Using GraphQL
GraphSQL - Sharing Data in A Microservices Architecture Using GraphQL
Images haven’t loaded yet. Please exit printing, wait for images to load, and try to
Bruno Soares Follow
Jul 12, 2017 · 7 min read print again.
Sharing data in a Microservices
Architecture using GraphQL
Here at GetNinjas we’ve faced some problems integrating components
of our Microservices Architecture and exploring available options,
GraphQL showed up as an excellent t. I’ll focus on exploring problems
with three common ways to do that, and in the end suggest GraphQL as
a better option for some cases.
Common strategies
Strategy #1 — Share database access
Integrate systems by their databases seems to be so easy that we feel
seduced to do this. All we have to do is share databases access among
the services, so the system A can access the database of the system B
and so forth. What is the problem with this approach? Here is a list of
some of them:
#1.1 Performance
Problems with performance in the service A can a ect the system B and
make it hard to scale them independently, actually they are not
independent.
Microservices prefer letting each service manage its own database, either
di erent instances of the same database technology, or entirely di erent
database systems.(Decentralized Data Management)
. . .
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 1/9
3/16/2018 Sharing data in a Microservices Architecture using GraphQL
Strategy #2 — Sync all data in a centralized database
If you have a data warehouse you may be feel tempted to use it but,
here’s some details you have to pay attention.
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 2/9
3/16/2018 Sharing data in a Microservices Architecture using GraphQL
other service want to use it. In a synced database strategy you will need
to duplicate that query, the problem with it is obvious, when the query
is changed, you will need to change all versions too.
Sync databases in one big place can be a good choice for analysis
purpose, but to share data between services, not so much.
. . .
Strategy #3 — REST APIs
At this point, things start to get much better. But we can list a few
problems.
For some cases you can stop reading here, REST APIs can do a really
great job integrating services, BUT GraphQL is here and understanding
this tool can be very useful!
. . .
GraphQL as an API for some services
I’ll describe below a few bene ts of GraphQL over REST.
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 3/9
3/16/2018 Sharing data in a Microservices Architecture using GraphQL
Flexibility
You can query the schema navigating through multiple resources by
their relations saving round trips to the server.
Performance
Ask for only what you need, not a resource that responds with all data.
Github’s GraphQL API example of query/response using the GraphiQL IDE.
Highlights
• The query matches exactly the response. You do not need to read
the documentation or run the request to know the response
structure.
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 4/9
3/16/2018 Sharing data in a Microservices Architecture using GraphQL
Documentation
Document eld by eld during development.
Github’s GraphQL API example of auto-generated documentation using the GraphiQL IDE.
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 5/9
3/16/2018 Sharing data in a Microservices Architecture using GraphQL
Highlights:
Development
Evolve the entire API schema instead of only one REST API endpoint.
When you add a eld it can be used by other consumers.
Organic versioning
Simply add elds when you need or mark them as deprecated when
you plan to stop using them.
Monitoring
Track the usage of each eld in your API, you can track who is using,
performance and the usage of deprecated elds.
Of course that GraphQL will not replace all cases that we are
accustomed to solve with other technologies, there are so many out
there, RESTful, SOAP, Sockets, Protobuf, some kind of binary protocol
over UDP, Queues, etc. Knowing other options will help you achieve
your goals quickly and better.
. . .
GraphQL as API Gateway
Instead of requesting information to a speci c service you can request
that information to an API Gateway which abstracts the service that is
the owner of the data.
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 6/9
3/16/2018 Sharing data in a Microservices Architecture using GraphQL
GraphQL as API Gateway.
In this approach it doesn’t really matter if the services uses a REST API
or a GraphQL API. Actually, if your service is really thin GraphQL will
only add an extra complexity, so a simple REST API can be better.
Merging the APIs in one place
Update: 02/26/2018
At this point, you may be asking yourself, how to join all APIs in only one
GraphQL schema? There are some tools to help you :)
This post summarizes how schema stitching works and here, you cam
nd examples to build it using the Apollo GraphQL Tools.
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 7/9
3/16/2018 Sharing data in a Microservices Architecture using GraphQL
...
person: {
type: PersonType,
args: {
id: { type: GraphQLString },
},
resolve: (root, args) => {
fetch(`${BASE_URL}/person/${args.id}/`)
.then(res => res.json())
.then(json => json.person)
},
},
...
. . .
Conclusion
Integrate services by their databases is not a good option, an
architecture that takes advantage of the best of each technology shall
bring better results (as always).
I hope you’ve enjoyed the ideas presented here. If you liked it, please
consider tapping or clicking the icon to recommend it to others so
they can enjoy it too. And feel free to share it widely in your favorite
social network :-)
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 8/9
3/16/2018 Sharing data in a Microservices Architecture using GraphQL
https://labs.getninjas.com.br/sharing-data-in-a-microservices-architecture-using-graphql-97db59357602 9/9