-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
feat: add multipart subscription network adapters for Relay and urql #11301
Merged
+261
−2
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a398c25
feat: add multipart subscription adapters for Relay and urql
alessbell f7d8e4c
fix: merge conflict
alessbell 163311b
chore: add changeset
alessbell 807e2cd
Move entrypoints to individual folders and update config/entryPoints.js
alessbell 7ddbc60
chore: commit new .api-reports files
alessbell 9a04453
chore: fix exports test
alessbell 78f3230
chore: fix merge conflicts
alessbell 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Observable } from "relay-runtime"; | ||
import type { RequestParameters, GraphQLResponse } from "relay-runtime"; | ||
import { | ||
handleError, | ||
readMultipartBody, | ||
} from "../../link/http/parseAndCheckHttpResponse.js"; | ||
import { maybe } from "../index.js"; | ||
import { serializeFetchParameter } from "../../core/index.js"; | ||
|
||
import type { OperationVariables } from "../../core/index.js"; | ||
import type { Body } from "../../link/http/selectHttpOptionsAndBody.js"; | ||
import { generateOptionsForMultipartSubscription } from "./shared.js"; | ||
import type { CreateMultipartSubscriptionOptions } from "./shared.js"; | ||
|
||
const backupFetch = maybe(() => fetch); | ||
|
||
export function createFetchMultipartSubscription( | ||
uri: string, | ||
{ fetch: preferredFetch, headers }: CreateMultipartSubscriptionOptions = {} | ||
) { | ||
return function fetchMultipartSubscription( | ||
operation: RequestParameters, | ||
variables: OperationVariables | ||
): Observable<GraphQLResponse> { | ||
const body: Body = { | ||
operationName: operation.name, | ||
variables, | ||
query: operation.text || "", | ||
}; | ||
const options = generateOptionsForMultipartSubscription(headers || {}); | ||
|
||
return Observable.create((sink) => { | ||
try { | ||
options.body = serializeFetchParameter(body, "Payload"); | ||
} catch (parseError) { | ||
sink.error(parseError); | ||
} | ||
|
||
const currentFetch = preferredFetch || maybe(() => fetch) || backupFetch; | ||
const observerNext = sink.next.bind(sink); | ||
|
||
currentFetch!(uri, options) | ||
.then((response) => { | ||
const ctype = response.headers?.get("content-type"); | ||
|
||
if (ctype !== null && /^multipart\/mixed/i.test(ctype)) { | ||
return readMultipartBody(response, observerNext); | ||
} | ||
|
||
sink.error(new Error("Expected multipart response")); | ||
}) | ||
.then(() => { | ||
sink.complete(); | ||
}) | ||
.catch((err: any) => { | ||
handleError(err, sink); | ||
}); | ||
}); | ||
}; | ||
} |
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,21 @@ | ||
import { fallbackHttpConfig } from "../../link/http/selectHttpOptionsAndBody.js"; | ||
|
||
export type CreateMultipartSubscriptionOptions = { | ||
fetch?: WindowOrWorkerGlobalScope["fetch"]; | ||
headers?: Record<string, string>; | ||
}; | ||
|
||
export function generateOptionsForMultipartSubscription( | ||
headers: Record<string, string> | ||
) { | ||
const options: { headers: Record<string, any>; body?: string } = { | ||
...fallbackHttpConfig.options, | ||
headers: { | ||
...(headers || {}), | ||
...fallbackHttpConfig.headers, | ||
accept: | ||
"multipart/mixed;boundary=graphql;subscriptionSpec=1.0,application/json", | ||
}, | ||
}; | ||
return options; | ||
} |
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,56 @@ | ||
import { Observable } from "../index.js"; | ||
import { | ||
handleError, | ||
readMultipartBody, | ||
} from "../../link/http/parseAndCheckHttpResponse.js"; | ||
import { maybe } from "../index.js"; | ||
import { serializeFetchParameter } from "../../core/index.js"; | ||
import type { Body } from "../../link/http/selectHttpOptionsAndBody.js"; | ||
import { generateOptionsForMultipartSubscription } from "./shared.js"; | ||
import type { CreateMultipartSubscriptionOptions } from "./shared.js"; | ||
|
||
const backupFetch = maybe(() => fetch); | ||
|
||
export function createFetchMultipartSubscription( | ||
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. I used the same function name here but maybe this should be a more urql-y |
||
uri: string, | ||
{ fetch: preferredFetch, headers }: CreateMultipartSubscriptionOptions = {} | ||
) { | ||
return function multipartSubscriptionForwarder({ | ||
query, | ||
variables, | ||
}: { | ||
query?: string; | ||
variables: undefined | Record<string, any>; | ||
}) { | ||
const body: Body = { variables, query }; | ||
const options = generateOptionsForMultipartSubscription(headers || {}); | ||
|
||
return new Observable((observer) => { | ||
try { | ||
options.body = serializeFetchParameter(body, "Payload"); | ||
} catch (parseError) { | ||
observer.error(parseError); | ||
} | ||
|
||
const currentFetch = preferredFetch || maybe(() => fetch) || backupFetch; | ||
const observerNext = observer.next.bind(observer); | ||
|
||
currentFetch!(uri, options) | ||
.then((response) => { | ||
const ctype = response.headers?.get("content-type"); | ||
|
||
if (ctype !== null && /^multipart\/mixed/i.test(ctype)) { | ||
return readMultipartBody(response, observerNext); | ||
} | ||
|
||
observer.error(new Error("Expected multipart response")); | ||
}) | ||
.then(() => { | ||
observer.complete(); | ||
}) | ||
.catch((err: any) => { | ||
handleError(err, observer); | ||
}); | ||
}); | ||
}; | ||
} |
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.
zen-observable-ts
'sObservable
is incompatible with Relay's networking API here, so I'm using therelay-runtime
'sObservable
. (Using the former produces anetwork.execute(...).do is not a function
error.)A related note about project dependencies: I added
@types/relay-runtime
to our dev dependencies, but did not specifyrelay-runtime
as an optional peer dependency. The risk of confusing a lot of people seemed to outweigh the chance at a mild annoyance if someone were to try to drop in this subscriptions network layer without already havingrelay-runtime
installed :)