diff --git a/src/registry/federation/register-schema.test.ts b/src/registry/federation/register-schema.test.ts index 371594c..2136ce0 100644 --- a/src/registry/federation/register-schema.test.ts +++ b/src/registry/federation/register-schema.test.ts @@ -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, diff --git a/src/registry/federation/register-schema.ts b/src/registry/federation/register-schema.ts index 5974f93..e58e8b0 100644 --- a/src/registry/federation/register-schema.ts +++ b/src/registry/federation/register-schema.ts @@ -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) } @@ -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)