@@ -36,6 +36,11 @@ function startServerAndCreateNextHandler<
36
36
} ,
37
37
} ) ;
38
38
39
+ const headers : Record < string , string > = { } ;
40
+ for ( const [ key , value ] of httpGraphQLResponse . headers ) {
41
+ headers [ key ] = value ;
42
+ }
43
+
39
44
if ( isNextApiRequest ( req ) ) {
40
45
if ( ! res ) {
41
46
throw new Error ( 'API Routes require you to pass both the req and res object.' ) ;
@@ -50,9 +55,7 @@ function startServerAndCreateNextHandler<
50
55
if ( httpGraphQLResponse . body . kind === 'complete' ) {
51
56
res . send ( httpGraphQLResponse . body . string ) ;
52
57
} else {
53
- res . writeHead ( 200 , {
54
- 'Content-Type' : 'multipart/mixed; boundary="-"' ,
55
- } ) ;
58
+ res . writeHead ( 200 , headers ) ;
56
59
57
60
for await ( const chunk of httpGraphQLResponse . body . asyncIterator ) {
58
61
res . write ( chunk ) ;
@@ -63,36 +66,25 @@ function startServerAndCreateNextHandler<
63
66
return ;
64
67
}
65
68
66
- const headers : Record < string , string > = { } ;
67
- for ( const [ key , value ] of httpGraphQLResponse . headers ) {
68
- headers [ key ] = value ;
69
- }
69
+ let responseObject : string | ReadableStream ;
70
70
71
71
if ( httpGraphQLResponse . body . kind === 'complete' ) {
72
- // eslint-disable-next-line consistent-return
73
- return new Response ( httpGraphQLResponse . body . string , {
74
- headers,
75
- status : httpGraphQLResponse . status || 200 ,
72
+ responseObject = httpGraphQLResponse . body . string ;
73
+ } else {
74
+ const responseBody = httpGraphQLResponse . body ;
75
+ responseObject = new ReadableStream ( {
76
+ async start ( controller ) {
77
+ for await ( const chunk of responseBody . asyncIterator ) {
78
+ controller . enqueue ( chunk ) ;
79
+ }
80
+ controller . close ( ) ;
81
+ } ,
76
82
} ) ;
77
83
}
78
84
79
- const responseBody = httpGraphQLResponse . body ;
80
- const stream = new ReadableStream ( {
81
- async start ( controller ) {
82
- for await ( const chunk of responseBody . asyncIterator ) {
83
- controller . enqueue ( chunk ) ;
84
- }
85
- controller . close ( ) ;
86
- } ,
87
- } ) ;
88
-
89
85
// eslint-disable-next-line consistent-return
90
- return new Response ( stream , {
91
- headers : {
92
- ...headers ,
93
- 'Content-Type' : 'application/json' ,
94
- 'Transfer-Encoding' : 'chunked' ,
95
- } ,
86
+ return new Response ( responseObject , {
87
+ headers,
96
88
status : httpGraphQLResponse . status || 200 ,
97
89
} ) ;
98
90
}
0 commit comments