Skip to content

Commit

Permalink
feat(api): GraphQL Custom Request Headers (#1938)
Browse files Browse the repository at this point in the history
  • Loading branch information
Equartey authored and Travis Sheppard committed Sep 14, 2022
1 parent a51ec91 commit 081ce93
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class GraphQLRequest<T> {
/// Only required if your backend has multiple GraphQL endpoints in the amplifyconfiguration.dart file. This parameter is then needed to specify which one to use for this request.
final String? apiName;

/// A map of Strings to dynamically use for custom headers in the http request.
final Map<String, String>? headers;

/// The body of the request, starting with the operation type and operation name.
///
/// See https://graphql.org/learn/queries/#operation-name for examples and more information.
Expand Down Expand Up @@ -57,12 +60,14 @@ class GraphQLRequest<T> {
{this.apiName,
required this.document,
this.variables = const <String, dynamic>{},
this.headers,
this.decodePath,
this.modelType});

Map<String, dynamic> serializeAsMap() => <String, dynamic>{
'document': document,
'variables': variables,
'headers': headers,
'cancelToken': id,
if (apiName != null) 'apiName': apiName,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Future<GraphQLResponse<T>> sendGraphQLRequest<T>({
}) async {
try {
final body = {'variables': request.variables, 'query': request.document};
final graphQLResponse = await client.post(uri, body: json.encode(body));
final graphQLResponse = await client.post(uri,
body: json.encode(body), headers: request.headers);

final responseBody = json.decode(graphQLResponse.body);

Expand Down

0 comments on commit 081ce93

Please sign in to comment.