graphql .net

安装
GraphQL 7.0.0
GraphQL.SystemTextJson 7.0.0

HelloWorld

public class Program
    {
        public static async Task Main(string[] args)
        {
            var schema = Schema.For(@"
      type Query {
        hello: String
      }
    ");
            var json = await schema.ExecuteAsync(_ =>
            {
                _.Query = "{ hello }";
                _.Root = new { Hello = "Hello World!" };
            });

            Console.WriteLine(json);
        }
    }

架构优先


        public class Droid
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }
var schema = Schema.For(@"
                type Droid{
                    id:String!
                    name:String!
                }
                type Query {
                    hero:Droid
                }
            ", _ =>
            {
                _.Types.Include<Query>();
            });


            var json = schema.ExecuteAsync(_ =>
            {
                _.Query = "{ hero {id name}}";
            });
            json.Wait();

            Console.WriteLine(json.Result);

定义类型


 public class DroidType : ObjectGraphType<Droid>
        {
            public DroidType()
            {
                Field(f => f.Id).Description("this is id");
                Field(f => f.Name).Description("this is name");
            }
        }

        public class StarWarsQuery : ObjectGraphType
        {
            public StarWarsQuery()
            {
                Field<DroidType>("hero")
                    .Resolve(context => new Droid { Id = "xx", Name = "f" });
            }

        }

        public class Droid
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        public class Query
        {
            [GraphQLMetadata("hero")]
            public Droid GetHero()
            {
                return new Droid { Id = "210", Name = "xx" };
            }
        }



var schema = new Schema { Query = new StarWarsQuery() };

            var json = schema.ExecuteAsync(_ =>
            {
                _.Query = "{hero{id name}}";
            });
            json.Wait();
            Console.WriteLine(json.Result);

嵌套多个顶级架构

public class Character
        {
            public string Name { get; set; }
        }


        public class Query
        {
            [GraphQLMetadata("hero")]
            public Droid GetHero()
            {
                return new Droid { Id = "1", Name = "R2-D2" };
            }
        }

        [GraphQLMetadata("Droid",IsTypeOf =typeof(Droid))]
        public class DroidType
        {
            public string Id([FromSource] Droid droid) => droid.Id;
            public string Name([FromSource] Droid droid) => droid.Name;

            public Character Friend(IResolveFieldContext context, [FromSource] Droid source)
            {
                return new Character { Name = "hello" };
            }
        }

var schema = Schema.For(@"
               type Droid{
                    id: String!
                    name: String!
                    friend: Character
                }

                type Character{
                    name: String!
                }

                type Query {
                    hero:Droid
                }
                ", _ =>
            {
                _.Types.Include<DroidType>();
                _.Types.Include<Query>();
            });

            var json = schema.ExecuteAsync(_ =>
            {
                _.Query = "{ hero { id name friend { name } } }";
            });

            json.Wait();
            Console.WriteLine(json.Result);

客户端工具
altair_4.6.2_x64_win 类似postman

[参考]
GraphQL.NET

posted @ 2022-08-16 13:15  Hey,Coder!  阅读(107)  评论(0)    收藏  举报