{"id":1141,"date":"2021-11-28T21:59:29","date_gmt":"2021-11-28T19:59:29","guid":{"rendered":"https:\/\/www.rocworks.at\/wordpress\/?p=1141"},"modified":"2023-05-01T20:56:56","modified_gmt":"2023-05-01T18:56:56","slug":"execute-a-graphql-query-in-unity-with-c-code","status":"publish","type":"post","link":"https:\/\/www.rocworks.at\/wordpress\/?p=1141","title":{"rendered":"GraphQL for Unity and  execute a GraphQL Query in Unity with C# Code"},"content":{"rendered":"\n<p>With <a href=\"https:\/\/u3d.as\/2D81\">GraphQL for Unity<\/a> you can execute GraphQL Queries in a Unity way with GameObjects. But with the asset you can also execute queries in Unity with C# code. <\/p>\n\n\n\n<p>Here is a simple example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public GraphQL Connection;\n\npublic void ScriptQuery()\n    {\n        var query = \"query($Token: String!) { doit($Token) { result } } \";\n        var args = new JObject\n        {\n            { \"Token\", \"123\" }\n        };\n        Connection.ExecuteQuery(query, args, (result) =&gt;\n        {\n            Debug.Log(result.Result.ToString());\n        });\n    }<\/code><\/pre>\n\n\n\n<p>Link the Connection variable to your GraphQL GameObject where the connection is set.<\/p>\n\n\n\n<p>Note: the result callback function is called asynchronous and it is not executed in the Game-loop.<\/p>\n\n\n\n<p>A bit more complex example how the args variable could look like. This is just an example how you can create a JSON object in C# in a convenient way. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JObject args = new JObject\n        {\n            &#91;\"UpdateList\"] = new JArray\n            {\n                new JObject\n                {\n                    &#91;\"id\"] = 28,\n                    &#91;\"updateAction\"] = \"EDIT\",\n                    &#91;\"status\"] = \"Present\"\n                },\n                new JObject\n                {\n                    &#91;\"id\"] = 29,\n                    &#91;\"updateAction\"] = \"EDIT\",\n                    &#91;\"status\"] = \"Absent\"\n                },\n                new JObject\n                {\n                    &#91;\"id\"] = 30,\n                    &#91;\"updateAction\"] = \"EDIT\",\n                    &#91;\"status\"] = \"Present\"\n                }\n            }\n        };<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>With GraphQL for Unity you can execute GraphQL Queries in a Unity way with GameObjects. But with the asset you can also execute queries in Unity with C# code. Here is a simple example: Link the Connection variable to your GraphQL GameObject where the connection is set. Note: the result callback function is called asynchronous &hellip; <a href=\"https:\/\/www.rocworks.at\/wordpress\/?p=1141\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">GraphQL for Unity and  execute a GraphQL Query in Unity with C# Code<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-1141","post","type-post","status-publish","format-standard","hentry","category-graphql"],"_links":{"self":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1141"}],"version-history":[{"count":5,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions"}],"predecessor-version":[{"id":1288,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions\/1288"}],"wp:attachment":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}