GraphQL Support in Flogo Enterprise

With the recent release of Flogo Enterprise 2.6 (please take your time to take a look at the release notes to know all the news that have been released), it has been included support to GraphQL.

I don’t want to make a full article about what GraphQL is and the advantages that include comparing with REST and so. Especially when you have so many articles here in medium about that topic, so please take a look at the following ones:

So, in summary, GraphQL it’s a different protocol to define your API interfaces with another approach in mind. So, let’s see how can include this kind of interfaces in our Flogo flows. We’re going to play with the following GraphQL Schema that is going to define our API

Image for post
Image for post
GraphQL Schema Definition

If this is the first time you see a GraphQL schema, let me give you some clarifications:

  • Schema is split into three parts: Queries, Mutations, and Model
  • Queries are GET-style request to get information. In our case, we have two queries currentUser and company.
  • Mutations are POST/PUT-style request to modify information. In our case, we have three mutations: registerUser, registerCompany, asignUser.
  • Model is the different objects and types our queries and mutations interact with.

So, now we are going to do the hard work in Flogo Environment, and we start creating a new application that we’re going to call GraphQL_Sample_1

Image for post
Image for post
Creation form of the new app named GraphQL_Sample_V1

Now, we have an empty application. Until this point it wasn’t hard, right? Ok, let’s see how it goes. Now we are going to create a new flow, and we can choose between three options:

  • Empty flow
  • Swagger specification
  • GraphQL Schema
Image for post
Image for post

So we choose the option from GraphQL Schema and we upload the file and it generates a skeleton of all the flows needed to support this specification. A new flow is going to be generated for each of the queries and each of the mutations we’ve defined, as we have two (2) queries and three (3) mutations, so five (5) flows in total as you can see in the picture below:

Image for post
Image for post
Autogenerated flows based on GraphQL schema

As you can see flows have been generated following the following naming convention:

<TYPE>_<name>

Where <TYPE> can be either Mutation or Query and <name> is the name this component has in the GraphQL Schema.

Now, everything is done regarding GraphQL part, and we only need to provide content to each of the flows. In this sample, I’m going to rely on a PostgreSQL database to store all the info regarding users and companies, but the content is going to be very straight forward.

  • Query_currentUser: This flow is going to ask for the customer data to the PostgreSQL to return its data and the company he belongs to. In case he doesn’t belong to anyone, we gather only the data user and in case it is not present to return an empty object.
Image for post
Image for post
Query_currentUser flow
  • Query_company: This flow is going to ask for the company data to the PostgreSQL to return and in case it is not present to return an empty object.
Image for post
Image for post
  • Mutation_registerUser: This flow is going to insert a user in the database and in case its mail already exists is going to return the existing data to the consumer.
Image for post
Image for post
  • Mutation_registerCompany: This flow is going to insert a company in the database and in case its name already exists is going to return the existing data to the consumer.
Image for post
Image for post
  • Mutation_asignUser: This flow is going to assign a user into the company to do that is going to return the user data based on its email and same thing with the company and update the PostgreSQL activities based on that situation.
Image for post
Image for post

Ok, now we have our app already built, let’s see how we can test it and play with the GraphQL API we’ve built. So…its show-time!!!!!

Image for post
Image for post
Photo by Yvette de Wit on Unsplash

First, we’re going to build the app. As you probably know you can choose for different kinds of builts: docker image or OS-based package. In my case, I’m going to generate a Windows build to ease all the process, but you can choose whatever feels good to you.

To do that we go to the menu app and click in the Build and choose the Windows option:

Image for post
Image for post
Build option to windows

And once the built has done we’re going to have a new EXE file in our Download folder. Yes, that easy! And now, how to launch it? Even easier… just execute the EXE file and … it’s done!!

Image for post
Image for post
GraphQL Flogo app running in Windows console

As you can see in the picture above we’re listening requests in port 7879 in the /graphql path. So, let’s open a Postman client and start sending requests to it!!

And we’re going to start with the queries and to be able to return data I’ve inserted in the database a sample record with test@test.com email so, If now I try to recover it, I can do this:

{
“query” : “query($email: String!) { currentUser( email: $email) { id firstName email } }”,
“variables” : {“email” : “test@test.com” }
}
Image for post
Image for post
Postman Execution of GraphQL

Only a few highlights about our GraphQL query:

  • We can choose the amount of attributes the query is going to recover, and thats the last part of the query, so If I change it, let’s see what happens:
{
“query” : “query($email: String!) { currentUser( email: $email) { id } }”,
“variables” : {“email” : “test@test.com” }
}

And now, we’re getting only this output:

Image for post
Image for post
GraphQL output with only returning id

PSG Senior Architect at TIBCO Software with a focus on Cloud Development, Event Processing and Enterprise Integration

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store