According to Amplify documentation,
A single @model directive configures the following AWS resources:
- An Amazon DynamoDB table with PAY_PER_REQUEST billing mode enabled by
default.
- An AWS AppSync DataSource configured to access the table
above.
- An AWS IAM role attached to the DataSource that allows AWS
AppSync to call the above table on your behalf.
- Up to 8 resolvers
(create, update, delete, get, list, onCreate, onUpdate, onDelete) but
this is configurable via the queries, mutations, and subscriptions
arguments on the @model directive.
- Input objects for create, update,
and delete mutations.
- Filter input objects that allow you to filter
objects in list queries and connection fields.
- For list queries the
default number of objects returned is 100. You can override this
behavior by setting the limit argument.
That is why your tables are being generated. If you want to connect you API to an existing table, this is what AWS says [Ref.]:
You can let AWS AppSync provision DynamoDB resources on your behalf.
Or, if you prefer, you can connect your existing tables to a GraphQL
schema by creating a data source and a resolver.
With that being said, if you remove @model directive from the type, you will then have to define datasource, queries, mutations and their respective resolvers by yourself.
Coming back to your question on how to connect existing DynamoDB table to your GraphQL API, you have the following options.
- Add AppSync DataSource in your Amplify project.
- For existing API, you can create Datasource using AWS AppSync console.
- Also for existing API, you can create Datasource by using Amplify CLI.
Amplify project/GraphQL Transform:
For example, in your stack’s Resources block of your API directory, you can define datasource as:
"DynamoDBDataSource": {
"Type":"AWS::AppSync::DataSource",
"Properties":{
"ApiId":{
"Ref":"<YOUR API ID>"
},
"Name":"DynamoDbDataSource",
"Type":"AMAZON_DYNAMODB",
"ServiceRoleArn":"<SERVICE ROLE ARN>",
"DynamoDBConfig":{
"AwsRegion":"<DYNAMODB REGION>",
"TableName":"Planet"
}
}
}
Datasource using Console:
This might require you to have extra permission but other than that, this is pretty straightforward. This is the link to their official guide.
Datasource using CLI:
Since you are using Amplify, following is an example of how you can add Datasource using CLI. {Ref.}
aws appsync create-data-source --api-id <YOUR API ID> --name <NAME OF YOUR DATASOURCE>
--type AMAZON_DYNAMODB --dynamodb-config <PLAIN JSON AS ABOVE OR LOCATION TO YOUR JSON CONFIG FILE>
--service-role-arn <ARN OF YOUR IAM ROLE> --profile default
Once your Datasource is sorted out, you can create resolvers and map them with your queries and mutations for which you can find good documentation on this link.