Codegen: Possibility to combine method arguments into a single object
See original GitHub issueFor example, the C# code below
public class FooQuery
{
public string Bar { get; set; }
public string Baz { get; set; }
public string FooBar { get; set; }
public string FooBaz { get; set; }
public int? FooBarBaz { get; set; }
}
public class FooController
{
[HttpGet, Route]
public ICollection<Foo> ReadAll([FromUri]FooQuery query = null)
{
// ...
}
}
generates the following TypeScript method:
export class FooApi {
readAll(bar: string | null, baz: string | null, fooBar: string | null, fooBaz: string | null, fooBarBaz: number | null): Observable<Foo | null> {
// ...
}
}
Clearly, the generated method is cumbersome and error-prone to use and code using it will break if we add another property to FooQuery
.
My proposal is to generate a FooApiReadAllQuery
class when there are >1 arguments and use that class as an argument to the method:
export class FooApiReadAllArgs {
bar: string | null;
baz: string | null;
fooBar: string | null;
fooBaz: string | null;
fooBarBaz: number | null;
}
export class FooApi {
readAll(query: FooApiReadAllArgs): Observable<Foo | null> {
// ...
}
}
This will:
- Make adding new arguments to the API controller backward-compatible with existing code
- Make such methods easier and safer to use
Is such an implementation welcome as a pull request in this repo? It seems that it’s relatively easy to this to the templates and configuration.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:41
- Comments:37 (9 by maintainers)
Top Results From Across the Web
typescript-operations – GraphQL Code Generator
Generate TypeScript operations from GraphQL queries. This plugin is based on the `typescript` plugin, but it generates operations instead of ...
Read more >Integrating GraphQL Code Generator in your frontend ...
In this article we'll try to explain and demonstrate common patterns for frontend development with GraphQL and GraphQL Code Generator.
Read more >swagger-codegen contains a template-driven engine ...
swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI ...
Read more >Transform Parameters refactoring - ReSharper
For non-void methods with out parameters, combine out parameters with return type in a tuple object or in a newly created class.
Read more >Model Configuration Parameters: Code Generation Interface
The Code Generation > Interface category includes parameters for ... Specify whether to combine global block signals and global state data into one...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This would really be a great feature when implementing collection endpoints with lots of filter parameters.
Any updates on this feature? It’s really painful to manage 20+ parameters.