public HumanObject(IHumanRepository humanRepository) { this.Name = "Human"; this.Description = "A humanoid creature from the Star Wars universe."; // this.AuthorizeWith(AuthorizationPolicyName.Admin); // To require authorization for all fields in this type. this.Field(x => x.Id, type: typeof(NonNullGraphType <IdGraphType>)) .Description("The unique identifier of the human."); this.Field(x => x.Name) .Description("The name of the human."); this.Field(x => x.DateOfBirth) .AuthorizeWith(AuthorizationPolicyName.Admin) // Require authorization to access the date of birth field. .Description("The humans date of birth."); this.Field(x => x.HomePlanet, nullable: true) .Description("The home planet of the human."); this.Field(x => x.AppearsIn, type: typeof(ListGraphType <EpisodeEnumeration>)) .Description("Which movie they appear in."); this.FieldAsync <ListGraphType <CharacterInterface>, List <Character> >( nameof(Human.Friends), "The friends of the character, or an empty list if they have none.", resolve: context => humanRepository.GetFriendsAsync(context.Source, context.CancellationToken)); this.Interface <CharacterInterface>(); }
public HumanGType(IHumanRepository humanRepository, IDataLoaderContextAccessor dataLoader) { _humanRepository = humanRepository; Connection <HumanGType>() .Name("humans") .Unidirectional() .PageSize(10) .ResolveAsync(async context => { var loader = dataLoader.Context.GetOrAddCollectionBatchLoader <Guid, Character>("FriendsLoader", _humanRepository.GetFriendsAsync); // IMPORTANT: In order to avoid deadlocking on the loader we use the following construct (next 2 lines): var loadHandle = loader.LoadAsync(context.Source.Id); var result = await loadHandle; return(await result.ToConnection(context)); }); Name = "Human"; Description = "A humanoid creature from the Star Wars universe."; Field(x => x.Id, type: typeof(IdGraphType)).Description("The unique identifier of the human."); Field(x => x.Name).Description("The name of the human."); Field(x => x.HomePlanet, nullable: true).Description("The home planet of the human."); Field <ListGraphType <EpisodeGType> >(nameof(Character.AppearsIn), "Which movie they appear in."); FieldAsync <ListGraphType <CharacterInterface>, List <Character> >( nameof(Human.Friends), "The friends of the character, or an empty list if they have none.", resolve: context => _humanRepository.GetFriendsAsync(context.Source, context.CancellationToken)); Interface <CharacterInterface>(); //Authorization on entire Type this.AuthorizeWith("AdminPolicy"); }
public Task <List <Character> > GetFriendsAsync( [Service] IHumanRepository humanRepository, [Parent] Human human, CancellationToken cancellationToken) => humanRepository.GetFriendsAsync(human, cancellationToken);