Skip to content

Commit

Permalink
fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Sep 15, 2023
1 parent 0b9640c commit fa7580c
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 105 deletions.
176 changes: 94 additions & 82 deletions src/__benchmarks__/benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,94 +113,106 @@ ${availableBenchmarks.map(([bench]) => ` - ${bench}`).join("\n")}
: availableBenchmarks;

const benchs = await Promise.all(
filteredBenchmarks.map(async ([bench, { query, schema, variables }]) => {
const compiledQuery = compileQuery(schema, query, undefined, {
debug: true
} as any);
if (!isCompiledQuery(compiledQuery)) {
// eslint-disable-next-line no-console
console.error(`${bench} failed to compile`);
return null;
}
// eslint-disable-next-line no-console
console.log(
`size of function for ${bench}: ${
(compiledQuery as any)
.__DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_compilation.length
}`
);
const graphqlJsResult = await execute({
schema,
document: query,
variableValues: variables || {}
});
const graphqlJitResult = await compiledQuery.query(
undefined,
undefined,
variables || {}
);
if (
JSON.stringify(graphqlJitResult) !== JSON.stringify(graphqlJsResult)
) {
filteredBenchmarks.map(
async ([bench, { query, schema, variables, options }]) => {
const compiledQuery = compileQuery(schema, query, undefined, {
...options,
debug: true
} as any);
if (!isCompiledQuery(compiledQuery)) {
// eslint-disable-next-line no-console
console.error(`${bench} failed to compile`);
return null;
}
// eslint-disable-next-line no-console
console.error(
JSON.stringify(graphqlJitResult),
"is different of",
JSON.stringify(graphqlJsResult)
console.log(
`size of function for ${bench}: ${
(compiledQuery as any)
.__DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_compilation.length
}`
);
return null;
}
const suite = new Benchmark.Suite(bench);
if (!skipJS) {
suite.add("graphql-js", {
minSamples: 150,
defer: true,
fn(deferred: any) {
const result = execute({
schema,
document: query,
variableValues: variables || {}
});
if (isPromise(result)) {
return result.then((res) =>
deferred.resolve(skipJSON ? res : JSON.stringify(res))
console.log(
`size of function for variableCompilation ${bench}: ${(
compiledQuery as any
).__DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_variableCompilation?.length}`
);
const graphqlJsResult = await execute({
schema,
document: query,
variableValues: variables || {}
});
const graphqlJitResult = await compiledQuery.query(
undefined,
undefined,
variables || {}
);
if (
JSON.stringify(graphqlJitResult) !== JSON.stringify(graphqlJsResult)
) {
// eslint-disable-next-line no-console
console.error(
JSON.stringify(graphqlJitResult),
"is different of",
JSON.stringify(graphqlJsResult)
);
return null;
}
const suite = new Benchmark.Suite(bench);
if (!skipJS) {
suite.add("graphql-js", {
minSamples: 150,
defer: true,
fn(deferred: any) {
const result = execute({
schema,
document: query,
variableValues: variables || {}
});
if (isPromise(result)) {
return result.then((res) =>
deferred.resolve(skipJSON ? res : JSON.stringify(res))
);
}
return deferred.resolve(
skipJSON ? result : JSON.stringify(result)
);
}
return deferred.resolve(skipJSON ? result : JSON.stringify(result));
}
});
}
suite
.add("graphql-jit", {
minSamples: 150,
defer: true,
fn(deferred: any) {
const result = compiledQuery.query(
undefined,
undefined,
variables || {}
);
if (isPromise(result)) {
return result.then((res) =>
deferred.resolve(skipJSON ? res : compiledQuery.stringify(res))
});
}
suite
.add("graphql-jit", {
minSamples: 150,
defer: true,
fn(deferred: any) {
const result = compiledQuery.query(
undefined,
undefined,
variables || {}
);
if (isPromise(result)) {
return result.then((res) =>
deferred.resolve(
skipJSON ? res : compiledQuery.stringify(res)
)
);
}
return deferred.resolve(
skipJSON ? result : compiledQuery.stringify(result)
);
}
return deferred.resolve(
skipJSON ? result : compiledQuery.stringify(result)
);
}
})
// add listeners
.on("cycle", (event: any) => {
// eslint-disable-next-line no-console
console.log(String(event.target));
})
.on("start", () => {
// eslint-disable-next-line no-console
console.log("Starting", bench);
});
return suite;
})
})
// add listeners
.on("cycle", (event: any) => {
// eslint-disable-next-line no-console
console.log(String(event.target));
})
.on("start", () => {
// eslint-disable-next-line no-console
console.log("Starting", bench);
});
return suite;
}
)
);

const benchsToRun = benchs.filter(isNotNull);
Expand Down
42 changes: 26 additions & 16 deletions src/__benchmarks__/variables-parsing-shallow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,9 @@ export function schema() {
resolvers: {
Query: {
async products(_, { filter }) {
return products.filter((product) => {
if (filter.and) {
return (
product.name.includes(filter.and.left.like) &&
product.name.includes(filter.and.right.like)
);
} else if (filter.or) {
return (
product.name.includes(filter.or.left.like) ||
product.name.includes(filter.or.right.like)
);
} else {
return product.name.includes(filter.like);
}
});
return products.filter((product) =>
productSatisfiesFilter(product, filter)
);
}
}
}
Expand All @@ -55,7 +43,10 @@ export function schema() {

export const query = parse(`
query ($filter1: Filter) {
products(filter: $filter1)
products(filter: $filter1) {
id
name
}
}
`);

Expand All @@ -79,6 +70,25 @@ export const variables = {
}
};

function productSatisfiesFilter(
product: (typeof products)[0],
filter: any
): boolean {
if (filter.and) {
return (
productSatisfiesFilter(product, filter.and.left) &&
productSatisfiesFilter(product, filter.and.right)
);
} else if (filter.or) {
return (
productSatisfiesFilter(product, filter.or.left) ||
productSatisfiesFilter(product, filter.or.right)
);
} else {
return product.name.includes(filter.like);
}
}

const products = [
{
id: "1",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/recursive-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("recursive input types", () => {
}
});

test("should not fail for recursive input without variables", () => {
test.only("should not fail for recursive input without variables", () => {
const query = parse(`
{
foo(input: {
Expand Down
17 changes: 12 additions & 5 deletions src/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ import { Maybe } from "./types";
import {
CoercedVariableValues,
getVariablesParser,
failToParseVariables
failToParseVariables,
compileVariableParsing
} from "./variables";
import { getGraphQLErrorOptions, getOperationRootType } from "./compat";

Expand Down Expand Up @@ -163,6 +164,7 @@ export interface CompilationContext extends GraphQLContext {
deferred: DeferredField[];
options: CompilerOptions;
depth: number;
variableCompliationFnLength?: number;
}

// prefix for the variable used ot cache validation results
Expand Down Expand Up @@ -204,6 +206,7 @@ export interface CompiledQuery<

interface InternalCompiledQuery extends CompiledQuery {
__DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_compilation?: string;
__DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_variableCompilation?: string;
}

/**
Expand Down Expand Up @@ -265,10 +268,12 @@ export function compileQuery<
stringify = JSON.stringify;
}

const getVariables = getVariablesParser(
schema,
context.operation.variableDefinitions || []
);
const getVariables = context.options.useJitVariablesParser
? compileVariableParsing(
schema,
context.operation.variableDefinitions || []
)
: getVariablesParser(schema, context.operation.variableDefinitions || []);

const type = getOperationRootType(context.schema, context.operation);
const fieldMap = collectFields(
Expand Down Expand Up @@ -317,6 +322,8 @@ export function compileQuery<
// and visualization tools like try-jit.
compiledQuery.__DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_compilation =
functionBody;
compiledQuery.__DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_variableCompilation =
(getVariables as any).rawFunctionBody;
}
return compiledQuery as CompiledQuery<TResult, TVariables>;
} catch (err: any) {
Expand Down
10 changes: 9 additions & 1 deletion src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function compileVariableParsing(

const generatedFn = gen.toString();

return Function.apply(
const ret = Function.apply(

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (18, 15)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (18, 15)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (18, 16)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (18, 16)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (18, 17.0.0-alpha.3)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (18, 17.0.0-alpha.3)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (20, 15)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (20, 15)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (20, 16)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (20, 16)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (20, 17.0.0-alpha.3)

Use the spread operator instead of '.apply()'

Check failure on line 166 in src/variables.ts

View workflow job for this annotation

GitHub Actions / build (20, 17.0.0-alpha.3)

Use the spread operator instead of '.apply()'
null,
["GraphQLJITError", "inspect"]
.concat(Array.from(dependencies.keys()))
Expand All @@ -172,6 +172,14 @@ export function compileVariableParsing(
null,
[GraphQLJITError, inspect].concat(Array.from(dependencies.values()))
);

Object.defineProperties(ret, {
rawFunctionBody: {
value: generatedFn
}
});

return ret;
}

// Int Scalars represent 32 bits
Expand Down

0 comments on commit fa7580c

Please sign in to comment.