Skip to content

Commit cef14e1

Browse files
committed
Simplify splitting service response into data and errors
1 parent 063dd54 commit cef14e1

13 files changed

+211
-32
lines changed

include/graphqlservice/GraphQLClient.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@
3131

3232
namespace graphql::client {
3333

34+
// Errors may specify the line number and column number where the error occurred.
35+
struct ErrorLocation
36+
{
37+
response::IntType line {};
38+
response::IntType column {};
39+
};
40+
41+
// Errors may specify a path to the field which triggered the error. The path consists of
42+
// field names and the indices of elements in a list.
43+
using ErrorPathSegment = std::variant<response::StringType, response::IntType>;
44+
45+
// Error returned from the service.
46+
struct Error
47+
{
48+
std::string message;
49+
std::vector<ErrorLocation> locations;
50+
std::vector<ErrorPathSegment> path;
51+
};
52+
53+
// Complete response from the service, split into the unparsed graphql::response::Value in
54+
// data and the (typically empty) collection of Errors in errors.
55+
struct ServiceResponse
56+
{
57+
response::Value data;
58+
std::vector<Error> errors;
59+
};
60+
61+
// Split a service response into separate ServiceResponse data and errors members.
62+
GRAPHQLCLIENT_EXPORT ServiceResponse parseServiceResponse(response::Value&& response);
63+
3464
// GraphQL types are nullable by default, but they may be wrapped with non-null or list types.
3565
// Since nullability is a more special case in C++, we invert the default and apply that modifier
3666
// instead when the non-null wrapper is not present in that part of the wrapper chain.

samples/client/BenchmarkClient.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#include "BenchmarkClient.h"
77

8-
#include "graphqlservice/GraphQLClient.h"
9-
108
#include <algorithm>
119
#include <array>
1210
#include <stdexcept>

samples/client/BenchmarkClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef BENCHMARKCLIENT_H
99
#define BENCHMARKCLIENT_H
1010

11+
#include "graphqlservice/GraphQLClient.h"
1112
#include "graphqlservice/GraphQLParse.h"
1213
#include "graphqlservice/GraphQLResponse.h"
1314

samples/client/MutateClient.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#include "MutateClient.h"
77

8-
#include "graphqlservice/GraphQLClient.h"
9-
108
#include <algorithm>
119
#include <array>
1210
#include <stdexcept>

samples/client/MutateClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef MUTATECLIENT_H
99
#define MUTATECLIENT_H
1010

11+
#include "graphqlservice/GraphQLClient.h"
1112
#include "graphqlservice/GraphQLParse.h"
1213
#include "graphqlservice/GraphQLResponse.h"
1314

samples/client/QueryClient.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#include "QueryClient.h"
77

8-
#include "graphqlservice/GraphQLClient.h"
9-
108
#include <algorithm>
119
#include <array>
1210
#include <stdexcept>

samples/client/QueryClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef QUERYCLIENT_H
99
#define QUERYCLIENT_H
1010

11+
#include "graphqlservice/GraphQLClient.h"
1112
#include "graphqlservice/GraphQLParse.h"
1213
#include "graphqlservice/GraphQLResponse.h"
1314

samples/client/SubscribeClient.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#include "SubscribeClient.h"
77

8-
#include "graphqlservice/GraphQLClient.h"
9-
108
#include <algorithm>
119
#include <array>
1210
#include <stdexcept>

samples/client/SubscribeClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef SUBSCRIBECLIENT_H
99
#define SUBSCRIBECLIENT_H
1010

11+
#include "graphqlservice/GraphQLClient.h"
1112
#include "graphqlservice/GraphQLParse.h"
1213
#include "graphqlservice/GraphQLResponse.h"
1314

samples/client/benchmark.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
#include "TodayMock.h"
54
#include "BenchmarkClient.h"
5+
#include "TodayMock.h"
66

77
#include <chrono>
88
#include <iostream>
@@ -128,6 +128,7 @@ int main(int argc, char** argv)
128128

129129
auto service = buildService();
130130
std::vector<std::chrono::steady_clock::duration> durationResolve(iterations);
131+
std::vector<std::chrono::steady_clock::duration> durationParseServiceResponse(iterations);
131132
std::vector<std::chrono::steady_clock::duration> durationParseResponse(iterations);
132133
const auto startTime = std::chrono::steady_clock::now();
133134

@@ -142,11 +143,14 @@ int main(int argc, char** argv)
142143
const auto startResolve = std::chrono::steady_clock::now();
143144
auto response =
144145
service->resolve(nullptr, query, "", response::Value(response::Type::Map)).get();
146+
const auto startParseServiceResponse = std::chrono::steady_clock::now();
147+
auto serviceResponse = client::parseServiceResponse(std::move(response));
145148
const auto startParseResponse = std::chrono::steady_clock::now();
146-
const auto parsed = parseResponse(std::move(response));
149+
const auto parsed = parseResponse(std::move(serviceResponse.data));
147150
const auto endParseResponse = std::chrono::steady_clock::now();
148151

149-
durationResolve[i] = startParseResponse - startResolve;
152+
durationResolve[i] = startParseServiceResponse - startResolve;
153+
durationParseServiceResponse[i] = startParseResponse - startParseServiceResponse;
150154
durationParseResponse[i] = endParseResponse - startParseResponse;
151155
}
152156
}
@@ -162,6 +166,7 @@ int main(int argc, char** argv)
162166
outputOverview(iterations, totalDuration);
163167

164168
outputSegment("Resolve"sv, durationResolve);
169+
outputSegment("ParseServiceResponse"sv, durationParseServiceResponse);
165170
outputSegment("ParseResponse"sv, durationParseResponse);
166171

167172
return 0;

0 commit comments

Comments
 (0)