@graphql-hive/gateway-usage 0.1.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/LICENSE +21 -0
- package/cjs/extract-coordinates.js +84 -0
- package/cjs/index.js +108 -0
- package/cjs/is-entity-request.js +14 -0
- package/cjs/package.json +1 -0
- package/cjs/path-to-coordinate.js +35 -0
- package/cjs/version.js +4 -0
- package/esm/extract-coordinates.js +81 -0
- package/esm/index.js +104 -0
- package/esm/is-entity-request.js +11 -0
- package/esm/path-to-coordinate.js +32 -0
- package/esm/version.js +1 -0
- package/package.json +52 -0
- package/typings/extract-coordinates.d.cts +6 -0
- package/typings/extract-coordinates.d.ts +6 -0
- package/typings/index.d.cts +6 -0
- package/typings/index.d.ts +6 -0
- package/typings/is-entity-request.d.cts +6 -0
- package/typings/is-entity-request.d.ts +6 -0
- package/typings/path-to-coordinate.d.cts +3 -0
- package/typings/path-to-coordinate.d.ts +3 -0
- package/typings/version.d.cts +2 -0
- package/typings/version.d.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 The Guild
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractSchemaCoordinates = extractSchemaCoordinates;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
/**
|
|
6
|
+
* Extracts true schema coordinates and counts their execution volume.
|
|
7
|
+
*/
|
|
8
|
+
function extractSchemaCoordinates(schema, document, resultData) {
|
|
9
|
+
const counts = {};
|
|
10
|
+
// 1. Find the root operation (Query, Mutation, Subscription)
|
|
11
|
+
const operation = document.definitions.find((def) => def.kind === graphql_1.Kind.OPERATION_DEFINITION);
|
|
12
|
+
if (!operation)
|
|
13
|
+
return counts;
|
|
14
|
+
// 2. Determine the root schema type
|
|
15
|
+
let rootType;
|
|
16
|
+
if (operation.operation === 'query')
|
|
17
|
+
rootType = schema.getQueryType();
|
|
18
|
+
else if (operation.operation === 'mutation')
|
|
19
|
+
rootType = schema.getMutationType();
|
|
20
|
+
else if (operation.operation === 'subscription')
|
|
21
|
+
rootType = schema.getSubscriptionType();
|
|
22
|
+
if (!rootType || !resultData)
|
|
23
|
+
return counts;
|
|
24
|
+
// 3. Start the synchronized walk
|
|
25
|
+
walkZip(resultData, operation.selectionSet.selections, rootType, schema, counts);
|
|
26
|
+
return counts;
|
|
27
|
+
}
|
|
28
|
+
function walkZip(data, selections, parentType, schema, counts) {
|
|
29
|
+
var _a;
|
|
30
|
+
if (Array.isArray(data)) {
|
|
31
|
+
for (const item of data) {
|
|
32
|
+
walkZip(item, selections, parentType, schema, counts);
|
|
33
|
+
}
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (data === null || typeof data !== 'object') {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const namedType = (0, graphql_1.getNamedType)(parentType);
|
|
40
|
+
if (!(0, graphql_1.isObjectType)(namedType) && !(0, graphql_1.isInterfaceType)(namedType) && !(0, graphql_1.isUnionType)(namedType)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
// Get fields, but safely handle Union types which don't have direct fields
|
|
44
|
+
const fields = 'getFields' in namedType ? namedType.getFields() : {};
|
|
45
|
+
for (const selection of selections) {
|
|
46
|
+
if (selection.kind === graphql_1.Kind.FIELD) {
|
|
47
|
+
const realFieldName = selection.name.value;
|
|
48
|
+
const responseKey = selection.alias ? selection.alias.value : realFieldName;
|
|
49
|
+
// Special case for __typename which isn't in the schema fields map
|
|
50
|
+
if (realFieldName === '__typename') {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (responseKey in data) {
|
|
54
|
+
const coordinate = `${namedType.name}.${realFieldName}`;
|
|
55
|
+
counts[coordinate] = (counts[coordinate] || 0) + 1;
|
|
56
|
+
if (selection.selectionSet && fields[realFieldName]) {
|
|
57
|
+
walkZip(data[responseKey], selection.selectionSet.selections, fields[realFieldName].type, schema, counts);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// --- INLINE FRAGMENT LOGIC ---
|
|
62
|
+
else if (selection.kind === graphql_1.Kind.INLINE_FRAGMENT) {
|
|
63
|
+
const typeConditionName = (_a = selection.typeCondition) === null || _a === void 0 ? void 0 : _a.name.value;
|
|
64
|
+
let matchesType = true;
|
|
65
|
+
let nextType = namedType;
|
|
66
|
+
if (typeConditionName) {
|
|
67
|
+
// In federated execution, abstract types (Interfaces/Unions) generally
|
|
68
|
+
// return __typename in the payload. We use that to verify the fragment match.
|
|
69
|
+
const runtimeTypeName = data.__typename || namedType.name;
|
|
70
|
+
matchesType = typeConditionName === runtimeTypeName;
|
|
71
|
+
if (matchesType) {
|
|
72
|
+
nextType = schema.getType(typeConditionName) || namedType;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// If the runtime data matches the fragment's type condition,
|
|
76
|
+
// recurse using the SAME data node, but the fragment's selection set.
|
|
77
|
+
if (matchesType && selection.selectionSet) {
|
|
78
|
+
walkZip(data, // <-- Pass the exact same data object
|
|
79
|
+
selection.selectionSet.selections, nextType, // <-- Pass the narrowed type
|
|
80
|
+
schema, counts);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHive = createHive;
|
|
4
|
+
exports.useHive = useHive;
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const core_1 = require("@graphql-hive/core");
|
|
7
|
+
const extract_coordinates_js_1 = require("./extract-coordinates.js");
|
|
8
|
+
const is_entity_request_js_1 = require("./is-entity-request.js");
|
|
9
|
+
const path_to_coordinate_js_1 = require("./path-to-coordinate.js");
|
|
10
|
+
const version_js_1 = require("./version.js");
|
|
11
|
+
function createHive(clientOrOptions) {
|
|
12
|
+
return (0, core_1.createHive)(Object.assign(Object.assign({}, clientOrOptions), { agent: Object.assign({ name: 'hive-client-yoga', version: version_js_1.version }, clientOrOptions.agent) }));
|
|
13
|
+
}
|
|
14
|
+
function useHive(clientOrOptions) {
|
|
15
|
+
const hive = (0, core_1.isHiveClient)(clientOrOptions)
|
|
16
|
+
? clientOrOptions
|
|
17
|
+
: createHive(Object.assign(Object.assign({}, clientOrOptions), { agent: Object.assign({ name: 'hive-client-envelop' }, clientOrOptions.agent) }));
|
|
18
|
+
void hive.info();
|
|
19
|
+
if (hive[core_1.autoDisposeSymbol]) {
|
|
20
|
+
if (global.process) {
|
|
21
|
+
const signals = Array.isArray(hive[core_1.autoDisposeSymbol])
|
|
22
|
+
? hive[core_1.autoDisposeSymbol]
|
|
23
|
+
: ['SIGINT', 'SIGTERM'];
|
|
24
|
+
for (const signal of signals) {
|
|
25
|
+
process.once(signal, () => hive.dispose());
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
console.error('It seems that GraphQL Hive is not being executed in Node.js. ' +
|
|
30
|
+
'Please attempt manual client disposal and use autoDispose: false option.');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
onSubgraphExecute({ executionRequest, subgraphName, subgraph: subgraphSchema }) {
|
|
35
|
+
var _a, _b;
|
|
36
|
+
const collection = (_a = executionRequest.context) === null || _a === void 0 ? void 0 : _a.__hiveUsageCollection;
|
|
37
|
+
if (!collection) {
|
|
38
|
+
// This is set onExecute so this should exist... but just to be safe
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const finishSubRequest = collection.subrequest({
|
|
42
|
+
subgraph: subgraphName,
|
|
43
|
+
type: (0, is_entity_request_js_1.isEntityRequest)(executionRequest.document) ? 'ENTITY' : 'ROOT',
|
|
44
|
+
/** @NOTE this field's format supports batched requests, but onSubgraphExecute does not. */
|
|
45
|
+
paths: ((_b = executionRequest.info) === null || _b === void 0 ? void 0 : _b.path)
|
|
46
|
+
? [(0, graphql_1.responsePathAsArray)(executionRequest.info.path).join('.')]
|
|
47
|
+
: [],
|
|
48
|
+
});
|
|
49
|
+
return function onSubgraphExecuteDone({ result }) {
|
|
50
|
+
var _a;
|
|
51
|
+
if (!(0, core_1.isAsyncIterable)(result)) {
|
|
52
|
+
let errors = undefined;
|
|
53
|
+
if (result.errors) {
|
|
54
|
+
errors = [];
|
|
55
|
+
for (const err of result.errors) {
|
|
56
|
+
const coordinate = err.path && (0, path_to_coordinate_js_1.pathToCoordinate)(subgraphSchema, err.path);
|
|
57
|
+
if (coordinate) {
|
|
58
|
+
errors.push({
|
|
59
|
+
coordinate,
|
|
60
|
+
code: (_a = err.extensions) === null || _a === void 0 ? void 0 : _a.code,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const fields = (0, extract_coordinates_js_1.extractSchemaCoordinates)(subgraphSchema, executionRequest.document, result.data);
|
|
66
|
+
finishSubRequest({
|
|
67
|
+
status: 200 /** @TODO figure out how to capture HTTP status codes */,
|
|
68
|
+
fields,
|
|
69
|
+
errors,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
onSchemaChange({ schema }) {
|
|
75
|
+
hive.reportSchema({ schema });
|
|
76
|
+
},
|
|
77
|
+
onExecute({ args }) {
|
|
78
|
+
const collection = hive.collectUsage();
|
|
79
|
+
// Inject the collection object into the GraphQL context
|
|
80
|
+
// so it can be accessed downstream by subgraph executions.
|
|
81
|
+
if (args.contextValue) {
|
|
82
|
+
args.contextValue.__hiveUsageCollection = collection;
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
onExecuteDone({ result }) {
|
|
86
|
+
if (!(0, core_1.isAsyncIterable)(result)) {
|
|
87
|
+
void collection.finish(args, result);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const errors = [];
|
|
91
|
+
return {
|
|
92
|
+
onNext(ctx) {
|
|
93
|
+
if (ctx.result.errors) {
|
|
94
|
+
errors.push(...ctx.result.errors);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
onEnd() {
|
|
98
|
+
void collection.finish(args, errors.length ? { errors } : {});
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
onSubscribe({ args }) {
|
|
105
|
+
hive.collectSubscriptionUsage({ args });
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isEntityRequest = isEntityRequest;
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the top-level selection is an '_entities' query
|
|
6
|
+
*/
|
|
7
|
+
function isEntityRequest(document) {
|
|
8
|
+
let isEntityRequest = false;
|
|
9
|
+
const operation = document.definitions.find(def => def.kind === 'OperationDefinition');
|
|
10
|
+
if (operation && operation.selectionSet) {
|
|
11
|
+
isEntityRequest = operation.selectionSet.selections.some(selection => selection.kind === 'Field' && selection.name.value === '_entities');
|
|
12
|
+
}
|
|
13
|
+
return isEntityRequest;
|
|
14
|
+
}
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pathToCoordinate = pathToCoordinate;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
function pathToCoordinate(schema, errorPath, operationType = 'query') {
|
|
6
|
+
// 1. Start at the root operation type
|
|
7
|
+
let currentType;
|
|
8
|
+
if (operationType === 'mutation')
|
|
9
|
+
currentType = schema.getMutationType();
|
|
10
|
+
else if (operationType === 'subscription')
|
|
11
|
+
currentType = schema.getSubscriptionType();
|
|
12
|
+
else
|
|
13
|
+
currentType = schema.getQueryType();
|
|
14
|
+
let coordinate = null;
|
|
15
|
+
for (const segment of errorPath) {
|
|
16
|
+
// 2. Skip array indices entirely (they don't change the underlying type)
|
|
17
|
+
if (typeof segment === 'number')
|
|
18
|
+
continue;
|
|
19
|
+
// 3. Unwrap Non-Null (!) and List ([]) types to get the base Object/Interface
|
|
20
|
+
currentType = (0, graphql_1.getNamedType)(currentType);
|
|
21
|
+
// 4. Ensure the current type has fields
|
|
22
|
+
if ((0, graphql_1.isObjectType)(currentType) || (0, graphql_1.isInterfaceType)(currentType)) {
|
|
23
|
+
const fields = currentType.getFields();
|
|
24
|
+
const field = fields[segment];
|
|
25
|
+
if (!field) {
|
|
26
|
+
throw new Error(`Field '${segment}' not found on type '${currentType.name}'. Was this aliased?`);
|
|
27
|
+
}
|
|
28
|
+
// Update the coordinate to the current Type.field
|
|
29
|
+
coordinate = `${currentType.name}.${field.name}`;
|
|
30
|
+
// Move deeper into the tree for the next iteration
|
|
31
|
+
currentType = field.type;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return coordinate !== null && coordinate !== void 0 ? coordinate : undefined;
|
|
35
|
+
}
|
package/cjs/version.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { getNamedType, isInterfaceType, isObjectType, isUnionType, Kind, } from 'graphql';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts true schema coordinates and counts their execution volume.
|
|
4
|
+
*/
|
|
5
|
+
export function extractSchemaCoordinates(schema, document, resultData) {
|
|
6
|
+
const counts = {};
|
|
7
|
+
// 1. Find the root operation (Query, Mutation, Subscription)
|
|
8
|
+
const operation = document.definitions.find((def) => def.kind === Kind.OPERATION_DEFINITION);
|
|
9
|
+
if (!operation)
|
|
10
|
+
return counts;
|
|
11
|
+
// 2. Determine the root schema type
|
|
12
|
+
let rootType;
|
|
13
|
+
if (operation.operation === 'query')
|
|
14
|
+
rootType = schema.getQueryType();
|
|
15
|
+
else if (operation.operation === 'mutation')
|
|
16
|
+
rootType = schema.getMutationType();
|
|
17
|
+
else if (operation.operation === 'subscription')
|
|
18
|
+
rootType = schema.getSubscriptionType();
|
|
19
|
+
if (!rootType || !resultData)
|
|
20
|
+
return counts;
|
|
21
|
+
// 3. Start the synchronized walk
|
|
22
|
+
walkZip(resultData, operation.selectionSet.selections, rootType, schema, counts);
|
|
23
|
+
return counts;
|
|
24
|
+
}
|
|
25
|
+
function walkZip(data, selections, parentType, schema, counts) {
|
|
26
|
+
var _a;
|
|
27
|
+
if (Array.isArray(data)) {
|
|
28
|
+
for (const item of data) {
|
|
29
|
+
walkZip(item, selections, parentType, schema, counts);
|
|
30
|
+
}
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (data === null || typeof data !== 'object') {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const namedType = getNamedType(parentType);
|
|
37
|
+
if (!isObjectType(namedType) && !isInterfaceType(namedType) && !isUnionType(namedType)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// Get fields, but safely handle Union types which don't have direct fields
|
|
41
|
+
const fields = 'getFields' in namedType ? namedType.getFields() : {};
|
|
42
|
+
for (const selection of selections) {
|
|
43
|
+
if (selection.kind === Kind.FIELD) {
|
|
44
|
+
const realFieldName = selection.name.value;
|
|
45
|
+
const responseKey = selection.alias ? selection.alias.value : realFieldName;
|
|
46
|
+
// Special case for __typename which isn't in the schema fields map
|
|
47
|
+
if (realFieldName === '__typename') {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (responseKey in data) {
|
|
51
|
+
const coordinate = `${namedType.name}.${realFieldName}`;
|
|
52
|
+
counts[coordinate] = (counts[coordinate] || 0) + 1;
|
|
53
|
+
if (selection.selectionSet && fields[realFieldName]) {
|
|
54
|
+
walkZip(data[responseKey], selection.selectionSet.selections, fields[realFieldName].type, schema, counts);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// --- INLINE FRAGMENT LOGIC ---
|
|
59
|
+
else if (selection.kind === Kind.INLINE_FRAGMENT) {
|
|
60
|
+
const typeConditionName = (_a = selection.typeCondition) === null || _a === void 0 ? void 0 : _a.name.value;
|
|
61
|
+
let matchesType = true;
|
|
62
|
+
let nextType = namedType;
|
|
63
|
+
if (typeConditionName) {
|
|
64
|
+
// In federated execution, abstract types (Interfaces/Unions) generally
|
|
65
|
+
// return __typename in the payload. We use that to verify the fragment match.
|
|
66
|
+
const runtimeTypeName = data.__typename || namedType.name;
|
|
67
|
+
matchesType = typeConditionName === runtimeTypeName;
|
|
68
|
+
if (matchesType) {
|
|
69
|
+
nextType = schema.getType(typeConditionName) || namedType;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// If the runtime data matches the fragment's type condition,
|
|
73
|
+
// recurse using the SAME data node, but the fragment's selection set.
|
|
74
|
+
if (matchesType && selection.selectionSet) {
|
|
75
|
+
walkZip(data, // <-- Pass the exact same data object
|
|
76
|
+
selection.selectionSet.selections, nextType, // <-- Pass the narrowed type
|
|
77
|
+
schema, counts);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { responsePathAsArray } from 'graphql';
|
|
2
|
+
import { autoDisposeSymbol, createHive as createHiveClient, isAsyncIterable, isHiveClient, } from '@graphql-hive/core';
|
|
3
|
+
import { extractSchemaCoordinates } from './extract-coordinates.js';
|
|
4
|
+
import { isEntityRequest } from './is-entity-request.js';
|
|
5
|
+
import { pathToCoordinate } from './path-to-coordinate.js';
|
|
6
|
+
import { version } from './version.js';
|
|
7
|
+
export function createHive(clientOrOptions) {
|
|
8
|
+
return createHiveClient(Object.assign(Object.assign({}, clientOrOptions), { agent: Object.assign({ name: 'hive-client-yoga', version }, clientOrOptions.agent) }));
|
|
9
|
+
}
|
|
10
|
+
export function useHive(clientOrOptions) {
|
|
11
|
+
const hive = isHiveClient(clientOrOptions)
|
|
12
|
+
? clientOrOptions
|
|
13
|
+
: createHive(Object.assign(Object.assign({}, clientOrOptions), { agent: Object.assign({ name: 'hive-client-envelop' }, clientOrOptions.agent) }));
|
|
14
|
+
void hive.info();
|
|
15
|
+
if (hive[autoDisposeSymbol]) {
|
|
16
|
+
if (global.process) {
|
|
17
|
+
const signals = Array.isArray(hive[autoDisposeSymbol])
|
|
18
|
+
? hive[autoDisposeSymbol]
|
|
19
|
+
: ['SIGINT', 'SIGTERM'];
|
|
20
|
+
for (const signal of signals) {
|
|
21
|
+
process.once(signal, () => hive.dispose());
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
console.error('It seems that GraphQL Hive is not being executed in Node.js. ' +
|
|
26
|
+
'Please attempt manual client disposal and use autoDispose: false option.');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
onSubgraphExecute({ executionRequest, subgraphName, subgraph: subgraphSchema }) {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
const collection = (_a = executionRequest.context) === null || _a === void 0 ? void 0 : _a.__hiveUsageCollection;
|
|
33
|
+
if (!collection) {
|
|
34
|
+
// This is set onExecute so this should exist... but just to be safe
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const finishSubRequest = collection.subrequest({
|
|
38
|
+
subgraph: subgraphName,
|
|
39
|
+
type: isEntityRequest(executionRequest.document) ? 'ENTITY' : 'ROOT',
|
|
40
|
+
/** @NOTE this field's format supports batched requests, but onSubgraphExecute does not. */
|
|
41
|
+
paths: ((_b = executionRequest.info) === null || _b === void 0 ? void 0 : _b.path)
|
|
42
|
+
? [responsePathAsArray(executionRequest.info.path).join('.')]
|
|
43
|
+
: [],
|
|
44
|
+
});
|
|
45
|
+
return function onSubgraphExecuteDone({ result }) {
|
|
46
|
+
var _a;
|
|
47
|
+
if (!isAsyncIterable(result)) {
|
|
48
|
+
let errors = undefined;
|
|
49
|
+
if (result.errors) {
|
|
50
|
+
errors = [];
|
|
51
|
+
for (const err of result.errors) {
|
|
52
|
+
const coordinate = err.path && pathToCoordinate(subgraphSchema, err.path);
|
|
53
|
+
if (coordinate) {
|
|
54
|
+
errors.push({
|
|
55
|
+
coordinate,
|
|
56
|
+
code: (_a = err.extensions) === null || _a === void 0 ? void 0 : _a.code,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const fields = extractSchemaCoordinates(subgraphSchema, executionRequest.document, result.data);
|
|
62
|
+
finishSubRequest({
|
|
63
|
+
status: 200 /** @TODO figure out how to capture HTTP status codes */,
|
|
64
|
+
fields,
|
|
65
|
+
errors,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
onSchemaChange({ schema }) {
|
|
71
|
+
hive.reportSchema({ schema });
|
|
72
|
+
},
|
|
73
|
+
onExecute({ args }) {
|
|
74
|
+
const collection = hive.collectUsage();
|
|
75
|
+
// Inject the collection object into the GraphQL context
|
|
76
|
+
// so it can be accessed downstream by subgraph executions.
|
|
77
|
+
if (args.contextValue) {
|
|
78
|
+
args.contextValue.__hiveUsageCollection = collection;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
onExecuteDone({ result }) {
|
|
82
|
+
if (!isAsyncIterable(result)) {
|
|
83
|
+
void collection.finish(args, result);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const errors = [];
|
|
87
|
+
return {
|
|
88
|
+
onNext(ctx) {
|
|
89
|
+
if (ctx.result.errors) {
|
|
90
|
+
errors.push(...ctx.result.errors);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
onEnd() {
|
|
94
|
+
void collection.finish(args, errors.length ? { errors } : {});
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
onSubscribe({ args }) {
|
|
101
|
+
hive.collectSubscriptionUsage({ args });
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the top-level selection is an '_entities' query
|
|
3
|
+
*/
|
|
4
|
+
export function isEntityRequest(document) {
|
|
5
|
+
let isEntityRequest = false;
|
|
6
|
+
const operation = document.definitions.find(def => def.kind === 'OperationDefinition');
|
|
7
|
+
if (operation && operation.selectionSet) {
|
|
8
|
+
isEntityRequest = operation.selectionSet.selections.some(selection => selection.kind === 'Field' && selection.name.value === '_entities');
|
|
9
|
+
}
|
|
10
|
+
return isEntityRequest;
|
|
11
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getNamedType, isInterfaceType, isObjectType, } from 'graphql';
|
|
2
|
+
export function pathToCoordinate(schema, errorPath, operationType = 'query') {
|
|
3
|
+
// 1. Start at the root operation type
|
|
4
|
+
let currentType;
|
|
5
|
+
if (operationType === 'mutation')
|
|
6
|
+
currentType = schema.getMutationType();
|
|
7
|
+
else if (operationType === 'subscription')
|
|
8
|
+
currentType = schema.getSubscriptionType();
|
|
9
|
+
else
|
|
10
|
+
currentType = schema.getQueryType();
|
|
11
|
+
let coordinate = null;
|
|
12
|
+
for (const segment of errorPath) {
|
|
13
|
+
// 2. Skip array indices entirely (they don't change the underlying type)
|
|
14
|
+
if (typeof segment === 'number')
|
|
15
|
+
continue;
|
|
16
|
+
// 3. Unwrap Non-Null (!) and List ([]) types to get the base Object/Interface
|
|
17
|
+
currentType = getNamedType(currentType);
|
|
18
|
+
// 4. Ensure the current type has fields
|
|
19
|
+
if (isObjectType(currentType) || isInterfaceType(currentType)) {
|
|
20
|
+
const fields = currentType.getFields();
|
|
21
|
+
const field = fields[segment];
|
|
22
|
+
if (!field) {
|
|
23
|
+
throw new Error(`Field '${segment}' not found on type '${currentType.name}'. Was this aliased?`);
|
|
24
|
+
}
|
|
25
|
+
// Update the coordinate to the current Type.field
|
|
26
|
+
coordinate = `${currentType.name}.${field.name}`;
|
|
27
|
+
// Move deeper into the tree for the next iteration
|
|
28
|
+
currentType = field.type;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return coordinate !== null && coordinate !== void 0 ? coordinate : undefined;
|
|
32
|
+
}
|
package/esm/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const version = '0.1.0';
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphql-hive/gateway-usage",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "GraphQL Hive + GraphQL Hive Gateway",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"@graphql-hive/gateway": "^2.0.0",
|
|
8
|
+
"graphql": "^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@graphql-hive/core": "0.21.0"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "graphql-hive/platform",
|
|
16
|
+
"directory": "packages/libraries/gateway"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://the-guild.dev/graphql/hive",
|
|
19
|
+
"author": {
|
|
20
|
+
"email": "contact@the-guild.dev",
|
|
21
|
+
"name": "The Guild",
|
|
22
|
+
"url": "https://the-guild.dev"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=16.0.0"
|
|
27
|
+
},
|
|
28
|
+
"main": "cjs/index.js",
|
|
29
|
+
"module": "esm/index.js",
|
|
30
|
+
"typings": "typings/index.d.ts",
|
|
31
|
+
"typescript": {
|
|
32
|
+
"definition": "typings/index.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"type": "module",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./typings/index.d.cts",
|
|
39
|
+
"default": "./cjs/index.js"
|
|
40
|
+
},
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./typings/index.d.ts",
|
|
43
|
+
"default": "./esm/index.js"
|
|
44
|
+
},
|
|
45
|
+
"default": {
|
|
46
|
+
"types": "./typings/index.d.ts",
|
|
47
|
+
"default": "./esm/index.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"./package.json": "./package.json"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DocumentNode, GraphQLSchema } from 'graphql';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts true schema coordinates and counts their execution volume.
|
|
4
|
+
*/
|
|
5
|
+
export declare function extractSchemaCoordinates(schema: GraphQLSchema, document: DocumentNode, resultData: any): Record<string, number>;
|
|
6
|
+
//# sourceMappingURL=extract-coordinates.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DocumentNode, GraphQLSchema } from 'graphql';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts true schema coordinates and counts their execution volume.
|
|
4
|
+
*/
|
|
5
|
+
export declare function extractSchemaCoordinates(schema: GraphQLSchema, document: DocumentNode, resultData: any): Record<string, number>;
|
|
6
|
+
//# sourceMappingURL=extract-coordinates.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { HiveClient, HivePluginOptions } from '@graphql-hive/core';
|
|
2
|
+
import type { GatewayPlugin } from '@graphql-hive/gateway';
|
|
3
|
+
export declare function createHive(clientOrOptions: HivePluginOptions): HiveClient;
|
|
4
|
+
export declare function useHive(clientOrOptions: HiveClient): GatewayPlugin;
|
|
5
|
+
export declare function useHive(clientOrOptions: HivePluginOptions): GatewayPlugin;
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { HiveClient, HivePluginOptions } from '@graphql-hive/core';
|
|
2
|
+
import type { GatewayPlugin } from '@graphql-hive/gateway';
|
|
3
|
+
export declare function createHive(clientOrOptions: HivePluginOptions): HiveClient;
|
|
4
|
+
export declare function useHive(clientOrOptions: HiveClient): GatewayPlugin;
|
|
5
|
+
export declare function useHive(clientOrOptions: HivePluginOptions): GatewayPlugin;
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|