-
Notifications
You must be signed in to change notification settings - Fork 1
/
built_graphql_test.dart
160 lines (153 loc) · 4.74 KB
/
built_graphql_test.dart
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import "dart:convert";
import 'package:built_graphql/built_graphql.dart';
import "package:test/test.dart";
import '../example/hero.dart';
import '../example/friend.dart';
import '../example/serializers.dart';
void main() {
test('Deserialize a Hero', () {
final String jsonStr = '''
{
"data": {
"hero": {
"name": "R2-D2",
"heroFriends": [
{
"id": "1000",
"name": "Luke Skywalker"
},
{
"id": "1002",
"name": null
},
{
"id": "1003",
"name": "Leia Organa"
}
]
}
}
}
''';
final GraphQLResponse<Hero> response = standardSerializers.deserialize(
json.decode(jsonStr),
specifiedType: GraphQLResponse.createFullTypeFor(Hero));
expect(response.data.name, equals("R2-D2"));
expect(response.data.heroFriends.length, equals(3));
expect(response.data.heroFriends.first.id, equals("1000"));
expect(response.data.heroFriends.first.name, equals("Luke Skywalker"));
});
test('Deserialize a Hero and errors', () {
final String jsonStr = '''
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [ { "line": 6, "column": 7 } ],
"path": [ "hero", "heroFriends", 1, "name" ]
}
],
"data": {
"hero": {
"name": "R2-D2",
"heroFriends": [
{
"id": "1000",
"name": "Luke Skywalker"
},
{
"id": "1002",
"name": null
},
{
"id": "1003",
"name": "Leia Organa"
}
]
}
}
}
''';
final GraphQLResponse<Hero> response = standardSerializers.deserialize(
json.decode(jsonStr),
specifiedType: GraphQLResponse.createFullTypeFor(Hero));
expect(response.data.name, equals("R2-D2"));
expect(response.data.heroFriends.length, equals(3));
expect(response.data.heroFriends.first.id, equals("1000"));
expect(response.data.heroFriends.first.name, equals("Luke Skywalker"));
expect(response.errors.length, equals(1));
expect(response.errors[0].message,
equals("Name for character with ID 1002 could not be fetched."));
expect(response.errors[0].locations.length, equals(1));
expect(response.errors[0].locations[0].line, equals(6));
expect(response.errors[0].locations[0].column, equals(7));
expect(
response.errors[0].path, equals(["hero", "heroFriends", "1", "name"]));
});
test('Deserialize errors', () {
final String jsonStr = '''
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [ { "line": 6, "column": 7 } ],
"path": [ "hero", "heroFriends", 1, "name" ],
"extensions": {
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018"
}
}
]
}
''';
final GraphQLResponse<Hero> response = standardSerializers.deserialize(
json.decode(jsonStr),
specifiedType: GraphQLResponse.createFullTypeFor(Hero));
expect(response.errors.length, equals(1));
expect(response.errors[0].message,
equals("Name for character with ID 1002 could not be fetched."));
expect(response.errors[0].locations.length, equals(1));
expect(response.errors[0].locations[0].line, equals(6));
expect(response.errors[0].locations[0].column, equals(7));
expect(
response.errors[0].path, equals(["hero", "heroFriends", "1", "name"]));
expect(
response.errors[0].extensions["code"], equals("CAN_NOT_FETCH_BY_ID"));
expect(response.errors[0].extensions["timestamp"],
equals("Fri Feb 9 14:33:09 UTC 2018"));
});
test('Deserialize a Friend from multiple results', () {
final String jsonStr = '''
{
"data": {
"friend": {
"id": "1000",
"name": "Luke Skywalker"
},
"hero": {
"name": "R2-D2",
"heroFriends": [
{
"id": "1000",
"name": "Luke Skywalker"
},
{
"id": "1002",
"name": null
},
{
"id": "1003",
"name": "Leia Organa"
}
]
}
}
}
''';
final GraphQLResponse<Friend> response = standardSerializers.deserialize(
json.decode(jsonStr),
specifiedType: GraphQLResponse.createFullTypeFor(Friend));
expect(response.data.id, equals("1000"));
expect(response.data.name, equals("Luke Skywalker"));
});
}