Skip to content

Commit

Permalink
Automatically disable JIT when the schema or the env is not compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Feb 12, 2024
1 parent d6d7e3c commit 3200abe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-boxes-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/runtime": patch
---

Compatibility with the environments and schemas do not support GraphQL JIT
4 changes: 2 additions & 2 deletions packages/runtime/src/get-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { MESH_CONTEXT_SYMBOL } from './constants.js';
import { getInContextSDK } from './in-context-sdk.js';
import { ExecuteMeshFn, GetMeshOptions, MeshExecutor, SubscribeMeshFn } from './types.js';
import { useSubschema } from './useSubschema.js';
import { isStreamOperation } from './utils.js';
import { isGraphQLJitCompatible, isStreamOperation } from './utils.js';

type SdkRequester = (document: DocumentNode, variables?: any, operationContext?: any) => any;

Expand Down Expand Up @@ -276,7 +276,7 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
},
{
enableIf(args) {
return !isStreamOperation(args.document);
return isGraphQLJitCompatible(args.schema) && !isStreamOperation(args.document);
},
},
),
Expand Down
6 changes: 5 additions & 1 deletion packages/runtime/src/useSubschema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
memoize1,
printSchemaWithDirectives,
} from '@graphql-tools/utils';
import { isGraphQLJitCompatible } from './utils';

enum IntrospectionQueryType {
FEDERATION = 'FEDERATION',
Expand Down Expand Up @@ -95,7 +96,10 @@ function getExecuteFn(subschema: Subschema) {
};
let executor = subschema.executor;
if (executor == null) {
if (isStream || operationAST.operation === 'subscription') {
if (
(!isGraphQLJitCompatible(subschema.schema) && isStream) ||
operationAST.operation === 'subscription'
) {
executor = createDefaultExecutor(subschema.schema);
} else {
executor = function subschemaExecutor(request: ExecutionRequest): any {
Expand Down
34 changes: 32 additions & 2 deletions packages/runtime/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ASTNode, BREAK, visit } from 'graphql';
import { memoize1 } from '@graphql-tools/utils';
import { ASTNode, BREAK, getNamedType, GraphQLSchema, visit } from 'graphql';
import { MapperKind, mapSchema, memoize1 } from '@graphql-tools/utils';

export const isStreamOperation = memoize1(function isStreamOperation(astNode: ASTNode): boolean {
let isStream = false;
Expand All @@ -16,3 +16,33 @@ export const isStreamOperation = memoize1(function isStreamOperation(astNode: AS
});
return isStream;
});

export const isGraphQLJitCompatible = memoize1(function isGraphQLJitCompatible(
schema: GraphQLSchema,
) {
let compatibleSchema = true;
mapSchema(schema, {
[MapperKind.INPUT_OBJECT_TYPE]: type => {
const fieldMap = type.getFields();
for (const fieldName in fieldMap) {
const fieldObj = fieldMap[fieldName];
const namedType = getNamedType(fieldObj.type);
if (namedType.name === type.name) {
compatibleSchema = false;
return undefined;
}
}
return undefined;
},
});
if (compatibleSchema) {
try {
// eslint-disable-next-line no-new-func
const a = new Function('return true');
return a();
} catch (e) {
return false;
}
}
return false;
});

0 comments on commit 3200abe

Please sign in to comment.