Skip to content

Commit

Permalink
update graphql feature branch with commits from main (#924)
Browse files Browse the repository at this point in the history
* chore(amplify_api): GraphQL helper move dependencies (#729)

* feat(amplify_api): GraphQL helper get query (#742)

* feat(amplify_api): GraphQL Response Decoding (#763)

* feat(amplify_api): GraphQL Query Helper - List  (#771)

* feat(amplify_api): GraphQL Mutate Helper - Create (#778)

* feat(amplify_api): GraphQL Mutate Helper - Update (#779)

* feat(amplify_api): GraphQL Mutate Helper - Delete (#780)

* feat(amplify_api): GraphQL Helpers Integration Tests (#785)

* fix(amplify_api): GraphQL Helper Clean Up (#803)

* some fixes for merge

* fix integration tests and some related analyze issues

* update graphql helpers to pass analysis

* correct some rebase errors

Co-authored-by: Elijah Quartey <[email protected]>
  • Loading branch information
Travis Sheppard and Equartey authored Oct 5, 2021
1 parent 8836d9b commit 9df843f
Show file tree
Hide file tree
Showing 31 changed files with 1,760 additions and 74 deletions.
55 changes: 0 additions & 55 deletions packages/amplify_api/example/integration_test/graph_ql_tests.dart

This file was deleted.

207 changes: 207 additions & 0 deletions packages/amplify_api/example/integration_test/graphql_tests.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import 'dart:convert';

import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api_example/amplifyconfiguration.dart';
import 'package:amplify_flutter/amplify.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';

import 'resources/Blog.dart';
import 'resources/ModelProvider.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
List<Blog> blogCache = [];

// utility to query blogs and return length
group('GraphQL', () {
// declare utility to delete all blogs async
Future<bool> deleteBlog(String id) async {
String graphQLDocument = '''mutation deleteBlog(\$id: ID!) {
deleteBlog(input: {id: \$id}) {
id
name
createdAt
}
}''';
var req = GraphQLRequest<String>(
document: graphQLDocument, variables: <String, String>{'id': id});

var operation = Amplify.API.mutate(request: req);

var response = await operation.response;
Map data = jsonDecode(response.data);

return true;
}

Future<bool> deleteAllBlogs() async {
await Future.wait(blogCache.map((blog) => deleteBlog(blog.id)));

return true;
}

// declare utility which creates blog with title as parameter
Future<Blog> addBlog(String name) async {
String graphQLDocument = '''mutation createBlog(\$name: String!) {
createBlog(input: {name: \$name}) {
id
name
createdAt
}
}''';

var _r = Amplify.API.mutate(
request: GraphQLRequest<String>(
document: graphQLDocument,
variables: <String, String>{'name': name}));

var response = await _r.response;
Map data = jsonDecode(response.data);
Blog blog = Blog.fromJson(data['createBlog']);
blogCache.add(blog);
return blog;
}

setUpAll(() async {
if (!Amplify.isConfigured) {
await Amplify.addPlugins(
[AmplifyAPI(modelProvider: ModelProvider.instance)]);
await Amplify.configure(amplifyconfig);
}
});

tearDownAll(() async {
await deleteAllBlogs();
});

testWidgets('should fetch', (WidgetTester tester) async {
const listBlogs = 'listBlogs';
const items = 'items';
String graphQLDocument = '''query MyQuery {
$listBlogs {
$items {
id
name
createdAt
}
}
}''';
var _r = Amplify.API
.query<String>(request: GraphQLRequest(document: graphQLDocument));
var response = await _r.response;
Map data = jsonDecode(response.data);
expect(data[listBlogs][items], hasLength(greaterThanOrEqualTo(0)));
});

// Queries
testWidgets('should GET a blog with Model helper',
(WidgetTester tester) async {
String name = 'Integration Test Blog to fetch';
Blog blog = await addBlog(name);

var req = ModelQueries.get(Blog.classType, blog.id);
var _r = Amplify.API.query(request: req);

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

expect(data, equals(blog));
});

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);

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];

expect(data.items, containsAll(blogs));
});

// Mutations
testWidgets('should CREATE a blog with Model helper',
(WidgetTester tester) async {
String name = 'Integration Test Blog - create';
Blog blog = Blog(name: name);

var req = ModelMutations.create(blog);

var _r = Amplify.API.mutate(request: req);

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

blogCache.add(data);

expect(data, equals(blog));
});

testWidgets('should UPDATE a blog with Model helper',
(WidgetTester tester) async {
String oldName = 'Integration Test Blog to update';
String newName = 'Integration Test Blog - updated';
Blog blog = await addBlog(oldName);

blog = blog.copyWith(name: newName);

var req = ModelMutations.update(blog);
var _r = Amplify.API.mutate(request: req);

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

expect(data, equals(blog));
});

testWidgets('should DELETE a blog with Model helper',
(WidgetTester tester) async {
String name = 'Integration Test Blog - delete';
Blog blog = await addBlog(name);

var req = ModelMutations.delete(blog);
var _r = Amplify.API.mutate(request: req);

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

expect(data, equals(blog));

try {
var checkReq = ModelQueries.get(Blog.classType, blog.id);
var _check = Amplify.API.query(request: checkReq);
var checkRes = await _check.response;
} on ApiException catch (e) {
expect(e.message, 'response from app sync was "null"');
expect(e.recoverySuggestion,
'Current GraphQLResponse is non-nullable, please ensure item exists before fetching');
}
});
});
}
6 changes: 4 additions & 2 deletions packages/amplify_api/example/integration_test/main_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import 'package:amplify_flutter/amplify.dart';
import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api_example/amplifyconfiguration.dart';

import 'graph_ql_tests.dart' as graph_ql_tests;
import 'graphql_tests.dart' as graph_ql_tests;
import 'resources/ModelProvider.dart';

void main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('amplify_api', () {
setUpAll(() async {
await Amplify.addPlugins([AmplifyAPI()]);
await Amplify.addPlugins(
[AmplifyAPI(modelProvider: ModelProvider.instance)]);
await Amplify.configure(amplifyconfig);
});

Expand Down
Loading

0 comments on commit 9df843f

Please sign in to comment.