@envelop/operation-field-permissions 4.0.0-alpha-d0d0776.0 → 4.0.0-alpha-ccec0fc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/{index.js → cjs/index.js} +17 -19
- package/cjs/package.json +1 -0
- package/{index.mjs → esm/index.js} +5 -7
- package/package.json +26 -17
- /package/{index.d.ts → typings/index.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -13,19 +13,19 @@ yarn add @envelop/operation-field-permissions
|
|
|
13
13
|
## Usage Example
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
|
-
import { envelop, useSchema } from '@envelop/core'
|
|
17
|
-
import { useOperationFieldPermissions } from 'envelop/operation-field-permissions'
|
|
16
|
+
import { envelop, useSchema } from '@envelop/core'
|
|
17
|
+
import { useOperationFieldPermissions } from 'envelop/operation-field-permissions'
|
|
18
18
|
|
|
19
19
|
const getEnveloped = envelop({
|
|
20
20
|
plugins: [
|
|
21
21
|
useSchema(schema),
|
|
22
22
|
useOperationFieldPermissions({
|
|
23
23
|
// we can access graphql context here
|
|
24
|
-
getPermissions: async context => new Set(['Query.greetings', ...context.viewer.permissions])
|
|
25
|
-
})
|
|
24
|
+
getPermissions: async context => new Set(['Query.greetings', ...context.viewer.permissions])
|
|
25
|
+
})
|
|
26
26
|
/* ... other envelops */
|
|
27
|
-
]
|
|
28
|
-
})
|
|
27
|
+
]
|
|
28
|
+
})
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
**Schema**
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const graphql = require('graphql');
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOperationFieldPermissions = void 0;
|
|
4
|
+
const core_1 = require("@envelop/core");
|
|
5
|
+
const extended_validation_1 = require("@envelop/extended-validation");
|
|
6
|
+
const graphql_1 = require("@graphql-tools/graphql");
|
|
9
7
|
const OPERATION_PERMISSIONS_SYMBOL = Symbol('OPERATION_PERMISSIONS_SYMBOL');
|
|
10
8
|
/**
|
|
11
9
|
* Returns a set of type names that allow access to all fields in the type.
|
|
@@ -40,7 +38,7 @@ const OperationScopeRule = (options) => (context, executionArgs) => {
|
|
|
40
38
|
// TODO: EnvelopError was a bad idea ;)
|
|
41
39
|
// We should use GraphQLError once the object constructor lands in stable GraphQL.js
|
|
42
40
|
// and useMaskedErrors supports it.
|
|
43
|
-
const error = new
|
|
41
|
+
const error = new core_1.EnvelopError(options.formatError(schemaCoordinate));
|
|
44
42
|
error.nodes = [node];
|
|
45
43
|
context.reportError(error);
|
|
46
44
|
}
|
|
@@ -49,31 +47,31 @@ const OperationScopeRule = (options) => (context, executionArgs) => {
|
|
|
49
47
|
Field(node) {
|
|
50
48
|
const type = context.getType();
|
|
51
49
|
if (type) {
|
|
52
|
-
const namedType =
|
|
53
|
-
if (
|
|
50
|
+
const namedType = (0, graphql_1.getNamedType)(type);
|
|
51
|
+
if ((0, graphql_1.isIntrospectionType)(namedType)) {
|
|
54
52
|
return false;
|
|
55
53
|
}
|
|
56
54
|
}
|
|
57
55
|
const parentType = context.getParentType();
|
|
58
56
|
if (parentType) {
|
|
59
|
-
if (
|
|
57
|
+
if ((0, graphql_1.isIntrospectionType)(parentType)) {
|
|
60
58
|
return false;
|
|
61
59
|
}
|
|
62
60
|
// We handle objects, interface and union permissions differently.
|
|
63
61
|
// When accessing an an object field, we check simply run the check.
|
|
64
|
-
if (
|
|
62
|
+
if ((0, graphql_1.isObjectType)(parentType)) {
|
|
65
63
|
handleField(node, parentType);
|
|
66
64
|
}
|
|
67
65
|
// To allow a union case, every type in the union has to be allowed/
|
|
68
66
|
// If one of the types doesn't permit access we should throw a validation error.
|
|
69
|
-
if (
|
|
67
|
+
if ((0, graphql_1.isUnionType)(parentType)) {
|
|
70
68
|
for (const objectType of parentType.getTypes()) {
|
|
71
69
|
handleField(node, objectType);
|
|
72
70
|
}
|
|
73
71
|
}
|
|
74
72
|
// Same goes for interfaces. Every implementation should allow the access of the given
|
|
75
73
|
// field to pass the validation rule.
|
|
76
|
-
if (
|
|
74
|
+
if ((0, graphql_1.isInterfaceType)(parentType)) {
|
|
77
75
|
for (const objectType of executionArgs.schema.getImplementations(parentType).objects) {
|
|
78
76
|
handleField(node, objectType);
|
|
79
77
|
}
|
|
@@ -87,14 +85,15 @@ const defaultFormatError = (schemaCoordinate) => `Insufficient permissions for s
|
|
|
87
85
|
const useOperationFieldPermissions = (opts) => {
|
|
88
86
|
return {
|
|
89
87
|
onPluginInit({ addPlugin }) {
|
|
90
|
-
|
|
88
|
+
var _a;
|
|
89
|
+
addPlugin((0, extended_validation_1.useExtendedValidation)({
|
|
91
90
|
rules: [
|
|
92
91
|
OperationScopeRule({
|
|
93
|
-
formatError: opts.formatError
|
|
92
|
+
formatError: (_a = opts.formatError) !== null && _a !== void 0 ? _a : defaultFormatError,
|
|
94
93
|
}),
|
|
95
94
|
],
|
|
96
95
|
}));
|
|
97
|
-
addPlugin(
|
|
96
|
+
addPlugin((0, core_1.useExtendContext)(async (context) => {
|
|
98
97
|
const permissions = await opts.getPermissions(context);
|
|
99
98
|
// Schema coordinates is a set of type-name field-name strings that
|
|
100
99
|
// describe the position of a field in the schema.
|
|
@@ -112,5 +111,4 @@ const useOperationFieldPermissions = (opts) => {
|
|
|
112
111
|
},
|
|
113
112
|
};
|
|
114
113
|
};
|
|
115
|
-
|
|
116
114
|
exports.useOperationFieldPermissions = useOperationFieldPermissions;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EnvelopError, useExtendContext } from '@envelop/core';
|
|
2
2
|
import { useExtendedValidation } from '@envelop/extended-validation';
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import { isUnionType, isObjectType, isInterfaceType, isIntrospectionType, getNamedType, } from '@graphql-tools/graphql';
|
|
5
4
|
const OPERATION_PERMISSIONS_SYMBOL = Symbol('OPERATION_PERMISSIONS_SYMBOL');
|
|
6
5
|
/**
|
|
7
6
|
* Returns a set of type names that allow access to all fields in the type.
|
|
@@ -80,13 +79,14 @@ const OperationScopeRule = (options) => (context, executionArgs) => {
|
|
|
80
79
|
};
|
|
81
80
|
};
|
|
82
81
|
const defaultFormatError = (schemaCoordinate) => `Insufficient permissions for selecting '${schemaCoordinate}'.`;
|
|
83
|
-
const useOperationFieldPermissions = (opts) => {
|
|
82
|
+
export const useOperationFieldPermissions = (opts) => {
|
|
84
83
|
return {
|
|
85
84
|
onPluginInit({ addPlugin }) {
|
|
85
|
+
var _a;
|
|
86
86
|
addPlugin(useExtendedValidation({
|
|
87
87
|
rules: [
|
|
88
88
|
OperationScopeRule({
|
|
89
|
-
formatError: opts.formatError
|
|
89
|
+
formatError: (_a = opts.formatError) !== null && _a !== void 0 ? _a : defaultFormatError,
|
|
90
90
|
}),
|
|
91
91
|
],
|
|
92
92
|
}));
|
|
@@ -108,5 +108,3 @@ const useOperationFieldPermissions = (opts) => {
|
|
|
108
108
|
},
|
|
109
109
|
};
|
|
110
110
|
};
|
|
111
|
-
|
|
112
|
-
export { useOperationFieldPermissions };
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/operation-field-permissions",
|
|
3
|
-
"version": "4.0.0-alpha-
|
|
3
|
+
"version": "4.0.0-alpha-ccec0fc.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "3.0.0-alpha-
|
|
7
|
-
"graphql": "
|
|
6
|
+
"@envelop/core": "3.0.0-alpha-ccec0fc.0",
|
|
7
|
+
"@graphql-tools/graphql": "0.1.0-alpha-e7752ba5.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@envelop/extended-validation": "2.0.0-alpha-
|
|
10
|
+
"@envelop/extended-validation": "2.0.0-alpha-ccec0fc.0"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -16,33 +16,42 @@
|
|
|
16
16
|
},
|
|
17
17
|
"author": "Laurin Quast <laurinquast@googlemail.com.com>",
|
|
18
18
|
"license": "MIT",
|
|
19
|
-
"main": "index.js",
|
|
20
|
-
"module": "index.
|
|
21
|
-
"typings": "index.d.ts",
|
|
19
|
+
"main": "cjs/index.js",
|
|
20
|
+
"module": "esm/index.js",
|
|
21
|
+
"typings": "typings/index.d.ts",
|
|
22
22
|
"typescript": {
|
|
23
|
-
"definition": "index.d.ts"
|
|
23
|
+
"definition": "typings/index.d.ts"
|
|
24
24
|
},
|
|
25
|
+
"type": "module",
|
|
25
26
|
"exports": {
|
|
26
27
|
".": {
|
|
27
28
|
"require": {
|
|
28
|
-
"
|
|
29
|
-
"
|
|
29
|
+
"types": "./typings/index.d.ts",
|
|
30
|
+
"default": "./cjs/index.js"
|
|
30
31
|
},
|
|
31
32
|
"import": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
33
|
+
"types": "./typings/index.d.ts",
|
|
34
|
+
"default": "./esm/index.js"
|
|
35
|
+
},
|
|
36
|
+
"default": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./esm/index.js"
|
|
34
39
|
}
|
|
35
40
|
},
|
|
36
41
|
"./*": {
|
|
37
42
|
"require": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
43
|
+
"types": "./typings/*.d.ts",
|
|
44
|
+
"default": "./cjs/*.js"
|
|
40
45
|
},
|
|
41
46
|
"import": {
|
|
42
|
-
"
|
|
43
|
-
"
|
|
47
|
+
"types": "./typings/*.d.ts",
|
|
48
|
+
"default": "./esm/*.js"
|
|
49
|
+
},
|
|
50
|
+
"default": {
|
|
51
|
+
"types": "./typings/*.d.ts",
|
|
52
|
+
"default": "./esm/*.js"
|
|
44
53
|
}
|
|
45
54
|
},
|
|
46
55
|
"./package.json": "./package.json"
|
|
47
56
|
}
|
|
48
|
-
}
|
|
57
|
+
}
|
|
File without changes
|