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

fix: handle undefined reply object inside mercurius context #30

Merged
merged 3 commits into from
Oct 18, 2023

Conversation

DavideArena
Copy link
Contributor

There's a scenario where "reply" object inside mercurius context is undefined. In this case, the plugin crashes whit the following error:

{ "error": "Internal Server Error", "message": "Cannot read properties of undefined (reading 'request')", "statusCode": 500 }

We found this problem in our use case:

  • Create a fastify GET endpoint, executing internally the graphql mercurius federation query for sdl { _service { sdl } }.
  • Create a test for new endpoint, triggering request via fastify.inject.

In this PR i added a guard, checking if context.reply is not undefined. In case of undefined, the plugin doesn't make anything.

Copy link
Owner

@Eomm Eomm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

It would be helpful to mention this usage in the documentation:

If the [mercurius `graphql` decorator](https://github.com/mercurius-js/mercurius/blob/master/docs/api/options.md#appgraphqlsource-context-variables-operationname) is used, it is necessary to provide a `context` object: `app.graphql(query, { reply })`.
Otherwise, this plugin will ignore the request.

test/handle-reply-undefined.test.js Show resolved Hide resolved
@DavideArena
Copy link
Contributor Author

@Eomm updated documentation with mention and added the test.

Copy link
Owner

@Eomm Eomm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grazie Davide!

@Eomm Eomm merged commit 79950ce into Eomm:main Oct 18, 2023
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants