-
Notifications
You must be signed in to change notification settings - Fork 155
Feature request: GraphQL API Event Handler #1166
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
Comments
Hi @FilipPyrek thank you for opening this feature request! As I said on Twitter, I think this is an interesting feature that we should consider adding to this library at some point. At the moment we are focused on implementing Parameters and Idempotency, as well as finishing Lambda Layers and investigating ESM support. I have added this feature request to the "Ideas" bucket and added the |
This comment was marked as outdated.
This comment was marked as outdated.
Still relevant |
Apologies @FilipPyrek. Yesterday I set up some new automation to handle issues and this was caught in the crossfire by mistake. Agree that the issue should stay open. I am working on a fix to the automation. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as outdated.
This comment was marked as outdated.
Very excited at the prospect of this feature! Have seen a few bad implementations of event routing for AppSync handlers out there and at my company, so it would be really great to have an open source one provided by AWS to standardize on so we can stop re-inventing the wheel 😄 |
We have added this item to our roadmap and we are looking to launch it before end of the year. I am posting here a comment from #3885 to keep the lineage of the issue and people's reactions - the original message was written by @leandrodamascena: Use caseCustomers can use AWS Lambda with AWS AppSync to resolve GraphQL fields through two types of integrations:
While Powertools for AWS can work in some cases with custom resolvers, it is recommended to use Powertools only with direct resolvers. This is because custom resolvers often modify or remove fields in the payload that Powertools depends on to function correctly. Since there is no guaranteed way for customers to ensure these necessary fields remain intact when using custom resolvers, relying on direct resolvers provides a more reliable and consistent integration and avoid Powertools's maintainers trying to identifying issues created by custom integrations. PayloadAmong other fields that I removed just to simplify the example, the payload that AppSync sends to Lambda is something like this: {
"arguments": {
"id": "my identifier"
},
"identity": {...},
"source": null,
"request": {...},
"prev": null,
"info": {
"selectionSetList": [
"id",
"field1",
"field2"
],
"selectionSetGraphQL": "{\n id\n field1\n field2\n}",
"parentTypeName": "Mutation",
"fieldName": "createSomething",
"variables": {}
},
"stash": {}
} Registering routesWhen resolving an event, Powertools needs to examine the fieldName/parentTypeName fields, being with fieldName @app.resolver(field_name="listLocations")
def get_locations(name: str, description: str = "") -> List[Location]:
return [{"name": name, "description": description}] with fieldName and parentTypeName @app.resolver(type_name="Query", field_name="listLocations")
def get_locations(name: str, description: str = "") -> List[Location]:
return [{"name": name, "description": description}] Transforming arguments into function variablesWhen working with GraphQL queries like the one below, customers may want to access the query query MyQuery {
getPost(post_id: "2") {
id
title
}
} payload {
"arguments": {
"post_id": "2"
},
...
} resolver @app.resolver(type_name="Query", field_name="getPost")
def get_locations(post_id: int) -> List[Post]: # match GraphQL Query arguments
#do stuff
return [{"name": name, "description": description}] Resolver not foundUnlike AppSync events, when the resolver is not found, Powertools should throw a Single and batch resolversCustomers who want to solve the N+1 problem can use batch resolvers to receive multiple events in the same payload and then resolve them. The keys difference between single resolvers and batch resolvers are: 1/ The return always must be a list and the list of results must match the size and order of the request payload entries so that AWS AppSync can match the results accordingly. Since Powertools TypeScript already has the whole strategy for registering and resolving routes for AppSync Events, I think the adaptation would be to change the payload fields and deal with batch resolvers, but the other things are more or less the same. I'm more than happy to do a session with you to demonstrate how it works in Powertools Python. In addition to the above, I would like to also propose to align the methods to a similar experience to what's already present in AppSync Events and REST Event Handlers and have: @app.query(field_name="getPost")
def get_post(post_id: int) -> List[Post]: # match GraphQL Query arguments
#do stuff
return [{"name": name, "description": description}]
@app.mutation(field_name="putPost")
def put_post(post_content: str, post_title: str) -> Post: # match GraphQL Query arguments
#do stuff
@app.subscribe(field_name="PostChange")
def get_locations() -> boolean:
#do stuff In terms of concrete plans for the implementation - we can most likely use the same code architecture as the one under |
I am willing to contribute. |
Hi @arnabrahman, that's amazing - thank you! I'll assign the issue to you. I expect this to be a significant amount of work, so I'd suggest you do an initial assessment and then let me know if you have any questions. If not, we can iterate in the PRs as usual. It's also ok to split the work in multiple PRs if you think it makes sense. |
Use case
The problem in our case is that when we use lambda in combination with GraphQL's nested resolvers feature (and mainly AppSync's implementation of nested resolvers) the lambda cold starts begin to stack-up which leads to couple of seconds of aggregated cold starts of all nested resolvers which is very unpleasant.
Solution/User Experience
GraphQL API handler in Lambda Powertools for Python has a resolver library which does the nested resolution inside a single lambda function and instead of creating the abstraction on infrastructure level, as AppSync does, it creates it on source code level. This resolution can be done thanks to informations from
$ctx.info
.This way lambda resolvers would be only for top-level Mutation/Query fields and the rest would be done inside lambda function(s).
And it will reduce any cold starts to minimum. (= cold start of single lambda function)
Alternative solutions
Acknowledgment
The text was updated successfully, but these errors were encountered: