aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Using an exisiting AppSync API w/ Amplify #1516

Closed packerfan626 closed 5 years ago

packerfan626 commented 5 years ago

What AWS Services are you utilizing? AppSync, DynamoDB, and Lambda

Provide additional details e.g. code snippets So I am a bit stuck on how to integrate AppSync within Amplify in React Native. I have an existing API within AWS AppSync that I created on the console. This API has several different models like: Users, Videos, VideoComments, etc. Some of the objects within these models have custom mapping templates and resolvers that are really important for the application.

For Example, this is a quick look at what we have schema wise. type User {

type Video {

We have a resolver that runs with a simple getVideo query that will retrieve the uploadedBy attribute by using the userId, and retrieves all the necessary information of that user.

Additionally, the data sources (dynamoDb tables) we created for the models have primary keys, and some have sort keys. Like a VideoLikes table keeps track of who liked a video and to avoid duplicates the primary key is the VideoID and the sort key is UserID. This is just a minor example, we have other places where we do this to also have access to LSIs.

When I started using Amplify, I tried recreating the AppSync API because I liked how powerful the CloudFormation capabilities were with the different staging environments. However, I noticed the DynamoDB for models were automatically defined and were automatically set to an id as a primary key. We use the LSIs to help sort by certain values, like for a video if we wanted to sort by the number of likes, or comments, so unfortunately this would not work for us. So when I noticed this, I used the "Codegen" command from my original AppSync API and ran into the issue where my resolvers and mapping templates were not copied down with the schema, queries, mutations, and subscriptions making most of the queries fail because those data sources were missing.

So my question is:

  1. Is there a way to integrate/use EVERYTHING from my exiting AppSync API within my React Native application? This includes the custom resolvers and mapping templates.

IF NOT

  1. Is there a way to set a primary and sort key for the DynamoDB of the Models when creating an API directly within the Amplify CLI?

IF NOT

  1. Is there another way to have data sorted efficiently within DynamoDB without using LSIs and GSIs? If the models automatically generate tables with GSIs, this can be problematic because I know GSIs are a bit more expensive so I would like to avoid those as much as possible. Is there another service that will sort the data that can be used within AppSync from DynamoDB?

Any help will be appreciated, thank you.

haverchuck commented 5 years ago

@packerfan626 I'm assuming you used codegen following the documentation here?

mikeparisstuff commented 5 years ago

Is there a way to integrate/use EVERYTHING from my exiting AppSync API within my React Native application? This includes the custom resolvers and mapping templates.

As of writing there is no automated command to import an existing API but it is possible to deploy a copy of your existing API that targets your existing data sources using custom resources within the API category. To do this you would need to pull the schema into schema.graphql, resolver templates into /resolvers, and lastly write custom cloudformation stacks that create the resolver, data source, and role resources to wire everything up in the /stacks directory. You can use custom stacks to target existing tables if necessary. As of writing you cannot import an existing table as a @model but this feature is in the backlog.

Is there a way to set a primary and sort key for the DynamoDB of the Models when creating an API directly within the Amplify CLI?

We are actively working on a new @key directive that does exactly this. The code (https://github.com/aws-amplify/amplify-cli/pull/1463) and docs (https://github.com/aws-amplify/docs/pull/689/files) are in PR.

kaustavghosh06 commented 5 years ago

@packerfan626 We released the @key directive. Please take a look at our @key documentation for more details around this - https://aws-amplify.github.io/docs/cli-toolchain/graphql#key

kaustavghosh06 commented 5 years ago

Re-using of existing AppSync API is a duplicate of #288

jaidisido commented 4 years ago

Is there a way to integrate/use EVERYTHING from my exiting AppSync API within my React Native application? This includes the custom resolvers and mapping templates.

As of writing there is no automated command to import an existing API but it is possible to deploy a copy of your existing API that targets your existing data sources using custom resources within the API category. To do this you would need to pull the schema into schema.graphql, resolver templates into /resolvers, and lastly write custom cloudformation stacks that create the resolver, data source, and role resources to wire everything up in the /stacks directory. You can use custom stacks to target existing tables if necessary. As of writing you cannot import an existing table as a @model but this feature is in the backlog.

Is there a way to set a primary and sort key for the DynamoDB of the Models when creating an API directly within the Amplify CLI?

We are actively working on a new @key directive that does exactly this. The code (#1463) and docs (https://github.com/aws-amplify/docs/pull/689/files) are in PR.

@mikeparisstuff Could we please have an example of how to integrate an existing API into the "stacks" directory? I am just confused about how Amplify would be able to automatically pick up these new resources and use them.

Also, when using a custom schema in schema.graphql, can we still use directives such @auth or is that not possible?

Thanks

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.