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

Unable to override an autogenerated pipeline resolver #2692

Closed
2 tasks done
jguipi opened this issue Jul 4, 2024 · 15 comments
Closed
2 tasks done

Unable to override an autogenerated pipeline resolver #2692

jguipi opened this issue Jul 4, 2024 · 15 comments
Assignees
Labels
extensibility question Further information is requested transferred

Comments

@jguipi
Copy link

jguipi commented Jul 4, 2024

How did you install the Amplify CLI?

npm install -g @aws-amplify/cli

If applicable, what version of Node.js are you using?

v22.2.0

Amplify CLI Version

12.12.4

What operating system are you using?

mac os 14.5 (23F79)

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

no manual change were made

Describe the bug

This schema is generating an autogenerated pipeline resolver that I'm unable to override using the amplify/backend/api/myapi/override.ts

schema.graphql

type User
  @model() {
  id: ID!
  firstname: String!
  lastname: String
  Children: [Child]
    @hasMany(indexName: "byUser", fields: ["id"])
}

type Child
  @model() {
  id: ID!
  firstname: String!
  lastname: String!
  userID: ID
    @index(name: "byUser")
  User: User
}

It seems like the key User.Children autogenerate, a resolver that I'm unable to override.

Override.ts

  const getChild = resources.models['Child'];
  const getChild2 = resources.models['User'];

  console.log(getChild)
  const getChildResolvers =
    getChild.resolvers['queryGetChildResolver'].pipelineConfig['functions'];
  getChild.resolvers['queryGetChildResolver'].pipelineConfig = {
    functions: [
      function_1,
      function_2,
      function_3,
      ...getChildResolvers,
    ],
  };

When I log the following, I'm unable to see the autogenerated pipeline resolver in the data structure:

const getChild = resources.models['Child'];
or 
const getChild = resources.models['User'];

On the other hand, when I look at the AWS UI I'm able to see the autogenerated function

Captura de pantalla 2024-07-05 a la(s) 10 43 51 a m Captura de pantalla 2024-07-05 a la(s) 10 43 56 a m
image image

Expected behavior

Being able to override the pipeline resolver that was automatically generated by amplify

Reproduction steps

  1. Add this schema
  2. Push it to amplify
  3. Create an override.ts file and try to override the autogenerated function

Project Identifier

No response

Log output

# Put your logs below this line


Additional information

No response

Before submitting, please confirm:

  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.
@ykethan
Copy link
Member

ykethan commented Jul 5, 2024

Hey @jguipi, 👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂.
But refer to https://docs.amplify.aws/gen1/react/build-a-backend/graphqlapi/custom-business-logic/#override-amplify-generated-resolvers providing information on overriding Amplify generated resolvers.

@ykethan ykethan transferred this issue from aws-amplify/amplify-cli Jul 5, 2024
@jguipi
Copy link
Author

jguipi commented Jul 5, 2024

As extra detail, I already copied the autogenerated resolver into the right path

From: amplify/backend/api/DevApi/build/resolvers
To : amplify/backend/api/DevApi/build/tsconfig.resource.json

image

Is it normal that the file that were autogenerated are not following the naming convention mentioned here. The current files name are presented in the screenshot

@AnilMaktala
Copy link
Member

Hi @jguipi, Thanks for bringing this up. To override the resolvers, you need to copy the autogenerated resolver from amplify/backend/api/<resource_name>/build/resolvers/ to amplify/backend/api/<resource_name>/resolvers/. The naming convention in the screenshot is expected for relationship tables. Please let us know if you still encounter any issues.

@AnilMaktala AnilMaktala added question Further information is requested pending-response and removed pending-triage labels Jul 15, 2024
@jguipi
Copy link
Author

jguipi commented Jul 15, 2024

Hi @jguipi, Thanks for bringing this up. To override the resolvers, you need to copy the autogenerated resolver from amplify/backend/api/<resource_name>/build/resolvers/ to amplify/backend/api/<resource_name>/resolvers/. The naming convention in the screenshot is expected for relationship tables. Please let us know if you still encounter any issues.

