Exemplo n.º 1
0
        public void RunExample()
        {
            var schema = GraphQL <Context> .CreateDefaultSchema(() =>
                                                                new Context
            {
                Heros = new List <Hero> {
                    new Hero {
                        Id = "1000", Name = "Luke Skywalker", Episode = "EMPIRE"
                    },
                    new Hero {
                        Id = "1001", Name = "R2-D2", Episode = "JEDI"
                    }
                }
            });

            schema.AddType <Hero>().AddAllFields();
            schema.AddField(
                "hero",
                new { episode = "EMPIRE" },
                (c, args) => c.Heros.SingleOrDefault(h => h.Episode == args.episode));

            schema.Complete();

            var gql         = new GraphQL <Context>(schema);
            var queryResult = gql.ExecuteQuery("{empireHero: hero(episode: \"EMPIRE\") {name}, jediHero: hero(episode: \"JEDI\") {name}}");

            DeepEquals(queryResult, "{empireHero: {name: 'Luke Skywalker'}, jediHero: {name: 'R2-D2'}}");
        }
Exemplo n.º 2
0
        public HRGraphSchema()
        {
            var data = new HR.Logic.HRContext();

            data.Employees = new List <Employee> {
                new HR.Models.Employee()
                {
                    Id   = "1",
                    Name = "Wakeel"
                },
                new HR.Models.Employee()
                {
                    Id   = "2",
                    Name = "Hanan"
                },
                new HR.Models.Employee()
                {
                    Id   = "3",
                    Name = "Fotouh1233121"
                }
            };


            var schema = GraphQL <HR.Logic.HRContext> .CreateDefaultSchema(() => data);

            schema.AddType <Employee>().AddAllFields();
            //
            schema.AddListField("Employees", (db) => db.Employees.AsQueryable());
            schema.Complete();
            GraphQL = new GraphQL <HR.Logic.HRContext>(schema);
        }
Exemplo n.º 3
0
        public GraphQLController()
        {
            _db     = new BeerContext();
            _schema = GraphQL <BeerContext> .CreateDefaultSchema(() => new BeerContext());

            InitializeBeerSchema(_schema);
            InitializeMutationSchema(_schema);
            _schema.Complete();

            // Queries
            //var query = @"{
            //                Beer(id:9) {
            //                    id
            //                    name
            //                    averageRatings
            //              }}";
            //var query = @"{
            //                Beers {
            //                    id
            //                    name
            //                    averageRatings
            //                    beerType
            //    }}";
            //var query = @"{
            //                FindBeer(match: ""as"") {
            //                    id : id
            //                    name : name
            //                }}";
            //var query = @"mutation { addBeer(id : 0, name:""shit kaam"", averageRatings : 4, beerTypeId :0) { id } }";
            //var query = @"mutation { deleteBeer(id : 0) { id } }";
            //var query = @"mutation { addBeerRating(id : 0, rating : 4, beerId :0) { id } }";
        }
        public void RunExample2()
        {
            var schema = GraphQL <Context> .CreateDefaultSchema(() =>
                                                                new Context
            {
                Humans = new List <Human> {
                    new Human {
                        Id = "1000", Name = "Luke Skywalker", Height = 1.72
                    }
                }
            });

            var humanSchema = schema.AddType <Human>();

            humanSchema.AddField(h => h.Id);
            humanSchema.AddField(h => h.Name);
            humanSchema.AddField(
                "height",
                new { unit = "METER" },
                (c, args, h) => args.unit == "FOOT" ? h.Height * 3.28084 : h.Height);

            schema.AddField(
                "human",
                new { id = "-1" },
                (c, args) => c.Humans.SingleOrDefault(h => h.Id == args.id));

            schema.Complete();

            var gql         = new GraphQL <Context>(schema);
            var queryResult = gql.ExecuteQuery("{human(id: \"1000\") {name, height(unit: \"FOOT\")}}");

            DeepEquals(queryResult, "{human: {name: 'Luke Skywalker', height: 5.6430448}}");
        }
        public void RunExample()
        {
            var schema = GraphQL <Context> .CreateDefaultSchema(() =>
                                                                new Context
            {
                Humans = new List <Human> {
                    new Human {
                        Id = "1000", Name = "Luke Skywalker", Height = 1.72
                    }
                }
            });

            schema.AddType <Human>().AddAllFields();
            schema.AddField(
                "human",
                new { id = "-1" },
                (c, args) => c.Humans.SingleOrDefault(h => h.Id == args.id));

            schema.Complete();

            var gql         = new GraphQL <Context>(schema);
            var queryResult = gql.ExecuteQuery("{human(id: \"1000\") {name, height}}");

            DeepEquals(queryResult, "{human: {name: 'Luke Skywalker', height: 1.72}}");
        }
