I have a class User
with a property Id
. I also have some binary data in my file system for each instance of that class - the names of the files are the Id
of the object.
I've written a type extension to provide a field with the text content from the files:
[ExtendObjectType<User>]
public sealed class UserExtensions
{
private readonly IStorageReaderWriter readerWriter;
public UserExtensions(IStorageReaderWriter readerWriter)
=> this.readerWriter = readerWriter;
public async Task<string?> GetData([Parent] User user, CancellationToken cancellationToken)
=> await this.readerWriter.Read($"{user.Id:D}.json", cancellationToken).ConfigureAwait(false);
}
This works fine as long as I query the user with its Id
:
query {
users{
id
data
}
}
In this case, GetData
gets a User
instance with the Id
already set and can look up the file content by this Id
.
As soon as I omit the Id
in the query, it's not populated in the User for the GetData
method. So, this query returns a list with data = null
, because for each record User.Id
is null.
query {
users{
data
}
}
Is there a way to tell hot chocolate that I need the Id
property, regardless whether the client query requests it or not?