Use GraphQL to fetch instance data

Use the instanceFetch file to fetch input data for the form, depending on the form type.

Overview

The instanceFetch file in the mobile extension instructs the backend service to get the input data required by the form. If the form is a Job form, which is displayed in the Job Details screen, then the data that is fetched is specific to the job. This means that the data differs depending on the job that you open the form in. If the form is a Global form, which is displayed in the More screen, then the data that is fetched is specific to the resource.

Generate GraphQL in instanceFetch

Note that the following example is in JSON format, not GraphQL. However, the root of the JSON object must be: "type": "GraphQl"

{
  "type": "GraphQl",
  "JobProducts": {
    "object": "JobProducts",
    "fields": [
      "UID",
      "ProductId",
      "ProductName",
      "Qty",
      "CreatedDate",
      "JobId",
      "Product.UID",
      "Product.Description",
      "Product.Name"
    ],
    "filter": "JobId == '${var1}'",
    "variables": {
      "var1": "$it.UID"
    }
  }
}

Then each sibling key, which can any name, is a GraphQL call. In the example that follows, instanceData has the key JobProducts and return a list of JobProducts objects from the database.

"JobProducts": {
  "object": "JobProducts", 
  "fields": [ 
    "UID",
    "ProductId",
    "ProductName",
    "Qty",
    "CreatedDate",
    "JobId",
    "Product.UID",
    "Product.Description",
    "Product.Name"
  ],
  "filter": "JobId == '${JobId}'", 
  "orderBy": "CreatedDate DESC" 
  "variables": {
    "JobId": "$it.UID"
  }
}
Element Description
object The object in the Skedulo schema that is being queried.
fields The list of fields you want to fetch for that object. You can get the fields for related objects as well.
filter A filter to apply to the query; you can get the fields for the object you are querying or a related object’s fields.
orderBy The way that the the data must be ordered when returned.
limit The number of objects that will be returned. Note that it must be combined with orderBy. If the limit = 1, then the final data will be an object {} instead of returning an array [].
variables The list of variables to be used inside filter.
job To obtain job fields. This only works in forms that have the contextObject Jobs.
res To obtain resource fields.
user To obtain user fields.

Example: The final data returned from running the above query for a Job

{
  "JobProducts": [
    {
      "JobId": "a0P5i00000JjgekEAB",
      "UID": "a0L5i000005fRWTEA2",
      "ProductId": "01t5i000003qq93AAA",
      "Qty": 5,
      "__typename": "JobProducts",
      "ProductName": "Sharps 1.4L",
      "CreatedDate": "2023-07-12T06:04:05.000Z",
      "Product": {
        "Name": "Sharps 1.4L",
        "__typename": "Products",
        "Description": null,
        "UID": "01t5i000003qq93AAA"
      }
    },
    {
      "ProductName": "White Manual Hand Sanitiser",
      "UID": "a0L5i000005fSGIEA2",
      "ProductId": "01t5i000003qq97AAA",
      "Product": {
        "Description": null,
        "UID": "01t5i000003qq97AAA",
        "__typename": "Products",
        "Name": "White Manual Hand Sanitiser"
      },
      "Qty": 12,
      "__typename": "JobProducts",
      "JobId": "a0P5i00000JjgekEAB",
      "CreatedDate": "2023-07-12T08:32:09.000Z"
    },
    {
      "CreatedDate": "2023-07-12T08:33:24.000Z",
      "JobId": "a0P5i00000JjgekEAB",
      "UID": "a0L5i000005fSGREA2",
      "Qty": 2,
      "__typename": "JobProducts",
      "Product": {
        "__typename": "Products",
        "Name": "Descale WC",
        "Description": null,
        "UID": "01t5i000003qq98AAA"
      },
      "ProductName": "Descale WC",
      "ProductId": "01t5i000003qq98AAA"
    },
    {
      "ProductId": "01t5i000003qq99AAA",
      "CreatedDate": "2023-07-13T04:02:38.000Z",
      "ProductName": "PCO Deltamethrin (MLS) 10g\/L Deltamethrin",
      "__typename": "JobProducts",
      "JobId": "a0P5i00000JjgekEAB",
      "Qty": 15,
      "UID": "a0L5i000005fUbMEAU",
      "Product": {
        "UID": "01t5i000003qq99AAA",
        "__typename": "Products",
        "Description": "PCO Deltamethrin (MLS) 10g\/L Deltamethrin",
        "Name": "PCO Deltamethrin (MLS) 10g\/L Deltamethrin"
      }
    }
  ]
}

Saving

If you follow the GraphQL method, then you do not need to worry about how saving works on the back end, as our services take care of any changes and make corresponding GraphQL calls to update the data.

Data that the MEX engine sends to the backend for processing is identical in structure to the data that the backend sends to the MEX engine

For example, this is data that the backend sends to the frontend:

[{
  "UID": "1",
  "Name": "This is not a name"
}]

When the MEX engine processes the data and shows it in the UI, if the user taps the back button, then the data will look as follows:

[{
  "UID": "1",
  "Name": "This is not a name (EDITED)"
}]

When processing, the backend will understand that the field name “Name“ has been changed for UID: “1“, for the object, and will send a corresponding GraphQL request to update the data.

If later, the MEX engine sends another payload like this:

[{
  "UID": "1",
  "Name": "This is not a name (EDITED)"
},
{
  "UID": "2",
  "Name": "This is not a name (EDITED)"
}]

When processing, the backend will understand that the object UID: 2 has been added and will make a graphQL request to insert that data.

If later the Mex Engine send another payload like this again

[{
  "UID": "1",
  "Name": "This is not a name (EDITED)"
}]

When processing, the backend will understand that the object UID: 2 has been removed. And will make a graphQL request to remove that data.