I'm getting an error that says `type "Null" is not a subtype of "String". I have no idea where this error is coming from. I can't understand, because It works just fine in amplify studio and it works just fine in AppSync. I'm out of options. Please help! :) I know that I don't have to write graphql query to get a list of activities. But this is the only way to get activityMessages data together because I have a 1:many relationship
For testing, I set a breakpoint where it says final test = await res.response
.
Debugger screenshot
AppSync screenshot
Amplify Studio screenshot
It doesn't get to the return
This is my code that throws me an error
Future<Request<List<Activity?>>> fetchActivitiesByDay(TemporalDate day, String schoolID) async {
try {
const listActivities = 'listActivities';
const graphQLDocument = '''query MyQuery {
listActivities {
items {
id
end_time
description
title
}
}
};
final getPostRequest = GraphQLRequest<Activity>(
document: graphQLDocument,
modelType: Activity.classType,
// variables: <String, String>{'id': 'b34f6dfc-fb18-4f20-85cf-57e97671d480'},
decodePath: listActivities,
);
final res = Amplify.API.query(request: getPostRequest);
final test = await res.response;
return formatRequestList<Activity?>(test);
} on ApiException catch (e) {
throw e.message;
}
}
I've tried without providing a query
const graphQLDocument = '''query {
listActivities {
items {
id
end_time
description
title
}
}
};
If I try to get a single activity it works just fine.
const graphQLDocument = '''query GetActivity(\$id: ID!) {
getActivity(id: \$id) {
id
}
};
This is how my schema looks like
type Activity
@model
@auth(
rules: [{ allow: private, operations: [read, update] }, { allow: owner }]
) {
id: ID!
schoolID: ID!
category: ActivityEnum!
title: String!
start_time: AWSTime!
end_time: AWSTime!
date: AWSDate!
description: String!
activityMessages: [ActivityMessage]
@hasMany(indexName: "byActivity", fields: ["id"])
}
type ActivityMessage
@model
@auth(rules: [{ allow: private, operations: [read] }, { allow: owner }]) {
id: ID!
schoolID: ID!
activityID: ID! @index(name: "byActivity", sortKeyFields: ["message"])
activity: Activity! @belongsTo(fields: ["activityID"])
message: String
isImage: Boolean!
imageKey: String
url: String
likes: Int!
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
EXPLANATION!!
The problem was that modelType: Activity.classType
is to get single instance. To get a list I had to use modelType: const PaginatedModelType(Activity.classType)
and this solved my problem.
Working Example