type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

Does emitting @Authorization roles into property/method description makes sense?

Open tomasstrejcek opened this issue 5 years ago • 5 comments

Before I send pull request I want to make sure I am thinking about this the right way. I patched the compiled metadata-generator code to test this idea. For real code I would presume some configuration, customized text, filtered (internal roles) not showing in docs etc.

        definitions.forEach(def => {
            const fields = this.fields.filter(field => field.target === def.target);
            fields.forEach(field => {
                ....
                if(field.roles) {
                  field.description = (field.description ? field.description : '') + `
user can authorize via roles: ${Array.isArray(field.roles) ? field.roles.join(): field.roles}
                `
                }
            });
            def.fields = fields;
        });
    }```

tomasstrejcek avatar Jan 08 '20 14:01 tomasstrejcek

I would rather give users an universal mechanism to reflect the TypeGraphQL metadata in description, rather than coupling with authorization or complexity directly. We can then expose a few built-in functions that will do the transformation so you can use them as simple plug in or write your custom one if you need more control over that.

MichalLytek avatar Jan 08 '20 16:01 MichalLytek

Maybe description can accept a function with two metadata arguments, like:

description?: string | ((objectMetadata, metadata: MetadataStorage) => string | undefined);

objectMetadata would be the metadata for the specific query/field/interface/etc., and metadata would be the global metadata (could be added later).

And then you'd need a way to configure a global default, perhaps:

defaults: {
    description?: string | ((objectMetadata, metadata: MetadataStorage) => string | undefined)
}

... in SchemaGeneratorOptions.

glen-84 avatar Jan 10 '20 13:01 glen-84

What is an use case for tuning description based on metadata for a single block (type, field)?

I was thinking about something like:

await buildSchema({
  transformDescription: (description, metadata) => 
    description + "\nAccess only for: " + metadata.roles.join(", ");
  },
});

So you can customize the message while not polluting decorators with additional noise.

MichalLytek avatar Jan 11 '20 12:01 MichalLytek

What is an use case for tuning description based on metadata for a single block (type, field)?

I didn't have a specific use case in mind, I was just trying to make it as flexible as possible (which is quite possibly unnecessary).

I like your suggestion of passing in the current description to a global transformation function.

glen-84 avatar Jan 12 '20 15:01 glen-84

This would be useful to me so that I can add info about Roles required to get access to a query/mutation. So that users can know which Role Access they need to request.

rohit-gohri avatar Apr 17 '21 08:04 rohit-gohri