Doing mutations with GraphQL using C#


#1

Description

In GraphQL, the HTTP GET method is supported on queries only. The HTTP POST method is supported on queries and operations (mutations).

GraphQL will first search for the parameters in the URL’s query string. If not found in the query string, it will look in the POST request body. If the POST body has not been parsed, GraphQL will interpret it depending on the provided Content-Type header:

  • application/json : the POST body will be parsed as a JSON object of parameters.

  • application/x-www-form-urlencoded : this POST body will be parsed as a url-encoded string of key-value pairs.

  • application/graphql : the POST body will be parsed as GraphQL query string, which provides the query parameter.

Solution

Below is an example that uses the application/json Content-Type.
The code performs a mutation where it modifies the value of a data of a specific request:

 string APIUsername = "USERNAME";
 string APIPassword = "PASSWORD";
 String url = System.Configuration.ConfigurationManager.AppSettings["ApplicationUrl"] + "/graphql";
 var request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(url);
	  
 var postData = "{\"query\":\"mutation { updateRequestDataset(input: {number:123, parameters:[{name:\\\"DATA_FIELD\\\",textValue:\\\""+FORM_FIELD_ID.Text+"\\\"}] }) { clientMutationId } }\"}";
 var data = Encoding.ASCII.GetBytes(postData);
	  
 request.Method = "POST";
 request.ContentType = "application/json";
 request.ContentLength = data.Length;
 request.Credentials = new System.Net.NetworkCredential(APIUsername, APIPassword);
	  
 using (var stream = request.GetRequestStream()){
   stream.Write(data, 0, data.Length);
 }

 var response = (System.Net.HttpWebResponse) request.GetResponse();
	  
 var responseString = new 
 System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();

For more information regarding GraphQL Integration:

https://docs.advantys.com/workflowgen-integration-guide/graphql


Custom Column (in view) access to User Profile data through javascript