@@ -5,9 +5,15 @@ import { SchemaDirectiveVisitor } from 'graphql-tools'
55
66import { ServiceContext } from '../../../typings'
77
8+ enum Scope {
9+ PUBLIC = 'PUBLIC' ,
10+ PRIVATE = 'PRIVATE'
11+ }
12+
813interface AuthDirectiveArgs {
914 readonly productCode : string
1015 readonly resourceCode : string
16+ readonly scope : Scope
1117}
1218
1319interface VtexIdParsedToken {
@@ -69,7 +75,7 @@ async function auth (ctx: ServiceContext, authArgs: AuthDirectiveArgs): Promise<
6975
7076function parseArgs ( authArgs : AuthDirectiveArgs ) : AuthDirectiveArgs {
7177 if ( ! authArgs . productCode || ! authArgs . resourceCode ) {
72- throw new UserInputError ( 'Invalid auth schema directive args. Usage: @auth(productCode: String, resourceCode: String).' )
78+ throw new UserInputError ( 'Invalid auth schema directive args. Usage: @auth(scope: Scope, productCode: String, resourceCode: String).' )
7379 }
7480 return authArgs
7581}
@@ -79,14 +85,22 @@ export class Auth extends SchemaDirectiveVisitor {
7985 const { resolve = defaultFieldResolver } = field
8086 field . resolve = async ( root , args , ctx , info ) => {
8187 const authArgs = parseArgs ( this . args as AuthDirectiveArgs )
82- await auth ( ctx , authArgs )
88+ if ( ! authArgs . scope || authArgs . scope == Scope . PRIVATE ) {
89+ await auth ( ctx , authArgs )
90+ }
8391 return resolve ( root , args , ctx , info )
8492 }
8593 }
8694}
8795
8896export const authDirectiveTypeDefs = `
97+ enum Scope {
98+ PUBLIC
99+ PRIVATE
100+ }
101+
89102directive @auth(
103+ scope: Scope
90104 productCode: String
91105 resourceCode: String
92106) on FIELD_DEFINITION
0 commit comments