Add operation arguments to inject extra params into generated operations#90
Merged
Merged
Conversation
Some operations need to forward something to the client that is not a GraphQL
variable: an actor, a tenant, a correlation or idempotency id. Until now the
generated execute()/executeOrThrow() signatures were derived solely from the
operation's variables, so there was no way to thread such transport-level
context through the generated code.
withOperationArgument() lets you declare an extra, typed parameter that is
prepended to the generated execute()/executeOrThrow() methods and forwarded to
the client's graphql() call as a named argument (e.g. actor: $actor):
Config::create(...)
->withOperationArgument(
name: 'actor',
type: Type::object(Actor::class),
directive: 'requiresActor',
);
Targeting is flexible:
- With a directive, the parameter is opt-in: only operations tagged with that
marker (e.g. mutation Foo @requiresActor) receive it. Without one, it applies
to every operation.
- operations restricts the argument to specific operation types; it defaults to
an empty list, meaning any type.
- Call it multiple times to inject more than one argument.
The marker directive is a code-generation concern, not part of the server's
schema, so it is handled end to end: auto-registered on the schema for the
configured operation types (so documents validate and KnownDirectives rejects
misuse such as putting it on a field), and stripped from the operation before
it is sent to the server. Running --update-schema writes these directive
definitions into the schema file.
Components:
- OperationArgument value object + Config::withOperationArgument()
- OperationArgumentDirectiveSchemaExtender registers the marker directive(s)
- OperationArgumentDirectiveRemover strips them before the query is printed
- Planner resolves the applicable arguments per operation; OperationClassGenerator
prepends the parameters and forwards them as named arguments to graphql()
Covered by the tests/OperationArgument fixture (opt-in mutation, opt-in query,
untouched query) plus a behavioural test asserting the actor reaches the client,
and documented in the README.
d6a45fc to
ca4fb3b
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some operations need to forward something to the client that is not a GraphQL variable: an actor, a tenant, a correlation or idempotency id. Until now the generated execute()/executeOrThrow() signatures were derived solely from the operation's variables, so there was no way to thread such transport-level context through the generated code.
withOperationArgument() lets you declare an extra, typed parameter that is prepended to the generated execute()/executeOrThrow() methods and forwarded to the client's graphql() call as a named argument (e.g. actor: $actor):
Targeting is flexible:
The marker directive is a code-generation concern, not part of the server's schema, so it is handled end to end: auto-registered on the schema for the configured operation types (so documents validate and KnownDirectives rejects misuse such as putting it on a field), and stripped from the operation before it is sent to the server. Running --update-schema writes these directive definitions into the schema file.
Components:
Covered by the tests/OperationArgument fixture (opt-in mutation, opt-in query, untouched query) plus a behavioural test asserting the actor reaches the client, and documented in the README.