Skip to content

Commit

Permalink
PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb committed Jan 19, 2024
1 parent 24e98af commit 16e8325
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 52 deletions.
6 changes: 3 additions & 3 deletions src/GraphqlQueryStore.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-disable max-classes-per-file */
/* eslint-disable class-methods-use-this */
import { createHash } from 'crypto'
import { parse, specifiedRules, validate } from 'graphql'
import { GraphQLSchema, parse, specifiedRules, validate } from 'graphql'
import { compileQuery, isCompiledQuery } from 'graphql-jit'
import { createComplexityRule, simpleEstimator } from 'graphql-query-complexity'
import { GraphqlValidationError } from './errors'

export default class GraphqlQueryStore {
schema: any
schema: GraphQLSchema
validationRules: any
maximumComplexity: number
defaultComplexity: number
store: Map<string, any>

constructor(
schema: any,
schema: GraphQLSchema,
{
validationRules = [],
queryComplexity: { maximumComplexity = 1000, defaultComplexity = 1 } = {},
Expand Down
3 changes: 2 additions & 1 deletion src/createGraphqlRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingMessage } from 'http'
import processGraphqlRequest from './processGraphqlRequest'
import { readRequestBody } from './utils'

Expand All @@ -7,7 +8,7 @@ export default function createGraphqlRequestHandler(
processFileUploads?: any
) {
return async (
req: any,
req: IncomingMessage,
res: {
writeHead: (arg0: number, arg1?: { 'Content-Type': string } | undefined) => void
end: (arg0: string) => void
Expand Down
23 changes: 15 additions & 8 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ const UNAUTHORIZED_ERROR = 'UNAUTHORIZED_ERROR'
const USER_INPUT_ERROR = 'USER_INPUT_ERROR'

export class GraphqlError extends Error {
originalError: Error | null
code: string
originalError: any
code: string | null
extensions: any
static CODE: string
errors?: any

constructor(
message = 'Something went wrong',
code = INTERNAL_SERVER_ERROR,
originalError?: Error | null,
originalError?: any,
extensions?: {
errors: any
}
Expand Down Expand Up @@ -61,7 +61,7 @@ GraphqlValidationError.CODE = VALIDATION_ERROR
export class GraphqlContextError extends GraphqlError {
constructor(
message = 'Context creation failed.',
originalError?: Error | null,
originalError?: any,
extensions?: {
errors: any
}
Expand All @@ -76,8 +76,8 @@ GraphqlContextError.CODE = CONTEXT_ERROR
export class GraphqlUnauthorizedError extends GraphqlError {
constructor(
message = 'Unauthorized.',
originalError: Error | null,
extensions: {
originalError?: any,
extensions?: {
errors: any
}
) {
Expand All @@ -91,8 +91,8 @@ GraphqlUnauthorizedError.CODE = UNAUTHORIZED_ERROR
export class GraphqlUserInputError extends GraphqlError {
constructor(
message = 'Bad user input error.',
originalError: Error | null,
extensions: {
originalError?: any,
extensions?: {
errors: any
}
) {
Expand All @@ -102,3 +102,10 @@ export class GraphqlUserInputError extends GraphqlError {
}

GraphqlUserInputError.CODE = USER_INPUT_ERROR

export class GraphqlErrorWithMessageAndOriginalError extends GraphqlError {
constructor(message = 'Something went wrong', originalError: any) {
super(message, undefined, originalError)
this.name = 'GraphqlErrorWithMessageAndOriginalError'
}
}
7 changes: 4 additions & 3 deletions src/processGraphqlRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// eslint-disable-next-line import/extensions
import { IncomingMessage } from 'http'
import { GraphqlContextError, GraphqlValidationError } from './errors'
import { badRequest, badRequestJson, json, methodNotAllowed } from './responses'

Expand All @@ -9,7 +10,7 @@ type GraphqlResponse = {
}

export default async function processGraphqlRequest(
req: { method: string; headers: { [x: string]: any } },
req: IncomingMessage,
{
store,
context = {},
Expand All @@ -18,8 +19,8 @@ export default async function processGraphqlRequest(
}: {
store: any
context: any
processFileUploads: any
readRequestBody: (req: any) => Promise<unknown>
processFileUploads: (req: IncomingMessage) => Promise<any>
readRequestBody: (req: IncomingMessage) => Promise<any>
}
): Promise<GraphqlResponse> {
if (!store) {
Expand Down
47 changes: 10 additions & 37 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,32 @@
import { IncomingMessage, ServerResponse } from 'http'

const OK = 200
const METHOD_NOT_ALLOWED = 405
const BED_REQUEST = 400
const BAD_REQUEST = 400

export const readRequestBody = (req: {
on: (
arg0: string,
arg1: (chunk: any) => number
) => {
(): any
new (): any
on: {
(arg0: string, arg1: (error: any) => void): {
(): any
new (): any
on: { (arg0: string, arg1: () => void): void; new (): any }
}
new (): any
}
}
}) =>
export const readRequestBody = (req: IncomingMessage) =>
new Promise((resolve, reject) => {
const body: any[] = []
req.on('data', (chunk: any) => body.push(chunk))
.on('error', (error: Error) => reject(error))
.on('error', (error: any) => reject(error))
.on('end', () => resolve(JSON.parse(Buffer.concat(body).toString())))
})

export const methodNotAllowed = (
res: { writeHead: (arg0: number) => void; end: (arg0: string) => void },
message = 'Method Not Allowed'
) => {
export const methodNotAllowed = (res: ServerResponse, message = 'Method Not Allowed') => {
res.writeHead(METHOD_NOT_ALLOWED)
res.end(message)
}

export const badRequest = (
res: { writeHead: (arg0: number) => void; end: (arg0: string) => void },
message = 'Bad Request'
) => {
res.writeHead(BED_REQUEST)
export const badRequest = (res: ServerResponse, message = 'Bad Request') => {
res.writeHead(BAD_REQUEST)
res.end(message)
}

export const json = (
res: {
writeHead: (arg0: number, arg1: { 'Content-Type': string }) => void
end: (arg0: string) => void
},
result: any,
statusCode = OK
) => {
export const json = (res: ServerResponse, result: any, statusCode = OK) => {
res.writeHead(statusCode, {
'Content-Type': 'application/json',
})
res.end(JSON.stringify(result))
}

export const badRequestJson = (res: any, result: any) => json(res, result, BED_REQUEST)
export const badRequestJson = (res: any, result: any) => json(res, result, BAD_REQUEST)

0 comments on commit 16e8325

Please sign in to comment.