-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAPIClient.cs
113 lines (93 loc) · 2.56 KB
/
APIClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using GraphQL;
public class APIClient : MonoBehaviour {
//not a real api url
string apiUrl = "https://graphqlapi.com/queries";
string query = @"query{
organizations {
id
}
}";
string mutation = @"mutation AddMember($input: RegistrationInput!) {
registration( input: $input ) {
contact {
name
email
}
}
}";
[System.Serializable]
private class Member {
public string name;
public string email;
}
public IEnumerator QueryCall (System.Action<bool> callback) {
GraphQLClient client = new GraphQLClient (apiUrl);
using( UnityWebRequest www = client.Query(query, "", "")) {
yield return www.Send();
if (www.isError) {
Debug.Log (www.error);
callback (false);
} else {
string responseString = www.downloadHandler.text;
JSONObject response = new JSONObject (responseString);
JSONObject data = response.GetField ("data");
JSONObject organizations = data.GetField ("organizations");
accesData( organizatios )
callback (true);
}
}
}
public IEnumarator MutationCall () {
GraphQLClient client = new GraphQLClient (apiUrl);
Member memberInput = new Member () {
name = "Bob Jones",
email = "[email protected]"
};
string variables = @"{""input"": "+ JsonUtility.ToJson(memberInput) +" }";
using( UnityWebRequest www = client.Query(mutation, variables, "AddMember")) {
yield return www.Send();
if (www.isError) {
Debug.Log (www.error);
// handle error
} else {
string responseString = www.downloadHandler.text;
JSONObject reponse = new JSONObject (responseString);
// handle json result with JSONObject
}
}
}
void accessData(JSONObject obj){
switch(obj.type){
case JSONObject.Type.OBJECT:
for(int i = 0; i < obj.list.Count; i++){
string key = (string)obj.keys[i];
JSONObject j = (JSONObject)obj.list[i];
Debug.Log(key);
accessData(j);
}
break;
case JSONObject.Type.ARRAY:
foreach(JSONObject j in obj.list){
accessData(j);
}
break;
case JSONObject.Type.STRING:
Debug.Log(obj.str);
break;
case JSONObject.Type.NUMBER:
Debug.Log(obj.n);
break;
case JSONObject.Type.BOOL:
Debug.Log(obj.b);
break;
case JSONObject.Type.NULL:
Debug.Log("NULL");
break;
}
}
}