Notifications
graphQL-client-unity
Showcase
89
0
It is an open source tool that creates an easy way of making queries and mutations to a graphQL server from Unity. While working on the API for one of my games, I used GraphQL. But when trying to communicate with this API from Unity, I found out it was quite difficult to make queries and mutations. It was also very difficult to insert variables as input parameters within this queries and mutations. Hence this graphQL client for Unity.

Motivation

While working on the API for one of my games, I used GraphQL. But when trying to communicate with this API from Unity, I found out it was quite difficult to make queries and mutations. It was also very difficult to insert variables as input parameters within this queries and mutations. Hence this graphQL client for Unity.

How it works

This script leverages Unity's WWW class to make a POST request to the server's endpoint. Since it's a graph API, it only has one endpoint. So with the url of that endpoint, it sends a POST request. The body of the POST request is the query. Behind the scenes, some functions work on the query sent as the body of the POST request and this functions allow the use of variables and arrays as input parameters with the query or mutation. After the POST request is made, a coroutine is started which waits until the data returned from the query is collected and this is stored in a variable. There is also a delegate that functions can subscribe to so that when the data is received, the function is called and things can be done with the query data stored. [SimpleJSON](http://wiki.unity3d.com/index.php/SimpleJSON) or other methods can then be used to parse the JSON results gotten from the query.
## Example
Within the github repository, there's a unitypackage that can be imported into your project and it has an example scene. The Unity project folder of the example is also available within the repository. This example does the basic functionality of querying the graph API created by [Lucas Bento](https://github.com/lucasbento) which is a server that gives information about different Pokemon and its stats [GraphQL Pokemon](https://github.com/lucasbento/graphql-pokemon). It allows you to display information about any Pokemon you put in the inspector in Unity. Then Parses the JSON result using [SimpleJSON](http://wiki.unity3d.com/index.php/SimpleJSON) and displays information about the Pokemon.

Olutade Ajiboye
VR Developer - Programmer
8
Comments