aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
88 stars 76 forks source link

Custom transformer - Can't add to resolver slots #2019

Open redjonzaci opened 10 months ago

redjonzaci commented 10 months ago

How did you install the Amplify CLI?

No response

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

No response

Amplify CLI Version

12.2.3

What operating system are you using?

Windows

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

No manual changes made

Describe the bug

This is the code in my custom transformer, but the contents of the request and response mapping templates don't make it either to disk or S3. (I have removed the contents of the mapping templates.)

    const models = context.output.getTypeDefinitionsOfKind('ObjectTypeDefinition');
    const createMutationResolvers: TransformerResolverProvider[] = [];
    for (const model of models) {
      const createMutationResolver = context.resolvers.getResolver('Mutation', 'create' + model.name.value) as TransformerResolverProvider;
      if (createMutationResolver) {
        createMutationResolvers.push(createMutationResolver);
      }
    }

    const requestMappingTemplate = MappingTemplate.inlineTemplateFromString();

    const responseMappingTemplate = MappingTemplate.inlineTemplateFromString();

    for (const resolver of createMutationResolvers) {
      resolver.addToSlot('init', requestMappingTemplate, responseMappingTemplate);
    }
  };

In addition, there may be an issue below as it always evaluated to false when I tried it so my code ended up as an S3 location. https://github.com/aws-amplify/amplify-category-api/blob/02b9cc0308d591f9caa21729ce5236c4611c31b3/packages/amplify-graphql-transformer-core/src/appsync-function.ts#L62

But even when I fixed that, my custom transformer template code was nowhere to be found.

Expected behavior

The mapping templates in my custom transformer should make it to either S3 or disk.

Reproduction steps

The code in the "Describe the bug" section should suffice.

Project Identifier

No response

Log output

``` # Put your logs below this line ```

Additional information

No response

Before submitting, please confirm:

redjonzaci commented 10 months ago

As an aside, the docs on making your own transformer/directive could be way better and more helpful.

redjonzaci commented 10 months ago

This issue is related to adding multitenancy support to our app so I would really appreciate some help.

redjonzaci commented 10 months ago

Another related question is: Let's say I have a "GuardMutationFunction" that I want to insert (or link or reference) into the resolver pipeline of every model in the postAuth.2 slot. How would I do that outside a transformer/directive and without adding a Mutation.(Delete|Update){MODEL}.postAuth.2.req.vtl resolver function for every model?

redjonzaci commented 10 months ago

I know you have plenty on your plate, but following up.

AnilMaktala commented 10 months ago

Hi @redjonzaci, Apologies for the delay. I'll contact the engineering team to explore potential workarounds for you while we investigate reproducing the issue on our end.

redjonzaci commented 10 months ago

@AnilMaktala it's okay and thanks!

redjonzaci commented 10 months ago

Hey @AnilMaktala, any update from the team or any additional information we can provide?

redjonzaci commented 10 months ago

We made it work by fitting https://www.npmjs.com/package/@aws-amplify/graphql-default-value-transformer to our need and updating the CLI to version 12.3.0. However, there are still some unclear things and the docs for adding a custom transformer need to be improved. If we will have any other question, will post here or in another issue if this is closed by then. Thank you!

redjonzaci commented 10 months ago

Another question came up: On mutation resolver pipelines, you do a query of the record in the auth slot (which I don't know why is it used), but that record isn't saved for subsequent slots to use it. I need to use it in a postAuth slot to check for attributes of a record.

Is there a way I can modify the auth slot without overriding the auth slots of every mutation or without adding another slot that does querying (as we would do 2 queries for each mutation then)?

craig-at-metashield-io commented 10 months ago

hey @redjonzaci - it looks like i am having similiar issues to you wrt creating custom transformers.

are you able to load your transformer into your project? if so, how do you do it? i am getting stuck on #2077

also, your multi-tenent transformer looks interesting. is this something you will release publically?

thanks

redjonzaci commented 10 months ago

Hey @craig-at-metashield-io, how are you?

  1. You could put the code of the following package https://www.npmjs.com/package/@aws-amplify/graphql-default-value-transformer in a local package of your repo (assuming it's a monorepo) and once you get it working, you can fit the code to your needs.

Or you could start an NPM package based on the defaultValue transformer and try to install that in your project.

  1. We still haven't got it working for proper multitenancy, but if we see that it works for general use cases, we might release it.
craig-at-metashield-io commented 10 months ago

thanks @redjonzaci - i'm doing well and i hope you are too 😄

i'll give that a try, but i must say that i've had a devil of time getting amplify stuff to build correctly. there's a lot of package conflicts.

craig-at-metashield-io commented 10 months ago

hey @redjonzaci - thanks again for your helpful advice.

i have managed to get around the loading issue and finally have a working directive that validates string/number fields.

keep well,

sundersc commented 9 months ago

@redjonzaci - Are you still having issues adding resolvers to custom slots? I understand that you have other questions on authoring custom transformers. I recommend to open a new issue for better visibility.

redjonzaci commented 9 months ago

@sundersc I would appreciate if someone could answer the only question left: https://github.com/aws-amplify/amplify-category-api/issues/2019#issuecomment-1812505293.

djorgji commented 9 months ago

Hi @sundersc,

Hi Amplify team, we've hit a bit of a snag with our current issue.

I'm wondering if anyone has any ideas on how we can better leverage the query that the @auth directive is already executing. We're currently checking some attributes in the postAuth slot, and there's also a need to compare the 'old' record with the 'new' one in the postUpdate slot.

The challenge is, it seems like we're having to unnecessarily use an extra query or a resolver slot to re-query the data. At the moment, there doesn't appear to be a straightforward way to get the @auth directive to either store the data in the stash or forward it down the chain via prev.results.

Any thoughts or suggestions on this would be greatly appreciated! With the slot quota being hardset and immovable we are trying to be very conservative, also it feel really wasteful to waste the query.

Thanks, Dhimitri.

phani-srikar commented 9 months ago

Hi @djorgji, we do not store the fetched item in stash and this might be something we need to take up as a feature request. I see the value in it especially for such customizations. Marking this as a feature request. For now, you might have to override the request template in Auth resolver slot that fetches the item to additionally also put it in a stash variable that you own.

djorgji commented 9 months ago

Hi @phani-srikar, any suggestion on what the easiest way to do that across all resolvers? We are at 40 tables at the moment, and that is quite a few resolvers to override manually?