-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Introduce query planner to router-bridge
fixes #1088 This commit is built on top of @BrynCooke 's work on #812, and exposes a query planning functionality, which has been living in a separate branch for a while. A few things happened: General: - Remove jest tests from the router bridge (they weren't being run anyway, and they are now superseded by the cargo tests) - Add router-bridge js files to the prettier configuration for formatting and linting - Add a dependency to query-planner-js to the router-bridge TypeScript/Javascript: - Add `do_plan.ts` that calls the query-planner behind the scenes. - Use `new QueryPlanner(schema).buildQueryPlan` instead of the old `buildQueryPlan` function, that doesn't exist anymore (depending on how long we want to keep this code, we might be able to keep the QueryPlanner object around, and skip subsequent schema parsing / composing. - Move the type `OperationResult` to a utils file (so we can use it in both `do_plan.ts` and `do_introspect.ts` Rust: - Use &str instead of String for schema SDL, since apollo-rs provides as_str() on the Schema type. - Add a plan function, that is generic over it's return type ( `Result<T, PlanningErrors>` where `T: DeserializeOwned + 'static` so we can keep the `QueryPlan` structure on the router-core side. - Add plan() tests that call plan<serde_json::Value>() so we have snapshots over the plan result.
- Loading branch information
1 parent
bbbfb3f
commit 0e86671
Showing
23 changed files
with
576 additions
and
118 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,35 @@ | ||
import type { plan } from '.'; | ||
import type { OperationResult } from './types'; | ||
import { BuildQueryPlanOptions } from '@apollo/query-planner'; | ||
|
||
/** | ||
* There are several global properties that we make available in our V8 runtime | ||
* and these are the types for those that we expect to use within this script. | ||
* They'll be stripped in the emitting of this file as JS, of course. | ||
*/ | ||
declare var bridge: { plan: typeof plan }; | ||
|
||
declare var done: (operationResult: OperationResult) => void; | ||
declare var schemaString: string; | ||
declare var queryString: string; | ||
declare var options: BuildQueryPlanOptions; | ||
declare var operationName: string | undefined; | ||
|
||
if (!options) { | ||
done({ | ||
Err: [{ message: 'Error in JS-Rust-land: options is missing.' }], | ||
}); | ||
} | ||
|
||
const planResult = bridge.plan( | ||
schemaString, | ||
queryString, | ||
options, | ||
operationName, | ||
); | ||
|
||
if (planResult.errors?.length > 0) { | ||
done({ Err: planResult.errors }); | ||
} else { | ||
done({ Ok: planResult.data }); | ||
} |
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,2 +1,2 @@ | ||
export { introspect, batchIntrospect } from "./introspection"; | ||
export { parse as parseGraphqlDocument } from "graphql"; | ||
export { introspect, batchIntrospect } from './introspection'; | ||
export { plan } from './plan'; |
File renamed without changes.
73 changes: 0 additions & 73 deletions
73
router-bridge/js-src/introspection/__tests__/index.test.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
import { ExecutionResult, parse } from 'graphql'; | ||
import { | ||
QueryPlanner, | ||
buildOperationContext, | ||
BuildQueryPlanOptions, | ||
buildComposedSchema, | ||
} from '@apollo/query-planner'; | ||
|
||
export function plan( | ||
schemaString: string, | ||
queryString: string, | ||
options: BuildQueryPlanOptions, | ||
operationName?: string, | ||
): ExecutionResult { | ||
try { | ||
const schema = parse(schemaString); | ||
const query = parse(queryString); | ||
const composedSchema = buildComposedSchema(schema); | ||
const operationContext = buildOperationContext( | ||
composedSchema, | ||
query, | ||
operationName, | ||
); | ||
|
||
const planner = new QueryPlanner(composedSchema); | ||
return { data: planner.buildQueryPlan(operationContext, options) }; | ||
} catch (e) { | ||
return { errors: [e] }; | ||
} | ||
} |
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 @@ | ||
export type OperationResult = | ||
| { Ok: any; Err?: undefined } | ||
| { Ok?: undefined; Err: any }; |
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,3 +1,3 @@ | ||
import "fast-text-encoding"; | ||
import * as url from "whatwg-url"; | ||
import 'fast-text-encoding'; | ||
import * as url from 'whatwg-url'; | ||
export default url; |
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
Oops, something went wrong.