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)
Once your API is deployed, updating the schema is easy with the CLI. You can edit the schema file(s) and run amplify push command to update your GraphQL backend. When you run the push command, you will notice that your schema change is automatically detected, and your backend will be updated respectively.
Create a basic GraphQL APIIn the terminal, use the command amplify add api to begin API creation. Choose GraphQL and select the default options (including “Single object with fields” at the “What best describes your project” stage). After the schema has been created, it should open in your default code editor.
AWS AppSync is a managed GraphQL service that makes it easy to connect disparate data sources into a single cohesive API. GraphQL APIs start with the definition of a schema that defines the data types and queries for accessing them. Data Sources are the backend services that the API will use to fulfill requests.
AppSync is a powerful service by AWS that allows you to deploy GraphQL APIs with ease, as well as connect those APIs with data sources like AWS DynamoDB, lambda, and more. It can work without Amplify, but when you pair Amplify with it, you get some extra benefits.
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
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With