Hotchocolate: Create custom attribute to modify members behaviour within type queried by UseSelection middleware

Created on 8 Mar 2020  路  8Comments  路  Source: ChilliCream/hotchocolate

I've been trying out feature made with #1446 and i've noticed couple of things i think should behave a bit different.

Let's have following context:
````csharp
class Query
{
[UseSelection]
public IQueryable GetPosts([Service] PostDbContext db) => db.Posts;
}

class Post
{
public int Id { get; set; }
public int Title { get; set; }
public IEnumerable GetTags() => new string[] { "hot-chocolate", "graph-ql" };
}
````

  1. When i query just id, title or both of them, i get what i need, and query is optimized properly. But when i include tags as well, query throws an exception:
    UseSelection is in a invalid state. Field tags should never have been visited!.
    Now, i understand that method that has it's own resolver (whatever it is) can't be part of the query, but i think this can be handled in a better way, not only for the purpose of having methods here, but excluding any field we don't want to be a part of the actual IQueryable expression.

    My idea is to have a universal attribute here, called SelectionField or something similar. It could have multiple purposes, but one of them could be following:
    csharp [SelectionField(Included = false)] public IEnumerable<string> GetTags() => new string[] { "hot-chocolate", "graph-ql" };
    This way we could ommit specific members we don't want to be a part of the query. Any field we don't annotate with this attribute can be considered as included, and equal to [SelectionField(Included = true)].

  2. I mentioned the SelectionField attribute in the previous point, and i think it could be used for something else as well, and that is specifying mandatory fields that should get included in dynamic expression, whether or not we requested it with GraphQL query. It won't get projected to us, of course, but it will be queried in the background.

    ````csharp
    class Post
    {
    [SelectionField(Mandatory = true)]
    public int Id { get; set; }
    public int Title { get; set; }

    public string GetSomeDataFrom3rdPartyService() => SomeClass.GetDataById(Id);
    }
    ````

    As you can see, it is sometimes useful to know that some field would always have data (Id in this case), because we may need it for some calculations, 3rd party service calls and similar.

bug

Most helpful comment

I think this was incorrectly tagged. It appears that 10.4.0-preview.18 tag is on the same commit as 11.0.0-preview.109. Also, the CI for the 10.4.0-preview.18 is failing, I don't see any published nuget packages for it, and I also cannot find any code related to the selection middleware in the 10.4.0-preview.18 tag. Meanwhile, I do see the changes in the version_10_0_0_master branch. Perhaps the wrong branch was tagged for the release?

All 8 comments

Thank you for reporting this:

  1. This is a bug:
UseSelection is in a invalid state. Field tags should never have been visited!.
  1. [SelectionField(Included = true)]
    We had something like this under development but kicked it out because we want to align the API with 11.
    We are currently designing meta data annotations for 11.
    cc @michaelstaib

@PascalSenn so what is the bug here?

Regarding the metadata ... Yes, something like that is coming in version 11 with the new execution engine.

@michaelstaib
This is a method and should therefor never be visited

 public IEnumerable<string> GetTags() => new string[] { "hot-chocolate", "graph-ql" };

Ah I see. OK

@rankdalibor Pushed a few fixes. Can you check with the following version? https://github.com/ChilliCream/hotchocolate/releases/tag/10.4.0-preview.19

I think this was incorrectly tagged. It appears that 10.4.0-preview.18 tag is on the same commit as 11.0.0-preview.109. Also, the CI for the 10.4.0-preview.18 is failing, I don't see any published nuget packages for it, and I also cannot find any code related to the selection middleware in the 10.4.0-preview.18 tag. Meanwhile, I do see the changes in the version_10_0_0_master branch. Perhaps the wrong branch was tagged for the release?

@TheJayMann You are right, there went something wrong:
https://github.com/ChilliCream/hotchocolate/releases/tag/10.4.0-preview.19

I can confirm that an error related to a method inside selection type is now fixed 馃憤

Was this page helpful?
0 / 5 - 0 ratings