Exemplo n.º 6
0
        public static GraphQLSchema <MemContext> CreateDefaultSchema()
        {
            var schema = GraphQL <MemContext> .CreateDefaultSchema(() => new MemContext());

            schema.AddScalar(new { year = 0, month = 0, day = 0 }, ymd => new DateTime(ymd.year, ymd.month, ymd.day));
            InitializeUserSchema(schema);
            InitializeAccountSchema(schema);
            InitializeMutationSchema(schema);
            InitializeNullRefSchema(schema);
            InitializeCharacterSchema(schema);
            return(schema);
        }
Exemplo n.º 7
0
        public UserService()
        {
            var schema = GraphQL <SocialNetworkContext> .CreateDefaultSchema(() => new SocialNetworkContext());

            var userType = schema.AddType <User>();

            userType.AddAllFields();
            schema.AddListField("users", new { ids = new List <int>() }, (db, args) => db.Users.Where(x => args.ids.Contains(x.Id)));
            schema.AddField("user", new { id = 0 }, (db, args) => db.Users.FirstOrDefault(x => x.Id == args.id));

            schema.Complete();
            _graphQl = new GraphQL <SocialNetworkContext>(schema);
        }
Exemplo n.º 8
0
        private static GraphQL <EfContext> CreateDefaultContext()
        {
            var schema = GraphQL <EfContext> .CreateDefaultSchema(() => new EfContext());

            schema.AddScalar(new { year = 0, month = 0, day = 0 }, ymd => new DateTime(ymd.year, ymd.month, ymd.day));
            InitializeUserSchema(schema);
            InitializeAccountSchema(schema);
            InitializeMutationSchema(schema);
            InitializeNullRefSchema(schema);
            InitializeCharacterSchema(schema);
            schema.Complete();
            return(new GraphQL <EfContext>(schema));
        }
Exemplo n.º 9
0
        public GraphQLSchema BillSchema()
        {
            var schema = GraphQL <KHParkContext> .CreateDefaultSchema(() => new KHParkContext());

            schema.AddType <M_BILL>().AddAllFields();
            schema.AddListField("M_BILL_RECS", db => db.M_BILL_REC);
            return(schema);
            //user.AddField(u => u.Id);
            //user.AddField(u => u.Name);
            //user.AddField(u => u.Account);
            //user.AddField("totalUsers", (db, u) => db.Users.Count());
            //user.AddField("accountPaid", (db, u) => u.Account.Paid);
        }
Exemplo n.º 10
0
        public void AddAllFields()
        {
            var schema = GraphQL <EfContext> .CreateDefaultSchema(() => new EfContext());

            schema.AddType <User>().AddAllFields();
            schema.AddType <Account>().AddAllFields();
            schema.AddField("user", new { id = 0 }, (db, args) => db.Users.FirstOrDefault(u => u.Id == args.id));
            schema.Complete();

            var gql     = new GraphQL <EfContext>(schema);
            var results = gql.ExecuteQuery("{ user(id:1) { id, name } }");

            Test.DeepEquals(results, "{ user: { id: 1, name: 'Joe User' } }");
        }
