-
Notifications
You must be signed in to change notification settings - Fork 36
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
implement multipart streaming for PreloadQuery
#389
Open
phryneas
wants to merge
20
commits into
next
Choose a base branch
from
pr/multipart-streaming
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f96ec74
add `(TeeTo|ReadFrom)ReadableStreamLink` links
phryneas 6890bd9
add `skipDataTransport` function
phryneas e77d7d7
also export type `ReadableStreamLinkEvent`
phryneas f3a27e6
forgot file
phryneas f6977a3
implement multipart streaming
phryneas 711208b
cleanup
phryneas 51562de
WIP
phryneas 09c1be0
handle re-consumption of readFromReadableStreamKey
phryneas 1554899
close on unsubscribe, catch errors
phryneas 6337e38
tryClose, test adjustments
phryneas 88e5e63
adjust errorlink type
phryneas ecc65ff
streamline bundling
phryneas 5e28770
fix jest integration test
phryneas 888130c
PreloadQuery: add `@defer` example
phryneas c4c4282
add detail, suspense-transition-bug
phryneas 56adfdb
work around bug for now
phryneas 92abc98
exports
phryneas b3ae471
add exported types
phryneas e0dbe98
Merge remote-tracking branch 'origin/next' into pr/multipart-streaming
phryneas 5bacf2e
fix up test for new deps
phryneas 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { TransformStream } from "node:stream/web"; | ||
|
||
globalThis.TransformStream = TransformStream; |
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
92 changes: 92 additions & 0 deletions
92
integration-test/nextjs/src/app/graphql/IncrementalSchemaLink.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { | ||
ApolloLink, | ||
FetchResult, | ||
Observable, | ||
Operation, | ||
} from "@apollo/client/index.js"; | ||
import type { SchemaLink } from "@apollo/client/link/schema"; | ||
import { | ||
experimentalExecuteIncrementally, | ||
SubsequentIncrementalExecutionResult, | ||
validate, | ||
} from "graphql"; | ||
import { ObjMap } from "graphql/jsutils/ObjMap"; | ||
|
||
export class IncrementalSchemaLink extends ApolloLink { | ||
public schema: SchemaLink.Options["schema"]; | ||
public rootValue: SchemaLink.Options["rootValue"]; | ||
public context: SchemaLink.Options["context"]; | ||
public validate: boolean; | ||
|
||
constructor(options: SchemaLink.Options) { | ||
super(); | ||
this.schema = options.schema; | ||
this.rootValue = options.rootValue; | ||
this.context = options.context; | ||
this.validate = !!options.validate; | ||
} | ||
|
||
public request(operation: Operation): Observable<FetchResult> { | ||
return new Observable<FetchResult>((observer) => { | ||
new Promise<SchemaLink.ResolverContext>((resolve) => | ||
resolve( | ||
typeof this.context === "function" | ||
? this.context(operation) | ||
: this.context | ||
) | ||
) | ||
.then((context) => { | ||
if (this.validate) { | ||
const validationErrors = validate(this.schema, operation.query); | ||
if (validationErrors.length > 0) { | ||
return { errors: validationErrors }; | ||
} | ||
} | ||
|
||
return experimentalExecuteIncrementally({ | ||
schema: this.schema, | ||
document: operation.query, | ||
rootValue: this.rootValue, | ||
contextValue: context, | ||
variableValues: operation.variables, | ||
operationName: operation.operationName, | ||
}); | ||
}) | ||
.then((data) => { | ||
if (!observer.closed) { | ||
if ("initialResult" in data) { | ||
observer.next(data.initialResult); | ||
return data.subsequentResults.next().then(function handleChunk( | ||
next: IteratorResult< | ||
SubsequentIncrementalExecutionResult< | ||
ObjMap<unknown>, | ||
ObjMap<unknown> | ||
>, | ||
void | ||
> | ||
): Promise<unknown> | void { | ||
if (!observer.closed) { | ||
if (next.value) { | ||
observer.next(next.value); | ||
} | ||
if (next.done) { | ||
observer.complete(); | ||
} else { | ||
return data.subsequentResults.next().then(handleChunk); | ||
} | ||
} | ||
}); | ||
} else { | ||
observer.next(data); | ||
observer.complete(); | ||
} | ||
} | ||
}) | ||
.catch((error) => { | ||
if (!observer.closed) { | ||
observer.error(error); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,54 +4,86 @@ import * as entryPoint from "@apollo/client-react-streaming"; | |
import type { IResolvers } from "@graphql-tools/utils"; | ||
|
||
const typeDefs = gql` | ||
directive @defer( | ||
if: Boolean! = true | ||
label: String | ||
) on FRAGMENT_SPREAD | INLINE_FRAGMENT | ||
|
||
type RatingWithEnv { | ||
value: String! | ||
env: String! | ||
} | ||
|
||
type Product { | ||
id: String! | ||
title: String! | ||
rating(delay: Int!): RatingWithEnv! | ||
} | ||
|
||
type Query { | ||
products(someArgument: String): [Product!]! | ||
env: String! | ||
} | ||
`; | ||
|
||
const products = [ | ||
{ | ||
id: "product:5", | ||
title: "Soft Warm Apollo Beanie", | ||
rating: "5/5", | ||
}, | ||
{ | ||
id: "product:2", | ||
title: "Stainless Steel Water Bottle", | ||
rating: "5/5", | ||
}, | ||
{ | ||
id: "product:3", | ||
title: "Athletic Baseball Cap", | ||
rating: "5/5", | ||
}, | ||
{ | ||
id: "product:4", | ||
title: "Baby Onesies", | ||
rating: "cuteness overload", | ||
}, | ||
{ | ||
id: "product:1", | ||
title: "The Apollo T-Shirt", | ||
rating: "5/5", | ||
}, | ||
{ | ||
id: "product:6", | ||
title: "The Apollo Socks", | ||
rating: "5/5", | ||
}, | ||
]; | ||
|
||
function getEnv(context?: any) { | ||
return context && context.from === "network" | ||
? "browser" | ||
: "built_for_ssr" in entryPoint | ||
? "SSR" | ||
: "built_for_browser" in entryPoint | ||
? "Browser" | ||
: "built_for_rsc" in entryPoint | ||
? "RSC" | ||
: "unknown"; | ||
} | ||
|
||
const resolvers = { | ||
Query: { | ||
products: async () => [ | ||
{ | ||
id: "product:5", | ||
title: "Soft Warm Apollo Beanie", | ||
}, | ||
{ | ||
id: "product:2", | ||
title: "Stainless Steel Water Bottle", | ||
}, | ||
{ | ||
id: "product:3", | ||
title: "Athletic Baseball Cap", | ||
}, | ||
{ | ||
id: "product:4", | ||
title: "Baby Onesies", | ||
}, | ||
{ | ||
id: "product:1", | ||
title: "The Apollo T-Shirt", | ||
}, | ||
{ | ||
id: "product:6", | ||
title: "The Apollo Socks", | ||
}, | ||
], | ||
env: (source, args, context) => { | ||
return context && context.from === "network" | ||
? "browser" | ||
: "built_for_ssr" in entryPoint | ||
? "SSR" | ||
: "built_for_browser" in entryPoint | ||
? "Browser" | ||
: "built_for_rsc" in entryPoint | ||
? "RSC" | ||
: "unknown"; | ||
products: async () => products.map(({ id, title }) => ({ id, title })), | ||
env: (source, args, context) => getEnv(context), | ||
}, | ||
Product: { | ||
rating: (source, args, context) => { | ||
return new Promise((resolve) => | ||
setTimeout(resolve, Math.random() * 2 * args.delay, { | ||
value: products.find((p) => p.id === source.id)?.rating, | ||
env: getEnv(context), | ||
}) | ||
); | ||
Comment on lines
+80
to
+86
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. adding a slow field that can be used to test |
||
}, | ||
}, | ||
} satisfies IResolvers; | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
For use in tests, essentially a
SchemaLink
with@defer
support.