Reputation: 2876
AWS-Amplify provides a couple of directives to build an GraphQL-API. But I haven't found out how to ensure uniqueness for fields.
I want to do something like in GraphCool:
type Tag @model @searchable {
id: ID!
label: String! @isUnique
}
This is an AWS-Amplify specific question. It's not about how to do this with generic GraphQL. It's very specifically about how to do this with AWS-Amplify's API module. (https://aws-amplify.github.io/docs/js/api)
Upvotes: 15
Views: 6890
Reputation: 209
Update: now you can use @primarykey and @index annotations: https://docs.amplify.aws/cli/migration/transformer-migration/#what-is-changing
basic:
profile @model {
name
email @primaryKey - has to be unique
other
}
so if you needed something like:
profile @model {
name
email: String! @hasOne
other
}
email @model {
email: String! @primaryKey
}
if you are on an older version see below I will eventually be testing this out to see if this works but you might be able to do something like rename the id to a string! so...
type Tag @model @key["id"] {
id: String!
}
or:
type Customer @model @key(fields: ["email"]) {
email: String!
username: String
}
this second one is taken directly from the docs: https://docs.amplify.aws/cli/graphql-transformer/key#designing-data-models-using-key
The docs were updated recently so hopefully they are easier for everyone to understand.
If you need a more advanced workflow with allot of keys, and stuff like that then you just have to separate things out and make more types for example:
type Customer @model {
id: String!
email: Email! @hasOne
username: String
}
type email @model @key(fields: ["email"]) {
email: String!
}
Upvotes: 1
Reputation: 3683
Hey thanks for the question. This is not yet possible by default using the amplify-cli but you could do this yourself using pipeline resolvers and an extra index on your DynamoDB table. The steps to do this are as follows:
Mutation.createTag
field in your schema. You can turn off the auto-generated Mutation.createTag
mutation by changing your @model definition to @model(mutations: { update: "updateTag", delete: "deleteTag" })
.LookupLabel
that issues a Query
against the new GSI where the label = $ctx.args.input.label
. If this returns a value, throw an error with $util.error("Label is not unique")
. If it returns no values then continue.CreateTag
that issues a PutItem
against the Tag table.You can read more about pipeline resolvers here https://docs.aws.amazon.com/appsync/latest/devguide/pipeline-resolvers.html.
As of writing amplify does not yet support custom & pipeline resolvers but you can read more about the feature here https://github.com/aws-amplify/amplify-cli/issues/574 as it will be supported in the future. For now you can add the resolver manually in the AWS AppSync console or via your own CloudFormation template that targets the id of the API created by Amplify. It would also be helpful if you create an issue here (https://github.com/aws-amplify/amplify-cli/issues) and tag this as a feature request because it would be possible to automate this with an @unique directive but this would need to be planned.
Thanks
Upvotes: 7