Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change data source for resolver that extends Amplify-generated one #9623

Closed
jk171505 opened this issue Jan 26, 2022 · 10 comments
Closed

Change data source for resolver that extends Amplify-generated one #9623

jk171505 opened this issue Jan 26, 2022 · 10 comments
Labels
api-graphql Issues related to GraphQL resources in the API category graphql-transformer-v1 Issue related to GraphQL Transformer v1 question General question

Comments

@jk171505
Copy link

jk171505 commented Jan 26, 2022

Which Category is your question related to?
api

Amplify CLI Version
7.6.8

Provide additional details e.g. code snippets
Is this possible to change data source for new resolver that extends Amplify-generated one in new version of graphQL transformer (V2)?

@jk171505 jk171505 added the question General question label Jan 26, 2022
@lazpavel
Copy link
Contributor

Hi @jk171505, you should be able to achieve what you need by using CDK to define custom VTL resolvers as described here: https://docs.amplify.aws/cli/graphql/custom-business-logic/#vtl-resolver.

Please let us know if you still have any issues

@lazpavel lazpavel added api-graphql Issues related to GraphQL resources in the API category graphql-transformer-v1 Issue related to GraphQL Transformer v1 pending-close-response-required labels Jan 27, 2022
@jk171505
Copy link
Author

@lazpavel where do I define the exact slot for which I want to change the data source?

@jk171505
Copy link
Author

Can I accomplish the same by defining the data source for specific slot in CustomResources.json?

@lazpavel
Copy link
Contributor

lazpavel commented Jan 27, 2022

I see, the link above will allow you to create and configure a custom resolver and not to update a different data source for a slot on an extended resolver.

For that you could use the documented way to extend a resolver (e.g: Mutation.createModelB.postAuth.2.req.vtl) and then use amplify override api and update the datasource for the specific appsyncFunction as below:

import { AmplifyApiGraphQlResourceStackTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyApiGraphQlResourceStackTemplate) { 
resources.models["**ModelB**"].appsyncFunctions["Mutationcreate**ModelB**postAuth1FunctionMutationcreate**ModelB**postAuth1Function.AppSyncFunction"].dataSourceName = "**ModelATable**";
}

@jk171505
Copy link
Author

Thanks! That's exactly what I needed.

@Dennis-Dekker
Copy link

@lazpavel Is there a difference between the replacement of **ModelB** and **ModelATable** in your example? I'm confused by this, so if model A is named "Item" should **ModelATable** be "Item" or "ItemTable"?

@jk171505
Copy link
Author

@Dennis-Dekker you are basically changing the dataSource for ModelB to ModelA. You should also add the suffix 'Table', so it should be:

resources.models["Item"].appsyncFunctions["MutationcreateItempostAuth1FunctionMutationcreateItempostAuth1Function.AppSyncFunction"].dataSourceName = "SomeOtherItemTable";

@ggorge-etiqa
Copy link

This is my override.ts:

import { AmplifyApiGraphQlResourceStackTemplate, AmplifyProjectInfo } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyApiGraphQlResourceStackTemplate, amplifyProjectInfo: AmplifyProjectInfo) {
    const model = resources.models["User"];
    model.appsyncFunctions["MutationcreateUserpreUpdate0Function.AppSyncFunction"].dataSourceName = "UniqueUsernameCheckLambdaDataSource";
}

On amplify push I' m getting this error:

Cannot set properties of undefined (setting 'dataSourceName')

Amplify version is 12.3.0.

Is what explained in this issue still valid?

@BradVidler
Copy link

@ggorge-etiqa Did you ever solve this? I'm facing the same issue.

@rob-lucky
Copy link

rob-lucky commented Jun 3, 2024

This is my override.ts:

import { AmplifyApiGraphQlResourceStackTemplate, AmplifyProjectInfo } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyApiGraphQlResourceStackTemplate, amplifyProjectInfo: AmplifyProjectInfo) {
    const model = resources.models["User"];
    model.appsyncFunctions["MutationcreateUserpreUpdate0Function.AppSyncFunction"].dataSourceName = "UniqueUsernameCheckLambdaDataSource";
}

On amplify push I' m getting this error:

Cannot set properties of undefined (setting 'dataSourceName')

Amplify version is 12.3.0.

Is what explained in this issue still valid?

Yes, this is still a valid solution with amplify cli v 12.11.1

For the function name you can also take a look into your build folder, where you can also find the generated resolvers. Here you can also find the CloudFormation stacks. Find the stack for your origin model. Find your function. The function should have this syntax: FunctionNameAppSyncFunctionRandomString.

You need the FunctionName part und use it in override.ts:
model.appsyncFunctions["FunctionName.AppSyncFunction"].dataSourceName = "SomeOtherItemTable";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-graphql Issues related to GraphQL resources in the API category graphql-transformer-v1 Issue related to GraphQL Transformer v1 question General question
Projects
None yet
Development

No branches or pull requests

6 participants