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

Prepare for fastify v5 #129

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const plugin = fp(
},
{
name: 'mercurius-auth',
fastify: '4.x',
fastify: '5.x',
dependencies: ['mercurius']
}
)
Expand Down
6 changes: 3 additions & 3 deletions lib/filter-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function filterIntrospectionSchema (app, policy, { applyPolicy: policyFunction }

// the filter hook must be the last one to be executed (after all the authContextHook ones)
app.ready(err => {
/* istanbul ignore next */
if (err) throw err
app.graphql.addHook('preExecution', filterGraphQLSchemaHook.bind(app))
if (!err) {
app.graphql.addHook('preExecution', filterGraphQLSchemaHook.bind(app))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure about this? Isn't it swallowing the error?

Copy link
Collaborator Author

@jonnydgreen jonnydgreen Sep 17, 2024

Choose a reason for hiding this comment

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

Yeah I thought the same and it's related to the issue I was coming up against! If I leave as it, app.ready() rejects with the expected error but then the error is thrown again within this callback and causes unhandled rejection issues.

Even though we're swallowing the error here, it's still being thrown and eventually surfaced, as shown and caught in this test here: https://github.com/mercurius-js/auth/blob/main/test/introspection-filter.js#L359

wdyt? Reckon it's worth looking for a better solution?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hey @mcollina ! Hope the Fastify release went okay this week! Just wondered what your thoughts were on my response to your comment? :)

Copy link
Collaborator Author

@jonnydgreen jonnydgreen Sep 29, 2024

Choose a reason for hiding this comment

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

I've actually refactored it to use the fastify onReady hook instead, lemme know what you think!

Edit: Just double checking when this hook actually runs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, it should be okay from what I've read and the tests cover it and all run absolutely fine: https://github.com/mercurius-js/auth/blob/main/test/introspection-filter.js#L104

The only things I can think of with changing to use the onReady hook:

  • It does fire slightly earlier that waiting until the app.ready callback to fire but crucially, it still ensures that it fires the filter schema hook after all the auth context hooks are called.
  • If another onReady hook was registered after the plugin registration, this this would run after (rather than before with the original implementation) but I would argue that in this case, this introduces dependencies and you could get around it by registering the onReady before the plugin registration

wdyt?

cc. @Eomm for visibility as I think you did the original implementation of this :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Need to check - I don't remember ATM

})
}

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
},
"homepage": "https://github.com/mercurius-js/auth",
"devDependencies": {
"@mercuriusjs/federation": "^2.0.0",
"@mercuriusjs/gateway": "^1.0.0",
"@mercuriusjs/federation": "^4.0.0",
"@mercuriusjs/gateway": "^4.0.0",
"@sinonjs/fake-timers": "^10.0.2",
"@types/node": "^20.1.0",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"autocannon": "^7.9.0",
"concurrently": "^8.0.1",
"fastify": "^4.2.0",
"mercurius": "^13.0.0",
"fastify": "^5.0.0-alpha.4",
mcollina marked this conversation as resolved.
Show resolved Hide resolved
"mercurius": "^15.0.0",
"pre-commit": "^1.2.2",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
Expand All @@ -50,8 +50,8 @@
"wait-on": "^7.0.1"
},
"dependencies": {
"@fastify/error": "^3.0.0",
"fastify-plugin": "^4.0.0",
"@fastify/error": "^4.0.0",
"fastify-plugin": "^5.0.0",
"graphql": "^16.2.0"
},
"tsd": {
Expand Down
26 changes: 0 additions & 26 deletions test/auth_on_type-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,32 +345,6 @@ test('gateway - should protect the schema, user object protected', async (t) =>
'0'
]
},
{
message: 'Failed auth policy check on User',
locations: [
{
line: 2,
column: 3
}
],
path: [
'_entities',
'1'
]
},
{
message: 'Failed auth policy check on User',
locations: [
{
line: 2,
column: 3
}
],
path: [
'_entities',
'0'
]
},
{
message: 'Failed auth policy check on User',
locations: [
Expand Down
Loading