Skip to content

Allow using attributes on queries/mutations #697

Closed
@mbenoukaiss

Description

@mbenoukaiss

Is your feature request related to a problem? Please describe.
I am trying to use proc macro attributes to automatically generate code to handle authorization and rate-limits for the different queries and mutations. Problem is, the function gets transformed into an async block and attributes get removed in the process.

The currently generated code inside the match arm matching the query/mutation name looks like this :

let f = async move {
    let res: /* my function return type */ = async move { 
        /* get the context and function arguments */ 
        /* the code in my function */ 
    }.await;

    /* some code generated by juniper */
};

FutureExt::boxed(f)

Describe the solution you'd like
I believe it is possible to get the work done by keeping the function (and therefore its attributes):

let f = async move {
    let res: FieldResult<Account> = {
        async fn inner(/* original function arguments, including the context */) -> /* my function return type */ {
            /* the code in my function */
        }

        /* get context and arguments */
        inner(/* context and arguments */).await
    };

    /* some code generated by juniper */
};

FutureExt::boxed(f)

Potential problems

  • Would it add any overhead? To me, it doesn't seem like it would, but I'm not an async expert
  • I've seen code that directly uses the executor variable to get the context. This type of code would get broken by this change since the executor will be "hidden" behind the function. But it sounds like a hacky and unnecessary way to get the context anyway.

I can make a pull request with the changes if this is ok since I like doing stuff with proc macros.

Metadata

Metadata

Assignees

Labels

enhancementImprovement of existing features or bugfix

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions