|
| 1 | +--- |
| 2 | +title: Interpreter |
| 3 | +layout: guide |
| 4 | +doc_stub: false |
| 5 | +search: true |
| 6 | +section: Queries |
| 7 | +desc: A New Runtime for GraphQL-Ruby |
| 8 | +experimental: true |
| 9 | +index: 11 |
| 10 | +--- |
| 11 | + |
| 12 | +GraphQL-Ruby 1.9.0 includes a new runtime module which you may use for your schema. Eventually, it will become the default. |
| 13 | + |
| 14 | +It's called `GraphQL::Execute::Interpreter` and you can hook it up with `use ...` in your schema class: |
| 15 | + |
| 16 | +```ruby |
| 17 | +class MySchema < GraphQL::Schema |
| 18 | + use GraphQL::Execute::Interpreter |
| 19 | +end |
| 20 | +``` |
| 21 | + |
| 22 | +Read on to learn more! |
| 23 | + |
| 24 | +## Rationale |
| 25 | + |
| 26 | +The new runtime was added to address a few specific concerns: |
| 27 | + |
| 28 | +- __Validation Performance__: The previous runtime depended on a preparation step (`GraphQL::InternalRepresentation::Rewrite`) which could be very slow in some cases. In many cases, the overhead of that step provided no value. |
| 29 | +- __Runtime Performance__: For very large results, the previous runtime was slow because it allocated a new `ctx` object for every field, even very simple fields that didn't need any special tracking. |
| 30 | +- __Extensibility__: Although the GraphQL specification supports custom directives, GraphQL-Ruby didn't have a good way to build them. |
| 31 | + |
| 32 | +## Installation |
| 33 | + |
| 34 | +You can opt in to the interpreter in your schema class: |
| 35 | + |
| 36 | +```ruby |
| 37 | +class MySchema < GraphQL::Schema |
| 38 | + use GraphQL::Execution::Interpreter |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +If you have a subscription root type, it will also need an update. Extend this new module: |
| 43 | + |
| 44 | +```ruby |
| 45 | +class Types::Subscription < Types::BaseObject |
| 46 | + # Extend this module to support subscription root fields with Interpreter |
| 47 | + extend GraphQL::Subscriptions::SubscriptionRoot |
| 48 | +end |
| 49 | +``` |
| 50 | + |
| 51 | +Some Relay configurations must be updated too. For example: |
| 52 | + |
| 53 | +```diff |
| 54 | +- field :node, field: GraphQL::Relay::Node.field |
| 55 | ++ add_field(GraphQL::Types::Relay::NodeField) |
| 56 | +``` |
| 57 | + |
| 58 | +(Alternatively, consider implementing `Query.node` in your own app, using `NodeField` as inspiration.) |
| 59 | + |
| 60 | +## Compatibility |
| 61 | + |
| 62 | +The new runtime works with class-based schemas only. Several features are no longer supported: |
| 63 | + |
| 64 | +- Proc-dependent field features: |
| 65 | + |
| 66 | + - Field Instrumentation |
| 67 | + - Middleware |
| 68 | + - Resolve procs |
| 69 | + |
| 70 | + All these depend on the memory- and time-hungry per-field `ctx` object. To improve performance, only method-based resolves are supported. If need something from `ctx`, you can get it with the `extras: [...]` configuration option. To wrap resolve behaviors, try {% internal_link "Field Extensions", "/type_definitions/field_extensions" %}. |
| 71 | + |
| 72 | +- Query analyzers and `irep_node`s |
| 73 | + |
| 74 | + These depend on the now-removed `Rewrite` step, which wasted a lot of time making often-unneeded preparation. Most of the attributes you might need from an `irep_node` are available with `extras: [...]`. Query analyzers can be refactored to be static checks (custom validation rules) or dynamic checks, made at runtime. The built-in analyzers have been refactored to run as validators. |
| 75 | + |
| 76 | + `irep_node`-based lookahead is not supported. Stay tuned for a replacement. |
| 77 | + |
| 78 | +- `rescue_from` |
| 79 | + |
| 80 | + This was built on middleware, which is not supported anymore. Stay tuned for a replacement. |
| 81 | + |
| 82 | +- `.graphql_definition` and `def to_graphql` |
| 83 | + |
| 84 | + The interpreter uses class-based schema definitions only, and never converts them to legacy GraphQL definition objects. Any custom definitions to GraphQL objects should be re-implemented on custom base classes. |
| 85 | + |
| 86 | +Maybe this section should have been called _incompatibility_ 🤔. |
| 87 | + |
| 88 | +## Extending the Runtime |
| 89 | + |
| 90 | +🚧 👷🚧 |
| 91 | + |
| 92 | +The internals aren't clean enough to build on yet. Stay tuned. |
| 93 | + |
| 94 | +## Implementation Notes |
| 95 | + |
| 96 | +Instead of a tree of `irep_nodes`, the interpreter consumes the AST directly. This removes a complicated concept from GraphQL-Ruby (`irep_node`s) and simplifies the query lifecycle. The main difference relates to how fragment spreads are resolved. In the previous runtime, the possible combinations of fields for a given object were calculated ahead of time, then some of those combinations were used during runtime, but many of them may not have been. In the new runtime, no precalculation is made; instead each object is checked against each fragment at runtime. |
| 97 | + |
| 98 | +Instead of creating a `GraphQL::Query::Context::FieldResolutionContext` for _every_ field in the response, the interpreter uses long-lived, mutable objects for execution bookkeeping. This is more complicated to manage, since the changes to those objects can be hard to predict, but it's worth it for the performance gain. When needed, those bookkeeping objects can be "forked", so that two parts of an operation can be resolved independently. |
| 99 | + |
| 100 | +Instead of calling `.to_graphql` internally to convert class-based definitions to `.define`-based definitions, the interpreter operates on class-based definitions directly. This simplifies the workflow for creating custom configurations and using them at runtime. |
0 commit comments