-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
198 additions
and
46 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 @@ | ||
--- | ||
'@graphql-tools/executor-http': patch | ||
--- | ||
|
||
Memoize the print result automatically, and able to accept a custom print function |
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-tools/executor-envelop': patch | ||
--- | ||
|
||
Skip validation if the schema is not provided |
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,6 @@ | ||
--- | ||
'@graphql-tools/executor-envelop': patch | ||
'@graphql-tools/executor-yoga': patch | ||
--- | ||
|
||
Move schema introspection logic to Envelop |
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,12 @@ | ||
import { DocumentNode, print } from 'graphql'; | ||
|
||
const printCache = new WeakMap<DocumentNode, string>(); | ||
|
||
export function defaultPrintFn(document: DocumentNode) { | ||
let printed = printCache.get(document); | ||
if (!printed) { | ||
printed = print(document); | ||
printCache.set(document, printed); | ||
} | ||
return printed; | ||
} |
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 |
---|---|---|
@@ -1,57 +1,32 @@ | ||
import { GraphQLSchema } from 'graphql'; | ||
import { Plugin } from 'graphql-yoga'; | ||
import { useExecutor as useEnvelopExecutor } from '@graphql-tools/executor-envelop'; | ||
import { Executor, isPromise, MaybePromise } from '@graphql-tools/utils'; | ||
import { schemaFromExecutor } from '@graphql-tools/wrap'; | ||
|
||
type Opts = Parameters<typeof schemaFromExecutor>[2] & { | ||
polling?: number; | ||
}; | ||
import type { Plugin } from 'graphql-yoga'; | ||
import { | ||
ExecutorPluginOpts, | ||
useExecutor as useEnvelopExecutor, | ||
} from '@graphql-tools/executor-envelop'; | ||
import { Executor } from '@graphql-tools/utils'; | ||
|
||
export function useExecutor( | ||
executor: Executor, | ||
opts?: Opts, | ||
opts?: ExecutorPluginOpts, | ||
): Plugin & { invalidateSupergraph: () => void } { | ||
let schema$: MaybePromise<GraphQLSchema> | undefined; | ||
let schema: GraphQLSchema | undefined; | ||
if (opts?.polling) { | ||
setInterval(() => { | ||
schema$ = undefined; | ||
schema = undefined; | ||
}, opts.polling); | ||
} | ||
const envelopPlugin = useEnvelopExecutor(executor, opts); | ||
return { | ||
onPluginInit({ addPlugin }) { | ||
addPlugin( | ||
// @ts-expect-error TODO: fix typings | ||
useEnvelopExecutor(executor), | ||
envelopPlugin, | ||
); | ||
}, | ||
onRequestParse({ serverContext }) { | ||
return { | ||
onRequestParseDone() { | ||
if (!schema$) { | ||
schema$ ||= schemaFromExecutor(executor, serverContext, opts); | ||
if (isPromise(schema$)) { | ||
return schema$.then(newSchema => { | ||
schema = newSchema; | ||
}) as Promise<void>; | ||
} | ||
envelopPlugin.ensureSchema(serverContext); | ||
if (envelopPlugin.pluginCtx.schemaSetPromise$) { | ||
return envelopPlugin.pluginCtx.schemaSetPromise$ as Promise<void>; | ||
} | ||
}, | ||
}; | ||
}, | ||
onEnveloped({ setSchema }) { | ||
if (!schema) { | ||
throw new Error( | ||
`You provide a promise of a schema but it hasn't been resolved yet. Make sure you use this plugin with GraphQL Yoga.`, | ||
); | ||
} | ||
setSchema(schema); | ||
}, | ||
invalidateSupergraph() { | ||
schema$ = undefined; | ||
schema = undefined; | ||
}, | ||
invalidateSupergraph: envelopPlugin.invalidateSupergraph, | ||
}; | ||
} |