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

Maintenance: AppSyncResolverEvent uses typeName instead of parentTypeName #5560

Open
1 of 2 tasks
aminalaee opened this issue Nov 14, 2024 · 2 comments
Open
1 of 2 tasks
Assignees
Labels
event_handlers internal Maintenance changes

Comments

@aminalaee
Copy link
Contributor

Why is this needed?

I'm new to GraphQL in general, but I think in the original PR #323 the AppSyncResolverEvent was based on AWS Amplify, so instead of reading parentTypeName, we are reading typeName.

It confused me that the code is consistent (in some ways) with AWS AppSync Context info:

type Info = {
  fieldName: string;
  parentTypeName: string;
  variables: any;
  selectionSetList: string[];
  selectionSetGraphQL: string;
};

But we are assigning variables in a way they are not clear:

info = {"fieldName": self.get("fieldName"), "parentTypeName": self.get("typeName")}

And we define different property names for reading the parentTypeName:

@property
def parent_type_name(self) -> str:
"""The name of the parent type for the field that is currently being resolved."""
return self["parentTypeName"]

@property
def type_name(self) -> str:
"""The name of the parent type for the field that is currently being resolved."""
return self.info.parent_type_name

This has the side-effect that when setting up an AppSync resolved in AWS like the following, it does not work out of the box and you have to pass typeName to make it work:

import { util } from '@aws-appsync/utils';

/**
 * Sends a request to a Lambda function. Passes all information about the request from the `info` object.
 * @param {import('@aws-appsync/utils').Context} ctx the context
 * @returns {import('@aws-appsync/utils').LambdaRequest} the request
 */
export function request(ctx) {
    return {
        operation: 'Invoke',
        payload: {
            fieldName: ctx.info.fieldName,
            parentTypeName: ctx.info.parentTypeName,
            typeName: ctx.info.parentTypeName,  // Extra field which is exactly the same as the one above
            arguments: {}, // Extra field
            variables: ctx.info.variables,
            selectionSetList: ctx.info.selectionSetList,
            selectionSetGraphQL: ctx.info.selectionSetGraphQL,
        },
    };
}

/**
 * Process a Lambda function response
 * @param {import('@aws-appsync/utils').Context} ctx the context
 * @returns {*} the Lambda function response
 */
export function response(ctx) {
    const { result, error } = ctx;
    if (error) {
        util.error(error.message, error.type, result);
    }
    return result;
}

Which area does this relate to?

Event Source Data Classes

Solution

I'm not sure if it was the intention and it should stay the same, but I think we can improve this in different ways:

  • If the old approach is not desired, we can just deprecate it and mention parent_type_name should be used
  • If the old approach is still valid, we can try to read info from parentTypeName after typeName is checked. This way it is consistent with the AWS Resolver context here and with the Amplify structure

I can prepare a PR if the suggestion makes sense.

Acknowledgment

@leandrodamascena
Copy link
Contributor

Hi @aminalaee! Thanks for reporting this! I'll check on Monday, but I think we have a bug here.

The reason I'll check out this is because we use some of these fields in our AppSync Event Handler utility and I don't know if we have this bug there as well, so I want to test everything together.

@leandrodamascena leandrodamascena self-assigned this Nov 16, 2024
@leandrodamascena leandrodamascena added event_handlers and removed triage Pending triage from maintainers labels Nov 16, 2024
@leandrodamascena leandrodamascena moved this from Triage to Pending review in Powertools for AWS Lambda (Python) Nov 16, 2024
@aminalaee
Copy link
Contributor Author

Thanks, And about your point of this usage, I think it's kind of the same that in other places type_name is used where we mean parent_type_name. To me seems like this was only to follow AWS Amplify and seems like that is not consistent with AWS Resolver Context.

def resolver(self, type_name: str = "*", field_name: str | None = None) -> Callable:

https://docs.amplify.aws/gen1/react/build-a-backend/graphqlapi/custom-business-logic/#structure-of-the-function-event

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers internal Maintenance changes
Projects
Status: Pending review
Development

No branches or pull requests

2 participants