From c4d7a909c0a880578226169f1072b7b4828fa18b Mon Sep 17 00:00:00 2001 From: Daniel Lehr Date: Thu, 31 Mar 2022 20:01:43 +0200 Subject: [PATCH] move around --- src/core.ts | 52 +++++++++++++++++++++------------------------- src/helix-flare.ts | 21 +++++++++++++------ 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/core.ts b/src/core.ts index 85d6c40..1035f8e 100644 --- a/src/core.ts +++ b/src/core.ts @@ -39,37 +39,33 @@ const core = async ({ if (isPreflight) { return new Response(null, { status: 204, headers }) } - try { - const helixRequest = await createHelixRequest(request) - const { operationName, query, variables } = - getGraphQLParameters(helixRequest) + const helixRequest = await createHelixRequest(request) - const result = await processRequest({ - operationName, - query, - variables, - request: helixRequest, - schema, - parse, - validate, - execute, - contextFactory, - }) + const { operationName, query, variables } = getGraphQLParameters(helixRequest) - switch (result.type) { - case 'RESPONSE': - return getResponse(result, headers) - case 'PUSH': - // @todo cors headers - return getPushResponseSSE(result, request) - case 'MULTIPART_RESPONSE': - return getMultipartResponse(result, Response, ReadableStream as any) - default: - return new Response('Not supported.', { status: 405 }) - } - } catch { - return new Response('Bad Request', { status: 400 }) + const result = await processRequest({ + operationName, + query, + variables, + request: helixRequest, + schema, + parse, + validate, + execute, + contextFactory, + }) + + switch (result.type) { + case 'RESPONSE': + return getResponse(result, headers) + case 'PUSH': + // @todo cors headers + return getPushResponseSSE(result, request) + case 'MULTIPART_RESPONSE': + return getMultipartResponse(result, Response, ReadableStream as any) + default: + return new Response('Not supported.', { status: 405 }) } } diff --git a/src/helix-flare.ts b/src/helix-flare.ts index 96ba9c6..fb9d795 100644 --- a/src/helix-flare.ts +++ b/src/helix-flare.ts @@ -36,12 +36,21 @@ const helixFlare = async ( schema: GraphQLSchema, { middlewares = [], access, contextFactory }: Options = {}, ) => { - return core({ - request, - schema: applyMiddleware(schema, ...middlewares), - contextFactory, - access, - }) + try { + return core({ + request, + schema: applyMiddleware(schema, ...middlewares), + contextFactory, + access, + }) + } catch (e) { + if (e instanceof SyntaxError) { + return new Response('Bad request', { + status: 400, + headers: { 'Content-Type': 'text/plain' }, + }) + } + } } export default helixFlare