Skip to content

Commit

Permalink
Fix: add URL to the global namespace. (#446)
Browse files Browse the repository at this point in the history
New functionality exposed in federation relies on the URL type being
available.
  • Loading branch information
o0Ignition0o authored Jan 23, 2024
1 parent fcbd2f1 commit a0d7b4a
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions federation-2/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion federation-2/harmonizer/esbuild/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ require("esbuild")
format: "iife",
plugins: [denoFsPlugin],
define: { Buffer: "buffer_shim" },
inject: ["./esbuild/buffer_shim.js"],
inject: ["./esbuild/buffer_shim.js", "./esbuild/url_shim.js"],
})
.catch(() => process.exit(1));
1 change: 1 addition & 0 deletions federation-2/harmonizer/esbuild/url_shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
globalThis.URL = require("url").URL;
2 changes: 1 addition & 1 deletion federation-2/router-bridge/esbuild/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ require("esbuild")
format: "iife",
plugins: [denoFsPlugin],
define: { Buffer: "buffer_shim" },
inject: ["./esbuild/buffer_shim.js"],
inject: ["./esbuild/buffer_shim.js", "./esbuild/url_shim.js"],
})
.catch(() => process.exit(1));
1 change: 1 addition & 0 deletions federation-2/router-bridge/esbuild/url_shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
globalThis.URL = require("url").URL;
13 changes: 13 additions & 0 deletions federation-2/router-bridge/js-src/test_url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @ts-nocheck

const assertEq = (a: unknown, b: unknown) => {
if (a !== b) {
throw `${a} is not equal to ${b}`;
}
};

const url = new URL("https://www.test.com/test2");

assertEq("/test2", url.pathname);
assertEq("www.test.com", url.hostname);
assertEq("https", url.scheme);
2 changes: 1 addition & 1 deletion federation-2/router-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "2.7.0",
"description": "Apollo Router JS Bridge Entrypoint",
"scripts": {
"build": "make-dir bundled js-dist && rm -f tsconfig.tsbuildinfo && tsc --build --verbose && node esbuild/bundler.js && cp js-dist/runtime.js js-dist/do_api_schema.js js-dist/do_introspect.js js-dist/plan_worker.js js-dist/test_logger_worker.js js-dist/test_get_random_values.js bundled/",
"build": "make-dir bundled js-dist && rm -f tsconfig.tsbuildinfo && tsc --build --verbose && node esbuild/bundler.js && cp js-dist/runtime.js js-dist/do_api_schema.js js-dist/do_introspect.js js-dist/plan_worker.js js-dist/test_logger_worker.js js-dist/test_get_random_values.js js-dist/test_url.js bundled/",
"clean": "rm -rf ./node_modules ./js-dist ./bundled ./tsconfig.tsbuildinfo",
"lint": "prettier --check ./esbuild/**/*.js ./js-src/**/*.ts ./js-src/**/*.js",
"format": "prettier --write ./esbuild/**/*.js ./js-src/**/*.ts ./js-src/**/*.js"
Expand Down
10 changes: 10 additions & 0 deletions federation-2/router-bridge/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,14 @@ mod worker_tests {

worker.quit().unwrap();
}

#[tokio::test]
// This test ensures the URL api is available.
// federation relies on it since 2.7
async fn test_url() {
let mut worker = JsWorker::new(include_str!("../bundled/test_get_random_values.js"));

JsWorker::new(include_str!("../bundled/test_url.js"));
worker.quit().unwrap();
}
}
3 changes: 3 additions & 0 deletions federation-2/supergraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ harmonizer = { path = "../harmonizer" }
camino = "1"
serde_json = "1"
structopt = "0.3"

[dev-dependencies]
insta = { version = "1.34.0", features = ["json"] }
11 changes: 11 additions & 0 deletions federation-2/supergraph/src/command/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ impl Compose {
harmonize(subgraph_definitions)
}
}

