Skip to content

Commit

Permalink
move document middleware, prettier fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed Mar 28, 2018
1 parent 50cfe17 commit 5febcbc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 179 deletions.
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -41,7 +44,9 @@
"bugs": {
"url": "https://github.com/graphcool/graphql-middleware/issues"
},
"keywords": ["graphql"],
"keywords": [
"graphql"
],
"prettier": {
"semi": false,
"trailingComma": "all",
Expand Down
77 changes: 0 additions & 77 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import { mergeSchemas } from 'graphql-tools'
import { IResolvers } from 'graphql-tools/dist/Interfaces'
import {
IDocumentMiddlewareFunction,
IFieldMiddleware,
IFieldMiddlewareFunction,
IFieldMiddlewareTypeMap,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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
}
29 changes: 6 additions & 23 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
import {
GraphQLResolveInfo,
GraphQLFieldResolver,
GraphQLField
} from "graphql";
import { GraphQLResolveInfo, GraphQLFieldResolver, GraphQLField } from 'graphql'

export type IFieldMiddlewareFunction = (
resolve: GraphQLFieldResolver<any, any>,
parent: any,
args: any,
context: any,
info: GraphQLResolveInfo
) => Promise<any>;
info: GraphQLResolveInfo,
) => Promise<any>

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<GraphQLResponse>;
| IFieldMiddlewareTypeMap
75 changes: 3 additions & 72 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
},
})
})
Expand All @@ -148,7 +130,7 @@ test('Field middleware - Function Middleware', async t => {
t.deepEqual(res, {
data: {
hello: 'Hello Bob and Trump!',
nothing: 'nothing'
nothing: 'nothing',
},
})
})
Expand Down Expand Up @@ -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()
// })
7 changes: 7 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["tslint-config-standard", "tslint-config-prettier"],
"rules": {
"no-use-before-declare": false,
"space-before-function-paren": false
}
}
24 changes: 21 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 5febcbc

Please sign in to comment.