Skip to content

Commit 7dba74a

Browse files
ardatanenisdenjo
authored andcommitted
Make it pass
1 parent fa4fd24 commit 7dba74a

File tree

2 files changed

+85
-44
lines changed

2 files changed

+85
-44
lines changed

packages/plugins/apollo-inline-trace/src/index.ts

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { isAsyncIterable, Plugin } from '@envelop/core';
2-
import { GraphQLError, ResponsePath } from 'graphql';
2+
import { ExecutionResult, GraphQLError, Kind, OperationTypeNode, ResponsePath } from 'graphql';
33
import { google, Trace } from 'apollo-reporting-protobuf';
44

55
const ctxKey = Symbol('ApolloInlineTracePluginContextKey');
6+
const errorsKey = Symbol('ApolloInlineTracePluginErrorsKey');
67

78
interface ApolloInlineTracePluginContext {
89
startHrTime: [number, number];
@@ -48,7 +49,7 @@ export function useApolloInlineTrace<PluginContext extends Record<string, any> =
4849
shouldTrace,
4950
rewriteError,
5051
}: ApolloInlineTracePluginOptions<PluginContext>): Plugin<
51-
PluginContext & { [ctxKey]: ApolloInlineTracePluginContext }
52+
PluginContext & { [ctxKey]: ApolloInlineTracePluginContext; [errorsKey]: GraphQLError[] }
5253
> {
5354
return {
5455
onEnveloped({ context, extendContext }) {
@@ -72,6 +73,7 @@ export function useApolloInlineTrace<PluginContext extends Record<string, any> =
7273
nodes: new Map([[responsePathToString(), rootNode]]),
7374
stopped: false,
7475
},
76+
[errorsKey]: [],
7577
});
7678
}
7779
},
@@ -100,34 +102,67 @@ export function useApolloInlineTrace<PluginContext extends Record<string, any> =
100102
};
101103
},
102104
onParse() {
103-
return ({ context, result }) => {
105+
return ({ context, result, replaceParseResult }) => {
104106
const ctx = context[ctxKey];
105107
if (!ctx) return;
106108

107-
if (result instanceof GraphQLError) {
108-
handleErrors(ctx, [result], rewriteError);
109-
} else if (result instanceof Error) {
110-
handleErrors(
111-
ctx,
112-
[
109+
const errors = context[errorsKey];
110+
if (errors && result instanceof Error) {
111+
if (result instanceof GraphQLError) {
112+
errors.push(result);
113+
} else {
114+
errors.push(
113115
new GraphQLError(result.message, {
114116
originalError: result,
115-
}),
117+
})
118+
);
119+
}
120+
replaceParseResult({
121+
kind: Kind.DOCUMENT,
122+
definitions: [
123+
{
124+
kind: Kind.OPERATION_DEFINITION,
125+
operation: OperationTypeNode.QUERY,
126+
selectionSet: {
127+
kind: Kind.SELECTION_SET,
128+
selections: [
129+
{
130+
kind: Kind.FIELD,
131+
name: {
132+
kind: Kind.NAME,
133+
value: '__typename',
134+
},
135+
},
136+
],
137+
},
138+
},
116139
],
117-
rewriteError
118-
);
140+
});
119141
}
120142
};
121143
},
122-
onValidate() {
123-
return ({ context, result: errors }) => {
124-
if (errors.length) {
125-
const ctx = context[ctxKey];
126-
if (ctx) handleErrors(ctx, errors, rewriteError);
144+
onValidate({ context, setResult }) {
145+
const errorsInContext = context[errorsKey];
146+
if (errorsInContext?.length) {
147+
setResult([]);
148+
}
149+
return ({ result: errors, setResult }) => {
150+
if (errorsInContext) {
151+
errorsInContext.push(...errors);
152+
setResult([]);
127153
}
128154
};
129155
},
130-
onExecute() {
156+
onExecute({ args: { contextValue }, setResultAndStopExecution }) {
157+
const errors = contextValue[errorsKey];
158+
if (errors?.length) {
159+
const ctx = contextValue[ctxKey];
160+
if (!ctx) return;
161+
const result = { errors };
162+
const updatedResult = updateResult(result, rewriteError, ctx);
163+
setResultAndStopExecution(updatedResult);
164+
return;
165+
}
131166
return {
132167
onExecuteDone({ args: { contextValue }, result, setResult }) {
133168
const ctx = contextValue[ctxKey];
@@ -136,38 +171,44 @@ export function useApolloInlineTrace<PluginContext extends Record<string, any> =
136171
// TODO: should handle streaming results? how?
137172
if (isAsyncIterable(result)) return;
138173

139-
if (result.extensions?.ftv1 !== undefined) {
140-
throw new Error('The `ftv1` extension is already present');
141-
}
174+
const updatedResult = updateResult(result, rewriteError, ctx);
142175

143-
if (result.errors?.length) {
144-
handleErrors(ctx, result.errors, rewriteError);
145-
}
176+
setResult(updatedResult);
177+
},
178+
};
179+
},
180+
};
181+
}
146182

147-
// onResultProcess will be called only once since we disallow async iterables
148-
if (ctx.stopped) throw new Error('Trace stopped multiple times');
183+
function updateResult(
184+
result: ExecutionResult,
185+
rewriteError: ApolloInlineTracePluginOptions['rewriteError'],
186+
ctx: ApolloInlineTracePluginContext
187+
) {
188+
if (result.extensions?.ftv1 !== undefined) {
189+
throw new Error('The `ftv1` extension is already present');
190+
}
149191

150-
ctx.stopped = true;
151-
ctx.trace.durationNs = hrTimeToDurationInNanos(process.hrtime(ctx.startHrTime));
152-
ctx.trace.endTime = nowTimestamp();
192+
if (result.errors?.length) {
193+
handleErrors(ctx, result.errors, rewriteError);
194+
}
153195

154-
const encodedUint8Array = Trace.encode(ctx.trace).finish();
155-
const encodedBuffer = Buffer.from(
156-
encodedUint8Array,
157-
encodedUint8Array.byteOffset,
158-
encodedUint8Array.byteLength
159-
);
196+
// onResultProcess will be called only once since we disallow async iterables
197+
if (ctx.stopped) throw new Error('Trace stopped multiple times');
160198

161-
result.extensions = {
162-
...result.extensions,
163-
ftv1: encodedBuffer.toString('base64'),
164-
};
199+
ctx.stopped = true;
200+
ctx.trace.durationNs = hrTimeToDurationInNanos(process.hrtime(ctx.startHrTime));
201+
ctx.trace.endTime = nowTimestamp();
165202

166-
setResult(result);
167-
},
168-
};
169-
},
203+
const encodedUint8Array = Trace.encode(ctx.trace).finish();
204+
const encodedBuffer = Buffer.from(encodedUint8Array, encodedUint8Array.byteOffset, encodedUint8Array.byteLength);
205+
206+
result.extensions = {
207+
...result.extensions,
208+
ftv1: encodedBuffer.toString('base64'),
170209
};
210+
211+
return result;
171212
}
172213

173214
/**

packages/plugins/apollo-inline-trace/test/use-apollo-inline-trace.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ it('should have proto tracing on parse fail', async () => {
212212
expect(result.errors).toBeDefined();
213213

214214
//
215-
215+
console.log(result);
216216
const ftv1 = result.extensions?.ftv1 as string;
217217
expect(typeof ftv1).toBe('string');
218218
const trace = Trace.decode(Buffer.from(ftv1, 'base64'));

0 commit comments

Comments
 (0)