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

upgrade to be compatible with mercurius 12 #101

Merged
merged 1 commit into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ const plugin = fp(
auth.registerAuthHandlers(app.graphql.schema, authSchema)

// Add hook to regenerate the resolvers when the schema is refreshed
app.graphql.addHook('onGatewayReplaceSchema', async (instance, schema) => {
const authSchema = auth.getPolicy(schema)
auth.registerAuthHandlers(schema, authSchema)
if (opts.filterSchema === true) {
filterSchema.updatePolicy(app, authSchema, opts)
}
})
if (app.graphqlGateway) {
app.graphqlGateway.addHook('onGatewayReplaceSchema', async (instance, schema) => {
const authSchema = auth.getPolicy(schema)
auth.registerAuthHandlers(schema, authSchema)
if (opts.filterSchema === true) {
filterSchema.updatePolicy(app, authSchema, opts)
}
})
}

if (typeof opts.authContext !== 'undefined') {
app.graphql.addHook('preExecution', auth.authContextHook.bind(auth))
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
},
"homepage": "https://github.com/mercurius-js/auth",
"devDependencies": {
"@mercuriusjs/federation": "^1.0.0",
"@mercuriusjs/gateway": "^1.0.0",
"@sinonjs/fake-timers": "^9.1.2",
"@types/node": "^18.0.3",
"@types/ws": "^8.5.3",
Expand All @@ -38,7 +40,7 @@
"autocannon": "^7.9.0",
"concurrently": "^7.2.2",
"fastify": "^4.2.0",
"mercurius": "^11.0.0",
"mercurius": "^12.0.0",
"pre-commit": "^1.2.2",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
Expand Down
10 changes: 5 additions & 5 deletions test/auth_on_type-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

const { test } = require('tap')
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const mercuriusGateway = require('@mercuriusjs/gateway')
const mercuriusAuth = require('..')

async function createTestService (t, schema, resolvers = {}) {
const service = Fastify()
service.register(mercurius, {
service.register(mercuriusFederationPlugin, {
schema,
resolvers,
federationMetadata: true
resolvers
})
service.register(mercuriusAuth, {
authContext (context) {
Expand Down Expand Up @@ -182,7 +182,7 @@ async function createTestGatewayServer (t) {
await userService.close()
await postService.close()
})
gateway.register(mercurius, {
gateway.register(mercuriusGateway, {
gateway: {
services: [
{
Expand Down
12 changes: 6 additions & 6 deletions test/basic-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

const { test } = require('tap')
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const mercuriusGateway = require('@mercuriusjs/gateway')
const mercuriusAuth = require('..')

async function createTestService (t, schema, resolvers = {}) {
const service = Fastify()
service.register(mercurius, {
service.register(mercuriusFederationPlugin, {
schema,
resolvers,
federationMetadata: true
resolvers
})
await service.listen({ port: 0 })
return [service, service.server.address().port]
Expand Down Expand Up @@ -149,7 +149,7 @@ async function createTestGatewayServer (t, authOpts) {
await userService.close()
await postService.close()
})
gateway.register(mercurius, {
gateway.register(mercuriusGateway, {
gateway: {
services: [{
name: 'user',
Expand Down Expand Up @@ -375,7 +375,7 @@ test('gateway - should handle when auth context is not defined', async (t) => {
authDirective: 'auth'
})

app.graphql.addHook('preGatewayExecution', async (schema, document, context, service) => {
app.graphqlGateway.addHook('preGatewayExecution', async (schema, document, context, service) => {
Object.assign(context, {
other: {
identity: context.reply.request.headers['x-user']
Expand Down
6 changes: 3 additions & 3 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { test } = require('tap')
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const mercuriusAuth = require('..')

const schema = `
Expand Down Expand Up @@ -1102,10 +1103,9 @@ test('basic - should work at type level, entity query', async (t) => {
const app = Fastify()
t.teardown(app.close.bind(app))

app.register(mercurius, {
app.register(mercuriusFederationPlugin, {
schema,
resolvers,
federationMetadata: true
resolvers
})
app.register(mercuriusAuth, {
authContext (context) {
Expand Down
10 changes: 5 additions & 5 deletions test/external-policy-gateway-auth-on-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

const t = require('tap')
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const mercuriusGateway = require('@mercuriusjs/gateway')
const mercuriusAuth = require('..')

async function createTestService (t, schema, resolvers = {}) {
const service = Fastify()
service.register(mercurius, {
service.register(mercuriusFederationPlugin, {
schema,
resolvers,
federationMetadata: true
resolvers
})

await service.listen({ port: 0 })
Expand Down Expand Up @@ -124,7 +124,7 @@ async function createTestGatewayServer (t) {
await userService.close()
await postService.close()
})
gateway.register(mercurius, {
gateway.register(mercuriusGateway, {
gateway: {
services: [
{
Expand Down
10 changes: 5 additions & 5 deletions test/external-policy-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

const t = require('tap')
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const mercuriusGateway = require('@mercuriusjs/gateway')
const mercuriusAuth = require('..')

async function createTestService (t, schema, resolvers = {}) {
const service = Fastify()
service.register(mercurius, {
service.register(mercuriusFederationPlugin, {
schema,
resolvers,
federationMetadata: true
resolvers
})
await service.listen({ port: 0 })
return [service, service.server.address().port]
Expand Down Expand Up @@ -123,7 +123,7 @@ async function createTestGatewayServer (t, authOpts) {
await userService.close()
await postService.close()
})
gateway.register(mercurius, {
gateway.register(mercuriusGateway, {
gateway: {
services: [{
name: 'user',
Expand Down
6 changes: 3 additions & 3 deletions test/external-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const t = require('tap')
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const mercuriusAuth = require('..')

const schema = `
Expand Down Expand Up @@ -882,10 +883,9 @@ t.test('external policy', t => {
const app = Fastify()
t.teardown(app.close.bind(app))

app.register(mercurius, {
app.register(mercuriusFederationPlugin, {
schema,
resolvers,
federationMetadata: true
resolvers
})
app.register(mercuriusAuth, {
authContext (context) {
Expand Down
21 changes: 10 additions & 11 deletions test/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { test } = require('tap')
const FakeTimers = require('@sinonjs/fake-timers')
const { promisify } = require('util')
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { mercuriusFederationPlugin, buildFederationSchema } = require('@mercuriusjs/federation')
const mercuriusGateway = require('@mercuriusjs/gateway')
const mercuriusAuth = require('..')

const immediate = promisify(setImmediate)
Expand Down Expand Up @@ -40,7 +41,7 @@ test('polling interval with a new schema should trigger refresh of schema policy
await userService.close()
})

userService.register(mercurius, {
userService.register(mercuriusFederationPlugin, {
schema: `
directive @auth on OBJECT | FIELD_DEFINITION

Expand All @@ -53,15 +54,14 @@ test('polling interval with a new schema should trigger refresh of schema policy
name: String @auth
}
`,
resolvers,
federationMetadata: true
resolvers
})

await userService.listen({ port: 0 })

const userServicePort = userService.server.address().port

await gateway.register(mercurius, {
await gateway.register(mercuriusGateway, {
gateway: {
services: [
{
Expand Down Expand Up @@ -127,7 +127,7 @@ test('polling interval with a new schema should trigger refresh of schema policy
}

userService.graphql.replaceSchema(
mercurius.buildFederationSchema(`
buildFederationSchema(`
directive @auth on OBJECT | FIELD_DEFINITION

extend type Query {
Expand Down Expand Up @@ -224,7 +224,7 @@ test('polling a filtered schema should complete the refresh succesfully', async
await userService.close()
})

userService.register(mercurius, {
userService.register(mercuriusFederationPlugin, {
schema: `
directive @auth on OBJECT | FIELD_DEFINITION

Expand All @@ -237,15 +237,14 @@ test('polling a filtered schema should complete the refresh succesfully', async
name: String @auth
}
`,
resolvers,
federationMetadata: true
resolvers
})

await userService.listen({ port: 0 })

const userServicePort = userService.server.address().port

await gateway.register(mercurius, {
await gateway.register(mercuriusGateway, {
gateway: {
services: [
{
Expand Down Expand Up @@ -341,7 +340,7 @@ test('polling a filtered schema should complete the refresh succesfully', async
}

userService.graphql.replaceSchema(
mercurius.buildFederationSchema(`
buildFederationSchema(`
directive @auth on OBJECT | FIELD_DEFINITION

extend type Query {
Expand Down
10 changes: 5 additions & 5 deletions test/repeteable-directives-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

const { test } = require('tap')
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const mercuriusGateway = require('@mercuriusjs/gateway')
const mercuriusAuth = require('..')

async function createTestService (t, schema, resolvers = {}) {
const service = Fastify()
service.register(mercurius, {
service.register(mercuriusFederationPlugin, {
schema,
resolvers,
federationMetadata: true
resolvers
})
await service.listen({ port: 0 })
return [service, service.server.address().port]
Expand Down Expand Up @@ -131,7 +131,7 @@ async function createTestGatewayServer (t, authOpts) {
await userService.close()
await postService.close()
})
gateway.register(mercurius, {
gateway.register(mercuriusGateway, {
gateway: {
services: [{
name: 'user',
Expand Down