From 905c74a7e633be899c862b6a5be57f2472a27954 Mon Sep 17 00:00:00 2001 From: Dan Caddigan Date: Sun, 9 Jun 2019 21:38:50 -0400 Subject: [PATCH] fix(graphql-binding): adds error utility (#110) --- .vscode/launch.json | 8 ++++++-- package.json | 2 +- src/core/server.ts | 8 +++++--- src/gql/binding.ts | 26 ++++++++++++++++++++++++++ src/index.ts | 1 + src/tgql/BaseResolver.ts | 2 +- tsconfig.json | 1 + yarn.lock | 23 +++++++++++++++++------ 8 files changed, 58 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 8b55394d..7dc31058 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -25,14 +25,18 @@ "protocol": "inspector" }, { - "name": "TS-Node", + "name": "ts-node", "type": "node", "request": "launch", "args": ["${relativeFile}"], "runtimeArgs": ["--nolazy", "-r", "ts-node/register"], "sourceMaps": true, "cwd": "${workspaceRoot}", - "protocol": "inspector" + "protocol": "inspector", + "env": { + "TS_NODE_IGNORE": "false", + "DEBUG": "*" + } } ] } diff --git a/package.json b/package.json index 65bfccbb..cb74c108 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,7 @@ "express": "^4.16.4", "faker": "^4.1.0", "graphql": "^14.0.2", - "graphql-binding": "^2.4.0", + "graphql-binding": "^2.5.1", "graphql-iso-date": "^3.6.1", "graphql-tools": "^4.0.3", "graphql-type-json": "^0.3.0", diff --git a/src/core/server.ts b/src/core/server.ts index d2c1d3eb..2a1412ae 100644 --- a/src/core/server.ts +++ b/src/core/server.ts @@ -186,14 +186,14 @@ export class Server { const contextGetter = this.appOptions.context || - (() => { + (async () => { return {}; }); debug('start:ApolloServerAllocation:start'); this.graphQLServer = new ApolloServer({ context: async (options: { req: Request }) => { - const consumerCtx = await contextGetter(options.req); + const consumerCtx = contextGetter(options.req); return { connection: this.connection, dataLoader: { @@ -231,7 +231,9 @@ export class Server { // Open playground in the browser if (this.shouldOpenPlayground()) { - open(url, { wait: false }); + // Assigning to variable and logging to appease linter + const process = open(url, { wait: false }); + debug('process', process); } debug('start:end'); diff --git a/src/gql/binding.ts b/src/gql/binding.ts index 4f8f0807..e0831c80 100644 --- a/src/gql/binding.ts +++ b/src/gql/binding.ts @@ -89,4 +89,30 @@ export async function generateBindingFile(inputSchemaPath: string, outputBinding debug('generateBindingFile:end'); } +export function getBindingError(err: any) { + const error = getOriginalError(err); + if ( + error && + error.extensions && + error.extensions.exception && + error.extensions.exception.validationErrors + ) { + error.extensions.exception.validationErrors.forEach((item: any) => { + error.validationErrors = error.validationErrors || {}; + error.validationErrors[item.property] = item.constraints; + }); + } + return error; +} + +export function getOriginalError(error: any): any { + if (error.originalError) { + return getOriginalError(error.originalError); + } + if (error.errors) { + return error.errors.map(getOriginalError)[0]; + } + return error; +} + export { Binding }; diff --git a/src/index.ts b/src/index.ts index 63251393..f79a65a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export * from './core'; export * from './decorators'; +export * from './gql'; export * from './middleware'; export * from './schema'; export * from './tgql'; diff --git a/src/tgql/BaseResolver.ts b/src/tgql/BaseResolver.ts index 35748ebe..a3ca7957 100644 --- a/src/tgql/BaseResolver.ts +++ b/src/tgql/BaseResolver.ts @@ -1,4 +1,4 @@ -import { DeepPartial, Repository } from 'typeorm'; +import { Repository } from 'typeorm'; import { BaseModel, BaseService, WhereInput } from '../core'; diff --git a/tsconfig.json b/tsconfig.json index e8470b12..af3dd32e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,6 +16,7 @@ "noImplicitReturns": true, "noImplicitThis": true, "noUnusedLocals": true, + "skipLibCheck": true, "strict": true, "strictNullChecks": true, "types": ["jest", "isomorphic-fetch", "node"] diff --git a/yarn.lock b/yarn.lock index 896f3d9a..d5da5f0f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3159,14 +3159,14 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6 resolved "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00" integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA== -graphql-binding@^2.4.0: - version "2.4.1" - resolved "https://registry.npmjs.org/graphql-binding/-/graphql-binding-2.4.1.tgz#5880b78670b5910b3589a6fa76441e9a7d8c289c" - integrity sha512-UHSVM2bfB4gGtwRcUXwejo2R825Rm8vnJGA6+IB+NquObXBdw6YvDSTIwgavsNM6I6qRlyl0CUaAf/r15YYqwQ== +graphql-binding@^2.5.1: + version "2.5.1" + resolved "https://registry.npmjs.org/graphql-binding/-/graphql-binding-2.5.1.tgz#cacd8d579c4098541dadb9325255c09e476ab65c" + integrity sha512-9Lejxa7h/WcNZYyTvFQG12QUK8pkgbeEbEnMw3o/DsRhlIB8hrh+a+mbJgHpZ+dcUzSBGyTByiLAI3OLg7B//g== dependencies: graphql "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0" graphql-import "^0.7.1" - graphql-tools "4.0.3" + graphql-tools "4.0.4" iterall "1.2.2" object-path-immutable "^3.0.0" resolve-cwd "^2.0.0" @@ -3219,7 +3219,18 @@ graphql-tag@^2.9.2: resolved "https://registry.npmjs.org/graphql-tag/-/graphql-tag-2.10.0.tgz#87da024be863e357551b2b8700e496ee2d4353ae" integrity sha512-9FD6cw976TLLf9WYIUPCaaTpniawIjHWZSwIRZSjrfufJamcXbVVYfN2TWvJYbw0Xf2JjYbl1/f2+wDnBVw3/w== -graphql-tools@4.0.3, graphql-tools@^4.0.0, graphql-tools@^4.0.3: +graphql-tools@4.0.4: + version "4.0.4" + resolved "https://registry.npmjs.org/graphql-tools/-/graphql-tools-4.0.4.tgz#ca08a63454221fdde825fe45fbd315eb2a6d566b" + integrity sha512-chF12etTIGVVGy3fCTJ1ivJX2KB7OSG4c6UOJQuqOHCmBQwTyNgCDuejZKvpYxNZiEx7bwIjrodDgDe9RIkjlw== + dependencies: + apollo-link "^1.2.3" + apollo-utilities "^1.0.1" + deprecated-decorator "^0.1.6" + iterall "^1.1.3" + uuid "^3.1.0" + +graphql-tools@^4.0.0, graphql-tools@^4.0.3: version "4.0.3" resolved "https://registry.npmjs.org/graphql-tools/-/graphql-tools-4.0.3.tgz#23b5cb52c519212b1b2e4630a361464396ad264b" integrity sha512-NNZM0WSnVLX1zIMUxu7SjzLZ4prCp15N5L2T2ro02OVyydZ0fuCnZYRnx/yK9xjGWbZA0Q58yEO//Bv/psJWrg==