Skip to content

Commit 88e090b

Browse files
committed
create factories to avoid using slow typeof each time an error is thrown
1 parent 0af8f21 commit 88e090b

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

packages/plugins/persisted-operations/src/index.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ export function usePersistedOperations<
9393
const operationASTByRequest = new WeakMap<Request, DocumentNode>()
9494
const persistedOperationRequest = new WeakSet<Request>()
9595

96+
const notFoundErrorFactory = createErrorFactory(
97+
'PersistedQueryNotFound',
98+
customErrors?.notFound,
99+
)
100+
const keyNotFoundErrorFactory = createErrorFactory(
101+
'PersistedQueryKeyNotFound',
102+
customErrors?.keyNotFound,
103+
)
104+
const persistentQueryOnlyErrorFactory = createErrorFactory(
105+
'PersistedQueryOnly',
106+
customErrors?.persistedQueryOnly,
107+
)
108+
96109
return {
97110
async onParams(payload) {
98111
const { request, params, setParams } = payload
@@ -103,32 +116,20 @@ export function usePersistedOperations<
103116
? allowArbitraryOperations
104117
: await allowArbitraryOperations(request)) === false
105118
) {
106-
throw createPersistedOperationError(
107-
'PersistedQueryOnly',
108-
payload,
109-
customErrors?.persistedQueryOnly,
110-
)
119+
throw persistentQueryOnlyErrorFactory(payload)
111120
}
112121
return
113122
}
114123

115124
const persistedOperationKey = extractPersistedOperationId(params)
116125

117126
if (persistedOperationKey == null) {
118-
throw createPersistedOperationError(
119-
'PersistedQueryNotFound',
120-
payload,
121-
customErrors?.keyNotFound,
122-
)
127+
throw keyNotFoundErrorFactory(payload)
123128
}
124129

125130
const persistedQuery = await getPersistedOperation(persistedOperationKey)
126131
if (persistedQuery == null) {
127-
throw createPersistedOperationError(
128-
'PersistedQueryNotFound',
129-
payload,
130-
customErrors?.notFound,
131-
)
132+
throw notFoundErrorFactory(payload)
132133
}
133134

134135
if (typeof persistedQuery === 'object') {
@@ -161,16 +162,19 @@ export function usePersistedOperations<
161162
}
162163
}
163164

164-
function createPersistedOperationError(
165+
function createErrorFactory(
165166
defaultMessage: string,
166-
payload: OnParamsEventPayload,
167167
options?: CustomErrorFactory,
168168
) {
169169
if (typeof options === 'string') {
170-
return createGraphQLError(options)
170+
return () => createGraphQLError(options)
171171
}
172+
172173
if (typeof options === 'function') {
173-
return options(payload)
174+
return options
175+
}
176+
177+
return () => {
178+
return createGraphQLError(options?.message ?? defaultMessage, options)
174179
}
175-
return createGraphQLError(options?.message ?? defaultMessage, options)
176180
}

website/src/pages/docs/features/persisted-operations.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ const yoga = createYoga({
307307
}
308308
},
309309
// Or customize with a factory function allowing you to use your own error class or format
310-
onlyPersisted: () => {
310+
persistedQueryOnly: () => {
311311
return new CustomErrorClass('Only Persisted Operations are allowed')
312312
}
313313
}

0 commit comments

Comments
 (0)