apollo-rs icon indicating copy to clipboard operation
apollo-rs copied to clipboard

Converting directive arguments to a struct

Open Geal opened this issue 1 year ago • 2 comments

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());

Geal avatar Nov 02 '23 09:11 Geal