@graphql-yoga/plugin-apollo-usage-report 0.8.0 → 0.8.1
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/cjs/index.js +35 -36
- package/esm/index.js +35 -36
- package/package.json +2 -1
- package/typings/index.d.cts +3 -3
- package/typings/index.d.ts +3 -3
package/cjs/index.js
CHANGED
|
@@ -6,6 +6,7 @@ const graphql_1 = require("graphql");
|
|
|
6
6
|
const graphql_yoga_1 = require("graphql-yoga");
|
|
7
7
|
const usage_reporting_protobuf_1 = require("@apollo/usage-reporting-protobuf");
|
|
8
8
|
const plugin_apollo_inline_trace_1 = require("@graphql-yoga/plugin-apollo-inline-trace");
|
|
9
|
+
const promise_helpers_1 = require("@whatwg-node/promise-helpers");
|
|
9
10
|
function getEnvVar(name, defaultValue) {
|
|
10
11
|
return globalThis.process?.env?.[name] || defaultValue || undefined;
|
|
11
12
|
}
|
|
@@ -42,7 +43,7 @@ function useApolloUsageReport(options = {}) {
|
|
|
42
43
|
},
|
|
43
44
|
onSchemaChange({ schema }) {
|
|
44
45
|
if (schema) {
|
|
45
|
-
schemaIdSet$ = hashSHA256((0, graphql_1.printSchema)(schema), yoga.fetchAPI)
|
|
46
|
+
schemaIdSet$ = (0, promise_helpers_1.handleMaybePromise)(() => hashSHA256((0, graphql_1.printSchema)(schema), yoga.fetchAPI), id => {
|
|
46
47
|
schemaId = id;
|
|
47
48
|
schemaIdSet$ = undefined;
|
|
48
49
|
});
|
|
@@ -108,50 +109,48 @@ function useApolloUsageReport(options = {}) {
|
|
|
108
109
|
},
|
|
109
110
|
};
|
|
110
111
|
}
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
112
|
+
function hashSHA256(text, api = globalThis) {
|
|
113
|
+
const inputUint8Array = new api.TextEncoder().encode(text);
|
|
114
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => api.crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array), arrayBuf => {
|
|
115
|
+
const outputUint8Array = new Uint8Array(arrayBuf);
|
|
116
|
+
let hash = '';
|
|
117
|
+
for (const byte of outputUint8Array) {
|
|
118
|
+
const hex = byte.toString(16);
|
|
119
|
+
hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
|
|
120
|
+
}
|
|
121
|
+
return hash;
|
|
122
|
+
});
|
|
121
123
|
}
|
|
122
|
-
|
|
124
|
+
function sendTrace(options, logger, fetch, schemaId, tracesPerQuery, agentVersion) {
|
|
123
125
|
const { graphRef = getEnvVar('APOLLO_GRAPH_REF'), apiKey = getEnvVar('APOLLO_KEY'), endpoint = DEFAULT_REPORTING_ENDPOINT, } = options;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
});
|
|
144
|
-
const responseText = await response.text();
|
|
126
|
+
const body = usage_reporting_protobuf_1.Report.encode({
|
|
127
|
+
header: {
|
|
128
|
+
agentVersion,
|
|
129
|
+
graphRef,
|
|
130
|
+
executableSchemaId: schemaId,
|
|
131
|
+
},
|
|
132
|
+
operationCount: 1,
|
|
133
|
+
tracesPerQuery,
|
|
134
|
+
}).finish();
|
|
135
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => fetch(endpoint, {
|
|
136
|
+
method: 'POST',
|
|
137
|
+
headers: {
|
|
138
|
+
'content-type': 'application/protobuf',
|
|
139
|
+
// The presence of the api key is already checked at Yoga initialization time
|
|
140
|
+
'x-api-key': apiKey,
|
|
141
|
+
accept: 'application/json',
|
|
142
|
+
},
|
|
143
|
+
body,
|
|
144
|
+
}), response => (0, promise_helpers_1.handleMaybePromise)(() => response.text(), responseText => {
|
|
145
145
|
if (response.ok) {
|
|
146
146
|
logger.debug('Traces sent:', responseText);
|
|
147
147
|
}
|
|
148
148
|
else {
|
|
149
149
|
logger.error('Failed to send trace:', responseText);
|
|
150
150
|
}
|
|
151
|
-
}
|
|
152
|
-
catch (err) {
|
|
151
|
+
}), err => {
|
|
153
152
|
logger.error('Failed to send trace:', err);
|
|
154
|
-
}
|
|
153
|
+
});
|
|
155
154
|
}
|
|
156
155
|
function isDocumentNode(data) {
|
|
157
156
|
const isObject = (data) => !!data && typeof data === 'object';
|
package/esm/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { getOperationAST, Kind, printSchema, stripIgnoredCharacters } from 'grap
|
|
|
2
2
|
import { isAsyncIterable, } from 'graphql-yoga';
|
|
3
3
|
import { Report } from '@apollo/usage-reporting-protobuf';
|
|
4
4
|
import { useApolloInstrumentation, } from '@graphql-yoga/plugin-apollo-inline-trace';
|
|
5
|
+
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
|
|
5
6
|
function getEnvVar(name, defaultValue) {
|
|
6
7
|
return globalThis.process?.env?.[name] || defaultValue || undefined;
|
|
7
8
|
}
|
|
@@ -38,7 +39,7 @@ export function useApolloUsageReport(options = {}) {
|
|
|
38
39
|
},
|
|
39
40
|
onSchemaChange({ schema }) {
|
|
40
41
|
if (schema) {
|
|
41
|
-
schemaIdSet$ = hashSHA256(printSchema(schema), yoga.fetchAPI)
|
|
42
|
+
schemaIdSet$ = handleMaybePromise(() => hashSHA256(printSchema(schema), yoga.fetchAPI), id => {
|
|
42
43
|
schemaId = id;
|
|
43
44
|
schemaIdSet$ = undefined;
|
|
44
45
|
});
|
|
@@ -104,50 +105,48 @@ export function useApolloUsageReport(options = {}) {
|
|
|
104
105
|
},
|
|
105
106
|
};
|
|
106
107
|
}
|
|
107
|
-
export
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
108
|
+
export function hashSHA256(text, api = globalThis) {
|
|
109
|
+
const inputUint8Array = new api.TextEncoder().encode(text);
|
|
110
|
+
return handleMaybePromise(() => api.crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array), arrayBuf => {
|
|
111
|
+
const outputUint8Array = new Uint8Array(arrayBuf);
|
|
112
|
+
let hash = '';
|
|
113
|
+
for (const byte of outputUint8Array) {
|
|
114
|
+
const hex = byte.toString(16);
|
|
115
|
+
hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
|
|
116
|
+
}
|
|
117
|
+
return hash;
|
|
118
|
+
});
|
|
117
119
|
}
|
|
118
|
-
|
|
120
|
+
function sendTrace(options, logger, fetch, schemaId, tracesPerQuery, agentVersion) {
|
|
119
121
|
const { graphRef = getEnvVar('APOLLO_GRAPH_REF'), apiKey = getEnvVar('APOLLO_KEY'), endpoint = DEFAULT_REPORTING_ENDPOINT, } = options;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
});
|
|
140
|
-
const responseText = await response.text();
|
|
122
|
+
const body = Report.encode({
|
|
123
|
+
header: {
|
|
124
|
+
agentVersion,
|
|
125
|
+
graphRef,
|
|
126
|
+
executableSchemaId: schemaId,
|
|
127
|
+
},
|
|
128
|
+
operationCount: 1,
|
|
129
|
+
tracesPerQuery,
|
|
130
|
+
}).finish();
|
|
131
|
+
return handleMaybePromise(() => fetch(endpoint, {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
headers: {
|
|
134
|
+
'content-type': 'application/protobuf',
|
|
135
|
+
// The presence of the api key is already checked at Yoga initialization time
|
|
136
|
+
'x-api-key': apiKey,
|
|
137
|
+
accept: 'application/json',
|
|
138
|
+
},
|
|
139
|
+
body,
|
|
140
|
+
}), response => handleMaybePromise(() => response.text(), responseText => {
|
|
141
141
|
if (response.ok) {
|
|
142
142
|
logger.debug('Traces sent:', responseText);
|
|
143
143
|
}
|
|
144
144
|
else {
|
|
145
145
|
logger.error('Failed to send trace:', responseText);
|
|
146
146
|
}
|
|
147
|
-
}
|
|
148
|
-
catch (err) {
|
|
147
|
+
}), err => {
|
|
149
148
|
logger.error('Failed to send trace:', err);
|
|
150
|
-
}
|
|
149
|
+
});
|
|
151
150
|
}
|
|
152
151
|
function isDocumentNode(data) {
|
|
153
152
|
const isObject = (data) => !!data && typeof data === 'object';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-yoga/plugin-apollo-usage-report",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Apollo's GraphOS usage report plugin for GraphQL Yoga.",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"graphql": "^15.2.0 || ^16.0.0",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@apollo/usage-reporting-protobuf": "^4.1.1",
|
|
11
|
+
"@whatwg-node/promise-helpers": "^1.2.4",
|
|
11
12
|
"tslib": "^2.8.1",
|
|
12
13
|
"@graphql-yoga/plugin-apollo-inline-trace": "^3.13.0"
|
|
13
14
|
},
|
package/typings/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Maybe, type Plugin, type YogaInitialContext } from 'graphql-yoga';
|
|
1
|
+
import { type Maybe, type Plugin, type PromiseOrValue, type YogaInitialContext } from 'graphql-yoga';
|
|
2
2
|
import { ApolloInlineGraphqlTraceContext, ApolloInlineRequestTraceContext, ApolloInlineTracePluginOptions } from '@graphql-yoga/plugin-apollo-inline-trace';
|
|
3
3
|
type ApolloUsageReportOptions = ApolloInlineTracePluginOptions & {
|
|
4
4
|
/**
|
|
@@ -47,8 +47,8 @@ export interface ApolloUsageReportGraphqlContext extends ApolloInlineGraphqlTrac
|
|
|
47
47
|
}
|
|
48
48
|
type StringFromRequestFn = (req: Request) => Maybe<string>;
|
|
49
49
|
export declare function useApolloUsageReport(options?: ApolloUsageReportOptions): Plugin;
|
|
50
|
-
export declare function hashSHA256(
|
|
50
|
+
export declare function hashSHA256(text: string, api?: {
|
|
51
51
|
crypto: Crypto;
|
|
52
52
|
TextEncoder: (typeof globalThis)['TextEncoder'];
|
|
53
|
-
}):
|
|
53
|
+
}): PromiseOrValue<string>;
|
|
54
54
|
export {};
|
package/typings/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Maybe, type Plugin, type YogaInitialContext } from 'graphql-yoga';
|
|
1
|
+
import { type Maybe, type Plugin, type PromiseOrValue, type YogaInitialContext } from 'graphql-yoga';
|
|
2
2
|
import { ApolloInlineGraphqlTraceContext, ApolloInlineRequestTraceContext, ApolloInlineTracePluginOptions } from '@graphql-yoga/plugin-apollo-inline-trace';
|
|
3
3
|
type ApolloUsageReportOptions = ApolloInlineTracePluginOptions & {
|
|
4
4
|
/**
|
|
@@ -47,8 +47,8 @@ export interface ApolloUsageReportGraphqlContext extends ApolloInlineGraphqlTrac
|
|
|
47
47
|
}
|
|
48
48
|
type StringFromRequestFn = (req: Request) => Maybe<string>;
|
|
49
49
|
export declare function useApolloUsageReport(options?: ApolloUsageReportOptions): Plugin;
|
|
50
|
-
export declare function hashSHA256(
|
|
50
|
+
export declare function hashSHA256(text: string, api?: {
|
|
51
51
|
crypto: Crypto;
|
|
52
52
|
TextEncoder: (typeof globalThis)['TextEncoder'];
|
|
53
|
-
}):
|
|
53
|
+
}): PromiseOrValue<string>;
|
|
54
54
|
export {};
|