#[test]
fn compose_test() {
let res = Compose {
config_file: "./tests/compose_test.yaml".into(),
};

let result = res.do_compose();

insta::assert_json_snapshot!(result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: supergraph/src/command/compose.rs
expression: result
---
{
"Ok": {
"supergraphSdl": "schema\n @link(url: \"https://specs.apollo.dev/link/v1.0\")\n @link(url: \"https://specs.apollo.dev/join/v0.4\", for: EXECUTION)\n @join__directive(graphs: [TEST], name: \"link\", args: {url: \"https://specs.apollo.dev/source/v0.1\", import: [\"@sourceAPI\", \"@sourceType\", \"@sourceField\"], for: \"EXECUTION\"})\n @join__directive(graphs: [TEST], name: \"sourceAPI\", args: {name: \"json\", http: {baseURL: \"http://a.co/\"}})\n{\n query: Query\n}\n\ndirective @join__directive(graphs: [join__Graph!], name: String!, args: join__DirectiveArguments) repeatable on SCHEMA | OBJECT | INTERFACE | FIELD_DEFINITION\n\ndirective @join__enumValue(graph: join__Graph!) repeatable on ENUM_VALUE\n\ndirective @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet, type: String, external: Boolean, override: String, usedOverridden: Boolean, overrideLabel: String) repeatable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION\n\ndirective @join__graph(name: String!, url: String!) on ENUM_VALUE\n\ndirective @join__implements(graph: join__Graph!, interface: String!) repeatable on OBJECT | INTERFACE\n\ndirective @join__type(graph: join__Graph!, key: join__FieldSet, extension: Boolean! = false, resolvable: Boolean! = true, isInterfaceObject: Boolean! = false) repeatable on OBJECT | INTERFACE | UNION | ENUM | INPUT_OBJECT | SCALAR\n\ndirective @join__unionMember(graph: join__Graph!, member: String!) repeatable on UNION\n\ndirective @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA\n\ninput HTTPHeaderMapping\n @join__type(graph: TEST)\n{\n name: String!\n as: String\n value: String\n}\n\ninput HTTPSourceAPI\n @join__type(graph: TEST)\n{\n baseURL: String!\n headers: [HTTPHeaderMapping!]\n}\n\ninput HTTPSourceField\n @join__type(graph: TEST)\n{\n GET: source__URLPathTemplate\n POST: source__URLPathTemplate\n PUT: source__URLPathTemplate\n PATCH: source__URLPathTemplate\n DELETE: source__URLPathTemplate\n body: source__JSONSelection\n headers: [HTTPHeaderMapping!]\n}\n\ninput HTTPSourceType\n @join__type(graph: TEST)\n{\n GET: source__URLPathTemplate\n POST: source__URLPathTemplate\n headers: [HTTPHeaderMapping!]\n body: source__JSONSelection\n}\n\nscalar join__DirectiveArguments\n\nscalar join__FieldSet\n\nenum join__Graph {\n TEST @join__graph(name: \"test\", url: \"http://localhost:4000/graphql\")\n}\n\ninput KeyTypeMap\n @join__type(graph: TEST)\n{\n key: String!\n typeMap: source__TypenameKeyMap\n}\n\nscalar link__Import\n\nenum link__Purpose {\n \"\"\"\n `SECURITY` features provide metadata necessary to securely resolve fields.\n \"\"\"\n SECURITY\n\n \"\"\"\n `EXECUTION` features provide metadata necessary for operation execution.\n \"\"\"\n EXECUTION\n}\n\ntype Query\n @join__type(graph: TEST)\n{\n users: [User] @join__directive(graphs: [TEST], name: \"sourceField\", args: {api: \"json\", http: {GET: \"/users\"}, selection: \"id name\"})\n}\n\nscalar source__JSONSelection\n @join__type(graph: TEST)\n\nscalar source__TypenameKeyMap\n @join__type(graph: TEST)\n\nscalar source__URLPathTemplate\n @join__type(graph: TEST)\n\ntype User\n @join__type(graph: TEST)\n{\n id: ID!\n name: String\n}",
"hints": []
}
}
25 changes: 25 additions & 0 deletions federation-2/supergraph/tests/compose_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
federation_version: =2.7.0
subgraphs:
test:
name: test
routing_url: "http://localhost:4000/graphql"
schema:
sdl: |
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])
@link(
url: "https://specs.apollo.dev/source/v0.1"
import: ["@sourceAPI", "@sourceType", "@sourceField"]
for: EXECUTION
)
@sourceAPI(name: "json", http: { baseURL: "http://a.co/" })
type Query {
users: [User]
@sourceField(api: "json", http: { GET: "/users" }, selection: "id name")
}
type User {
id: ID!
name: String
}

0 comments on commit a0d7b4a

Please sign in to comment.