Skip to content

Commit

Permalink
chore(amplify_api): clean up some code to limit analysis infos (#1404)
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Sheppard authored Feb 28, 2022
1 parent 53ba820 commit 881c86b
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 59 deletions.
19 changes: 11 additions & 8 deletions packages/amplify_api/example/integration_test/graphql_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ void main() {
final req = GraphQLRequest<String>(
document: graphQLDocument, variables: <String, String>{'id': id});
final response = await Amplify.API.mutate(request: req).response;
Map data = jsonDecode(response.data!);
if (response.errors.isNotEmpty) {
fail(
'GraphQL error while deleting a blog: ${response.errors.toString()}');
}
return true;
}

Expand Down Expand Up @@ -171,20 +174,20 @@ void main() {

testWidgets('should LIST blogs with Model helper',
(WidgetTester tester) async {
String blog_1_name = 'Integration Test Blog 1';
String blog_2_name = 'Integration Test Blog 2';
String blog_3_name = 'Integration Test Blog 3';
Blog blog_1 = await addBlog(blog_1_name);
Blog blog_2 = await addBlog(blog_2_name);
Blog blog_3 = await addBlog(blog_3_name);
String blog1Name = 'Integration Test Blog 1';
String blog2Name = 'Integration Test Blog 2';
String blog3Name = 'Integration Test Blog 3';
Blog blog1 = await addBlog(blog1Name);
Blog blog2 = await addBlog(blog2Name);
Blog blog3 = await addBlog(blog3Name);

var req = ModelQueries.list<Blog>(Blog.classType);
var _r = Amplify.API.query(request: req);

var res = await _r.response;
var data = res.data;

final blogs = [blog_1, blog_2, blog_3];
final blogs = [blog1, blog2, blog3];

expect(data?.items, containsAll(blogs));
});
Expand Down
49 changes: 12 additions & 37 deletions packages/amplify_api/lib/src/graphql/graphql_request_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* permissions and limitations under the License.
*/

// ignore_for_file: public_member_api_docs

import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -45,28 +47,6 @@ class GraphQLRequestFactory {
: schema.name;
}

String _getModelType(ModelFieldTypeEnum val) {
switch (val) {
case ModelFieldTypeEnum.string:
return "String";
case ModelFieldTypeEnum.model:
return "ID";
case ModelFieldTypeEnum.int:
return "Int";
case ModelFieldTypeEnum.double:
return "Float";
case ModelFieldTypeEnum.dateTime:
return "AWSDateTime";
case ModelFieldTypeEnum.time:
return "AWSTime";
case ModelFieldTypeEnum.timestamp:
return "AWSTimestamp";
// TODO: Expand type support: enumeration, collection
default:
return "Error: unsupported type!";
}
}

String _getSelectionSetFromModelSchema(
ModelSchema schema, GraphQLRequestOperation operation,
{bool ignoreParents = false}) {
Expand Down Expand Up @@ -114,31 +94,28 @@ class GraphQLRequestFactory {
ModelSchema schema, GraphQLRequestOperation operation) {
String upperOutput = '';
String lowerOutput = '';
List<String> upperList = [];
List<String> lowerList = [];

String modelName = schema.name;

// build inputs based on request operation
switch (operation) {
case GraphQLRequestOperation.get:
upperOutput = r"($id: ID!)";
lowerOutput = r"(id: $id)";
upperOutput = r'($id: ID!)';
lowerOutput = r'(id: $id)';
break;
case GraphQLRequestOperation.list:
upperOutput =
"(\$filter: Model${modelName}FilterInput, \$limit: Int, \$nextToken: String)";
'(\$filter: Model${modelName}FilterInput, \$limit: Int, \$nextToken: String)';
lowerOutput =
r"(filter: $filter, limit: $limit, nextToken: $nextToken)";
r'(filter: $filter, limit: $limit, nextToken: $nextToken)';
break;
case GraphQLRequestOperation.create:
case GraphQLRequestOperation.update:
case GraphQLRequestOperation.delete:
String operationValue = _capitalize(describeEnum(operation));

upperOutput =
"(\$input: ${operationValue}${modelName}Input!, \$condition: Model${modelName}ConditionInput)";
lowerOutput = r"(input: $input, condition: $condition)";
'(\$input: $operationValue${modelName}Input!, \$condition: Model${modelName}ConditionInput)';
lowerOutput = r'(input: $input, condition: $condition)';
break;
case GraphQLRequestOperation.onCreate:
case GraphQLRequestOperation.onUpdate:
Expand All @@ -153,10 +130,8 @@ class GraphQLRequestFactory {
return DocumentInputs(upperOutput, lowerOutput);
}

/**
* Example:
* query getBlog($id: ID!, $content: String) { getBlog(id: $id, content: $content) { id name createdAt } }
*/
/// Example:
/// query getBlog($id: ID!, $content: String) { getBlog(id: $id, content: $content) { id name createdAt } }
GraphQLRequest<T> buildRequest<T extends Model>(
{required ModelType modelType,
Model? model,
Expand All @@ -180,7 +155,7 @@ class GraphQLRequestFactory {
// e.g. "id name createdAt" - fields to retrieve
String fields = _getSelectionSetFromModelSchema(schema, requestOperation);
// e.g. "getBlog"
String requestName = "$requestOperationVal$name";
String requestName = '$requestOperationVal$name';
// e.g. query getBlog($id: ID!, $content: String) { getBlog(id: $id, content: $content) { id name createdAt } }
String document =
'''$requestTypeVal $requestName${documentInputs.upper} { $requestName${documentInputs.lower} { $fields } }''';
Expand Down Expand Up @@ -250,7 +225,7 @@ class GraphQLRequestFactory {
};
}
// Public not() API only allows 1 condition but QueryPredicateGroup
// technically allows multiple conditions so explictly disallow multiple.
// technically allows multiple conditions so explicitly disallow multiple.
throw ApiException(
'Unable to translate not() with multiple conditions.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* permissions and limitations under the License.
*/

// ignore_for_file: public_member_api_docs

import 'dart:convert';

import 'package:amplify_api/amplify_api.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* permissions and limitations under the License.
*/

// ignore_for_file: public_member_api_docs

import 'dart:async';

import 'package:amplify_api/amplify_api.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api/src/graphql/graphql_request_factory.dart';
import 'package:amplify_core/amplify_core.dart';

// ignore_for_file: public_member_api_docs

class ModelMutationsFactory extends ModelMutationsInterface {
// Singleton methods/properties
// usage: ModelQueriesFactory.instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* permissions and limitations under the License.
*/

// ignore_for_file: public_member_api_docs

import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api/src/graphql/graphql_request_factory.dart';
import 'package:amplify_api/src/graphql/paginated_model_type_impl.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api/src/graphql/graphql_request_factory.dart';
import 'package:amplify_core/amplify_core.dart';

// ignore_for_file: public_member_api_docs

class ModelSubscriptionsFactory extends ModelSubscriptionsInterface {
// Singleton methods/properties
// usage: ModelSubscriptionsFactory.instance;
Expand Down
10 changes: 3 additions & 7 deletions packages/amplify_api/test/amplify_api_mutate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api_plugin_interface/amplify_api_plugin_interface.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_test/test_models/ModelProvider.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -92,8 +91,7 @@ void main() {

Blog blog = Blog(id: id, name: name, createdAt: dateTime);

var operation =
await api.mutate(request: ModelMutations.create<Blog>(blog));
var operation = api.mutate(request: ModelMutations.create<Blog>(blog));

var response = await operation.response;
expect(response.data?.equals(blog), isTrue);
Expand Down Expand Up @@ -121,8 +119,7 @@ void main() {

Blog blog = Blog(id: id, name: name, createdAt: dateTime);

var operation =
await api.mutate(request: ModelMutations.delete<Blog>(blog));
var operation = api.mutate(request: ModelMutations.delete<Blog>(blog));

var response = await operation.response;
expect(response.data, equals(blog));
Expand Down Expand Up @@ -150,8 +147,7 @@ void main() {

Blog blog = Blog(id: id, name: name, createdAt: dateTime);

var operation =
await api.mutate(request: ModelMutations.update<Blog>(blog));
var operation = api.mutate(request: ModelMutations.update<Blog>(blog));

var response = await operation.response;
expect(response.data, equals(blog));
Expand Down
5 changes: 2 additions & 3 deletions packages/amplify_api/test/amplify_api_query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api_plugin_interface/amplify_api_plugin_interface.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_test/test_models/ModelProvider.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -111,7 +110,7 @@ void main() {

GraphQLRequest<Blog> req = ModelQueries.get<Blog>(Blog.classType, id);

var operation = await api.query<Blog>(request: req);
var operation = api.query<Blog>(request: req);

var response = await operation.response;

Expand Down Expand Up @@ -159,7 +158,7 @@ void main() {
GraphQLRequest<PaginatedResult<Blog>> req =
ModelQueries.list<Blog>(Blog.classType, limit: 4);

var operation = await api.query<PaginatedResult<Blog>>(request: req);
var operation = api.query<PaginatedResult<Blog>>(request: req);

var response = await operation.response;

Expand Down
1 change: 0 additions & 1 deletion packages/amplify_api/test/amplify_api_subscribe_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import 'dart:async';

import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api_plugin_interface/amplify_api_plugin_interface.dart';
import 'package:amplify_test/test_models/ModelProvider.dart';
import 'package:async/async.dart';
import 'package:flutter/services.dart';
Expand Down
6 changes: 3 additions & 3 deletions packages/amplify_api/test/graphql_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import 'package:flutter_test/flutter_test.dart';

void main() {
group('with ModelProvider', () {
final AmplifyAPI api = AmplifyAPI(modelProvider: ModelProvider.instance);
AmplifyAPI(modelProvider: ModelProvider.instance);
const blogSelectionSet =
'id name createdAt file { bucket region key meta { name } } files { bucket region key meta { name } } updatedAt';

Expand Down Expand Up @@ -787,9 +787,9 @@ void main() {

group('without ModelProvider', () {
test('should handle no ModelProvider instance', () {
AmplifyAPI api = AmplifyAPI();
AmplifyAPI();
try {
GraphQLRequest<Blog> req = ModelQueries.get<Blog>(Blog.classType, '');
ModelQueries.get<Blog>(Blog.classType, '');
} on ApiException catch (e) {
expect(e.message, 'No modelProvider found');
expect(e.recoverySuggestion,
Expand Down

0 comments on commit 881c86b

Please sign in to comment.