Exemplo n.º 1
0
        public ISchema CreateSchema(Starwars starwars)
        {
            var schema     = StarwarsSchema.Create();
            var resolvers  = StarwarsResolvers.BuildResolvers(starwars);
            var executable = SchemaTools.MakeExecutableSchemaWithIntrospection(
                schema,
                resolvers);

            return(executable);
        }
Exemplo n.º 2
0
        public static ObjectTypeMap BuildResolvers(Starwars starwars)
        {
            async ValueTask <IResolverResult> ResolveCharacter(IResolverContext context)
            {
                var id        = (string)context.Arguments["id"];
                var character = await starwars.GetCharacter(id).ConfigureAwait(false);

                return(As(context.ExecutionContext.Schema.GetNamedType <ObjectType>("Human"), character));
            }

            async ValueTask <IResolverResult> ResolveHuman(IResolverContext context)
            {
                var id = (string)context.Arguments["id"];

                var human = await starwars.GetHuman(id).ConfigureAwait(false);

                return(As(human));
            }

            async ValueTask <IResolverResult> ResolveFriends(IResolverContext context)
            {
                var character = (Starwars.Character)context.ObjectValue;
                var friends   = character.GetFriends();
                await Task.Delay(0).ConfigureAwait(false);

                return(As(friends, friend => CharacterIsTypeOf(friend, context)));
            }

            IType CharacterIsTypeOf(object character, IResolverContext context)
            {
                return(character switch
                {
                    Starwars.Human human => context.ExecutionContext.Schema.GetNamedType <ObjectType>("Human"),
                    _ => throw new ArgumentOutOfRangeException(nameof(character))
                });
            }
Exemplo n.º 3
0
 public Human(Starwars starwars)
 {
     _starwars = starwars;
 }