Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stabilise rhai #1365

Merged
merged 6 commits into from
Jul 14, 2022
Merged

stabilise rhai #1365

merged 6 commits into from
Jul 14, 2022

Conversation

garypen
Copy link
Contributor

@garypen garypen commented Jul 6, 2022

Add support for rhai's module system and propose stabilising rhai.

This deepens the integration between rhai and the router and makes it possible to modularise
functionality within rhai code.

This change makes it possible for a router to have multiple rhai scripts loaded within a router. A single "main" script and an arbitrary number of "imported scripts" which are imported using rhai's module resolution mechanism from a specified location.

This kind of thing then becomes possible:

rhai/router.yaml:
===
rhai:
  scripts: rhai

rhai/also.rhai:
===
fn also() {
    print("also");
}

rhai/helpful.rhai:
===
fn helpful() {
    print("helpful");
}

rhai/main.rhai:
===
import "helpful.rhai" as helpful;
import "also.rhai" as also;

// Call map_request with our service and pass in a string with the name
// of the function to callback
fn subgraph_service(service, subgraph) {
    helpful::helpful(); <= calling functions from imported modules
    also::also(); <= calling functions from imported modules
    print(`registering request callback for ${subgraph}`);
    const request_callback = Fn("process_request");
    service.map_request(request_callback);
}

The above would process the "main" script as part of the config processing, including importing the specified modules.

You can do a lot of interesting things with rhai's modules, including executing them directly on inclusion or dynamically loading them within the scope of a function (or a closure). For more details:

https://rhai.rs/book/ref/modules/import.html

garypen added 2 commits July 6, 2022 14:08
And add support for rhai's module system. This deepens the integration
between rhai and the router and makes it possible to modularise
functionality within rhai code.
tidy up the code.
@garypen garypen requested a review from a team July 6, 2022 14:52
@garypen garypen self-assigned this Jul 6, 2022
@garypen garypen requested review from SimonSapin, bnjjj, BrynCooke and abernix and removed request for a team July 6, 2022 14:52
@github-actions

This comment has been minimized.

@garypen garypen requested review from o0Ignition0o and Geal July 6, 2022 14:52
apollo-router/src/plugins/rhai.rs Show resolved Hide resolved
apollo-router/src/plugins/rhai.rs Outdated Show resolved Hide resolved
@garypen garypen marked this pull request as ready for review July 13, 2022 14:18
@garypen garypen requested a review from StephenBarlow as a code owner July 13, 2022 14:18
describing the change in status of the rhai plugin and how to migrate
your configuration.
@garypen garypen merged commit 671c413 into main Jul 14, 2022
@garypen garypen deleted the garypen/stabilise-rhai branch July 14, 2022 10:10
garypen added a commit that referenced this pull request Jul 15, 2022
The tests weren't updated correctly during the rhai promotion from
experimentation. Fixing them now.
garypen added a commit that referenced this pull request Jul 15, 2022
fix rhai examples following rhai stabilisation PR (#1365)

The examples weren't updated correctly during the rhai promotion from
experimentation. Fixing them now.
@o0Ignition0o o0Ignition0o mentioned this pull request Jul 18, 2022
o0Ignition0o added a commit that referenced this pull request Jul 18, 2022
# [0.12.0] - 2022-08-18

## ❗ BREAKING ❗

### Move `experimental.rhai` out of `experimental` [PR #1365](#1365)

You will need to update your YAML configuration file to use the correct name for `rhai` plugin.

```diff
- plugins:
-   experimental.rhai:
-     filename: /path/to/myfile.rhai
+ rhai:
+   scripts: /path/to/directory/containing/all/my/rhai/scripts (./scripts by default)
+   main: <name of main script to execute> (main.rhai by default)
```

You can now modularise your rhai code. Rather than specifying a path to a filename containing your rhai code, the rhai plugin will now attempt to execute the script specified via `main`. If modules are imported, the rhai plugin will search for those modules in the `scripts` directory. for more details about how rhai makes use of modules, look at [the rhai documentation](https://rhai.rs/book/ref/modules/import.html).

The simplest migration will be to set `scripts` to the directory containing your `myfile.rhai` and to rename your `myfile.rhai` to `main.rhai`.

By [@garypen](https://github.com/garypen) in #1365

## 🐛 Fixes

### The opentelemetry-otlp crate needs a http-client feature [PR #1392](#1392)

The opentelemetry-otlp crate only checks at runtime if a HTTP client was added through
cargo features. We now use reqwest for that.

By [@Geal](https://github.com/geal) in #1392

### Expose the custom endpoints from RouterServiceFactory ([PR #1402](#1402))

Plugin HTTP endpoints registration was broken during the Tower refactoring. We now make sure that the list
of endpoints is generated from the `RouterServiceFactory` instance.

By [@Geal](https://github.com/geal) in #1402

## 🛠 Maintenance

### Dependency updates [PR #1389](#1389) [PR #1394](#1394) [PR #1395](#1395)

Dependency updates were blocked for some time due to incompatibilities:

- #1389: the router-bridge crate needed a new version of `deno_core` in its workspace that would not fix the version of `once_cell`. Now that it is done we can update `once_cell` in the router
- #1395: `clap` at version 3.2 changed the way values are extracted from matched arguments, which resulted in panics. This is now fixed and we can update `clap` in the router and related crates
- #1394: broader dependency updates now that everything is locked
- #1410: revert tracing update that caused two telemetry tests to fail (the router binary is not affected)

By [@Geal](https://github.com/Geal) in #1389 #1394 #1395 and [@o0Ignition0o](https://github.com/o0Ignition0o) in #1410
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants