Support for initializing classes/records via constructor
See original GitHub issueI’m building my own sorting argument:
#nullable enable
class SortArgument
{
// CS8618 warning: Non-nullable property 'Path' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Path { get; set; }
// CS8618 warning: Non-nullable property 'Direction' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Direction { get; set; }
}
...
schema.AddType<SortArgument>("sortArgument", null).AddAllFields();
schema.UpdateQuery(queryType =>
{
queryType.ReplaceField(
"users",
new
{
sort = (SortArgument[]?)null
},
(db, args) => ...,
"..."
);
});
It’d be cool if EntityGraphQL
supported initializing SortArgument
by calling its constructor, so that I could easily do additional validation and ensure that Path
and Direction
are non-null:
class SortArgument
{
public string Path { get; }
public string Direction { get; }
public SortArgument(string path = null, string direction = null)
{
Path = path ?? throw new ArgumentNullException(nameof(path));
if(direction == null || direction.Length < 10)
throw new ArgumentException("Direction must be at least 10 letters long", nameof(direction));
Direction = direction;
}
}
Just a suggestion 🙂 Thanks for the great library! ^_^
Issue Analytics
- State:
- Created a year ago
- Comments:9
Top Results From Across the Web
Custom Constructor in Java Records
Learn how to create custom constructors for Java Records and the benefits they provide.
Read more >Initialize class fields in constructor or at declaration?
I've been programming in C# and Java recently and I am curious where the best place is to initialize my class fields. ......
Read more >3.4 Account Class: Initializing Objects with Constructors
When you declare a class, you can provide your own constructor to specify custom initialization for objects of your class. For example, you ......
Read more >2.2. Creating and Initializing Objects: Constructors
There can be more than one constructor defined in a class. This is called overloading the constructor. There is usually a constructor that...
Read more >Explicit initialization with constructors (C++ only)
Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class ...
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 FreeTop 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
Top GitHub Comments
In an example like that it makes sense. The GraphQL spec does not support that. So other tools etc may complain.
The spec states in simple terms
Small thing in your example.
You are using
SortArgument
as an input type. You will need to use