Skip to content

Commit

Permalink
fix: compare against all available services
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed May 2, 2021
1 parent ceac29d commit c1fef5e
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 197 deletions.
18 changes: 12 additions & 6 deletions src/core/basic-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ test('Should return 200 because credentials are valid', async (t) => {
t.teardown(() => app.prisma.$disconnect())

let res = await app.inject({
method: 'GET',
url: '/schema/latest',
query: {
method: 'POST',
url: '/schema/push',
payload: {
type_defs: `type Query { world: String }`,
version: '2',
service_name: `${t.context.testPrefix}_bar`,
graph_name: `${t.context.graphName}`,
},
headers: {
Expand All @@ -36,9 +39,12 @@ test('Should support multiple secrets comma separated', async (t) => {
t.teardown(() => app.prisma.$disconnect())

let res = await app.inject({
method: 'GET',
url: '/schema/latest',
query: {
method: 'POST',
url: '/schema/push',
payload: {
type_defs: `type Query { world: String }`,
version: '3',
service_name: `${t.context.testPrefix}_bar`,
graph_name: `${t.context.graphName}`,
},
headers: {
Expand Down
5 changes: 2 additions & 3 deletions src/core/hook-handler/user-scope.prevalidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ export interface RequestContext {

/**
* Validate if the client is able to register a schema in the name of the service
* TODO: Should not be necessary to pass type to FastifyRequest
*/
export const checkUserServiceScope = function (
req: FastifyRequest<RequestContext>,
res: FastifyReply,
next: HookHandlerDoneFunction,
) {
// JWT context ?
if (req.user) {
if (req.body.service_name && !req.user.services.find((service) => service === req.body.service_name)) {
if (req.user && req.body.service_name) {
if (!req.user.services.find((service) => service === req.body.service_name)) {
return next(InvalidServiceScopeError(req.body.service_name))
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/core/services/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export class SchemaService {
this.dbClient = client
}

async findByServiceVersions(graphName: string, services: ServiceSchemaVersionMatch[]) {
async findByServiceVersions(graphName: string, serviceMatches: ServiceSchemaVersionMatch[]) {
const schemas = []
let error: Error | null = null

const serviceItems = await this.dbClient.service.findMany({
where: {
name: {
in: services.map((s) => s.name),
in: serviceMatches.map((s) => s.name),
},
graph: {
name: graphName,
Expand All @@ -28,19 +28,23 @@ export class SchemaService {
isActive: true,
},
orderBy: {
name: 'asc',
updatedAt: 'desc',
},
select: {
name: true,
},
})

for await (const service of serviceItems) {
const match = services.find((s) => s.name === service.name)
const version = match?.version
for await (const serviceMatch of serviceMatches) {
const service = serviceItems.find((s) => s.name === serviceMatch.name)

if (!match) {
error = new Error(`Service "${service.name}" does not exists`)
if (!service) {
error = new Error(`In graph "${graphName}" service "${serviceMatch.name}" could not be found`)
break
}

const version = serviceMatch.version

if (version) {
const schema = await this.dbClient.schema.findFirst({
where: {
Expand All @@ -66,7 +70,7 @@ export class SchemaService {
})

if (!schema) {
error = new Error(`Service "${service.name}" has no schema in version "${version}" registered`)
error = new Error(`In graph "${graphName}", service "${service.name}" has no schema in version "${version}" registered`)
break
}

Expand Down Expand Up @@ -102,7 +106,7 @@ export class SchemaService {
})

if (!schemaTag) {
error = new Error(`Service "${service.name}" has no schema registered`)
error = new Error(`In graph "${graphName}", service "${service.name}" has no schema registered`)
break
}

Expand Down
2 changes: 1 addition & 1 deletion src/registry/federation/deactivate-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test('Should deactivate schema', async (t) => {
t.deepEqual(
res.json(),
{
error: `Service "${t.context.testPrefix}_foo" has no schema in version "1" registered`,
error: `In graph "${t.context.graphName}", service "${t.context.testPrefix}_foo" has no schema in version "1" registered`,
success: false,
},
'response payload match',
Expand Down
16 changes: 8 additions & 8 deletions src/registry/federation/get-composed-schema-versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ test('Should return latest schema when no version was specified', async (t) => {
t.is(response.data.length, 2)

t.like(response.data[0], {
serviceName: `${t.context.testPrefix}_bar`,
typeDefs: 'type Query { world: String }',
version: '3',
})

t.like(response.data[1], {
serviceName: `${t.context.testPrefix}_foo`,
typeDefs: 'type Query { hello: String }',
version: '1',
})

t.like(response.data[1], {
serviceName: `${t.context.testPrefix}_bar`,
typeDefs: 'type Query { world: String }',
version: '3',
})
})

test('Should return 404 when schema in version could not be found', async (t) => {
Expand Down Expand Up @@ -192,7 +192,7 @@ test('Should return 404 when schema in version could not be found', async (t) =>
t.deepEqual(
res.json(),
{
error: `Service "${t.context.testPrefix}_foo" has no schema in version "2" registered`,
error: `In graph "${t.context.graphName}", service "${t.context.testPrefix}_foo" has no schema in version "2" registered`,
success: false,
},
'response payload match',
Expand Down Expand Up @@ -249,7 +249,7 @@ test('Should return 400 when schema in specified version was deactivated', async
t.deepEqual(
res.json(),
{
error: `Service "${t.context.testPrefix}_foo" has no schema in version "1" registered`,
error: `In graph "${t.context.graphName}", service "${t.context.testPrefix}_foo" has no schema in version "1" registered`,
success: false,
},
'response payload match',
Expand Down
4 changes: 2 additions & 2 deletions src/registry/federation/get-composed-schema-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ export default function getComposedSchemaVersions(fastify: FastifyInstance) {
throw InvalidGraphNameError(req.body.graph_name)
}

const allServiceVersions: ServiceVersionMatch[] = req.body.services.map((s) => ({
const allServicesWithVersion: ServiceVersionMatch[] = req.body.services.map((s) => ({
name: s.name,
version: s.version,
}))

const schmemaService = new SchemaService(fastify.prisma)
const { schemas, error: findError } = await schmemaService.findByServiceVersions(
req.body.graph_name,
allServiceVersions,
allServicesWithVersion,
)

if (findError) {
Expand Down
24 changes: 16 additions & 8 deletions src/registry/federation/get-composed-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const schema: FastifySchema = {

export default function getComposedSchema(fastify: FastifyInstance) {
fastify.get<RequestContext>('/schema/latest', { schema }, async (req, res) => {
const graph = await fastify.prisma.graph.findMany({
const graph = await fastify.prisma.graph.findFirst({
where: {
name: req.query.graph_name,
isActive: true,
Expand All @@ -46,26 +46,34 @@ export default function getComposedSchema(fastify: FastifyInstance) {
}

const serviceModels = await fastify.prisma.service.findMany({
where: {
isActive: true,
graph: {
name: req.query.graph_name,
},
},
orderBy: {
updatedAt: 'desc',
},
select: {
name: true,
},
})
const allServiceNames = serviceModels.map((s) => s.name)
const allServiceVersions = allServiceNames.map((s) => ({
name: s,
}))

if (!allServiceNames.length) {
if (serviceModels.length === 0) {
return res.send({
success: true,
data: [],
})
}

const allLatestServices = serviceModels.map((s) => ({
name: s.name,
}))

const schmemaService = new SchemaService(fastify.prisma)
const { schemas, error: findError } = await schmemaService.findByServiceVersions(
req.query.graph_name,
allServiceVersions,
allLatestServices,
)

if (findError) {
Expand Down
4 changes: 3 additions & 1 deletion src/registry/federation/register-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ test('Should be able to register schemas from multiple clients', async (t) => {
typeDefs: `type Query { hello: String }`,
version: '1',
})


})

test('Should not be able to push invalid schema', async (t) => {
Expand Down Expand Up @@ -379,7 +381,7 @@ test('Should return 400 because an service has no active schema registered', asy
})

t.is(res.statusCode, 400)
t.deepEqual(res.json().error, `Service "${t.context.testPrefix}_foo" has no schema registered`)
t.deepEqual(res.json().error, `In graph "${t.context.graphName}", service "${t.context.testPrefix}_foo" has no schema registered`)
})

test('Should be able to register a schema with a valid JWT', async (t) => {
Expand Down
Loading

0 comments on commit c1fef5e

Please sign in to comment.