Skip to content

Commit

Permalink
Document HTTP Executor & Fix ending multipart requests (#2137)
Browse files Browse the repository at this point in the history
* test: http executor defer/stream

update docs

* Pass

* chore(dependencies): updated changesets for modified dependencies

* Snapshots

---------

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 31, 2023
1 parent 169f92e commit 3a8446d
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-yoga_urql-exchange-2137-dependencies.md
@@ -0,0 +1,5 @@
---
'@graphql-yoga/urql-exchange': patch
---
dependencies updates:
- Updated dependency [`@graphql-tools/executor-http@0.1.3` ↗︎](https://www.npmjs.com/package/@graphql-tools/executor-http/v/0.1.3) (from `0.1.2`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/strange-beers-promise.md
@@ -0,0 +1,5 @@
---
'graphql-yoga': patch
---

Close multipart responses correctly
Expand Up @@ -11,7 +11,6 @@ Content-Type: application/json; charset=utf-8
Content-Length: 78
{"incremental":[{"data":{"slowField":"I am slow"},"path":[]}],"hasNext":false}
---
-----
"
`;
Expand Down Expand Up @@ -157,7 +156,6 @@ Content-Type: application/json; charset=utf-8
Content-Length: 17
{"hasNext":false}
---
-----
"
`;
2 changes: 1 addition & 1 deletion packages/client/urql-exchange/package.json
Expand Up @@ -49,7 +49,7 @@
"access": "public"
},
"dependencies": {
"@graphql-tools/executor-http": "0.1.2",
"@graphql-tools/executor-http": "0.1.3",
"@graphql-tools/executor-urql-exchange": "0.0.6",
"tslib": "^2.4.0"
},
Expand Down
Expand Up @@ -65,7 +65,7 @@ export function processMultipartResult(
controller.enqueue(textEncoder.encode('---'))
}
if (done) {
controller.enqueue(textEncoder.encode('\r\n-----\r\n'))
controller.enqueue(textEncoder.encode('--\r\n'))
controller.close()
}
},
Expand Down
90 changes: 88 additions & 2 deletions packages/plugins/defer-stream/__tests__/defer-stream.spec.ts
Expand Up @@ -3,10 +3,12 @@ import {
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
parse,
} from 'graphql'
import { useDeferStream } from '@graphql-yoga/plugin-defer-stream'
import { createSchema, createYoga, Repeater } from 'graphql-yoga'

import { buildHTTPExecutor } from '@graphql-tools/executor-http'
import { createPushPullAsyncIterable } from './push-pull-async-iterable.js'

function multipartStream<TType = unknown>(source: ReadableStream<Uint8Array>) {
Expand Down Expand Up @@ -127,7 +129,6 @@ describe('Defer/Stream', () => {
Content-Length: 74
{"incremental":[{"data":{"goodbye":"goodbye"},"path":[]}],"hasNext":false}
---
-----
"
`)
Expand Down Expand Up @@ -170,7 +171,6 @@ describe('Defer/Stream', () => {
Content-Length: 17
{"hasNext":false}
---
-----
"
`)
Expand Down Expand Up @@ -459,4 +459,90 @@ describe('Defer/Stream', () => {
)
})
})

describe('@graphql-tools/buildHTTPExecutor', () => {
it('execute stream operation', async () => {
const yoga = createYoga({ schema, plugins: [useDeferStream()] })
const executor = buildHTTPExecutor({
fetch: yoga.fetch,
})

const result = await executor({
document: parse(/* GraphQL */ `
query {
stream @stream(initialCount: 2)
}
`),
})

if (Symbol.asyncIterator in result) {
let counter = 0
for await (const item of result) {
if (counter === 0) {
expect(item).toEqual({
data: { stream: ['A', 'B'] },
})
counter++
} else if (counter >= 1) {
expect(item).toEqual({
data: { stream: ['A', 'B', 'C'] },
})
counter++
} else {
throw new Error('LOL, this should not happen.')
}
}
} else {
throw new Error('Expected AsyncIterator')
}
})
it('execute defer operation', async () => {
const yoga = createYoga({ schema, plugins: [useDeferStream()] })
const executor = buildHTTPExecutor({
endpoint: 'http://yoga/graphql',
fetch: yoga.fetch,
})

const result = await executor({
document: parse(/* GraphQL */ `
query {
hello
... on Query @defer {
goodbye
}
}
`),
})

if (Symbol.asyncIterator in result) {
let counter = 0
for await (const item of result) {
if (counter === 0) {
expect(item).toMatchInlineSnapshot(`
{
"data": {
"hello": "hello",
},
}
`)
counter++
} else if (counter === 1) {
expect(item).toMatchInlineSnapshot(`
{
"data": {
"goodbye": "goodbye",
"hello": "hello",
},
}
`)
counter++
} else {
throw new Error('LOL, this should not happen.')
}
}
} else {
throw new Error('Expected AsyncIterator')
}
})
})
})
1 change: 1 addition & 0 deletions packages/plugins/defer-stream/package.json
Expand Up @@ -44,6 +44,7 @@
"graphql-yoga": "^3.5.0"
},
"devDependencies": {
"@graphql-tools/executor-http": "0.1.3",
"@whatwg-node/fetch": "0.6.5"
},
"type": "module",
Expand Down
23 changes: 20 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3a8446d

Please sign in to comment.