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

Create custom attribute to modify members behaviour within type queried by UseSelection middleware #1533

Closed
ghost opened this issue Mar 8, 2020 · 8 comments
Assignees
Milestone

Comments

@ghost
Copy link

ghost commented Mar 8, 2020

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:

class Query 
{
   [UseSelection]
   public IQueryable<Post> GetPosts([Service] PostDbContext db) => db.Posts;
}

class Post 
{
   public int Id { get; set; }
   public int Title { get; set; }
   public IEnumerable<string> 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:

    [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.

    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.

@PascalSenn PascalSenn self-assigned this Mar 9, 2020
@PascalSenn PascalSenn added this to the HC-10.4.0 milestone Mar 9, 2020
@PascalSenn
Copy link
Member

PascalSenn commented Mar 9, 2020

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

@michaelstaib
Copy link
Member

@PascalSenn so what is the bug here?

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

@PascalSenn
Copy link
Member

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

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

@PascalSenn PascalSenn reopened this Mar 9, 2020
@michaelstaib
Copy link
Member

Ah I see. OK

PascalSenn added a commit that referenced this issue Mar 11, 2020
@PascalSenn
Copy link
Member

PascalSenn commented Mar 11, 2020

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

@TheJayMann
Copy link

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?

@PascalSenn
Copy link
Member

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

@ghost
Copy link
Author

ghost commented Mar 11, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants