GameMaker [SOLVED] HTTP GET/POST Help for GraphQL

Posho

Member
Hello,

I am trying to get access to an API using HTTP functions. Normally you would make queries using JSON files but apparently GM:S forcefully uses ds_maps, which is a tad different.

I am also having trouble sending the Auth Token, which from my understanding is done through the Header of a request.

Dunno, how does one generally approach this in GameMaker: Studio 2? :confused:

[Edit] I am using GraphQL and I'm having trouble parsing the requests as strings.
 
Last edited:

Binsk

Member
I believe this is two fold.

First, check out this page in the manual. This will format the map for you containing the headers you need.

Second, JSON is just text formatted with specific rules. If it is just text then it can be passed as a string. GameMaker has a way to decode/encode JSON and any JSON parsing on your server has nothing to do with GameMaker.

Note that the GameMaker JSON encoding takes a map and returns a string, which you then can use in your query.

I don't know if GameMaker's json encoder handles nested maps or maps with arrays etc. but that is something you can easily test with a show_message. I know you can use ds_map_add_map which makes me assume you can nest them.

Edit: Also remember that a map is just a 'dictionary' in other languages. JSON uses dictionaries and arrays so it is perfectly logical for GameMaker to be using maps.
 

Posho

Member
I believe this is two fold.

First, check out this page in the manual. This will format the map for you containing the headers you need.

Second, JSON is just text formatted with specific rules. If it is just text then it can be passed as a string. GameMaker has a way to decode/encode JSON and any JSON parsing on your server has nothing to do with GameMaker.

Note that the GameMaker JSON encoding takes a map and returns a string, which you then can use in your query.

I don't know if GameMaker's json encoder handles nested maps or maps with arrays etc. but that is something you can easily test with a show_message. I know you can use ds_map_add_map which makes me assume you can nest them.

Edit: Also remember that a map is just a 'dictionary' in other languages. JSON uses dictionaries and arrays so it is perfectly logical for GameMaker to be using maps.
Thank you for replying.

I have already read that page and a bunch of others related to HTTP and Async functions. I've also made some reading about how to decode JSON files as ds_maps and how to retrieve values from there.

My issue here is that I am working with GraphQL (forgot to mention that) and I'm having trouble parsing requests as strings.
 

Posho

Member
Okay so I managed to make this work and it took me way, way too much fiddling. So I'm writing a short guide for the one lonely soul that will want to make GraphQL queries using GameMaker: Studio, lol.

For this example I'll use the Smash.gg API, which has the following endpoint:
https://api.smash.gg/gql/alpha

And we're using the "Tournaments by Owner" request in particular. In the website it appears like this:
JSON:
query TournamentsByOwner($perPage: Int!, $ownerId: ID!) {
    tournaments(query: {
      perPage: $perPage
      filter: {
        ownerId: $ownerId
      }
    }) {
    nodes {
      id
      name
      slug
    }
  }
},
{
  "ownerId": 161429,
  "perPage": 4
}
Now we need to parse this as an URL so we can http_request() it in GameMaker: Studio.
  1. Remove the first line up to the first curly brace ('{').
  2. Insert the endpoint at the very beginning, followed by ?query=.
  3. Replace whatever has a '$' with the values on the bottom (remove the '$' as well).
  4. Remove the bottom inputs, starting from the comma (',').
  5. Add commas (',') after each object that has another after it.

The result URL should look like this:
Code:
https://api.smash.gg/gql/alpha?query=
    {
        tournaments(query: {
          perPage: 4,
          filter: {
            ownerId: 161429
          }
        }) {
        nodes {
          id,
          name,
          slug
        }
      }
    }
So now that we have our URL, we're going to go ahead and do the following in GameMaker: Studio... Whenever you want to trigger the request you do:
  1. Create a ds_map that will work as the header of your request. In this case we're putting the Auth Key (which I'm not providing, sorry) in it.
  2. Paste the URL we wrote above. Use an '@' before the quotation marks to be able to type it as a multi line string.
  3. The body goes empty in this case.
  4. Trigger the http_request() with the provided info.
GML:
    var map = ds_map_create();
    ds_map_add(map, "Authorization", "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    
    var url =
    @"https://api.smash.gg/gql/alpha?query=
    {
        tournaments(query: {
          perPage: 4,
          filter: {
            ownerId: 161429
          }
        }) {
        nodes {
          id,
          name,
          slug
        }
      }
    }"
    
    http_request(url, "GET", map, "");
Now we go to the Async HTTP event and type:
json = async_load[? "result"];
This puts our received info into a string which we can then turn into a ds_map to use the values as we please with:
map = json_decode(json);


Easy as cake.
 
Top