Closed tr-asimpson closed 2 years ago
Thanks @tr-asimpson for reporting this issue.
Observing the attached compiled GraphQL schema, we can see this input type:
input DeleteBikeInput {
key: String!
}
type Mutation {
...
deleteBike(input: DeleteBikeInput!, condition: ModelBikeConditionInput): Bike @aws_api_key @aws_cognito_user_pools
...
}
The CLI GraphQL transformer seems honor the custom PK settings @key(fields: ["key"])
, where the id
fields is not the primary key.
Currently amplify-codegen and amplify-flutter don't fully support custom primary key. I will investigate this feature gap, and also test and verify the behavior GraphQL transformer v2. Where the schema would be:
type Bike @model @auth(rules: [
{ allow: owner, operations: [create,delete]}
{ allow: private, operations: [read,update]}
{ allow: public, provider: apiKey, operations: [read,create,update,delete]}
]) {
id: ID!
key: String! @primaryKey
name: String
sharedUsers: String
battery: Float
autoUnlock: Boolean
armed: Boolean
muteAudioAlarm: Boolean
firmwareVersion: String
locationLat: Float
locationLon: Float
locationAltitude: Float
odometer: String
tripOdometer: String
owner: String
updatedAt: AWSDateTime
alarmTriggered: Boolean
}
Hi @tr-asimpson I did some digging, unfortunately, that should be case that amplify-codegen and amplify-flutter (amplify-ios and amplify-flutter, the dependencies of amplify-fluter) have never fully supported custom primary key.
With the current implementation of all platform libraries, it always assumes id
field is the primary key, therefore when you do
final request = ModelMutations.delete(bike);
It internally generates a GraphQL document that supplies the id
field as the parameter, however, in the compiled GraphQL type, it expects the key
field.
This feature gap will be addressed with https://github.com/aws-amplify/amplify-flutter/issues/1426.
The workaround you can take:
@key(fields: ["key"])
? If it's for creating index, can you use global secondary index instead (@key(name: "byKey", fields: ["key"])
)?// following the type DeleteBikeInput
// input: { key: \$key } is expected to invoke deleteBike mutation
String graphQLDocument =
'''mutation deleteBike(\$key: ID!) {
deleteTodo(input: { key: \$key }) {
id
key
// ... other fields
}
}''';
var operation = Amplify.API.mutate(
request: GraphQLRequest<String>(document: graphQLDocument, variables: {
'key': 'the-key-you-want-to-delete'
}));
Thankyou @HuiSF I have implemented the manual GraphQL request for now.
Appreciate the help and all the work you and the team put into this amazing plugin!
Thanks.
Glad to hear @tr-asimpson I'm going to close this issue in favor of https://github.com/aws-amplify/amplify-flutter/issues/1426 please feel free to follow up, and if you have any specific use cases with custom primary key, please post in the linked issue. Thanks!
Description
Hi,
I am attempting to delete an item using the GraphQL helpers as below. It seems this was previously working in 4.0.0
This is the response
This is the model
I have attached the compiled schema.
schema.zip
Categories
Steps to Reproduce
Have a model with a custom primary key and attempt to delete an item using
See the response
You can also try deleteById
This is the response which is the same as not using deleteById
Dependencies
Device
Samsung S21 Ultra
OS
Android 11
CLI Version
7.6.23
Additional Context
No response