-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement multipart streaming for
PreloadQuery
(#389)
Co-authored-by: Jerel Miller <[email protected]>
- Loading branch information
1 parent
9acf761
commit 8209093
Showing
42 changed files
with
1,057 additions
and
385 deletions.
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 @@ | ||
--- | ||
"@apollo/client-react-streaming": minor | ||
--- | ||
|
||
Implement multipart streaming support for `PreloadQuery` |
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
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
import { | ||
ApolloLink, | ||
FetchResult, | ||
Observable, | ||
Operation, | ||
} from "@apollo/client/index.js"; | ||
import type { SchemaLink } from "@apollo/client/link/schema"; | ||
import { experimentalExecuteIncrementally, validate } from "graphql"; | ||
|
||
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) => { | ||
(async () => { | ||
try { | ||
const context = await (typeof this.context === "function" | ||
? this.context(operation) | ||
: this.context); | ||
if (this.validate) { | ||
const validationErrors = validate(this.schema, operation.query); | ||
if (validationErrors.length > 0) { | ||
return { errors: validationErrors }; | ||
} | ||
} | ||
|
||
if (observer.closed) return; | ||
const data = await experimentalExecuteIncrementally({ | ||
schema: this.schema, | ||
document: operation.query, | ||
rootValue: this.rootValue, | ||
contextValue: context, | ||
variableValues: operation.variables, | ||
operationName: operation.operationName, | ||
}); | ||
|
||
if ("initialResult" in data) { | ||
if (observer.closed) return; | ||
observer.next(data.initialResult); | ||
|
||
for await (const value of data.subsequentResults) { | ||
if (observer.closed) return; | ||
observer.next(value); | ||
} | ||
|
||
if (observer.closed) return; | ||
observer.complete(); | ||
} else { | ||
if (observer.closed) return; | ||
|
||
observer.next(data); | ||
observer.complete(); | ||
} | ||
} catch (error) { | ||
if (observer.closed) return; | ||
|
||
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
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.