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

Converting directive arguments to a struct #717

Open
Geal opened this issue Nov 2, 2023 · 2 comments
Open

Converting directive arguments to a struct #717

Geal opened this issue Nov 2, 2023 · 2 comments
Labels
apollo-compiler issues/PRs pertaining to semantic analysis & validation

Comments

@Geal
Copy link
Contributor

Geal commented Nov 2, 2023

I often write code like this:

let ty: ExtendedType = ...;

let arg: Option<&str> = ty.directives.get("directive_name")
  .and_then(|directive| directive
    .argument_by_name("arg")
    .and_then(|arg| arg.as_str()));

It gets tedious when doing that for multiple directives with multiple arguments. Could we have a more automatic way of extracting directives and arguments?

As an example, if I had directive @dir(arg1: String! arg2: Number), could I do this:

struct Dir {
  arg1: String,
  arg2: Option<i64>,
}

let ty: ExtendedType = ...;

let args: Option<Dir> = ty.directives.get("directive_name").and_then(|dir| Dir::try_from(dir).ok());
@SimonSapin
Copy link
Contributor

#645 contains code for "input value coercion" (as part of execution) that returns a JSON map. "Deserializing" that map to a dedicated Rust struct definitely sounds interesting! It may be doable with derive(serde::Deserialize) a custom deserializer, but if we also want to type check the Rust struct definition against the GraphQL directive definition we may want a custom proc macro.

@SimonSapin
Copy link
Contributor

Also note that input value coercion takes a map of variables values as a parameter, to handle @dir(arg: $var)

@goto-bus-stop goto-bus-stop added the apollo-compiler issues/PRs pertaining to semantic analysis & validation label Nov 3, 2023
@goto-bus-stop goto-bus-stop changed the title Converting directives to a struct Converting directive arguments to a struct Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apollo-compiler issues/PRs pertaining to semantic analysis & validation
Projects
None yet
Development

No branches or pull requests

3 participants