Examples

In this article:


    Attention   

     

    In the following examples, we assume a shop that has a total of 42 orders.

     

    Fetching orders

    If we want to get the ID, language, site ID and order comment for all orders in the shop, we can use the following query:

    POST shop99999.mywebshop.io/api/graphql
    (Replace "shop99999" with your own shop number)

    
    query {
      orders {
        data {
          id
          languageISO
          siteId
          orderComment
        }
      }
    }
    

    Response

    
    {
      "data": {
        "orders": {
          "data": [
            {
              "id": "1",
              "languageISO": "DK",
              "siteId": "1"
            },
            ...,
            {
              "id": "42",
              "languageISO": "DK",
              "siteId": "1"
            }
          ]
        }
      }
    }

     

     


    Using pagination

    In order to limit the number of results returned, we can supply pagination options. The following query demonstrates fetching at most 2 orders per page, and getting results from page 3.

    POST shop99999.mywebshop.io/api/graphql

    
    query {
      orders (pagination:{
        limit: 2,
        page: 3
      }) {
        pagination {
          currentPage
          from
          perPage
          to
          total
        }
        data {
          id
          languageISO
          siteId
        }
      }
    }
    

    Response

    
    {
      "data": {
        "orders": {
          "pagination": {
            "currentPage": 3,
            "from": 5,
            "perPage": 2,
            "to": 6,
            "total": 6
          },
          "data": [
            {
              "id": "8",
              "languageISO": "DK",
              "siteId": "1"
            },
            {
              "id": "9",
              "languageISO": "DK",
              "siteId": "1"
            }
          ]
        }
      }
    }

     

     


    Using sorting

    It is also possible to sort the returned results according to a specified field and in the given order:

    POST shop99999.mywebshop.io/api/graphql

    
    query {
      orders (order:{
        field: id,
        direction: DESC
      }) {
        data {
          id
        }
      }
    }
    

    Response

    
    {
      "data": {
        "orders": {
          "data": [
            {
              "id": "42"
            },
            {
              "id": "41"
            },
            ...,
            {
              "id": "1"
            }
          ]
        }
      }
    }

     

     


    Using search

    Another way of limiting the results returned is by using search parameters. The following example shows how to get all orders whose IDs are less than 3 or greater than 41.

    POST shop99999.mywebshop.io/api/graphql

    
    query {
      orders (search:[
        {
          field: id,
          comparator: LESS_THAN
          value:"3"
        },
        {
          field: id,
          comparator: GREATER_THAN
          value:"41",
          operator:OR
        }
      ]) {
        data {
          id
        }
      }
    }
    

    Response

    
    {
      "data": {
        "orders": {
          "data": [
            {
              "id": "1"
            },
            {
              "id": "2"
            },
            {
              "id": "42"
            }
          ]
        }
      }
    }

     

     


    Combining it all

    We can combine all of the above into a single query:

    POST shop99999.mywebshop.io/api/graphql

    
    query {
      orders(
        pagination:{limit:3,page:2}
        order:{field:id, direction:DESC}
        search: [{field: id, comparator: GREATER_THAN, value: "30"}]
      ) {
        data {
          id
          createdAt
        }
      }
    }
    

    Response

    
    {
      "data": {
        "orders": {
          "data": [
            {
              "id": "39",
              "createdAt": "2019-04-08T09:09:44+00:00"
            },
            {
              "id": "38",
              "createdAt": "2019-04-08T08:36:43+00:00"
            },
            {
              "id": "37",
              "createdAt": "2019-04-05T14:10:05+00:00"
            }
          ]
        }
      }
    }

     

    .NET/C# example

    
        using System;
        using System.Threading.Tasks;
        using System.Text.Json;
        using System.Net.Http.Headers;
        using System.Net.Http;
    
        // This example uses https://github.com/graphql-dotnet/graphql-client
        using GraphQL;
        using GraphQL.Client.Http;
    
        namespace GraphQlClientDemo
        {
            class Program
            {
                static void Main(string[] args)
                {
                    MainAsync().Wait();
                }
    
                static async Task MainAsync()
                {
                    var client = new System.Net.Http.HttpClient();
    
                    // Check https://webshop-help.dandomain.dk/authentication/ for information about how to obtain the access token
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "<client token>");
    
                    // Replace "shop99999" with your own shop number
                    var options = new GraphQLHttpClientOptions()
                    {
                        EndPoint = new Uri("https://shop99999.mywebshop.io/api/graphql")
                    };
    
                    // Set up the client
                    var graphQLClient = new GraphQLHttpClient(options, client);
    
                    // Prepare a request to fetch all orders with id, languageISO and siteId
                    var ordersRequest = new GraphQLRequest
                    {
                      Query = @"
                        query {
                          orders {
                            data {
                              id
                              languageISO
                              siteId
                            }
                          }
                       }"
                    };
    
                    // Send the request and store the response as an OrderResponse object
                    var graphQLResponse = await graphQLClient.SendQueryAsync<OrderResponse>(ordersRequest);
    
                    // Write out the response in the console
                    Console.WriteLine(JsonSerializer.Serialize(graphQLResponse, new JsonSerializerOptions { WriteIndented = true }));
    
                    // Wait for a key to be pressed before closing down
                    Console.WriteLine("Press any key to quit...");
                    Console.ReadKey();
                }
            }
    
            public class OrderResponse
            {
                public Orders orders { get; set; }
    
                public class Orders
                {
                public Data[] data { get; set; }
                }
    
                public class Data
                {
                    public string Id { get; set; }
                    public string LanguageISO { get; set; }
                    public int SiteId { get; set; }
                }
            }
        }