GraphQL for Unity and how to set headers in code…

If you use the GraqhQL for Unity asset and if you want to set the headers on HTTP or Websockets in the code, then you must use the the GraphQLHttp or GraphQLWebsockets class. At the base class “GraphQL” there is no “Headers” property!

public GraphQLHttp MyGraphQL;

void Start()
{        
    if (MyGraphQL != null)
    {
        MyGraphQL.Headers = new List<Header>()
        {
            new Header() { Key = "token", Value = "secret"}
        };
        ...

Then you can also execute a GraphQL query in the code.

MyGraphQL.ExecuteQuery("query { test }", null, (message) =>
{
    if (message.Type == MessageType.GQL_DATA)
    {
        Debug.Log("Data: " + message.Result.ToString());
    }
    else
    {
        Debug.Log("Error: " + message.Type);
    }
});