How GraphQL is used in WorkflowGen

What is GraphQL?

From the GraphQL website presentation:

"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

Where can I test WorkflowGen’s GraphQL API?

You can use the GraphiQL interactive in-browser GraphQL IDE to test queries and operations, and to browse the schema documentation. You can access this tool by going to http://[yoursite]/wfgen/graphql.

Use case example

Let’s say you want to display the action history of request #16 within the form itself: the action number, the action name, the action’s assignee, and the date of the action’s completion.

Your query would look something like this:

{
  actions(filter: {requestNumber: 16}) {
    items {
      number
      closedAt
      name
      assignee {
        lastName
        firstName
        userName
      }
    }
  }
}

And here’s a response example:

{
  "data": {
    "actions": {
      "items": [
        {
          "number": 1,
          "closedAt": "2018-12-24T21:30:18.113Z",
          "name": "1-INITIATES",
          "assignee": {
            "lastName": "Smith",
            "firstName": "John",
            "userName": "john_smith"
          }
        },
        {
          "number": 2,
          "closedAt": null,
          "name": "2-VALIDATES",
          "assignee": {
            "lastName": "Doe",
            "firstName": "John",
            "userName": "john_doe"
          }
        }
      ]
    }
  }
}

How to use GraphQL in C# code

In the example below, we take the exact query mentioned in the use case above and we simply put it as a query parameter in the following request:

http://[yoursite]/wfgen/graphql?query={actions(filter:{requestNumber: 16}){items{number closedAt name assignee {lastName firstName userName }}}}

As indicated above, the following request has this JSON response:

{
  "data":{
      "actions":{
          "items":[
              {
                  "number":1,
                  "closedAt":"2018-12-24T21:30:18.113Z",
                  "name":"1-INITIATES",
                  "assignee":{
                      "lastName":"Smith",
                      "firstName":"John",
                      "userName":"john_smith"
                  }
              },
              {
                  "number":2,
                  "closedAt":null,
                  "name":"2-VALIDATES",
                  "assignee":{
                      "lastName":"Doe",
                      "firstName":"John",
                      "userName":"john_doe"
                  }
              }
          ]
      }
  }
}

You can use a dynamic object to deserialize this JSON response and access its values to populate the table.

Here’s a full example of GraphQL usage in the form’s code behind.

	protected void Page_Load(object sender, EventArgs e)
	{
	    base.Page_Load(sender, e);
	    if(!IsPostBack){
    	    System.Uri uri = new System.Uri(Request.Url.ToString());

    	    string url = System.Configuration.ConfigurationManager.AppSettings["ApplicationUrl"] + "/graphql?query={actions(filter:{requestNumber: "+CURRENT_REQUEST.Text+"}){items{number closedAt name assignee {lastName firstName userName }}}}";
            
             // Create HttpWebRequest
    	    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.Credentials = new System.Net.NetworkCredential("wfgen_admin","password");
            // Get Response from GraphQL
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse ();
            // Read Response
            System.IO.Stream receiveStream = response.GetResponseStream();
            System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, Encoding.UTF8);
            // Convert response to JSON object
            dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(readStream.ReadToEnd());
            this.FormData.Tables["HISTORY_ACTION"].Rows.Clear();
           // Populate the table using the dynamic JSON object.
            foreach (var item in obj.data.actions.items){
                      System.Data.DataRow newRow = this.FormData.Tables["HISTORY_ACTION"].NewRow();
                      newRow[0] = item.name;
                      newRow[1] = "By " + item.assignee.firstName + " " + item.assignee.lastName + " on " + string.Format("{0:f}", item.closedAt.ToString());
                      this.FormData.Tables["HISTORY_ACTION"].Rows.Add(newRow);
                  
              }
              
              
              this.FormData.Tables["HISTORY_ACTION"].AcceptChanges();
        	  SaveFormData(this.FormData);
        	  BindFormDataToFields(this.FormData);
	    }
        
	}

Note: In order to encapsulate the username/password, it is recommended to use assemblies to make the GraphQL calls. This is just a simple example to show you how it can be used.