question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Codegen: Possibility to combine method arguments into a single object

See original GitHub issue

For 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:open
  • Created 6 years ago
  • Reactions:41
  • Comments:37 (9 by maintainers)

github_iconTop GitHub Comments

19reactions
goldsamcommented, Jun 7, 2018

This would really be a great feature when implementing collection endpoints with lots of filter parameters.

16reactions
feafarotcommented, Apr 26, 2018

Any updates on this feature? It’s really painful to manage 20+ parameters.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found