This is an Amplify Plugin which helps your to seed the databases of your Amplify projects with data using an AppSync GraphQL API. It can be used to seed local mock databases, as well as remote databases, e.g. for testing purposes. This plugin allows you to customize and auto-generate mock data, including data with relationships between models, beyond the capabilities of the 'Auto-generate data' functionality in Amplify Studio.
With this plugin, you can improve testing of your code π―, reduce time by creating test data through automation β±οΈ, and unify mock-data across your project by codifying it in your Version Control System π.
Disclaimer: this is a beta-release of the Amplify GraphQL Seed Plugin and may contain unforeseen bugs. This is a 3rd-party plugin and is not associated with the official Amplify project. Use this plugin at your own discretion. Please let us know if you encounter any bugs, or have feedback / suggestions.
Contents:
- Installation π οΈ
- Getting started π₯π₯π₯
- Available commands π€
- How does it work? π€
- Common errors β
- How to use this plugin in CI/CD pipelines ποΈ
- Uninstall the plugin ποΈ
- About this project π‘
To use this plugin with your Amplify CLI, you must have the following prerequisites setup:
- amplify-cli @ 7.6.5^ - available here
- jq - available here
- use Node versions @ 14.14.0 or above
- Amplify project with a GraphQL API - getting started with Amplify
Once you have installed the above packages, you can install the GraphQL Seed plugin using the following commands:
npm install -g amplify-graphql-seed-plugin
amplify plugin add amplify-graphql-seed-pluginYou can validate the installation by running:
amplify graphql-seed version
The plugin will be available to use with your amplify-cli across all your projects.
We assume that by this stage you already have a GraphQL API configured in your project. If you don't, you can create one by running amplify add api and follow the prompts.
amplify graphql-seed initThe init file has created the amplify/backend/seeding/seed-data.js file. Adjust mutations and data to run your custom seeding. See this section of the readme for more details.
Option 1: Start your mock database, and seed it:
amplify mock --seedOption 2: Seed your database remotely after pushing
You can seed your database by running the command below in your terminal.
amplify push --seedOption 3: Run the plugin directly
You can seed your database by running the command below in your terminal.
amplify graphql-seed runor for remote databases in the AWS Cloud:
amplify graphql-seed run --remoteBelow, you can find the available commands to interact with our plugin:
| Command Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Flags Β Β Β Β Β Β Β Β Β Β Β Β | Description |
|---|---|---|
amplify graphql-seed init |
--remote--local --overwrite |
creates the files which you can customize to use our plugin |
amplify mock --seed |
Seeds the database after starting the mock database locally. | |
amplify mock --refresh |
Deletes the local mock database, starts a new database, and subsequently seeds it. | |
amplify mock --delete |
Deletes the local mock database, and starts a clean database locally. | |
amplify graphql-seed run |
--remote --username --password |
Runs the plugin to seed your database using GraphQL (either locally or remotely) |
amplify graphql-seed help |
Shows you information on how to use the plugin, including arguments | |
amplify graphql-seed version |
Shows the current version of the plugin. | |
amplify graphql-seed remove |
Allows you to remove the files created by this plugin. |
Using the init command, the plugin will create a set of files for you which it uses for seeding your database which you can customize. In particular, it creates two new directories in your amplify folder called backend/seeding and hooks with the following files:
.
βββ ...
βββ amplify
β βββ backend
β β βββ integration
β βββ api
β βββ auth
β βββ ...
β βββ seeding
β βββ configurations.json # new
β βββ credentials.json # new
β βββ customMutations.js # new
β βββ seed-data.js # new
β βββ .gitignore # new
β
βββ hooks
βββ post-mock.sh # new
βββ post-push.sh # new
βββ pre-mock.sh # new
The files have the following contents:
configurations.json- holds the configuration of the plugin based on information the plugin found. You can override any values in this file.seed-data.jsis the file which holds the actual seed data that will be added through GraphQL- [Optional]
credentials.json- stores the credentials you can use to authenticate against your Cognito User Pool. It is important to only use this in your test environment, we don't recommend this for Production environments or security sensitive credentials. - [Optional]
customMutations.js- in this file, you can specify additional custom mutations to use in your seed-data script.
The hooks files are used to run custom code before or after an Amplify command is run. Learn more about Command Hooks here. Using the hooks files, you can automatically use our plugin when running amplify mock and amplify push with our custom --seed argument.
- pre-mock.sh - custom code to run before running
amplify mock, in this case, it will delete the local database if you instruct it to. - post-mock.sh - custom code to automatically run our plugin to seed the database after running
amplify mock - post-push.sh - custom code to seed the database after finishing
amplify push.
Let's assume the following scenario, you have the following schema.graphql in your Amplify project
type Todo @model @auth(rules: [
{allow: owner, identityClaim: "email"},
{allow: private, provider: userPools, operations: [read]},
{allow: public, provider: apiKey, operations: [read, create]},
{allow: private, provider: iam}
]) {
id: ID!
name: String!
description: String
}**Note: the above example allows all different authentication types supported by this plugin. Your case might differ. Take a look at this section **
When you created the file with the init command, it automatically added some sample code to amplify/backend/seeding/seed-data.js. If you have auto-generated mutations in your project (from Amplify codegen), they will be imported at the top of the file. To create seed data, you'll add entries to the seed-data.js file in the following format (an example is automatically added):
import * as mutations from "../../../src/graphql/mutations.js"
import * as customMutations from "./customMutations.js"
export const createTodo = {
mutation: mutations.createTodo,
//override_auth: "API_KEY", // One of ["AWS_IAM", "API_KEY", "AMAZON_COGNITO_USER_POOLS"]
data: [
{ id: 1, name: "some", description: "Lorem ipsum stuff" },
{ id: 2, name: "nothing", description: "Lorem ipsum stuff" }
]
};By default, the query will use your default authentication method specified in aws-exports.js, which you can verify in the generated configuration.json file as well. Within seed-data.js, you can override the authentication which should be used for individual mutations by using the override_auth attribute.
Our Amplify GraphQL seeding plugin supports 3 different authentication modes. Each of them requires changes to be performed via amplify cli command if not setup already.
- Cognito User Pools
- To use this AuthN method, you need to create a test user in Cognito User Pools first. You can then use the following command to confirm the users credentials to be used later on. Note: you must run the command with the appropriate permissions in place.
aws cognito-idp admin-set-user-password --user-pool-id <your_user_pool_id> --username <your_user_id> --password <your_password> --permanent- Make sure that Cognito User Pools is enabled as Authorization (AuthZ) option for your GraphQL API. You can do that by running
amplify api update. Remember to deploy your changes to the amplify by runningamplify push - To be able to write/read to a table, you must enable that authorization option for your schema. Take a look at the documentation to configure the appropriate authorization modes here. For instance, your schema could have a line like this
type Todo @model @auth(rules: [{allow: owner, identityClaim: "email"}, {allow: private, provider: userPools, operations: [read]}])
-
AWS IAM
- Make sure that IAM is enabled as AuthZ option for your GraphQL API. You can do that by running
amplify api update. Remember to deploy your changes to the amplify by runningamplify push - To be able to write/read to a table, you must enable that authorization option for your schema. Take a look the documentation to configure the appropriate authorization modes here. For instance, your schema could have a line like this
type Todo @model @auth(rules: [{allow: private, provider: iam}]) - !! important !! If you intent to use IAM authorization to seed a remote database in the AWS Cloud, you need to create a
custom-roles.jsonfile in youramplify/api/<your-api>folder to give permissions to the appropriate IAM role. Otherwise, you well get an unauthorized exception. For more details, see this page.
- Make sure that IAM is enabled as AuthZ option for your GraphQL API. You can do that by running
-
API KEY
- Make sure that API_KEY is enabled as AuthZ option for your GraphQL API. You can do that by running
amplify api update. Remember to deploy your changes to the amplify by runningamplify push - To be able to write/read to a table, you must enable that authorization option for your schema. Take a look the documentation to configure the appropriate authorization modes here. For instance, your schema could have a line like this
type Todo @model @auth({allow: public, provider: apiKey, operations: [read, create]})
- Make sure that API_KEY is enabled as AuthZ option for your GraphQL API. You can do that by running
- If you see the "GraphQL error: The conditional request failed" error, it is likely that you're trying to create an item with an existing index to your local or remote database. The plugin will skip these elements automatically.
- If you see an error like "fsPromises.rm is not a function", make sure that your npm version >= 14.14.0
- As of late February 2022, the plugin might be flagged up with 11 medium-level vulnerabilities. They're coming from the aws-amplify library directly and we are unable to fix them as of now. Take a look at this issue for updates
You can also use this plugin to seed your remote databases as part of your deployment pipelines. For example, if you're using the Amplify pipelines, you can adjust your amplify.yml file (in build settings), to include the following:
backend:
phases:
preBuild:
commands:
- npm install -g amplify-graphql-seed-plugin
- printf 'Y' | amplify plugin add amplify-graphql-seed-plugin
- yum -y install jq
build:
commands:
- '# Execute Amplify CLI with the helper script'
- amplifyPush --simple
- amplify graphql-seed run --username $USERNAME --password $PASSWORD --remoteThis will install the plugin using npm, add the plugin to the Amplify environment (note that "printf 'Y'" is required to confirm adding it without user-input), and it will install jq as a pre-requisite package.
During the build, the environment will be pushed, and the seeding script will be run based on the code checked in under amplify/backend/seeding in the underlying Version Control System.
npm uninstall -g amplify-graphql-seed-pluginamplify plugin removeThis Plugin was created by Michal Juras and Laurens Brinker, Solution Architects at AWS. The project was initially created for one of our projects, and we've decided to publish it so that it can hopefully help out other people as well. It has already saved us quite a lot of time managing our testing environments, and hope it will do the same for you.
See CONTRIBUTING for more information.
This project is licensed under the Apache-2.0 License.
