Skip to content

Commit 58d80ea

Browse files
authored
[@envelop/sentry] improve context type (#2103)
* [@envelop/sentry] improve context type * changeset
1 parent 3b01a9f commit 58d80ea

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

.changeset/grumpy-weeks-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@envelop/sentry': minor
3+
---
4+
5+
Allow to provide the context type as a generic parameter

packages/plugins/sentry/src/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { ExecutionArgs, GraphQLError, Kind, OperationDefinitionNode, print } from 'graphql';
1+
import { GraphQLError, Kind, OperationDefinitionNode, print } from 'graphql';
22
import {
33
getDocumentString,
44
handleStreamOrSingleExecutionResult,
55
isOriginalGraphQLError,
66
OnExecuteDoneHookResultOnNextHook,
7+
TypedExecutionArgs,
78
type Plugin,
89
} from '@envelop/core';
910
import * as Sentry from '@sentry/node';
1011
import type { Span, TraceparentData } from '@sentry/types';
1112

12-
export type SentryPluginOptions = {
13+
export type SentryPluginOptions<PluginContext extends Record<string, any>> = {
1314
/**
1415
* Starts a new transaction for every GraphQL Operation.
1516
* When disabled, an already existing Transaction will be used.
@@ -45,34 +46,34 @@ export type SentryPluginOptions = {
4546
/**
4647
* Adds custom tags to every Transaction.
4748
*/
48-
appendTags?: (args: ExecutionArgs) => Record<string, unknown>;
49+
appendTags?: (args: TypedExecutionArgs<PluginContext>) => Record<string, unknown>;
4950
/**
5051
* Callback to set context information onto the scope.
5152
*/
52-
configureScope?: (args: ExecutionArgs, scope: Sentry.Scope) => void;
53+
configureScope?: (args: TypedExecutionArgs<PluginContext>, scope: Sentry.Scope) => void;
5354
/**
5455
* Produces a name of Transaction (only when "renameTransaction" or "startTransaction" are enabled) and description of created Span.
5556
*
5657
* @default operation's name or "Anonymous Operation" when missing)
5758
*/
58-
transactionName?: (args: ExecutionArgs) => string;
59+
transactionName?: (args: TypedExecutionArgs<PluginContext>) => string;
5960
/**
6061
* Produces tracing data for Transaction
6162
*
6263
* @default is empty
6364
*/
64-
traceparentData?: (args: ExecutionArgs) => TraceparentData | undefined;
65+
traceparentData?: (args: TypedExecutionArgs<PluginContext>) => TraceparentData | undefined;
6566
/**
6667
* Produces a "op" (operation) of created Span.
6768
*
6869
* @default execute
6970
*/
70-
operationName?: (args: ExecutionArgs) => string;
71+
operationName?: (args: TypedExecutionArgs<PluginContext>) => string;
7172
/**
7273
* Indicates whether or not to skip the entire Sentry flow for given GraphQL operation.
7374
* By default, no operations are skipped.
7475
*/
75-
skip?: (args: ExecutionArgs) => boolean;
76+
skip?: (args: TypedExecutionArgs<PluginContext>) => boolean;
7677
/**
7778
* Indicates whether or not to skip Sentry exception reporting for a given error.
7879
* By default, this plugin skips all `GraphQLError` errors and does not report it to Sentry.
@@ -82,10 +83,12 @@ export type SentryPluginOptions = {
8283

8384
export const defaultSkipError = isOriginalGraphQLError;
8485

85-
export const useSentry = (options: SentryPluginOptions = {}): Plugin => {
86-
function pick<K extends keyof SentryPluginOptions>(
86+
export const useSentry = <PluginContext extends Record<string, any> = {}>(
87+
options: SentryPluginOptions<PluginContext> = {},
88+
): Plugin<PluginContext> => {
89+
function pick<K extends keyof SentryPluginOptions<PluginContext>>(
8790
key: K,
88-
defaultValue: NonNullable<SentryPluginOptions[K]>,
91+
defaultValue: NonNullable<SentryPluginOptions<PluginContext>[K]>,
8992
) {
9093
return options[key] ?? defaultValue;
9194
}

0 commit comments

Comments
 (0)