That was already done, but when I try to add the code in the override.ts to override the pipeline resolver that fonction is not there.

As shown above, I was not able to found the function in either of those model:

const getChildModel= resources.models['Child'];
const getUserModel = resources.models['User'];

console.log(getChildModel)
console.log(getUserModel)

getChildModel return

image

getUserModel return

image

There is no mention of that autogenerated function

@AnilMaktala
Copy link
Member

Hi @jguipi, Could we arrange a call to explore this issue in detail?

@AnilMaktala
Copy link
Member

Hey @jguipi, Are you still experiencing this issue?

@jguipi
Copy link
Author

jguipi commented Aug 2, 2024

Hi @jguipi, Could we arrange a call to explore this issue in detail?

Hi !
That would be great since I’m still facing the same issue.

sorry I’m currently on vacation and I don’t have my laptop. Can we have a call when I come back ? After August 10th.

@jguipi
Copy link
Author

jguipi commented Aug 12, 2024

Let me know when you're available. I'm free from 3 PM Montreal time almost every day. (7 PM UTC)

https://juwrl.setmore.com/j

@AnilMaktala
Copy link
Member

Hey @jguipi ,
Are you on Discord? If so, could you please share your Discord username with me?

@jguipi
Copy link
Author

jguipi commented Aug 18, 2024

Hey @jguipi , Are you on Discord? If so, could you please share your Discord username with me?

Sure

discord: j.g7636

@AnilMaktala
Copy link
Member

@jguipi Thanks for sharing your username. I’ll connect with you on Discord.

@palpatim
Copy link
Member

Hi @jguipi,

I believe the configurations you're looking for are exposed under the ConnectionStack key of resources.models. In your sample schema, it looks like the pipeline you want is under userChildrenResolver

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

export function override(resources: AmplifyApiGraphQlResourceStackTemplate, amplifyProjectInfo: AmplifyProjectInfo) {
  console.log('\n### resources.models:', JSON.stringify(Object.keys(resources.models)));
  // ### resources.models: ["User","Child","ConnectionStack"]

  console.log('\n### resources.models[ConnectionStack].resolvers:', JSON.stringify(Object.keys(resources.models['ConnectionStack']?.resolvers)));
  // ### resources.models[ConnectionStack].resolvers: ["userChildrenResolver","childUserResolver"]

  const model = resources.models['ConnectionStack']!;
  const resolvers = model.resolvers!;
  const getResolver = resolvers['userChildrenResolver']!;

  console.log('### original pipelineConfig', JSON.stringify(getResolver.pipelineConfig));
  // ### original pipelineConfig {"functions":["${Token[TOKEN.725]}"]}

  const pipelineConfig = getResolver.pipelineConfig!;
  const functions = (pipelineConfig as CfnResolver.PipelineConfigProperty).functions;
  getResolver.pipelineConfig = {
    functions: [
      'pipelinefn1-dev',
      'pipelinefn2-dev',
      ...functions
    ]
  };

  console.log('### updated pipelineConfig', JSON.stringify(getResolver.pipelineConfig));
  // ### updated pipelineConfig {"functions":["pipelinefn1-dev","pipelinefn2-dev","${Token[TOKEN.725]}"]}
}

Hope this helps

@jguipi
Copy link
Author

jguipi commented Aug 30, 2024

Thank you for the reply ! I'll try this out during the weekend

@jguipi
Copy link
Author

jguipi commented Aug 30, 2024

The solution worked! Thank you all for your help! In the end, the autogenerated pipeline was stored in resources.models['ConnectionStack'].

Hopefully it will help other people !

@jguipi jguipi closed this as completed Aug 30, 2024
Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extensibility question Further information is requested transferred
Projects
None yet
Development

No branches or pull requests

4 participants