forked from apollographql/federation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequiresScopesSpec.ts
76 lines (67 loc) · 2.59 KB
/
requiresScopesSpec.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
import { DirectiveLocation } from "graphql";
import {
CorePurpose,
FeatureDefinition,
FeatureDefinitions,
FeatureUrl,
FeatureVersion,
} from "./coreSpec";
import { DirectiveDefinition, ListType, NonNullType, Schema } from "./definitions";
import { createDirectiveSpecification, createScalarTypeSpecification } from "./directiveAndTypeSpecification";
import { registerKnownFeature } from "./knownCoreFeatures";
import { ARGUMENT_COMPOSITION_STRATEGIES } from "./argumentCompositionStrategies";
import { assert } from "./utils";
export enum RequiresScopesTypeName {
SCOPE = 'Scope',
}
export class RequiresScopesSpecDefinition extends FeatureDefinition {
public static readonly directiveName = "requiresScopes";
public static readonly identity =
`https://specs.apollo.dev/${RequiresScopesSpecDefinition.directiveName}`;
constructor(version: FeatureVersion) {
super(
new FeatureUrl(
RequiresScopesSpecDefinition.identity,
RequiresScopesSpecDefinition.directiveName,
version,
)
);
this.registerType(createScalarTypeSpecification({ name: RequiresScopesTypeName.SCOPE }));
this.registerDirective(createDirectiveSpecification({
name: RequiresScopesSpecDefinition.directiveName,
args: [{
name: 'scopes',
type: (schema, feature) => {
assert(feature, "Shouldn't be added without being attached to a @link spec");
const scopeName = feature.typeNameInSchema(RequiresScopesTypeName.SCOPE);
const scopeType = schema.type(scopeName);
assert(scopeType, () => `Expected "${scopeName}" to be defined`);
return new NonNullType(new ListType(new NonNullType(new ListType(new NonNullType(scopeType)))));
},
compositionStrategy: ARGUMENT_COMPOSITION_STRATEGIES.UNION,
}],
locations: [
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.OBJECT,
DirectiveLocation.INTERFACE,
DirectiveLocation.SCALAR,
DirectiveLocation.ENUM,
],
composes: true,
supergraphSpecification: () => REQUIRES_SCOPES_VERSIONS.latest(),
}));
}
requiresScopesDirective(
schema: Schema
): DirectiveDefinition<{ name: string }> {
return this.directive(schema, RequiresScopesSpecDefinition.directiveName)!;
}
get defaultCorePurpose(): CorePurpose {
return 'SECURITY';
}
}
export const REQUIRES_SCOPES_VERSIONS =
new FeatureDefinitions<RequiresScopesSpecDefinition>(
RequiresScopesSpecDefinition.identity
).add(new RequiresScopesSpecDefinition(new FeatureVersion(0, 1)));
registerKnownFeature(REQUIRES_SCOPES_VERSIONS);