1
I created 2 different methods using GraphQL HotChocolate which return the same object type. I need to hide different fields for each method. However, it does not happen when I look at the schema definition. This is an example for code first approach:
public class Query
{
private readonly CharacterRepository _repository;
public Query(CharacterRepository repository)
{
_repository = repository
?? throw new ArgumentNullException(nameof(repository));
}
public Human GetHuman(string id)
{
return _repository.GetHuman(id);
}
public Human GetHumanClone(string id)
{
return _repository.GetHuman(id);
}
}
The object Human is just a simple class with properties
public class Human
: ICharacter
{
public string Id { get; set; }
public string Name { get; set; }
public IReadOnlyList<string> Friends { get; set; }
public IReadOnlyList<Episode> AppearsIn { get; set; }
public string HomePlanet { get; set; }
public double Height { get; } = 1.72d;
}
The graphql type defined for Human and HumanClone. The HomePlanet property should be ignored for HumanType and Name property should be ignored for HumanCloneType.
public class HumanType
: ObjectType<Human>
{
protected override void Configure(IObjectTypeDescriptor<Human> descriptor)
{
descriptor.Interface<CharacterType>();
descriptor.Field(t => t.Id)
.Type<NonNullType<IdType>>();
descriptor.Ignore(f => f.HomePlanet);
descriptor.Field(t => t.AppearsIn)
.Type<ListType<EpisodeType>>();
descriptor.Field<SharedResolvers>(r => r.GetCharacter(default, default))
.UsePaging<CharacterType>()
.Name("friends");
descriptor.Field<SharedResolvers>(t => t.GetHeight(default, default))
.Type<FloatType>()
.Argument("unit", a => a.Type<EnumType<Unit>>())
.Name("height");
}
}
public class HumanCloneType
: ObjectType<Human>
{
protected override void Configure(IObjectTypeDescriptor<Human> descriptor)
{
descriptor.Interface<CharacterType>();
descriptor.Field(t => t.Id)
.Type<NonNullType<IdType>>();
descriptor.Ignore(f => f.Name);
descriptor.Field(t => t.AppearsIn)
.Type<ListType<EpisodeType>>();
descriptor.Field<SharedResolvers>(r => r.GetCharacter(default, default))
.UsePaging<CharacterType>()
.Name("friends");
descriptor.Field<SharedResolvers>(t => t.GetHeight(default, default))
.Type<FloatType>()
.Argument("unit", a => a.Type<EnumType<Unit>>())
.Name("height");
}
}
As both graphql types return the same object, these types can not be registered globally.
It seems it should be possible to return the same object type from different graphql queries according to https://chillicream.com/docs/hotchocolate/v11/defining-a-schema/object-types but I cannot figure out how to do it.
Any help would be greatly appreciated. Thank you.
1 Answer
Reset to default
0
Instead of using a common type, I recommend to create two different Payload objects like "GetHumanPayload" & "GetHumanClonePayload" inheriting the common property from the parent class and add the require fields in the respective payload classes to manage the same.
2
-
Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare meta.stackexchange.com/questions/214173/… ). For example like "If your problem is … then the solution is to …. because …. ."
– Yunnosch1 hour ago
-
@starball I noticed your edit when I had this one just before posting… Amazingly similar. I added my approach to also remove the "?"-free non-question. No OP only needs to add some explanation about why this is supposed to help and how it achieves that advantage.
– Yunnosch1 hour ago
Not the answer you're looking for? Browse other questions tagged
or ask your own question.
or ask your own question.
|