Skip to content

Commit

Permalink
fix(url-loader): use multipart/mixed in accept if operationAst has de…
Browse files Browse the repository at this point in the history
…fer or stream directives (#4655)

* fix(url-loader): use multipart/mixed in accept if operationAst ha
s defer or stream directives

* Add test
  • Loading branch information
ardatan committed Aug 15, 2022
1 parent 39b76cd commit b6f1f5c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-tips-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/url-loader': patch
---

Fix defer/stream multipart response support
15 changes: 14 additions & 1 deletion packages/loaders/url/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/* eslint-disable no-case-declarations */
/// <reference lib="dom" />
import { print, IntrospectionOptions, GraphQLError, buildASTSchema, buildSchema } from 'graphql';
import {
print,
IntrospectionOptions,
GraphQLError,
buildASTSchema,
buildSchema,
OperationDefinitionNode,
} from 'graphql';

import {
AsyncExecutor,
Expand Down Expand Up @@ -319,6 +326,12 @@ export class UrlLoader implements Loader<LoadFromUrlOptions> {
if (operationType === 'subscription' || isLiveQueryOperationDefinitionNode(operationAst)) {
method = 'GET';
accept = 'text/event-stream';
} else if (
(operationAst as OperationDefinitionNode).directives?.some(
({ name }) => name.value === 'defer' || name.value === 'stream'
)
) {
accept += ', multipart/mixed';
}

const endpoint = request.extensions?.endpoint || HTTP_URL;
Expand Down
5 changes: 4 additions & 1 deletion packages/loaders/url/tests/helix-yoga-compat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ describe('helix/yoga compat', () => {
];
const serverPort = 1335;
const serverHost = 'http://localhost:' + serverPort;
httpServer = http.createServer((_, res) => {
let receivedAcceptHeader: string | undefined;
httpServer = http.createServer((req, res) => {
receivedAcceptHeader = req.headers['accept'];
res.writeHead(200, {
// prettier-ignore
"Connection": "keep-alive",
Expand Down Expand Up @@ -92,6 +94,7 @@ describe('helix/yoga compat', () => {
expect(data).toEqual(expectedDatas.shift()!);
}
expect(expectedDatas.length).toBe(0);
expect(receivedAcceptHeader).toBe('application/json');
});

it('should handle SSE subscription result', async () => {
Expand Down

1 comment on commit b6f1f5c

@vercel
Copy link

@vercel vercel bot commented on b6f1f5c Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.