Exemplo n.º 11
0
        public GraphQLApi(HeroContext DB)
        {
            _DB = DB;

            var schema = GraphQL <HeroContext> .CreateDefaultSchema(() => _DB);

            schema.AddType <Hero>().AddAllFields();
            schema.AddListField("heros", db => db.Heroes);
            schema.AddField("user", new { id = 0 }, (db, args) => db.Heroes.Where(u => u.HeroId == args.id).FirstOrDefault());

            schema.Complete();

            this.Current = new GraphQL <HeroContext>();
        }
        public RepositorioDeConsulta()          //ModeloPeliculas modelo)
        {
            var modelo = new ModeloPeliculas(); // = modelo;
            var pel    = modelo.Peliculas.FirstOrDefault();

            _schema = GraphQL <ModeloPeliculas> .CreateDefaultSchema(() => modelo);

            var pelicula = _schema.AddType <Pelicula>();

            pelicula.AddField(p => p.Id);
            pelicula.AddField(p => p.Nombre);

            _schema.AddListField("peliculas", db => db.Peliculas);
            _schema.AddField("pelicula", new { id = 0 }, (db, args) => db.Peliculas.Where(p => p.Id == args.id).FirstOrDefault());
            _schema.Complete();
        }
Exemplo n.º 13
0
        public GraphQLSchemaProvider()
        {
            var options2 = new DbContextOptionsBuilder <AdventureWorksDbContext>();

            options2.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            GraphQLSchema <AdventureWorksDbContext> schema = GraphQL <AdventureWorksDbContext> .CreateDefaultSchema(() => new AdventureWorksDbContext(options2.Options));

            schema.AddType <Employee>().AddAllFields();
            schema.AddListField("employees", db => db.Employees);
            schema.AddField("employee", new { id = 0 }, (db, args) => db.Employees.FirstOrDefault(u => u.EmployeeID == args.id));

            schema.AddType <JobCandidate>().AddAllFields();
            schema.AddType <Shift>().AddAllFields();
            schema.AddType <Department>().AddAllFields();

            schema.Complete();

            gql = new GraphQL <AdventureWorksDbContext>(schema);
        }
Exemplo n.º 14
0
        public GraphQLService(IContext dbContext)
        {
            GraphQLSchema <TestContext> schema = GraphQL <TestContext> .CreateDefaultSchema(() => dbContext.Context as TestContext);



            var user = schema.AddType <User>();

            user.AddAllFields();
            user.AddField("totalUsers", (db, u) => db.Users.Count());
            user.AddField("accountPaid", (db, u) => u.Account.Paid);

            schema.AddType <Account>().AddAllFields();
            schema.AddListField("users", db => db.Users);
            schema.AddField("user", new { id = 0 }, (db, args) => db.Users.Where(u => u.Id == args.id).FirstOrDefault());
            schema.Complete();


            graphQL = new GraphQL <TestContext>(schema);
        }
Exemplo n.º 15
0
        public void RunExample()
        {
            var schema = GraphQL <Context> .CreateDefaultSchema(CreateDefaultContext);

            schema.AddType <Character>().AddAllFields();
            schema.AddField("hero", c => c.Hero);

            schema.Complete();

            var gql         = new GraphQL <Context>(schema);
            var queryResult = gql.ExecuteQuery(
                @"{
                    hero {
                        name,
                        friends {
                            name
                        }
                    }
                  }"
                );

            DeepEquals(
                queryResult,
                @"{
                    hero: {
                        name: 'R2-D2',
                        friends: [
                            {
                              name: 'Luke Skywalker'
                            },
                            {
                                name: 'Han Solo'
                            },
                            {
                                name: 'Leia Organa'
                            }
                        ]
                    }
                  }"
                );
        }
