Пример #1
0
        public ISchema CreateSchema(Starwars starwars)
        {
            var schema     = StarwarsSchema.Create();
            var resolvers  = StarwarsResolvers.BuildResolvers(starwars);
            var executable = SchemaTools.MakeExecutableSchemaWithIntrospection(
                schema,
                resolvers);

            return(executable);
        }
Пример #2
0
 public Human(Starwars starwars)
 {
     _starwars = starwars;
 }
Пример #3
0
        public static ResolverMap BuildResolvers(Starwars starwars)
        {
            async ValueTask <IResolveResult> ResolveCharacter(ResolverContext context)
            {
                var id        = (string)context.Arguments["id"];
                var character = await starwars.GetCharacter(id).ConfigureAwait(false);

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

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

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

                return(As(human));
            }

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

                return(As(friends.Select(c => As(context.Schema.GetNamedType <ObjectType>("Human"), c))));
            }

            async ValueTask <IResolveResult> ResolveCharacters(ResolverContext context)
            {
                await Task.Delay(0).ConfigureAwait(false);

                return(As(starwars.Characters.Select(c => As(context.Schema.GetNamedType <ObjectType>("Human"), c))));
            }

            async ValueTask <IResolveResult> AddHuman(ResolverContext context)
            {
                var humanInput = (IDictionary <string, object>)context.Arguments["human"];
                var human      = starwars.AddHuman(humanInput["name"].ToString());

                await Task.Delay(0).ConfigureAwait(false);

                return(As(human));
            }

            var resolverMap = new ResolverMap
            {
                // Root query
                ["Query"] = new FieldResolverMap
                {
                    { "human", ResolveHuman },
                    { "character", ResolveCharacter },
                    { "characters", ResolveCharacters }
                },

                // Root mutation
                ["Mutation"] = new FieldResolverMap
                {
                    { "addHuman", AddHuman }
                },

                // ObjectType
                ["Human"] = new FieldResolverMap
                {
                    { "id", PropertyOf <Starwars.Human>(c => c.Id) },
                    { "name", PropertyOf <Starwars.Human>(c => c.Name) },
                    { "homePlanet", PropertyOf <Starwars.Human>(c => c.HomePlanet) },
                    { "friends", ResolveFriends },
                    { "appearsIn", PropertyOf <Starwars.Human>(h => h.AppearsIn) }
                }
            };

            return(resolverMap);
        }