Skip to content

Commit

Permalink
fix(runtime): ensure a plugin registered once in onPluginInit's addPl…
Browse files Browse the repository at this point in the history
…ugin
  • Loading branch information
ardatan committed Oct 19, 2022
1 parent 3f26a85 commit 398af2b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-dryers-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/runtime': patch
'@graphql-mesh/types': patch
---

Fix a bug causing the plugin is registered twice with onPluginInit's addPlugin
57 changes: 31 additions & 26 deletions packages/runtime/src/get-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export interface MeshInstance {
sdkRequesterFactory(globalContext: any): SdkRequester;
}

const memoizedGetEnvelopedFactory = memoize1(function getEnvelopedFactory(plugins: MeshPlugin<any>[]) {
return envelop({
plugins,
});
});

const memoizedGetOperationType = memoize1((document: DocumentNode) => {
const operationAST = getOperationAST(document, undefined);
if (!operationAST) {
Expand Down Expand Up @@ -265,37 +271,36 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {

const subschema = new Subschema(unifiedSubschema);

const getEnveloped = envelop({
plugins: [
useEngine({
validate,
specifiedRules,
}),
useSubschema(subschema),
useExtendContext(() => {
if (!inContextSDK$) {
const onDelegateHooks: OnDelegateHook<any>[] = [];
for (const plugin of initialPluginList) {
if (plugin?.onDelegate != null) {
onDelegateHooks.push(plugin.onDelegate);
}
const plugins = [
useEngine({
validate,
specifiedRules,
}),
useSubschema(subschema),
useExtendContext(() => {
if (!inContextSDK$) {
const onDelegateHooks: OnDelegateHook<any>[] = [];
for (const plugin of initialPluginList) {
if (plugin?.onDelegate != null) {
onDelegateHooks.push(plugin.onDelegate);
}
inContextSDK$ = getInContextSDK(subschema.transformedSchema, rawSources, logger, onDelegateHooks);
}
return inContextSDK$;
}),
useExtendedValidation({
rules: [OneOfInputObjectsRule],
}),
...initialPluginList,
],
});
inContextSDK$ = getInContextSDK(subschema.transformedSchema, rawSources, logger, onDelegateHooks);
}
return inContextSDK$;
}),
useExtendedValidation({
rules: [OneOfInputObjectsRule],
}),
...initialPluginList,
];

const EMPTY_ROOT_VALUE: any = {};
const EMPTY_CONTEXT_VALUE: any = {};
const EMPTY_VARIABLES_VALUE: any = {};

function createExecutor(globalContext: any = EMPTY_CONTEXT_VALUE): MeshExecutor {
const getEnveloped = memoizedGetEnvelopedFactory(plugins);
const { schema, parse, execute, subscribe, contextFactory } = getEnveloped(globalContext);
return async function meshExecutor<TVariables = any, TContext = any, TRootValue = any, TData = any>(
documentOrSDL: GraphQLOperation<TData, TVariables>,
Expand Down Expand Up @@ -341,10 +346,10 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
pubsub,
destroy: meshDestroy,
logger,
get plugins() {
return getEnveloped._plugins;
plugins,
get getEnveloped() {
return memoizedGetEnvelopedFactory(plugins);
},
getEnveloped,
createExecutor,
get execute() {
return createExecutor();
Expand Down
74 changes: 55 additions & 19 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,39 @@
}
]
},
"deduplicateRequest": {
"anyOf": [
{
"type": "object",
"additionalProperties": true
},
{
"type": "string"
},
{
"type": "array",
"additionalItems": true
}
]
},
"httpCache": {
"anyOf": [
{
"type": "object",
"additionalProperties": true
},
{
"type": "string"
},
{
"type": "array",
"additionalItems": true
}
]
},
"httpDetailsExtensions": {
"$ref": "#/definitions/HTTPDetailsExtensionsConfig"
},
"liveQuery": {
"$ref": "#/definitions/LiveQueryConfig"
},
Expand Down Expand Up @@ -530,21 +563,6 @@
},
"statsd": {
"$ref": "#/definitions/StatsdPlugin"
},
"httpCache": {
"anyOf": [
{
"type": "object",
"additionalProperties": true
},
{
"type": "string"
},
{
"type": "array",
"additionalItems": true
}
]
}
}
},
Expand Down Expand Up @@ -1951,6 +1969,28 @@
},
"required": ["type", "fieldName"]
},
"HTTPDetailsExtensionsConfig": {
"additionalProperties": false,
"type": "object",
"title": "HTTPDetailsExtensionsConfig",
"properties": {
"if": {
"anyOf": [
{
"type": "object",
"additionalProperties": true
},
{
"type": "string"
},
{
"type": "array",
"additionalItems": true
}
]
}
}
},
"LiveQueryConfig": {
"additionalProperties": false,
"type": "object",
Expand Down Expand Up @@ -3859,10 +3899,6 @@
],
"description": "Path to a custom W3 Compatible Fetch Implementation"
},
"includeHttpDetailsInExtensions": {
"type": "boolean",
"description": "Include HTTP details to the extensions"
},
"skipSSLValidation": {
"type": "boolean",
"description": "Allow connections to an SSL endpoint without certificates"
Expand Down
11 changes: 6 additions & 5 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export interface Config {
* Path to a custom W3 Compatible Fetch Implementation
*/
customFetch?: any;
/**
* Include HTTP details to the extensions
*/
includeHttpDetailsInExtensions?: boolean;
/**
* Allow connections to an SSL endpoint without certificates
*/
Expand Down Expand Up @@ -1727,6 +1723,9 @@ export interface PubSubConfig {
export interface Plugin {
maskedErrors?: MaskedErrorsPluginConfig;
immediateIntrospection?: any;
deduplicateRequest?: any;
httpCache?: any;
httpDetailsExtensions?: HTTPDetailsExtensionsConfig;
liveQuery?: LiveQueryConfig;
mock?: MockingConfig;
newrelic?: NewrelicConfig;
Expand All @@ -1736,12 +1735,14 @@ export interface Plugin {
responseCache?: ResponseCacheConfig;
snapshot?: SnapshotPluginConfig;
statsd?: StatsdPlugin;
httpCache?: any;
[k: string]: any;
}
export interface MaskedErrorsPluginConfig {
errorMessage?: string;
}
export interface HTTPDetailsExtensionsConfig {
if?: any;
}
export interface LiveQueryConfig {
/**
* Invalidate a query or queries when a specific operation is done without an error
Expand Down

0 comments on commit 398af2b

Please sign in to comment.