Exemplo n.º 16
0
        public void RunExample()
        {
            var defaultContext = new Context
            {
                Reviews = new List <Review> {
                    new Review {
                        Stars      = 5,
                        Episode    = "EMPIRE",
                        Commentary = "Great movie"
                    }
                }
            };

            var schema = GraphQL <Context> .CreateDefaultSchema(() => defaultContext);

            schema.AddType <Review>().AddAllFields();
            schema.AddScalar(
                new
            {
                stars      = default(int),
                commentary = default(string)
            },
                i => new ReviewInput {
                Stars = i.stars, Commentary = i.commentary
            },
                "ReviewInput"
                );
            schema.AddMutation(
                "createReview",
                new { episode = "EMPIRE", review = default(ReviewInput) },
                (db, args) =>
            {
                var newId  = db.Reviews.Select(r => r.Id).Max() + 1;
                var review = new Review
                {
                    Id         = newId,
                    Episode    = args.episode,
                    Commentary = args.review.Commentary,
                    Stars      = args.review.Stars
                };
                db.Reviews.Add(review);
                return(newId);
            },
                (db, args, rId) => db.Reviews.AsQueryable().SingleOrDefault(r => r.Id == rId)
                );
            schema.Complete();

            var gql         = new GraphQL <Context>(schema);
            var queryResult = gql.ExecuteQuery(
                @"mutation CreateReviewForEpisode($ep: String!, $review: ReviewInput!) {
                    createReview(episode: ""JEDI"", review: {commentary: ""This is a great movie!"", stars: 5}) {
                        stars
                        commentary
                    }
                }"
                );

            DeepEquals(
                queryResult,
                @"{
                    ""createReview"": {
                        ""stars"": 5,
                        ""commentary"": ""This is a great movie!""
                    }
                  }
                ");
        }
Exemplo n.º 17
0
        public void RunExample()
        {
            var schema = GraphQL <Context> .CreateDefaultSchema(() =>
                                                                new Context
            {
                Heros = new List <Character> {
                    new Character {
                        Id        = "1000",
                        Name      = "Luke Skywalker",
                        Episode   = "EMPIRE",
                        AppearsIn = new string[] { "NEWHOPE", "EMPIRE", "JEDI" },
                        Friends   = new List <Character> {
                            new Character {
                                Name = "Han Solo"
                            },
                            new Character {
                                Name = "Leia Organa"
                            },
                            new Character {
                                Name = "C-3PO"
                            },
                            new Character {
                                Name = "R2-D2"
                            }
                        }
                    },
                    new Character {
                        Id        = "1001",
                        Name      = "R2-D2",
                        Episode   = "JEDI",
                        AppearsIn = new string[] { "NEWHOPE", "EMPIRE", "JEDI" },
                        Friends   = new List <Character> {
                            new Character {
                                Name = "Luke Skywalker"
                            },
                            new Character {
                                Name = "Han Solo"
                            },
                            new Character {
                                Name = "Leia Organa"
                            }
                        }
                    }
                }
            });

            schema.AddType <Character>().AddAllFields();
            schema.AddField(
                "hero",
                new { episode = "EMPIRE" },
                (c, args) => c.Heros.SingleOrDefault(h => h.Episode == args.episode));

            schema.Complete();

            var gql         = new GraphQL <Context>(schema);
            var queryResult = gql.ExecuteQuery(
                @"{
                      leftComparison: hero(episode: ""EMPIRE"") {
                        ...comparisonFields
                      }
                      rightComparison: hero(episode: ""JEDI"") {
                        ...comparisonFields
                      }
                  }
                  fragment comparisonFields on Character {
                    name
                    appearsIn
                    friends {
                        name
                    }
                  }"
                );

            DeepEquals(
                queryResult,
                @"{
                    ""leftComparison"": {
                        ""name"": ""Luke Skywalker"",
                        ""appearsIn"": [
                            ""NEWHOPE"",
                            ""EMPIRE"",
                            ""JEDI""
                        ],
                        ""friends"": [
                            {
                              ""name"": ""Han Solo""
                            },
                            {
                              ""name"": ""Leia Organa""
                            },
                            {
                              ""name"": ""C-3PO""
                            },
                            {
                              ""name"": ""R2-D2""
                            }
                        ]
                    },
                    ""rightComparison"": {
                      ""name"": ""R2-D2"",
                      ""appearsIn"": [
                        ""NEWHOPE"",
                        ""EMPIRE"",
                        ""JEDI""
                      ],
                      ""friends"": [
                        {
                          ""name"": ""Luke Skywalker""
                        },
                        {
                          ""name"": ""Han Solo""
                        },
                        {
                          ""name"": ""Leia Organa""
                        }
                      ]
                    }
                  }
                ");
        }
