Skip to content

Commit 4d6eda0

Browse files
committed
Add Unit test to explicitly validate use of Fragments for common fields, etc.
1 parent dc2abc1 commit 4d6eda0

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
6+
7+
namespace FlurlGraphQL.Querying.Tests
8+
{
9+
[TestClass]
10+
public class FlurlGraphQLMutationTests : BaseFlurlGraphQLTest
11+
{
12+
13+
[TestMethod]
14+
public async Task TestMutationWithQueryResultsAsync()
15+
{
16+
var inputPayload = JArray.Parse(@"
17+
[{
18+
""eventId"": 23,
19+
""eventUUID"": ""c5cd1cca-fe4d-490d-a948-985023c6185c"",
20+
""name"": ""RÜFÜS DU SOL"",
21+
""eventType"": ""ONE_OFF"",
22+
""status"": ""APPROVED"",
23+
""eventDate"": ""2018-11-01T07:00:00"",
24+
""announceDate"": null,
25+
""onSaleDate"": null,
26+
""doorTime"": null,
27+
""newElvisVenueId"": 10811,
28+
""internalBudget"": 15000.00000000,
29+
""externalBudget"": 15000.00000000,
30+
""budgetCurrencyCode"": ""USD"",
31+
""budgetExchangeRate"": 1.000000,
32+
""bookerDescription"": null,
33+
""companyMasterId"": 1,
34+
""subledger"": ""H6367996"",
35+
""genreDescription"": ""Dance / Electronic / DJ / Techno / House / Trance"",
36+
""notes"": null,
37+
""headlinerArtists"": [{
38+
""artistMasterId"": 0,
39+
""artistOrdinalSortId"": 1
40+
}
41+
],
42+
""supportingArtists"": [{
43+
""artistMasterId"": 0,
44+
""artistOrdinalSortId"": 1
45+
}
46+
],
47+
""eventContacts"": null,
48+
""shows"": [{
49+
""showId"": 101,
50+
""showName"": ""Show #3"",
51+
""showOrdinalSortId"": 3,
52+
""showDate"": ""2018-11-03T07:00:00"",
53+
""announceDate"": null,
54+
""onSaleDate"": null,
55+
""doorTime"": null
56+
}, {
57+
""showId"": 102,
58+
""showName"": ""Show #2"",
59+
""showOrdinalSortId"": 2,
60+
""showDate"": ""2018-11-02T07:00:00"",
61+
""announceDate"": null,
62+
""onSaleDate"": null,
63+
""doorTime"": null
64+
}, {
65+
""showId"": 103,
66+
""showName"": ""Show #1"",
67+
""showOrdinalSortId"": 1,
68+
""showDate"": ""2018-11-01T07:00:00"",
69+
""announceDate"": null,
70+
""onSaleDate"": null,
71+
""doorTime"": null
72+
}
73+
],
74+
""createdDate"": ""2018-11-02T21:13:34"",
75+
""lastUpdatedDate"": ""2020-06-11T10:00:12""
76+
}]
77+
");
78+
79+
var mutationResult = await "http://localhost:7072/api/v1/graphql?code=Tsk4dixKihdUyRlvlcDu1dHETHYIiCPpLawk%2F27apOsRTr1LbhF7vw%3D%3D"
80+
.WithGraphQLQuery(@"
81+
mutation ($eventInputArray: [EventCreateOrUpdateInput]) {
82+
eventsCreateOrUpdate(input: $eventInputArray) {
83+
eventResults {
84+
eventUUID
85+
eventId
86+
}
87+
errors {
88+
... on Error {
89+
errorCode
90+
message
91+
}
92+
}
93+
}
94+
}
95+
")
96+
.SetGraphQLVariables(new { eventInputArray = inputPayload })
97+
.PostGraphQLQueryAsync()
98+
.ReceiveGraphQLMutationResult<EventsCreateOrUpdateResult>()
99+
.ConfigureAwait(false);
100+
101+
102+
Assert.IsNotNull(mutationResult);
103+
Assert.IsTrue(mutationResult.EventResults.Length > 0);
104+
105+
var jsonText = JsonConvert.SerializeObject(mutationResult, Formatting.Indented);
106+
TestContext.WriteLine(jsonText);
107+
}
108+
}
109+
110+
public class EventsCreateOrUpdateResult
111+
{
112+
public EventResult[] EventResults { get; set; }
113+
}
114+
115+
public class EventResult
116+
{
117+
public Guid? EventUUID { get; set; }
118+
public int? EventId { get; set; }
119+
}
120+
}

FlurlGraphQL.Tests/FlurlGraphQLQueryingSimplePostTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,66 @@ public async Task TestSimplePostSingleQueryDirectResultsAsync()
4646
TestContext.WriteLine(jsonText);
4747
}
4848

49+
[TestMethod]
50+
public async Task TestSimplePostSingleQueryDirectResultsUsingFragmentsAsync()
51+
{
52+
var results = await GraphQLApiEndpoint
53+
.WithGraphQLQuery(@"
54+
query ($ids: [Int!], $friendsCount: Int!) {
55+
charactersById(ids: $ids) {
56+
...commonFields
57+
appearsIn
58+
height
59+
friends(first: $friendsCount) {
60+
nodes {
61+
...commonFields
62+
}
63+
}
64+
}
65+
}
66+
67+
fragment commonFields on Character {
68+
personalIdentifier
69+
name
70+
}
71+
")
72+
.SetGraphQLVariables(new { ids = new[] { 1000, 2001 }, friendsCount = 2 })
73+
.PostGraphQLQueryAsync()
74+
.ReceiveGraphQLQueryResults<StarWarsCharacter>()
75+
.ConfigureAwait(false);
76+
77+
Assert.IsNotNull(results);
78+
Assert.AreEqual(2, results.Count);
79+
80+
var char1 = results[0];
81+
Assert.IsNotNull(char1);
82+
Assert.AreEqual(1000, char1.PersonalIdentifier);
83+
Assert.AreEqual("Luke Skywalker", char1.Name);
84+
Assert.IsTrue(char1.Height > (decimal)1.5);
85+
Assert.AreEqual(2, char1.Friends.Count);
86+
char1.Friends.ForEach(f =>
87+
{
88+
Assert.IsTrue(!string.IsNullOrWhiteSpace(f.Name));
89+
Assert.IsTrue(f.PersonalIdentifier > 0);
90+
});
91+
92+
93+
var char2 = results[1];
94+
Assert.IsNotNull(char2);
95+
Assert.AreEqual(2001, char2.PersonalIdentifier);
96+
Assert.AreEqual("R2-D2", char2.Name);
97+
Assert.IsTrue(char2.Height > (decimal)1.5);
98+
Assert.AreEqual(2, char2.Friends.Count);
99+
char2.Friends.ForEach(f =>
100+
{
101+
Assert.IsTrue(!string.IsNullOrWhiteSpace(f.Name));
102+
Assert.IsTrue(f.PersonalIdentifier > 0);
103+
});
104+
105+
var jsonText = JsonConvert.SerializeObject(results, Formatting.Indented);
106+
TestContext.WriteLine(jsonText);
107+
}
108+
49109
[TestMethod]
50110
public async Task TestSinglePostQueryRawJsonResponseAsync()
51111
{

0 commit comments

Comments
 (0)