I'm new to flutter and graphQL and currently I'm integrating mutations into my app. So, I have the server side using some custom types defined in the schema, but I don't know how to specify them on the flutter side. Let's see some code:
input DiaryGroupPermission {
groupId: Int!
permission: Int!
}
input DiaryInsideCommunity {
communityId: Int!
permissions: [DiaryGroupPermission]!
}
createDiary(community: DiaryInsideCommunity, description: String, title: String!): Diary
But on the client I don't know how to specify the DiaryInsideCommunity inside the mutation. I've tried something like this:
String createDiary = """
mutation CreateDiary(\$title: String!, \$description: String!, \$community: DiaryInsideCommunity) {
createDiary(
title: \$title,
description: \$description,
community: \$community
) {
id
}
)}""".replaceAll('\n', ' ');
And passing my runMutation as follows:
runMutation({
"title": _generalPage.title(),
"description": _generalPage.description(),
"community": {
"communityId": 1,
"permissions": _permissionPage.selectedGroups().map((group) {
return {
"groupId": group.id,
"permission": 1,
};
}).toList(),
}
});
Any idea? Can't find anything on google.
Love to see the community that is created around the graphql_flutter library.
class DiaryGroupPermission {
int groupId;
int permission;
DiaryGroupPermission.fromJson(Map json)
: groupId = json['groupId'],
permission = json['permission'];
}
class DiaryInsideCommunity {
int communityId;
List<DiaryGroupPermission> permissions;
DiaryInsideCommunity.fromJson(Map json)
: communityId = json['communityId'],
permissions = json['permissions']
.map<DiaryGroupPermission>((Map permisionJson) =>
DiaryGroupPermission.fromJson(permisionJson))
.toList();
}
class Diary {
String body;
Diary(dynamic value) : body = value.toString();
}
typedef Diary createDiaryFunction(
DiaryInsideCommunity community, String description, String title);
DiaryInsideCommunity community = DiaryInsideCommunity.fromJson({
'communityId': 1,
'permissions': [
{'groupId': 1, 'permission': 1}
]
});
Diary mutation(DiaryInsideCommunity community,
{String description, @required String title}) =>
Diary(community.permissions[0].groupId);
Diary mutationResult = mutation(community, description: "a", title: "b");
I implemented the types that you wanted to in dart and created a mockup mutation function to show you how to call it.
There is no easier way to do types in dart.
Cheers from the creator of this library,
Eus
Assuming you are using graphql_flutter , You may specify it in variables an example is say you have this definitions for a mutation
type Mutation {
createMeterReading(
createMeterReadingInput: CreateMeterReadingInput
): MeterReading }
your input type definition
input CreateMeterReadingInput {
reading: Float
companyId: Int
companyStaffId: Int
customerId: Int
readingDataType: String
latitude: Float
longitude: Float
masterMeterId: Int
analogMeterId: Int
type: String
editedStatus: String
base64Image: String
readingTime: String
}
In flutter have
final response = await client.value.mutate(MutationOptions(
variables: {
'createMeterReadingInput': {
'reading': double.parse(_meterReadingController.text),
'companyStaffId': _decodedToken["companyStaffId"],
'analogMeterId': waterMeter["analogMeterId"],
'type': "actual",
'base64Image': getBase64(),
'readingTime': timeNow,
'latitude': widget?.currentPosition?.latitude,
'longitude': widget?.currentPosition?.longitude
}
},
documentNode: gql(r"""
mutation createMeterReading($createMeterReadingInput:CreateMeterReadingInput ) {
createMeterReading(createMeterReadingInput: $createMeterReadingInput) {
meterReadingId
reading
companyId
companyStaffId
imageUrl
editedStatus
companyStaff{
firstName
}
}
}
"""),
));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.