question-mark
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.

[Hydration] Is there any helper that collects the requested root fields and returns them as an array?

See original GitHub issue

I have a resolver that fetch a list of authors. Author’s entity have many columns in database such as id, name, role, isActive, createdAt, updatedAt etc. However my GraphQL API exposes only three fields: id, name, role.

descriptor.Field(t => t.Authors)
    .Type<NonNullType<ListType<AuthorType>>>()
    .Resolver(ctx =>
    {
        <repository fetching stuff>
    }
}

To optimize the speed of the resolver, I want to request from the repository only the necessary columns, and not all in a row. Actually, the question arose: How can I get a list of requested fields from the ctx?

I know that in context there is a property ctx.FieldSelection.SelectionSet, and I can just select each item’s name with Linq. But fragments can also act as fields. And then you need to read the whole scheme in search of this fragment and get a list of fields there.

Does the HotChocolate have any helper to solve this problem?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
michaelstaibcommented, Aug 21, 2019

@acelot your example will work when ctx.Field.Type is an object type. If you have a union or an interface you can ask for all possible types ctx.Schema.GetPossibleTypes(cox.Field.Type.NamedType()).

With that you can ask for the fields of each case.

If you want to walk the graph further you can pass in any selectionSet to CollectFields and the type context for that selection set.

If you want to generalize your solution you can put that in a middleware and use it on multiple fields…

public class ArticleType : ObjectType<Article>
{
    protected override void Configure(IObjectTypeDescriptor<Article> descriptor)
    {
        <...>

        descriptor.Field(t => t.Authors)
            .Type<NonNullType<ListType<AuthorType>>>()
            .UseDynamicSQL();

        <...>
    }
}

https://hotchocolate.io/docs/middleware#field-middleware

1reaction
michaelstaibcommented, Aug 20, 2019

So the easiest way to do that is with our IResolverContext.CollectFields fields. This will pull in the fields that have to be resolved in the next level. There are two overloads that allow you to walk the graph and look at all the levels in all possible combinations. This will let you focus on what fields to fetch. CollectFields will compute @skip and @include and also resolve all fragments.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Hydration] Is there any helper that collects the requested ...
I have a resolver that fetch a list of authors. Author's entity have many columns in database such as id, name, role, isActive,...
Read more >
Advanced topics on caching in Apollo Client
This read function uses the toReference helper utility to generate and return a cache reference for a Book object, based on its __typename...
Read more >
Matching a request by size of array on root level in body
1 Answer. Instead of traversing into the root $ and using the element there @ , I simply use the root directly in...
Read more >
All 76 Aranara Locations - In Depth Follow Along Route
Hello guys, in this video i'll show you all Aranara's locations in Sumeru. You need to help Aranara if you want open all...
Read more >
Zapier Platform CLI docs | Zapier Platform UI Documentation
Zapier Platform CLI docs. Zapier is a platform for creating integrations and workflows. This CLI is your gateway to creating custom applications on...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found