From 5febcbc32d19ca258bff55bcbd0b3f8a7c7bd594 Mon Sep 17 00:00:00 2001 From: Matic Zavadlal Date: Wed, 28 Mar 2018 14:30:40 +0200 Subject: [PATCH] move document middleware, prettier fixes --- package.json | 13 ++++++--- src/index.ts | 77 ---------------------------------------------------- src/types.ts | 29 ++++---------------- test.js | 75 ++------------------------------------------------ tslint.json | 7 +++++ yarn.lock | 24 ++++++++++++++-- 6 files changed, 46 insertions(+), 179 deletions(-) create mode 100644 tslint.json diff --git a/package.json b/package.json index 67b60328..2fbf9b8b 100644 --- a/package.json +++ b/package.json @@ -15,21 +15,24 @@ "ava": "^0.25.0", "graphql-tools": "^2.21.0", "prettier": "^1.11.1", + "prettier-check": "^2.0.0", "semantic-release": "^12.4.1", "tslint": "^5.9.1", - "tslint-config-prettier": "^1.7.0", + "tslint-config-prettier": "^1.10.0", "tslint-config-standard": "^7.0.0", "typescript": "^2.7.1" }, "scripts": { "prepublish": "npm run test", "build": "rm -rf dist && tsc -d", - "lint": "tslint --project tsconfig.json {src}/**/*.ts", + "lint": "tslint --project tsconfig.json {src}/**/*.ts && prettier-check --ignore-path .gitignore {src,.}/{*.ts,*.js}", "test-ava": "ava test.js --verbose", "test": "npm run lint && npm run build && npm run test-ava", "semantic-release": "semantic-release" }, - "files": ["dist"], + "files": [ + "dist" + ], "release": { "branch": "master" }, @@ -41,7 +44,9 @@ "bugs": { "url": "https://github.com/graphcool/graphql-middleware/issues" }, - "keywords": ["graphql"], + "keywords": [ + "graphql" + ], "prettier": { "semi": false, "trailingComma": "all", diff --git a/src/index.ts b/src/index.ts index 4a164ab6..e316aaeb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,6 @@ import { import { mergeSchemas } from 'graphql-tools' import { IResolvers } from 'graphql-tools/dist/Interfaces' import { - IDocumentMiddlewareFunction, IFieldMiddleware, IFieldMiddlewareFunction, IFieldMiddlewareTypeMap, @@ -51,28 +50,6 @@ function wrapResolverInMiddleware( } } -// Inspired by graphql-tools. -function transformDocumentMiddlewareToFieldMiddleware( - middleware: IDocumentMiddlewareFunction, -): IFieldMiddlewareFunction { - let value = null - const randomNumber = Math.random() - - return async (resolve, parent, args, ctx, info) => { - if (!info.operation['__runAtMostOnce']) { - info.operation['__runAtMostOnce'] = {} - } - - if (!info.operation['__runAtMostOnce'][randomNumber]) { - info.operation['__runAtMostOnce'][randomNumber] = true - value = await middleware(resolve, parent, ctx, info) - return value - } - - return value - } -} - // Merge function applyMiddlewareToField( @@ -171,32 +148,6 @@ function generateResolverFromSchemaAndFieldMiddleware( } } -function generateResolverFromSchemaAndDocumentMiddleware( - schema: GraphQLSchema, - middleware: IDocumentMiddlewareFunction, -): IResolvers { - const typeMap = { - Query: schema.getQueryType(), - Mutation: schema.getMutationType(), - Subscription: schema.getSubscriptionType(), - } - - const resolvers = Object.keys(typeMap) - .filter(type => isGraphQLObjectType(typeMap[type])) - .reduce( - (resolvers, type) => ({ - ...resolvers, - [type]: applyMiddlewareToType( - typeMap[type] as GraphQLObjectType, - transformDocumentMiddlewareToFieldMiddleware(middleware), - ), - }), - {}, - ) - - return {} -} - // Reducers function addFieldMiddlewareToSchema( @@ -214,21 +165,6 @@ function addFieldMiddlewareToSchema( }) } -function addDocumentMiddlewareToSchema( - schema: GraphQLSchema, - middleware: IDocumentMiddlewareFunction, -): GraphQLSchema { - const resolvers = generateResolverFromSchemaAndDocumentMiddleware( - schema, - middleware, - ) - - return mergeSchemas({ - schemas: [schema], - resolvers, - }) -} - // Exposed functions export function applyFieldMiddleware( @@ -243,16 +179,3 @@ export function applyFieldMiddleware( return schemaWithMiddleware } - -export function applyDocumentMiddleware( - schema: GraphQLSchema, - ...middlewares: IDocumentMiddlewareFunction[] -): GraphQLSchema { - const schemaWithMiddleware = middlewares.reduce( - (currentSchema, middleware) => - addDocumentMiddlewareToSchema(currentSchema, middleware), - schema, - ) - - return schemaWithMiddleware -} diff --git a/src/types.ts b/src/types.ts index 55a46e82..0a43ca59 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,38 +1,21 @@ -import { - GraphQLResolveInfo, - GraphQLFieldResolver, - GraphQLField -} from "graphql"; +import { GraphQLResolveInfo, GraphQLFieldResolver, GraphQLField } from 'graphql' export type IFieldMiddlewareFunction = ( resolve: GraphQLFieldResolver, parent: any, args: any, context: any, - info: GraphQLResolveInfo -) => Promise; + info: GraphQLResolveInfo, +) => Promise export interface IFieldMiddlewareTypeMap { - [key: string]: IFieldMiddlewareFunction | IFieldMiddlewareFieldMap; + [key: string]: IFieldMiddlewareFunction | IFieldMiddlewareFieldMap } export interface IFieldMiddlewareFieldMap { - [key: string]: IFieldMiddlewareFunction; + [key: string]: IFieldMiddlewareFunction } export type IFieldMiddleware = | IFieldMiddlewareFunction - | IFieldMiddlewareTypeMap; - -export interface GraphQLResponse { - data: any; - errors?: any[]; - extensions?: any; -} - -export type IDocumentMiddlewareFunction = ( - execute: Function, - rootValue: any, - context: any, - info: GraphQLResolveInfo -) => Promise; + | IFieldMiddlewareTypeMap diff --git a/test.js b/test.js index 319c3798..e19ffac0 100644 --- a/test.js +++ b/test.js @@ -83,24 +83,6 @@ const subscriptionMiddleware = { }, } -// Document - -const documentMiddleware = async (execute, rootValue, context, info) => { - -} - -const trackDocumentMiddlewareExecution = t => async ( - execute, - rootValue, - context, - info, -) => { - t.pass() - return execute(rootValue, context, info) -} - -const partialDocumentMiddleware = async resolve => resolve() - // Test ---------------------------------------------------------------------- // Field @@ -125,7 +107,7 @@ test('Field middleware - Mixed middlewares', async t => { t.deepEqual(res, { data: { hello: 'Well Hello Bob and beep!', - nothing: 'Well nothing' + nothing: 'Well nothing', }, }) }) @@ -148,7 +130,7 @@ test('Field middleware - Function Middleware', async t => { t.deepEqual(res, { data: { hello: 'Hello Bob and Trump!', - nothing: 'nothing' + nothing: 'nothing', }, }) }) @@ -192,58 +174,7 @@ test('Field middleware - Partial resolver arguments', async t => { t.deepEqual(res, { data: { hello: 'Hello Emma!', - nothing: 'nothing' + nothing: 'nothing', }, }) }) - -// Document - -// test('Document middleware', async t => { -// const schema = getSchema() -// const schemaWithDocumentMiddlewares = applyDocumentMiddleware( -// schema, -// documentMiddleware -// ) - -// const query = ` - -// ` - -// t.pass() -// }) - -// test('Document middleware - execute only once per request', async t => { -// t.plan(1) - -// const schema = getSchema() -// const schemaWithDocumentMiddlewares = applyDocumentMiddleware( -// schema, -// trackDocumentMiddlewareExecution(t), -// ) - -// const query = ` -// query { -// hello(name: "Trump") -// nothing -// } -// ` -// const res = await graphql(schemaWithDocumentMiddlewares, query) -// }) - -// test('Document middleware - partial resolver', async t => { -// const schema = getSchema() -// const schemaWithDocumentMiddlewares = applyDocumentMiddleware( -// schema, - -// ) - -// const query = ` -// { -// hello(name: "Trump") -// } -// ` -// const res = await graphql(schemaWithDocumentMiddlewares, query) - -// t.pass() -// }) \ No newline at end of file diff --git a/tslint.json b/tslint.json new file mode 100644 index 00000000..cd6c0296 --- /dev/null +++ b/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": ["tslint-config-standard", "tslint-config-prettier"], + "rules": { + "no-use-before-declare": false, + "space-before-function-paren": false + } +} diff --git a/yarn.lock b/yarn.lock index ab7f75d5..d0061aef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1384,6 +1384,18 @@ esutils@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" +execa@^0.6.0: + version "0.6.3" + resolved "https://registry.yarnpkg.com/execa/-/execa-0.6.3.tgz#57b69a594f081759c69e5370f0d17b9cb11658fe" + dependencies: + cross-spawn "^5.0.1" + get-stream "^3.0.0" + is-stream "^1.1.0" + npm-run-path "^2.0.0" + p-finally "^1.0.0" + signal-exit "^3.0.0" + strip-eof "^1.0.0" + execa@^0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777" @@ -2897,6 +2909,12 @@ preserve@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b" +prettier-check@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/prettier-check/-/prettier-check-2.0.0.tgz#edd086ee12d270579233ccb136a16e6afcfba1ae" + dependencies: + execa "^0.6.0" + prettier@^1.11.1: version "1.11.1" resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.11.1.tgz#61e43fc4cd44e68f2b0dfc2c38cd4bb0fccdcc75" @@ -3638,9 +3656,9 @@ tslib@^1.0.0, tslib@^1.8.0, tslib@^1.8.1: version "1.9.0" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.0.tgz#e37a86fda8cbbaf23a057f473c9f4dc64e5fc2e8" -tslint-config-prettier@^1.7.0: - version "1.9.0" - resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.9.0.tgz#391887644b66de4623f745a6c85672405cbcdcee" +tslint-config-prettier@^1.10.0: + version "1.10.0" + resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.10.0.tgz#5063c413d43de4f6988c73727f65ecfc239054ec" tslint-config-standard@^7.0.0: version "7.0.0"