Skip to content

Commit 7c4ce05

Browse files
committed
Define public and private scope to the auth directive
1 parent 0cab9cd commit 7c4ce05

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/service/worker/runtime/graphql/schema/schemaDirectives/Auth.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import { SchemaDirectiveVisitor } from 'graphql-tools'
55

66
import { ServiceContext } from '../../../typings'
77

8+
enum Scope {
9+
PUBLIC = 'PUBLIC',
10+
PRIVATE = 'PRIVATE'
11+
}
12+
813
interface AuthDirectiveArgs {
914
readonly productCode: string
1015
readonly resourceCode: string
16+
readonly scope: Scope
1117
}
1218

1319
interface VtexIdParsedToken {
@@ -69,7 +75,7 @@ async function auth (ctx: ServiceContext, authArgs: AuthDirectiveArgs): Promise<
6975

7076
function 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

8896
export const authDirectiveTypeDefs = `
97+
enum Scope {
98+
PUBLIC
99+
PRIVATE
100+
}
101+
89102
directive @auth(
103+
scope: Scope
90104
productCode: String
91105
resourceCode: String
92106
) on FIELD_DEFINITION
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defaultFieldResolver, GraphQLField } from "graphql";
2+
import { SchemaDirectiveVisitor } from "graphql-tools"
3+
4+
export class Public extends SchemaDirectiveVisitor {
5+
visitFieldDefinition(field: GraphQLField<any, any>) {
6+
const { resolve = defaultFieldResolver } = field;
7+
8+
field.resolve = async function (...args) {
9+
return resolve.apply(this, args);
10+
};
11+
}
12+
}
13+
14+
export const publicDirectiveTypeDefs = `directive @public on FIELD_DEFINITION`

src/service/worker/runtime/graphql/schema/schemaDirectives/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Auth, authDirectiveTypeDefs } from './Auth'
22
import { CacheControl, cacheControlDirectiveTypeDefs } from './CacheControl'
33
import { Deprecated, deprecatedDirectiveTypeDefs } from './Deprecated'
44
import { Metric, metricDirectiveTypeDefs } from './Metric'
5+
import { Public, publicDirectiveTypeDefs } from './Public'
56
import { SanitizeDirective, sanitizeDirectiveTypeDefs } from './Sanitize'
67
import { SettingsDirective, settingsDirectiveTypeDefs } from './Settings'
78
import { SmartCacheDirective, smartCacheDirectiveTypeDefs } from './SmartCacheDirective'
@@ -20,6 +21,7 @@ export const nativeSchemaDirectives = {
2021
smartcache: SmartCacheDirective,
2122
translatableV2: TranslatableV2,
2223
translateTo: TranslateTo,
24+
public: Public
2325
}
2426

2527
export const nativeSchemaDirectivesTypeDefs = [
@@ -32,4 +34,5 @@ export const nativeSchemaDirectivesTypeDefs = [
3234
smartCacheDirectiveTypeDefs,
3335
translatableV2DirectiveTypeDefs,
3436
translateToDirectiveTypeDefs,
37+
publicDirectiveTypeDefs
3538
].join('\n\n')

0 commit comments

Comments
 (0)