Skip to content

Commit

Permalink
allow updating routingUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed May 24, 2021
1 parent 0f78f18 commit ab49cda
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
61 changes: 61 additions & 0 deletions src/registry/federation/register-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 354,67 @@ test('Should return 400 when routingUrl is not valid URI', async (t) => {
)
})

test('Should be able to update the routingUrl', async (t) => {
const app = build({
databaseConnectionUrl: t.context.connectionUrl,
})
t.teardown(() => app.close())

let res = await app.inject({
method: 'POST',
url: '/schema/push',
payload: {
typeDefs: /* GraphQL */ `
type Query {
hello: String
}
`,
version: '1',
routingUrl: `http://${t.context.testPrefix}_foo:3000/api/graphql`,
serviceName: `${t.context.testPrefix}_foo`,
graphName: `${t.context.graphName}`,
},
})

t.is(res.statusCode, 200)

res = await app.inject({
method: 'POST',
url: '/schema/push',
payload: {
typeDefs: /* GraphQL */ `
type Query {
hello: String
}
`,
version: '1',
routingUrl: `http://${t.context.testPrefix}_foo:3003/api/graphql`,
serviceName: `${t.context.testPrefix}_foo`,
graphName: `${t.context.graphName}`,
},
})

t.is(res.statusCode, 200)
t.is(res.statusCode, 200)
t.like(
res.json(),
{
data: {
serviceName: `${t.context.testPrefix}_foo`,
routingUrl: `http://${t.context.testPrefix}_foo:3003/api/graphql`,
typeDefs: trimDoc/* GraphQL */ `
type Query {
hello: String
}
`,
version: '1',
},
success: true,
},
'response payload match',
)
})

test('Should not be possible to register two schemas with the same routingUrl', async (t) => {
const app = build({
databaseConnectionUrl: t.context.connectionUrl,
Expand Down
22 changes: 18 additions & 4 deletions src/registry/federation/register-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 131,12 @@ export default function registerSchema(fastify: FastifyInstance) {
name: req.body.serviceName,
})

const serviceByRoutingUrl = await serviceRepository.findByRoutingUrl({
graphName: req.body.graphName,
routingUrl: req.body.routingUrl,
})

if (!service) {
const serviceByRoutingUrl = await serviceRepository.findByRoutingUrl({
graphName: req.body.graphName,
routingUrl: req.body.routingUrl,
})
if (serviceByRoutingUrl) {
throw DuplicateServiceUrlError(serviceByRoutingUrl.name, serviceByRoutingUrl.routingUrl)
}
Expand All @@ -144,6 145,19 @@ export default function registerSchema(fastify: FastifyInstance) {
graphId: graph.id,
routingUrl: req.body.routingUrl,
})
} else if (req.body.routingUrl !== service.routingUrl) {
if (serviceByRoutingUrl) {
throw DuplicateServiceUrlError(serviceByRoutingUrl.name, serviceByRoutingUrl.routingUrl)
}
const updatedService = await serviceRepository.updateOne(
{
routingUrl: req.body.routingUrl,
},
{
id: service.id,
},
)
service = updatedService!
}

const mormalizedTypeDefs = normalizeSchema(req.body.typeDefs)
Expand Down

0 comments on commit ab49cda

Please sign in to comment.