Exemplo n.º 18
0
        static void Main()
        {
            var schema = GraphQL <TestContext> .CreateDefaultSchema(() => new TestContext());

            schema.AddType <User>()
            .AddField(u => u.Id)
            .AddField(u => u.Name)
            .AddField(u => u.Account)
            .AddField("total", (db, u) => db.Users.Count())
            .AddField("accountPaid", (db, u) => u.Account.Paid);
            schema.AddQuery("users", db => db.Users);
            schema.AddQuery("user", new { id = 0 }, (db, args) => db.Users.Where(u => u.Id == args.id).FirstOrDefault());

            //schema.AddType<Account>().AddAllFields(); // TODO:
            schema.AddType <Account>()
            .AddField(a => a.Id)
            .AddField(a => a.Name)
            .AddField(a => a.Paid);

            //Initialize();

            var queryStr1 = @"
query user(id:1) {
    idAlias : id,
    nameAlias : name,
    account {
        id
    },
    total
}";

            var queryStr2 = @"
query user(id:0) {
    idAlias : id,
    nameAlias : name,
    account {
        id
    }
}";

            var queryStr3 = @"
query users {
    idAlias : id,
    nameAlias : name,
    account {
        id
    }
    total
    accountPaid
}";

            var dict = GraphQL <TestContext> .Execute(queryStr1);

            Console.WriteLine(JsonConvert.SerializeObject(dict, Formatting.Indented));

            dict = GraphQL <TestContext> .Execute(queryStr2);

            Console.WriteLine(JsonConvert.SerializeObject(dict, Formatting.Indented));

            dict = GraphQL <TestContext> .Execute(queryStr3);

            Console.WriteLine(JsonConvert.SerializeObject(dict, Formatting.Indented));

            Console.ReadLine();
        }
Exemplo n.º 19
0
        public void RunExample()
        {
            var defaultContext = new Context
            {
                Heros = new List <Character> {
                    new Human
                    {
                        Id     = 1,
                        Name   = "Han Solo",
                        Height = 5.6430448
                    },
                    new Stormtrooper
                    {
                        Id             = 2,
                        Name           = "FN-2187",
                        Height         = 4.9,
                        Specialization = "Imperial Snowtrooper"
                    },
                    new Droid
                    {
                        Id              = 3,
                        Name            = "R2-D2",
                        PrimaryFunction = "Astromech"
                    }
                }
            };

            var schema = GraphQL <Context> .CreateDefaultSchema(() => defaultContext);

            schema.AddType <Character>().AddAllFields();
            schema.AddType <Human>().AddAllFields();
            schema.AddType <Stormtrooper>().AddAllFields();
            schema.AddType <Droid>().AddAllFields();
            schema.AddListField(
                "heros",
                db => db.Heros.AsQueryable()
                );
            schema.Complete();

            var gql         = new GraphQL <Context>(schema);
            var queryResult = gql.ExecuteQuery(
                @"query Heros {
                    heros {
                        name
                        ... on Droid {
                            primaryFunction
                        }
                        ... on Stormtrooper {
                            specialization
                        }
                        ... on Human {
                            height
                        }
                    }
                }"
                );

            DeepEquals(
                queryResult,
                @"{
                  ""heros"": [
                    {
                      ""name"": ""Han Solo"",
                      ""height"": 5.6430448
                    },
                    {
                      ""name"": ""FN-2187"",
                      ""specialization"": ""Imperial Snowtrooper"",
                      ""height"": 4.9
                    },
                    {
                      ""name"": ""R2-D2"",
                      ""primaryFunction"": ""Astromech""
                    }
                  ]
                }
                ");
        }