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.

Support for initializing classes/records via constructor

See original GitHub issue

I’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 =>
            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:open
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

lukemurraycommented, Sep 2, 2022

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

  • each type needs a unique name
  • Types used as arguments are Input Types
  • Input types are different as they have more restrictions - no fields with arguments etc.
lukemurraycommented, Aug 26, 2022

Small thing in your example.

schema.AddType<SortArgument>("sortArgument", null).AddAllFields();

You are using SortArgument as an input type. You will need to use

schema.AddInputType<SortArgument>("sortArgument", null).AddAllFields();
Read more comments on GitHub >

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

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 Post

No results found

github_iconTop Related Hashnode Post

No results found