MCPcopy
hub / github.com/graphql/graphiql / tryParseJsonObject

Function tryParseJsonObject

packages/graphiql-react/src/execution.tsx:293–318  ·  view source on GitHub ↗
({
  json,
  errorMessageParse,
  errorMessageType,
}: {
  json: string | undefined;
  errorMessageParse: string;
  errorMessageType: string;
})

Source from the content-addressed store, hash-verified

291export const useExecutionContext = createContextHook(ExecutionContext);
292
293function tryParseJsonObject({
294 json,
295 errorMessageParse,
296 errorMessageType,
297}: {
298 json: string | undefined;
299 errorMessageParse: string;
300 errorMessageType: string;
301}) {
302 let parsed: Record<string, any> | undefined;
303 try {
304 parsed = json && json.trim() !== '' ? JSON.parse(json) : undefined;
305 } catch (error) {
306 throw new Error(
307 `${errorMessageParse}: ${
308 error instanceof Error ? error.message : error
309 }.`,
310 );
311 }
312 const isObject =
313 typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed);
314 if (parsed !== undefined && !isObject) {
315 throw new Error(errorMessageType);
316 }
317 return parsed;
318}
319
320type IncrementalResult = {
321 data?: Record<string, unknown> | null;

Callers 1

runFunction · 0.85

Calls 1

parseMethod · 0.80

Tested by

no test coverage detected