Accessing an AppSync GraphQl function from a Flutter App. This example is using a list of users as a data
Asuming you have a query with the following structure:
query ListUsers {
listUsers {
ID
user_login
user_email
# ... other fields ...
}
}
add the dependencies to the Flutter dependencies file:
aws_appsync_api: ^2.0.0
graphql_flutter: ^5.1.2
Create your screen and add the function to be triggered by a click button or something else.
Dont forget to change the endpoint address and api-key:
Future _showNotification() async {
final HttpLink httpLink = HttpLink(
'https://ENDPOINT-ADDRESS.appsync-api.REGION.amazonaws.com/graphql',
defaultHeaders: {
'x-api-key': 'APIKEY-FROM-AWS',
},
);
final GraphQLClient client = GraphQLClient(
cache: GraphQLCache(),
link: httpLink,
);
String listUsersQuery = '''
query ListUsers {
listUsers {
ID
user_login
user_email
# ... other fields ...
}
}
''';
final QueryOptions options = QueryOptions(
document: gql(listUsersQuery),
);
final QueryResult result = await client.query(options);
if (result.hasException) {
print("GraphQL Error: ${result.exception.toString()}");
} else {
if (result.data != null) {
final List<dynamic> users = result.data?['listUsers'];
print(users);
} else {
print('data null');
}
}
;
}
run the app, trigger the function and get the data:
[{__typename: User, ID: 2, user_login:...
list of data...
...@gmail.com}]