-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
libang
committed
Dec 25, 2018
1 parent
303db9f
commit b8f4175
Showing
12 changed files
with
162 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ Thumbs.db | |
*.swp | ||
config.yml | ||
**/node_modules/ | ||
.vscode | ||
**/.vscode | ||
**/dist/ |
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,16 @@ | ||
import { MiddlewareFn } from 'type-graphql'; | ||
|
||
export const ErrorInterceptor: MiddlewareFn = async ( | ||
{ context, info }, | ||
next | ||
) => { | ||
try { | ||
return await next(); | ||
} catch (err) { | ||
// write error to file log | ||
console.log(err, context, info); | ||
|
||
// rethrow the error | ||
throw err; | ||
} | ||
}; |
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,4 @@ | ||
export { ErrorInterceptor } from './error-interceptor'; | ||
export { LogAccessMiddleware } from './log-access'; | ||
export { ResolveTime } from './resolve-time'; | ||
export { DataInterceptor } from './result-format'; |
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,11 @@ | ||
import { MiddlewareInterface, NextFn, ResolverData } from 'type-graphql'; | ||
|
||
export class LogAccessMiddleware implements MiddlewareInterface<any> { | ||
async use({ context, info }: ResolverData<any>, next: NextFn) { | ||
const username: string = context.username || 'guest'; | ||
console.log( | ||
`Logging access: ${username} -> ${info.parentType.name}.${info.fieldName}` | ||
); | ||
return next(); | ||
} | ||
} |
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,12 @@ | ||
import { MiddlewareFn } from 'type-graphql'; | ||
|
||
export const ResolveTime: MiddlewareFn = async ({ info }, next) => { | ||
const isRoot = info.parentType.name === 'Query'; | ||
const start = Date.now(); | ||
await next(); | ||
const resolveTime = Date.now() - start; | ||
if (isRoot) | ||
console.log( | ||
`${info.parentType.name}.${info.fieldName} [${resolveTime} ms]` | ||
); | ||
}; |
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 { MiddlewareFn } from 'type-graphql'; | ||
|
||
export const DataInterceptor: MiddlewareFn = async ({ info }, next) => { | ||
let result = await next(); | ||
|
||
if (info.fieldName === 'emp_no') result = 'ID:' + result; | ||
return result; | ||
}; |
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,21 @@ | ||
import { AuthChecker } from 'type-graphql'; | ||
|
||
export const authChecker: AuthChecker<any> = ({ context: { user } }, roles) => { | ||
if (roles.length === 0) { | ||
// if `@Authorized()`, check only is user exist | ||
return user !== undefined; | ||
} | ||
// there are some roles defined now | ||
|
||
if (!user) { | ||
// and if no user, restrict access | ||
return false; | ||
} | ||
if (user.roles.some(role => roles.includes(role))) { | ||
// grant access if the roles overlap | ||
return true; | ||
} | ||
|
||
// no roles matched, restrict access | ||
return false; | ||
}; |