forked from Quramy/graphql-decorator
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from indigotech/feature/after-decorator
Add @after decorator
- Loading branch information
Showing
26 changed files
with
262 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { AfterMiddleware } from '../middleware'; | ||
import { AfterOption } from '../metadata'; | ||
import { getMetadataArgsStorage } from '../metadata-builder'; | ||
|
||
/** | ||
* Adding ability to declarative override function resolution on schemas with an implementation analog to an express middleware. | ||
* | ||
* Usage example: | ||
* ``` | ||
* let middleware: AfterMiddleware = ( | ||
* context: any, | ||
* args: { [key: string]: any }, | ||
* result: Promise<any> | any, | ||
* next: (error?: Error, value?: any) => any | ||
* ): any => { | ||
* if(context.user.role != 'any role') { | ||
* // can use context and resolve/return value as `1000`, for example, regardless of what resolve function actually implements | ||
* next(null, 1000); | ||
* } else { | ||
* // keeps regular resolution flow | ||
* next(); | ||
* } | ||
* }; | ||
* | ||
* ... | ||
* | ||
* class ObjType { | ||
* @Field() | ||
* @After(middleware) | ||
* myFunction(input: number): number { | ||
* return input * 2; | ||
* } | ||
* } | ||
* | ||
* ``` | ||
* @param option Options for an Schema | ||
*/ | ||
export function After(option: AfterOption | AfterMiddleware) { | ||
return function (target: any, propertyKey: any, index: number) { | ||
getMetadataArgsStorage().afters.push({ | ||
target: target, | ||
name: target.name || propertyKey, | ||
description: option && (option as AfterOption).description ? (option as AfterOption).description : null, | ||
index: index, | ||
property: propertyKey, | ||
middleware: option && (option as AfterOption).middleware ? (option as AfterOption).middleware : option as AfterMiddleware, | ||
}); | ||
} as Function; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { AfterMiddleware } from '../../middleware'; | ||
import { Argument } from './argument'; | ||
|
||
export interface AfterArg extends Argument { | ||
index: number; | ||
property: string; | ||
middleware: AfterMiddleware; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { Argument } from './argument'; | ||
import { Middleware } from '../../middleware'; | ||
import { BeforeMiddleware } from '../../middleware'; | ||
|
||
export interface BeforeArg extends Argument { | ||
index: number; | ||
property: string; | ||
middleware: Middleware; | ||
middleware: BeforeMiddleware; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { AfterMiddleware } from '../../middleware'; | ||
import { Option } from './option'; | ||
|
||
/** | ||
* After (aka AfterMiddleware) options | ||
*/ | ||
export interface AfterOption extends Option { | ||
/** | ||
* Middeware to change resolver behavior. | ||
* Check decorator docs for example usage. | ||
*/ | ||
middleware: AfterMiddleware; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { AfterMiddleware } from '../../middleware'; | ||
import { FieldArgumentMetadata } from './field-argument.metadata'; | ||
|
||
export interface AfterMetadata extends FieldArgumentMetadata { | ||
middleware: AfterMiddleware; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { BeforeMiddleware } from '../../middleware'; | ||
import { FieldArgumentMetadata } from './field-argument.metadata'; | ||
import { Middleware } from '../../middleware'; | ||
|
||
export interface BeforeMetadata extends FieldArgumentMetadata { | ||
middleware: Middleware; | ||
middleware: BeforeMiddleware; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type AfterMiddleware = ( | ||
context: any, | ||
args: { [key: string]: any }, | ||
result: Promise<any> | any, | ||
next: (error?: Error, value?: any) => any, | ||
) => Promise<any> | any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type BeforeMiddleware = ( | ||
context: any, | ||
args: { [key: string]: any }, | ||
next: (error?: Error, value?: any) => any, | ||
) => Promise<any> | any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './after.middleware'; | ||
export * from './before.middleware'; | ||
export * from './pagination.middleware'; |
8 changes: 4 additions & 4 deletions
8
src/pagination.middleware.ts → src/middleware/pagination.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.