deno.land / x / pothos@release-1713397530 / packages / plugin-scope-auth / steps.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// @ts-nocheckimport { GraphQLFieldResolver } from 'https://cdn.skypack.dev/graphql?dts';import { isThenable, SchemaTypes } from '../core/index.ts';import { FieldAuthScopes, FieldGrantScopes, ResolveStep, TypeAuthScopes, TypeGrantScopes, } from './types.ts';export function createTypeAuthScopesStep<Types extends SchemaTypes>(authScopes: TypeAuthScopes<Types, unknown>, type: string): ResolveStep<Types> { if (typeof authScopes === "function") { return { run: (state, parent, args, context, info) => state.evaluateTypeScopeFunction(authScopes, type, parent, info), errorMessage: `Not authorized to read fields for ${type}`, }; } return { run: (state, parent, args, context, info) => state.evaluateScopeMap(authScopes, info), errorMessage: `Not authorized to read fields for ${type}`, };}export function createTypeGrantScopesStep<Types extends SchemaTypes>(grantScopes: TypeGrantScopes<Types, unknown>, type: string, forField: boolean): ResolveStep<Types> { return { run: (state, parent, args, context, info) => state.grantTypeScopes(type, parent, forField ? info.path.prev : info.path, () => grantScopes(parent, context)), errorMessage: `Unknown error creating grants for ${type}`, };}export function createFieldAuthScopesStep<Types extends SchemaTypes>(authScopes: FieldAuthScopes<Types, {}, {}>): ResolveStep<Types> { if (typeof authScopes === "function") { return { errorMessage: (parent, args, context, info) => `Not authorized to resolve ${info.parentType}.${info.fieldName}`, run: (state, parent, args, context, info) => { const scopeMap = authScopes(parent as {}, args, context, info); if (isThenable(scopeMap)) { return scopeMap.then((resolved) => state.evaluateScopeMap(resolved, info)); } return state.evaluateScopeMap(scopeMap, info); }, }; } return { errorMessage: (parent, args, context, info) => `Not authorized to resolve ${info.parentType}.${info.fieldName}`, run: (state, parent, args, context, info) => state.evaluateScopeMap(authScopes, info), };}export function createFieldGrantScopesStep<Types extends SchemaTypes>(grantScopes: FieldGrantScopes<Types, {}, {}>): ResolveStep<Types> { return { errorMessage: (parent, args, context, info) => `Unknown issue generating grants for ${info.parentType}.${info.fieldName}`, run: (state, parent, args, context, info) => { if (typeof grantScopes !== "function") { state.saveGrantedScopes(grantScopes, info.path); return null; } const result = grantScopes(parent as {}, args, context, info); if (isThenable(result)) { return result.then((resolved) => { state.saveGrantedScopes(resolved, info.path); return null; }); } state.saveGrantedScopes(result, info.path); return null; }, };}export function createResolveStep<Types extends SchemaTypes>(resolver: GraphQLFieldResolver<unknown, Types["Context"], object>): ResolveStep<Types> { return { errorMessage: (parent, args, context, info) => `Unknown issue resolving ${info.parentType}.${info.fieldName}`, run: (state, parent, args, context, info, setResolved) => { const result: unknown = resolver(parent, args, context, info); if (isThenable(result)) { return Promise.resolve(result).then((resolved) => { setResolved(resolved); return null; }); } setResolved(result); return null; }, };}
pothos

Version Info

Tagged at
a year ago