Skip to content

Commit

Permalink
use map as kv storage, return always string as error
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Apr 21, 2021
1 parent 22de7a7 commit d4feef6
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 100 deletions.
12 changes: 7 additions & 5 deletions src/federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface ServiceSchema {

export function composeAndValidateSchema(servicesSchemaMap: ServiceSchema[]) {
let schema
let errors: GraphQLError[] = []
let error = null

try {
const serviceList = servicesSchemaMap.map((schema) => {
Expand All @@ -29,10 +29,12 @@ export function composeAndValidateSchema(servicesSchemaMap: ServiceSchema[]) {
errors: validationErrors,
} = composeAndValidate(serviceList)
schema = validatedSchema
errors = validationErrors || []
} catch (error) {
errors = [error]
if (validationErrors && validationErrors.length > 0) {
error = `${validationErrors[0]}`
}
} catch (err) {
error = `${err.message}`
}

return { schema, errors }
return { schema, error }
}
27 changes: 8 additions & 19 deletions src/routes/add-persisted-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import { addPersistedQuery } from './add-persisted-query'
import { NewNamespace, Request, Response } from '../test-utils'

test.serial('Should store PQ from KV', async (t) => {
const store = NewNamespace(
{
name: 'PERSISTED_QUERIES',
},
[],
)
const store = NewNamespace({
name: 'PERSISTED_QUERIES',
})

const req = Request('POST', '', { key: '123', query: 'query' })
const res = Response()
Expand All @@ -17,22 +14,14 @@ test.serial('Should store PQ from KV', async (t) => {
t.deepEqual(res.body as any, {
success: true,
})
t.deepEqual(store, [
{
key: 'pq::123',
value: 'query',
},
])
t.deepEqual(store, new Map([['pq::123', 'query']]))
})
test.serial(
'Should return validation error because no query was provided',
async (t) => {
const store = NewNamespace(
{
name: 'PERSISTED_QUERIES',
},
[],
)
const store = NewNamespace({
name: 'PERSISTED_QUERIES',
})

const req = Request('POST', '', { key: '123' })
const res = Response()
Expand All @@ -43,6 +32,6 @@ test.serial(
success: false,
error: 'At path: query -- Expected a string, but received: undefined',
})
t.deepEqual(store, [])
t.deepEqual(store, new Map())
},
)
6 changes: 3 additions & 3 deletions src/routes/get-composed-schema-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export const getComposedSchemaByVersions: Handler = async function (req, res) {
typeDefs: s.type_defs,
}))

const { errors } = composeAndValidateSchema(serviceSchemas)
const { error: schemaError } = composeAndValidateSchema(serviceSchemas)

if (errors.length > 0) {
if (error) {
return res.send(400, {
success: false,
error: errors,
error: schemaError,
})
}

Expand Down
9 changes: 3 additions & 6 deletions src/routes/get-composed-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { getComposedSchema } from './get-composed-schema'
import { registerSchema } from './register-schema'

test.serial('Should return schema of two services', async (t) => {
NewNamespace(
{
name: 'SERVICES',
},
[],
)
NewNamespace({
name: 'SERVICES',
})

let req = Request('POST', '', {
type_defs: 'type Query { hello: String }',
Expand Down
6 changes: 3 additions & 3 deletions src/routes/get-composed-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export const getComposedSchema: Handler = async function (req, res) {
typeDefs: s.type_defs,
}))

const { errors } = composeAndValidateSchema(serviceSchemas)
const { error: schemaError } = composeAndValidateSchema(serviceSchemas)

if (errors.length > 0) {
if (schemaError) {
return res.send(400, {
success: false,
error: errors,
error: schemaError,
})
}

Expand Down
17 changes: 4 additions & 13 deletions src/routes/get-persisted-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ test.serial('Should load PQ from KV', async (t) => {
{
name: 'PERSISTED_QUERIES',
},
[
{
key: key_item('123'),
value: '123',
},
],
new Map([[key_item('123'), '123']]),
)

const req = Request('GET', 'key=123')
const res = Response()
await getPersistedQuery(req, res)
Expand All @@ -27,12 +21,9 @@ test.serial('Should load PQ from KV', async (t) => {
})

test.serial('Should return 404 when key does not exist', async (t) => {
NewNamespace(
{
name: 'PERSISTED_QUERIES',
},
[],
)
NewNamespace({
name: 'PERSISTED_QUERIES',
})

const req = Request('GET', 'key=123')
const res = Response()
Expand Down
9 changes: 3 additions & 6 deletions src/routes/get-schema-diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { getSchemaDiff } from './get-schema-diff'
import { registerSchema } from './register-schema'

test.serial('Should calculate schema diff', async (t) => {
NewNamespace(
{
name: 'SERVICES',
},
[],
)
NewNamespace({
name: 'SERVICES',
})

let req = Request('POST', '', {
type_defs: 'type Query { hello: String }',
Expand Down
8 changes: 4 additions & 4 deletions src/routes/get-schema-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export const getSchemaDiff: Handler = async function (req, res) {
if (!original.schema) {
return res.send(400)
}
if (original.errors.length > 0) {
if (original.error) {
return res.send(400, {
success: false,
error: original.errors,
error: original.error,
})
}

Expand All @@ -69,10 +69,10 @@ export const getSchemaDiff: Handler = async function (req, res) {
if (!updated.schema) {
return res.send(400)
}
if (updated.errors.length > 0) {
if (updated.error) {
return res.send(400, {
success: false,
error: updated.errors,
error: updated.error,
})
}

Expand Down
13 changes: 8 additions & 5 deletions src/routes/get-schema-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ test.serial('Should validate schema as valid', async (t) => {
NewNamespace(
{
name: 'SERVICES',
},
[],
}
)

let req = Request('POST', '', {
Expand All @@ -28,8 +27,7 @@ test.serial('Should validate schema as invalid', async (t) => {
NewNamespace(
{
name: 'SERVICES',
},
[],
}
)

let req = Request('POST', '', {
Expand All @@ -43,5 +41,10 @@ test.serial('Should validate schema as invalid', async (t) => {

t.is(res.statusCode, 400)
t.is(body.success, false)
t.truthy(body.error)


t.deepEqual(body as any, {
success: false,
error: 'Error: Unknown type: "String22".',
})
})
4 changes: 2 additions & 2 deletions src/routes/get-schema-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export const getSchemaValidation: Handler = async function (req, res) {
if (!updated.schema) {
return res.send(400)
}
if (updated.errors.length > 0) {
if (updated.error) {
return res.send(400, {
success: false,
error: updated.errors,
error: updated.error,
})
}

Expand Down
41 changes: 17 additions & 24 deletions src/routes/register-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import { getComposedSchema } from './get-composed-schema'
import { registerSchema } from './register-schema'

test.serial('Should register new schema', async (t) => {
NewNamespace(
{
name: 'SERVICES',
},
[],
)
NewNamespace({
name: 'SERVICES',
})

let req = Request('POST', '', {
type_defs: 'type Query { hello: String }',
Expand Down Expand Up @@ -62,12 +59,9 @@ test.serial('Should register new schema', async (t) => {
test.serial(
'Should not create multiple schemas when type_defs does not change',
async (t) => {
NewNamespace(
{
name: 'SERVICES',
},
[],
)
NewNamespace({
name: 'SERVICES',
})

let req = Request('POST', '', {
type_defs: 'type Query { hello: String }',
Expand Down Expand Up @@ -134,12 +128,9 @@ test.serial(
)

test.serial('Should register schemas from multiple clients', async (t) => {
NewNamespace(
{
name: 'SERVICES',
},
[],
)
NewNamespace({
name: 'SERVICES',
})

let req = Request('POST', '', {
type_defs: 'type Query { hello: String }',
Expand Down Expand Up @@ -193,12 +184,9 @@ test.serial('Should register schemas from multiple clients', async (t) => {
})

test.serial('Should not be possible to push invalid schema', async (t) => {
NewNamespace(
{
name: 'SERVICES',
},
[],
)
NewNamespace({
name: 'SERVICES',
})

let req = Request('POST', '', {
type_defs: 'foo',
Expand All @@ -210,6 +198,11 @@ test.serial('Should not be possible to push invalid schema', async (t) => {

t.is(res.statusCode, 400)

t.deepEqual(res.body as any, {
success: false,
error: 'Syntax Error: Unexpected Name "foo".',
})

const body = (res.body as any) as ErrorResponse

t.is(body.success, false)
Expand Down
6 changes: 3 additions & 3 deletions src/routes/register-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ export const registerSchema: Handler = async function (req, res) {
typeDefs: input.type_defs,
})

const { errors } = composeAndValidateSchema(serviceSchemas)
const { error: schemaError } = composeAndValidateSchema(serviceSchemas)

if (errors.length > 0) {
if (schemaError) {
return res.send(400, {
success: false,
error: errors,
error: schemaError,
})
}

Expand Down
13 changes: 6 additions & 7 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,19 @@ export const Request = (

export const NewNamespace = (
bindingConfig: { name: string },
store: { key: string; value: any }[],
store: Map<string, any> = new Map(),
) => {
let binding = Namespace()
binding.get = (key: string, format: string) => {
const m = store.findIndex((i) => i.key === `${key}`)
if (m !== -1) {
return Promise.resolve(
format === 'json' ? JSON.parse(store[m].value) : store[m].value,
)
const m = store.has(key)
if (m) {
const val = store.get(key)
return Promise.resolve(format === 'json' ? JSON.parse(val) : val)
}
return Promise.resolve(null)
}
binding.put = (key: string, value: any) => {
store.unshift({ key, value })
store.set(key, value)
return Promise.resolve()
}
// @ts-ignore
Expand Down

0 comments on commit d4feef6

Please sign in to comment.