-
Notifications
You must be signed in to change notification settings - Fork 565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect Accept headers in a more strict way #1516
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'graphql-yoga': major | ||
--- | ||
|
||
Now it is possible to decide the returned `Content-Type` by specifying the `Accept` header. So if `Accept` header has `text/event-stream` without `application/json`, Yoga respects that returns `text/event-stream` instead of `application/json`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -621,6 +621,7 @@ describe('Incremental Delivery', () => { | |
body: formData, | ||
}) | ||
|
||
expect(response.status).toBe(200) | ||
const body = await response.json() | ||
|
||
expect(body.errors).toBeUndefined() | ||
|
@@ -1481,3 +1482,58 @@ describe('404 Handling', () => { | |
expect(body).toEqual('Do you really like em?') | ||
}) | ||
}) | ||
|
||
describe('Respect Accept headers', () => { | ||
const yoga = createYoga({ | ||
schema, | ||
}) | ||
const server = createServer(yoga) | ||
let port: number | ||
let url: string | ||
beforeAll((done) => { | ||
port = Math.floor(Math.random() * 100) + 4000 | ||
url = `http://localhost:${port}/graphql` | ||
server.listen(port, done) | ||
}) | ||
afterAll(() => { | ||
server.close() | ||
}) | ||
it('should force the server return event stream even if the result is not', async () => { | ||
const response = await fetch(`${url}?query=query{ping}`, { | ||
headers: { | ||
Accept: 'text/event-stream', | ||
}, | ||
}) | ||
expect(response.headers.get('content-type')).toEqual('text/event-stream') | ||
const iterator = response.body![Symbol.asyncIterator]() | ||
const { value } = await iterator.next() | ||
const valueStr = Buffer.from(value).toString('utf-8') | ||
expect(valueStr).toContain( | ||
`data: ${JSON.stringify({ data: { ping: 'pong' } })}`, | ||
) | ||
}) | ||
it('should force the server return multipart even if the result is not', async () => { | ||
const response = await fetch(`${url}?query=query{ping}`, { | ||
headers: { | ||
Accept: 'multipart/mixed', | ||
}, | ||
}) | ||
expect(response.headers.get('content-type')).toEqual( | ||
'multipart/mixed; boundary="-"', | ||
) | ||
const iterator = response.body![Symbol.asyncIterator]() | ||
const { value } = await iterator.next() | ||
const valueStr = Buffer.from(value).toString('utf-8') | ||
expect(valueStr).toContain(`Content-Type: application/json; charset=utf-8`) | ||
expect(valueStr).toContain(`Content-Length: 24`) | ||
expect(valueStr).toContain(`${JSON.stringify({ data: { ping: 'pong' } })}`) | ||
}) | ||
it('should not allow to return if the result is an async iterable and accept is just json', async () => { | ||
const response = await fetch(`${url}?query=subscription{counter}`, { | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}) | ||
expect(response.status).toEqual(406) | ||
}) | ||
Comment on lines
+1531
to
+1538
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmmmm, here it could actually be debatable whether it is okay for the client to only wait for the first event and then ditch the subscription 🤔 |
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 26 additions & 5 deletions
31
packages/graphql-yoga/src/plugins/resultProcessor/regular.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these tests do not necessarily require running a Node HTTP server, but we can change it later on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is safer to test stream requests and responses via the really HTTP