-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(amplify_api): GraphQL Response Decoding (#763)
- Loading branch information
Showing
6 changed files
with
206 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/amplify_api/lib/src/graphql/graphql_response_decoder.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:amplify_api/amplify_api.dart'; | ||
|
||
class GraphQLResponseDecoder { | ||
// Singleton methods/properties | ||
// usage: GraphQLResponseDecoder.instance; | ||
GraphQLResponseDecoder._(); | ||
|
||
static final GraphQLResponseDecoder _instance = GraphQLResponseDecoder._(); | ||
|
||
static GraphQLResponseDecoder get instance => _instance; | ||
|
||
GraphQLResponse<T> decode<T>( | ||
{required GraphQLRequest request, | ||
required String data, | ||
required List<GraphQLResponseError> errors}) { | ||
// if no ModelType fallback to default | ||
if (request.modelType == null) { | ||
if (T == String) { | ||
return GraphQLResponse( | ||
data: data as T, errors: errors); // <T> is implied | ||
} else { | ||
throw ApiException( | ||
'Decoding of the response type provided is currently unsupported', | ||
recoverySuggestion: "Please provide a Model Type or type 'String'"); | ||
} | ||
} | ||
|
||
if (request.decodePath == null) { | ||
throw ApiException('No decodePath found', | ||
recoverySuggestion: 'Include decodePath when creating a request'); | ||
} | ||
|
||
Map<String, dynamic>? dataJson = json.decode(data); | ||
if (dataJson == null) { | ||
throw ApiException( | ||
'Unable to decode json response, data received was null'); | ||
} | ||
|
||
request.decodePath!.split(".").forEach((element) { | ||
if (dataJson![element] == null) { | ||
throw ApiException( | ||
'decodePath did not match the structure of the JSON response', | ||
recoverySuggestion: | ||
'Include decodePath when creating a request that includes a modelType.'); | ||
} | ||
dataJson = dataJson![element]; | ||
}); | ||
|
||
T decodedData = request.modelType!.fromJson(dataJson!) as T; | ||
|
||
return GraphQLResponse<T>(data: decodedData, errors: errors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters