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: apply updated schema on refresh #81

Merged
merged 3 commits into from
Mar 21, 2024
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
3 changes: 3 additions & 0 deletions lib/gateway/build-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,9 @@ async function buildGateway (serviceMap, gatewayOpts, app, lruGatewayResolvers)

defineResolvers(schema, typeToServiceMap, serviceMap, typeFieldsToService, factory, lruGatewayResolvers)

this.schema = schema
simoneb marked this conversation as resolved.
Show resolved Hide resolved
app.graphql.replaceSchema(this.schema)

return schema
},
close
Expand Down
2 changes: 1 addition & 1 deletion test/pollingInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ test("Polling schemas (if service is down, schema shouldn't be changed)", async
})
}

await userService.close()
userService.close()
await t.context.clock.tickAsync(500)

{
Expand Down
152 changes: 152 additions & 0 deletions test/schemaRefresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
'use strict'

const { test } = require('tap')

const Fastify = require('fastify')
const { buildFederationSchema } = require('@mercuriusjs/federation')
const GQL = require('mercurius')
const plugin = require('../index')

test('Refreshing gateway schema', async (t) => {
simoneb marked this conversation as resolved.
Show resolved Hide resolved
const resolvers = {
Query: {
me: () => user
},
User: {
__resolveReference: (user) => user
}
}

const user = {
id: 'u1',
name: 'John',
lastName: 'Doe'
}

const userService = Fastify()
const gateway = Fastify()
t.teardown(async () => {
await gateway.close()
await userService.close()
})

userService.register(GQL, {
schema: buildFederationSchema(`
extend type Query {
me: User
}

type User @key(fields: "id") {
id: ID!
name: String!
}
`),
resolvers
})

await userService.listen({ port: 0 })

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

await gateway.register(plugin, {
gateway: {
services: [
{
name: 'user',
url: `http://localhost:${userServicePort}/graphql`
}
],
cache: false
}
})

const res = await gateway.inject({
method: 'POST',
headers: {
'content-type': 'application/json'
},
url: '/graphql',
body: JSON.stringify({
query: `
query introspect {
__type(name: "User") {
fields {
name
}
}
}
`
})
})

t.same(res.json(), {
data: {
__type: {
fields: [
{
name: 'id'
},
{
name: 'name'
}
]
}
}
})

// Update User service schema

userService.graphql.replaceSchema(
buildFederationSchema(`
extend type Query {
me: User
}

type User @key(fields: "id") {
id: ID!
name: String!
lastName: String!
}
`)
)
userService.graphql.defineResolvers(resolvers)

await gateway.graphqlGateway.refresh()

const updatedRes = await gateway.inject({
method: 'POST',
headers: {
'content-type': 'application/json'
},
url: '/graphql',
body: JSON.stringify({
query: `
query introspect {
__type(name: "User") {
fields {
name
}
}
}
`
})
})

t.same(updatedRes.json(), {
data: {
__type: {
fields: [
{
name: 'id'
},
{
name: 'name'
},
{
name: 'lastName'
}
]
}
}
})
})
Loading