Marketplaces / Tesco (on Marketplacer) / Graph QL

Graph QL

Marketplacer is a tool which uses Graph QL for their API. More about Graph QL can be read here but in short - it allows us to query for the resources we need in a way it is convenient for us to work with. For example, we can get only orders or orders with order items. Or even order with their refunds or shipments. (The last example is not convenient for us but, still, we could do it if we wanted to).

This is an example request (Create listing) which we need to be able to send:

On the left side is the Graph QL part which is simply a description of the response which we want to receive. In this case we’d like to receive the status of the “Advert”, advert’s id and legacyId and a list of errors.

On the right side, we’ve got the variables which is a json we all know how to construct with the current cmc.

The left and the right side are concatenated and sent as a json body this way:

{
    "query":"mutation AdvertCreate($input: AdvertUpsertMutationInput!) {
        advertUpsert(input: $input) {
            status
            advert {
                id
                legacyId
            }
            errors {
                field
                messages
            }
        }",
    "variables": {
    "input": {
      "attributes": {
        "brandId": "QnJhbmQtNA==",
        "taxonId": "VGF4b24tMjk2OQ==",
        "title": "Product Title",
        "description": "Product Description",
        "price": "199.99",
        "attemptAutoPublish": true,
        "images": [
          {
            "sourceUrl": "https://picsum.photos/640/480/?image=166"
          }
        ],
        "advertOptionValues": [
          {
            "optionValueId": "T3B0aW9uVmFsdWUtNzI3"
          }
          ...
        ],
        ...
      }
    }
  }
}

There are two approaches which we could take in order to make this kind of requests:

  1. Simpler, easier and faster solution
  2. Cooler, more interesting and more time-consuming solution

Simpler, easier and faster solution

Here we could construct the Graph QL part once manually and just paste it as a string into the body of our Request class, then concatenate/replace the generated variables json from our Map to the “variables” part. This is possible because the Graph QL part is not much likely to change while the variables part is the one which must be constructed dynamically for each request (listing in this case).

This solution does not require additional work into CMC.

Cooler, more interesting and more time-consuming solution

This will be a more challenging solution and here I will give you only a brief idea of a possible solution - here we could create the Graph QL part by using the response map’s keys. It will certainly require additional work around the Map and MapValue classes and a custom PayloadBuilder which works only with Graph QL type of requests.

This solution requires additional work into CMC.

Is this article helpful?
0 0 0