-
Notifications
You must be signed in to change notification settings - Fork 1
/
MakeGraphqlRequest.cs
85 lines (79 loc) · 2.69 KB
/
MakeGraphqlRequest.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
// Example: Make an arbitrary GraphQL request to the Anvil API
//
// Feel free to copy and paste queries and mutations from the GraphQL reference
// docs into the functions in this script.
//
// * GraphQL guide: https://www.useanvil.com/docs/api/graphql
// * GraphQL ref docs: https://www.useanvil.com/docs/api/graphql/reference
// * Anvil C#/.NET client: https://www.nuget.org/packages/Anvil/
//
// This example is runnable as is, all you need to do is supply your own API key
// in the ANVIL_API_KEY environment variable.
//
// ANVIL_API_KEY=<yourAPIKey> dotnet run make-graphql-request
using Anvil.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace AnvilExamples.examples;
class MakeGraphqlRequest : RunnableBaseExample
{
public static async Task<JObject> CallCurrentUserQuery(GraphQLClient client)
{
var query = @"query CurrentUser {
currentUser {
eid
name
organizations {
eid
slug
name
casts {
eid
name
}
welds {
eid
name
}
}
}
}";
return await client.SendQuery(query, null);
}
static async Task<JObject> CallWeldQuery(GraphQLClient client, string weldEid)
{
var query = @"
query WeldQuery (
$eid: String,
) {
weld (
eid: $eid,
) {
eid
name
forges {
eid
slug
name
}
}
}";
return await client.SendQuery(query, new {eid = weldEid});
}
/**
* Main method that gets run from the `Program.cs` entrypoint.
*
* `apiKey` is provided through the `ANVIL_API_KEY` environment variable.
*/
public override async Task Run(string apiKey)
{
Console.WriteLine("Make request");
var client = new GraphQLClient(apiKey);
var userResponse = await CallCurrentUserQuery(client);
var userJson = JsonConvert.SerializeObject(userResponse);
Console.WriteLine("Current user\n" + userJson);
var firstWeld = userResponse["currentUser"]["organizations"][0]["welds"][0];
var weldResponse = await CallWeldQuery(client, (string) firstWeld["eid"]);
Console.WriteLine("First weld details:\n" + weldResponse);
}
}