Skip to content

Commit

Permalink
Go
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jul 30, 2022
1 parent 982337f commit 3286faf
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 202 deletions.
39 changes: 38 additions & 1 deletion packages/graphql-yoga/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UseMaskedErrorsOpts } from '@envelop/core'
import { createGraphQLError } from '@graphql-tools/utils'
import { GraphQLError } from 'graphql'
import { GraphQLError, GraphQLHTTPErrorExtensions } from 'graphql'
import { ResultProcessorInput } from './plugins/types'
import { YogaMaskedErrorOpts } from './types'

declare module 'graphql' {
Expand Down Expand Up @@ -53,3 +54,39 @@ export function handleError(
}
return errors
}

export function getResponseInitByRespectingErrors(
result: ResultProcessorInput,
headers: Record<string, string> = {},
): ResponseInit {
let status: number | undefined

if ('errors' in result && result.errors?.length) {
for (const error of result.errors) {
if (error.extensions?.http) {
if (
error.extensions.http.status &&
(!status || error.extensions.http.status > status)
) {
status = error.extensions.http.status
}
if (error.extensions.http.headers) {
Object.assign(headers, error.extensions.http.headers)
}
// Remove http extensions from the final response
error.extensions.http = undefined
}
}

if (!status) {
status = 500
}
} else {
status = 200
}

return {
status,
headers,
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isAsyncIterable } from '@envelop/core'
import { ExecutionResult } from 'graphql'
import { getResponseInitByRespectingErrors } from '../../error.js'
import { FetchAPI } from '../../types.js'
import { ResultProcessorInput } from '../types.js'

Expand All @@ -12,15 +13,13 @@ export function processMultipartResult(
result: ResultProcessorInput,
fetchAPI: FetchAPI,
): Response {
const headersInit: HeadersInit = {
const headersInit = {
Connection: 'keep-alive',
'Content-Type': 'multipart/mixed; boundary="-"',
'Transfer-Encoding': 'chunked',
}
const responseInit: ResponseInit = {
headers: headersInit,
status: 200,
}

const responseInit = getResponseInitByRespectingErrors(result, headersInit)

let iterator: AsyncIterator<ExecutionResult<any>>

Expand Down
9 changes: 4 additions & 5 deletions packages/graphql-yoga/src/plugins/resultProcessor/push.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isAsyncIterable } from '@envelop/core'
import { ExecutionResult } from 'graphql'
import { getResponseInitByRespectingErrors } from '../../error.js'
import { FetchAPI } from '../../types.js'
import { ResultProcessorInput } from '../types.js'

Expand All @@ -12,16 +13,14 @@ export function processPushResult(
result: ResultProcessorInput,
fetchAPI: FetchAPI,
): Response {
const headersInit: HeadersInit = {
const headersInit = {
'Content-Type': 'text/event-stream',
Connection: 'keep-alive',
'Cache-Control': 'no-cache',
'Content-Encoding': 'none',
}
const responseInit: ResponseInit = {
headers: headersInit,
status: 200,
}

const responseInit = getResponseInitByRespectingErrors(result, headersInit)

let iterator: AsyncIterator<ExecutionResult<any>>

Expand Down
13 changes: 8 additions & 5 deletions packages/graphql-yoga/src/plugins/resultProcessor/regular.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isAsyncIterable } from '@graphql-tools/utils'
import { getResponseInitByRespectingErrors } from '../../error.js'
import { FetchAPI } from '../../types.js'
import { ResultProcessorInput } from '../types.js'

Expand Down Expand Up @@ -38,13 +39,15 @@ export function processRegularResult(
const responseBody = JSON.stringify(executionResult)
const decodedString = textEncoder.encode(responseBody)
const contentType = acceptHeaderByResult.get(executionResult)
const headersInit: HeadersInit = {
const headersInit = {
'Content-Type': contentType || 'application/json',
'Content-Length': decodedString.byteLength.toString(),
}
const responseInit: ResponseInit = {
headers: headersInit,
status: 200,
}

const responseInit = getResponseInitByRespectingErrors(
executionResult,
headersInit,
)

return new fetchAPI.Response(decodedString, responseInit)
}
Loading

0 comments on commit 3286faf

Please sign in to comment.