Skip to content

Commit

Permalink
Setup CircleCI/Lerna/npm to compile and publish `@apollo/query-planne…
Browse files Browse the repository at this point in the history
…r-wasm` (#181)

* Setup CircleCI/Lerna/npm to compile and publish `@apollo/query-planner-wasm`

This allows us to compile and depend upon the local version of the WASM
query planner package and get the tarballs that are packed with the right
version.  Additionally, this allows Lerna to handle the version bumping and
publishing for `@apollo/query-planner-wasm`, since it is uniquely positioned
to do that best in a monorepo orchestration that involves multiple npm
packages with relative `file:` dependencies on each other and coupled with
our existing needs for Lerna and publishing workflows we have built.

Technically speaking, this relieves the `Cargo.toml` from its responsibility
of managing the `version` and shifts that responsibility to `lerna` and
other npm-specific tooling we have connected to that.  In fact, this change
completely eliminates the need for the `wasm-pack` generated `package.json`
that is derived from the Cargo.toml (and rendered into the "out-dir",
previously `pkg/`).

While I would believe that there are some advanced `wasm-pack` cases where
this above change might be undesirable, for now, the constraint incurred by
that transposition is unblocking in other ways.  For example, rather than
necessitating parallel metadata support in the `Cargo.toml` for every
`package.json` property (think about npm specific properties like "labels",
"bugs", "private"), we can simply change our `package.json` as we see fit.

I am claiming that this side-stepping of version control defined within
`Cargo.toml` is acceptable since we do not have any intention of publishing
the `query-planner-wasm` package to crates.io on its own.  Instead, we will
continue to publish the `wasm-pack`'d-from-this-crate
`@apollo/query-planner-wasm` package directly to npm's registry.  To reflect
that desire, I've marked the `query-planner-wasm` package as "private" using
the `private = true` property in its `Cargo.toml`.

In theory, this change could de-stabilize the work that `wasm-pack` does and
that's worth calling out.  For example, by not allowing it to generate the
`files` property (which indicates which artifacts are emitted into the `npm
pack`'d bundle), we might be not allowing it to add other (Future?
Unexpected?) important emitted files.  I've instead put those `files`
directly into the new `package.json` source of truth, along with other
appropriate properties, like `types` and `main`.  The largest risk here -
based on our intended use of the package - seems to be relevant only if we
were to rename the host Crate.  To demitigate that risk, I've explicitly
wired up the `package.json` with `--out-dir` and `--out-file` flags to
ensure that it always emits `index`-prefixed files.  Furthermore, they are
now emitted into the `dist` directory, to be parallel with all our other npm
package patterns which do the same.

I will note that, `wasm-pack` may address some of the work-arounds here in
the future, but best I can tell, they will be roughly compatible with what
we're doing here.  I am basing this outstanding issues, PRs, RFCs and
documentation on the `wasm-pack` project, referenced below.

Ref: https://rustwasm.github.io/rfcs/008-npm-dependencies.html#unresolved-questions (See last section)
Ref: https://rustwasm.github.io/docs/wasm-pack/commands/build.html#extra-options (See footnote)
Ref: rustwasm/wasm-pack#606
Ref: rustwasm/wasm-pack#840

* Update query-planner-wasm/package.json

Co-authored-by: Trevor Scheer <trevor@apollographql.com>

Co-authored-by: Trevor Scheer <trevor@apollographql.com>
  • Loading branch information
abernix and trevor-scheer authored Sep 24, 2020
1 parent 6b65b6a commit a32684c
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
19 changes: 19 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ commands:
common_test_steps:
description: "Commands to run on every Node.js environment"
steps:
- run:
# rustup must be installed prior to installing `wasm-pack`.
# If rustup isn't already in our environment, install it.
# On the `circleci/rust:*-node` images, it's already installed, so
# it's marginally faster to skip this when not necessary.
name: Ensure / Install rustup
command: |
which rustup > /dev/null 2>&1 ||
(
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs |
bash -s -- -y &&
echo 'source $HOME/.cargo/env' >> $BASH_ENV
# This last line here is for subsequent "run" steps.
)
- run:
name: Install wasm-pack
command: |
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf |
sh
- oss/install_specific_npm_version
- checkout
- oss/npm_clean_install_with_caching
Expand Down
2 changes: 1 addition & 1 deletion 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 gateway-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@apollo/federation": "file:../federation-js",
"@apollo/query-planner-wasm": "0.0.4",
"@apollo/query-planner-wasm": "file:../query-planner-wasm",
"@types/node-fetch": "2.5.4",
"apollo-engine-reporting-protobuf": "^0.5.2",
"apollo-graphql": "^0.6.0",
Expand Down
7 changes: 6 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"packages": ["federation-js", "federation-integration-testsuite-js", "gateway-js"],
"packages": [
"federation-js",
"federation-integration-testsuite-js",
"gateway-js",
"query-planner-wasm"
],
"version": "independent",
"command": {
"version": {
Expand Down
6 changes: 2 additions & 4 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"repository": "github:apollographql/federation",
"scripts": {
"clean": "git clean -dfqX -- ./node_modules **/{dist,node_modules}/ ./*/tsconfig*tsbuildinfo",
"clean": "git clean -dfqX -- ./target ./node_modules **/{dist,node_modules}/ ./*/tsconfig*tsbuildinfo",
"compile": "tsc --build tsconfig.build.json",
"compile:clean": "tsc --build tsconfig.build.json --clean",
"watch": "tsc --build tsconfig.build.json --watch",
Expand All @@ -26,6 +26,7 @@
"@apollographql/apollo-tools": "0.4.8",
"@apollo/federation": "file:federation-js",
"@apollo/gateway": "file:gateway-js",
"@apollo/query-planner-wasm": "file:query-planner-wasm",
"apollo-federation-integration-testsuite": "file:federation-integration-testsuite-js"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion query-planner-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "query-planner-wasm" # this ends up being the name in the published npm package.
version = "0.0.4"
version = "0.0.0-DO.NOT.CHANGE" # The version is managed in the package.json, by Lerna.
private = true
authors = ["Apollo <opensource@apollographql.com>"]
homepage = "https://github.com/apollographql/federation"
description = "Bridge code written in Rust to Javascript/Typescript, to be internally used by Apollo Gateway. This package is not meant to be independently consumed."
Expand Down
34 changes: 34 additions & 0 deletions query-planner-wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@apollo/query-planner-wasm",
"version": "0.0.4",
"description": "Bridge code written in Rust to Javascript/Typescript, to be internally used by Apollo Gateway. This package is not meant to be independently consumed.",
"scripts": {
"wasm-pack": "wasm-pack build --target nodejs --out-dir dist --out-name index --scope apollo",
"preinstall": "npm run wasm-pack"
},
"author": "opensource@apollographql.com",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/apollographql/federation.git",
"directory": "query-planner-wasm/"
},
"bugs": {
"url": "https://github.com/apollographql/federation/issues"
},
"homepage": "https://github.com/apollographql/federation#readme",
"keywords": [
"GraphQL",
"rust",
"wasm",
"apollo"
],
"files": [
"dist/index_bg.wasm",
"dist/index.js",
"dist/index_bg.js",
"dist/index.d.ts"
],
"main": "dist/index.js",
"types": "dist/index.d.ts"
}

0 comments on commit a32684c

Please sign in to comment.