Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default query arguments in Strawberry Shake #4594

Closed
retterbot opened this issue Dec 23, 2021 · 2 comments
Closed

Support default query arguments in Strawberry Shake #4594

retterbot opened this issue Dec 23, 2021 · 2 comments
Labels
⌛ stale Nothing happened with this issue in quite a while 🌶️ strawberry shake

Comments

@retterbot
Copy link
Contributor

Is your feature request related to a problem?

Sort and Filter inputs generated by Hot Chocolate are nullable by default, so for example the following C# code

[UseSorting(typeof(AmenitySortInputType))]
[UseFiltering(typeof(AmenityFilterInputType))]
public IQueryable<AmenityEntity> GetAmenities( ...

Will generate the following graphql schema

type Query {
  amenities(order: [AmenitySortInput!] where: AmenityFilterInput): [Amenity!]!
}

I can then define the following query in Strawberry Shake

query GetAmenities(
    $order: [AmenitySortInput!] = null
    $where: AmenityFilterInput = null) {
  amenities(
    order: $order
    where: $where) {
    id
    name
  }
}

Currently it ignores the argument defaults in the generated code and generates

Task<IOperationResult<IGetAmenityCategoriesResult>> IGetAmenityCategoriesQuery.ExecuteAsync(
   IReadOnlyList<AmenityCategorySortInput>? order,
   AmenityCategoryFilterInput? where,
   CancellationToken cancellationToken = default)

The solution you'd like

I would like Strawberry Shake to generate the following code

Task<IOperationResult<IGetAmenityCategoriesResult>> IGetAmenityCategoriesQuery.ExecuteAsync(
   IReadOnlyList<AmenityCategorySortInput>? order = default,
   AmenityCategoryFilterInput? where = default,
   CancellationToken cancellationToken = default)

In the slack discussion it was noted by Pascal that

query GetAmenities(
    $order: [AmenitySortInput!] = null
    $where: AmenityFilterInput! ) {

Is also valid graphql, but verbatim transpiling to C# would generate

Task<IOperationResult<IGetAmenityCategoriesResult>> IGetAmenityCategoriesQuery.ExecuteAsync(
   IReadOnlyList<AmenityCategorySortInput>? order = default,
   AmenityCategoryFilterInput? where,
   CancellationToken cancellationToken = default)

Which is not valid C#. Reordering the parameters would be a breaking change.

I think the problem described above is the primary use case for this particular feature, however scalar defaults are also possible.

My preference for a first cut of this feature would be to:

  • Only support default arguments for reference types.
  • Only support default arguments when verbatim transpilation generates valid C#, i.e. not ($x = null, $y).

When default arguments are detected that do not meet the above criteria a warning should be emitted. There should be a setting in .graphqlrc.json to disable these warnings, by default these warnings should be emitted.

Product

Strawberry Shake

@stale
Copy link

stale bot commented May 4, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the ⌛ stale Nothing happened with this issue in quite a while label May 4, 2022
@stale stale bot closed this as completed May 11, 2022
@kyurkchyan
Copy link

I wonder if there is a plan to support default arguments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⌛ stale Nothing happened with this issue in quite a while 🌶️ strawberry shake
Projects
None yet
Development

No branches or pull requests

4 participants