Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS-Amplify API module: how to make GraphQL fields unique?

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)

like image 238
Philip Claren Avatar asked Jan 04 '19 10:01

Philip Claren


People also ask

How do I update the Amplify schema in GraphQL?

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.

How do I use amplify in GraphQL?

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.

Does AWS API gateway support GraphQL?

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.

Can you use AppSync without amplify?

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.


2 Answers

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:

  1. Create a GSI on the table where the label is the HASH KEY.
  2. Create a pipeline resolver on the 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" }).
  3. Create a function named 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.
  4. Create a function named CreateTag that issues a PutItem against the Tag table.
  5. Add those two functions in order to your pipeline resolver.

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

like image 114
mparis Avatar answered Sep 21 '22 05:09

mparis


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.

like image 30
akili Sosa Avatar answered Sep 19 '22 05:09

akili Sosa