GraphQL is a query language for API developed by Facebook and recently adopted by many other services. Basically it should allow you to query just the required data from the API – and have full control over the response JSON.
Let's have an example of a simple GraphQL query in Postman:
$pageId:Int
) you can set their value in Variables window.data
node and the structure is always defined by the query structure.So how to send this query with RESTwithUS? Well, before we go there, let's have a quick look under the hood and see, what really happens during this GraphQL request:
This is the same request sent to the GraphQL API, but this time we are sending it as a raw JSON. Notice the request structure:
query
node. Please note that for this to work you need to delete all line breaks in the query.variables
node.Now that we know how to send the GraphQL query as a simple JSON request, we can do the same in RESTwithUS.
First create an API provider (and set up authentication if needed):
Then create new POST operation and point it to the GraphQL endpoint (e.g. if you have multiple GraphQL operations, they will all have same Endpoint):
Open the operation Request Body, use function Payload / Import Payload and paste an example of the JSON request there:
This will prepare an empty schema for you:
Let's now make two simple changes:
query
node and paste the GraphQL query there. Don't forget to remove all line breaks before pasting the query!{{pageId}}
into the pageId
node.Now open the operation Response Body and use function Payload / Import Payload to import the response JSON schema:
We will leave it blank for now but you can map it to Business Central tables and fields if needed.
Let's test the operation and get a Wiki page with id 1
:
var
APIScriptRWU: Codeunit "API Script RWU";
APIScriptRWU.INIT('WIKI_GRAPHQL_GET_PAGE'); //Init the operation and set Batch Code
APIScriptRWU.ENDPOINT('Wiki GraphQL','Get Page'); //Select the provider and operation
APIScriptRWU.ADD_VARIABLE('/variables/pageId','pageId',1); //Assign the page ID to the {{pageId}} variable
APIScriptRWU.EXECUTE(); //Start the operation
Tip: For more API Script functions see guideline API Script Functions Reference.