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 static void ChildFieldWithParameters <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ account(id:1) { id, name, firstUserWithActive(active:true) { id, name } } }"); Test.DeepEquals(results, "{ account: { id: 1, name: 'My Test Account', firstUserWithActive: { id: 1, name: 'Joe User' } } }"); results = gql.ExecuteQuery("{ account(id:1) { id, name, firstUserWithActive(active:false) { id, name } } }"); Test.DeepEquals(results, "{ account: { id: 1, name: 'My Test Account', firstUserWithActive: null } }"); }
public static bool ChildListFieldWithParameters <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ account(id:1) { id, name, usersWithActive(active:true) { id, name } } }"); return(Test.DeepEquals(results, "{ account: { id: 1, name: 'My Test Account', usersWithActive: [{ id: 1, name: 'Joe User' }] } }")); results = gql.ExecuteQuery("{ account(id:1) { id, name, usersWithActive(active:false) { id, name } } }"); return(Test.DeepEquals(results, "{ account: { id: 1, name: 'My Test Account', usersWithActive: [] } }")); }
public static void EnumerableSubField <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ account(id:1) { activeUsers { id, name } } }"); Test.DeepEquals(results, "{ account: { activeUsers: [{ id: 1, name: 'Joe User' }] } }"); var results2 = gql.ExecuteQuery("{ account(id:2) { activeUsers { id, name } } }"); Test.DeepEquals(results2, "{ account: { activeUsers: [] } }"); }
public static void SimpleMutation <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("mutation { mutate(id:1,newVal:5) { id, value } }"); Test.DeepEquals(results, "{ mutate: { id: 1, value: 5 } }"); var results2 = gql.ExecuteQuery("mutation { mutate(id:1,newVal:123) { id, value } }"); Test.DeepEquals(results2, "{ mutate: { id: 1, value: 123 } }"); }
public static void EnumFieldQuery <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ accountsByType(accountType:accountType_Gold) { id, accountType } }"); Test.DeepEquals(results, @"{ accountsByType: [ { id: 1, accountType: 'Gold' } ] }"); }
public static void GuidParameter <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ accountsByGuid(guid:\"00000000-0000-0000-0000-000000000000\") { id, someGuid } }"); Test.DeepEquals(results, @"{ accountsByGuid: [ { id: 1, someGuid: '00000000-0000-0000-0000-000000000000' }, { id: 2, someGuid: '00000000-0000-0000-0000-000000000000' }, ] }"); }
public static void UnionTypeDroid <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery( @"query UnionTypeStarshipQuery { search(text: ""droid"") { __typename } }"); Test.DeepEquals(results, "{ search: { __typename: 'Droid'} }"); }
public static void IntrospectionDroidType <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery( @"query IntrospectionDroidTypeQuery { __type(name: ""Droid"") { name } }"); Test.DeepEquals(results, "{ __type: { name: 'Droid' } }"); }
public static void InlineFragementWithoutTypenameField <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ heros { name, ... on Stormtrooper { height, specialization } } }"); Test.DeepEquals( results, "{ heros: [ " + "{ name: 'Han Solo'}, " + "{ name: 'FN-2187', height: 4.9, specialization: 'Imperial Snowtrooper'}, " + "{ name: 'R2-D2' } ] }" ); }
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); }
public void AddAllFields() { var schema = GraphQL<MemContext>.CreateDefaultSchema(() => new MemContext()); schema.AddType<User>().AddAllFields(); schema.AddType<Account>().AddAllFields(); schema.AddField("user", new { id = 0 }, (db, args) => db.Users.AsQueryable().FirstOrDefault(u => u.Id == args.id)); schema.Complete(); var gql = new GraphQL<MemContext>(schema); var results = gql.ExecuteQuery("{ user(id:1) { id, name } }"); Test.DeepEquals(results, "{ user: { id: 1, name: 'Joe User' } }"); }
public static void FragementWithMultiLevelInheritance <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ heros { name, __typename, ... on Stormtrooper { height, specialization } } }"); Test.DeepEquals( results, "{ heros: [ " + "{ name: 'Han Solo', __typename: 'Human'}, " + "{ name: 'FN-2187', __typename: 'Stormtrooper', height: 4.9, specialization: 'Imperial Snowtrooper'}, " + "{ name: 'R2-D2', __typename: 'Droid' } ] }" ); }
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); }
public static bool FragementWithMultipleTypenameFields <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery( "{ heros { name, ...stormtrooper, __typename } }, fragment stormtrooper on Stormtrooper { height, specialization, __typename } "); return(Test.DeepEquals( results, "{ heros: [ " + "{ name: 'Han Solo', __typename: 'Human'}, " + "{ name: 'FN-2187', height: 4.9, specialization: 'Imperial Snowtrooper', __typename: 'Stormtrooper'}, " + "{ name: 'R2-D2', __typename: 'Droid'} ] }" )); }
public static void IntrospectionCharacterInterface <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery( @"query IntrospectionCharacterKindQuery { __type(name: ""ICharacter"") { name kind } }"); Test.DeepEquals(results, "{ __type: { name: 'ICharacter', kind: 'INTERFACE' } }"); }
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); }
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)); }
public static void InlineFragements <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery( "{ heros { name, __typename, ... on Human { height }, ... on Stormtrooper { specialization }, " + "... on Droid { primaryFunction } } }"); Test.DeepEquals( results, "{ heros: [ " + "{ name: 'Han Solo', __typename: 'Human', height: 5.6430448}, " + "{ name: 'FN-2187', __typename: 'Stormtrooper', height: 4.9, specialization: 'Imperial Snowtrooper'}, " + "{ name: 'R2-D2', __typename: 'Droid', primaryFunction: 'Astromech' } ] }" ); }
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 HttpResponseMessage GraphQLData(HttpRequestMessage query) { try { var gql = new GraphQL <BeerContext>(_schema); var dict = gql.ExecuteQuery(query.Content.ReadAsStringAsync().Result); return(Request.CreateResponse(HttpStatusCode.Accepted, dict)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex)); } }
public static void InlineFragementWithListField <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery( "{ heros { name, __typename, ... on Human { height, vehicles { name } }, ... on Stormtrooper { specialization }, " + "... on Droid { primaryFunction } } }"); Test.DeepEquals( results, "{ heros: [ " + "{ name: 'Han Solo', __typename: 'Human', height: 5.6430448, vehicles: [ {name: 'Millennium falcon'}] }, " + "{ name: 'FN-2187', __typename: 'Stormtrooper', height: 4.9, vehicles: [ {name: 'Speeder bike'}], specialization: 'Imperial Snowtrooper'}, " + "{ name: 'R2-D2', __typename: 'Droid', primaryFunction: 'Astromech' } ] }" ); }
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' } }"); }
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(); }
public static bool Fragements <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery( "{ heros { name, __typename, ...human, ...stormtrooper, ...droid } }, " + "fragment human on Human { height }, " + "fragment stormtrooper on Stormtrooper { specialization }, " + "fragment droid on Droid { primaryFunction }"); return(Test.DeepEquals( results, "{ heros: [ " + "{ name: 'Han Solo', __typename: 'Human', height: 5.6430448}, " + "{ name: 'FN-2187', __typename: 'Stormtrooper', height: 4.9, specialization: 'Imperial Snowtrooper'}, " + "{ name: 'R2-D2', __typename: 'Droid', primaryFunction: 'Astromech' } ] }" )); }
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); }
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); }
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' } ] } }" ); }
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}}"); }
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'}}"); }
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' } ] } }" ); }
public static void FragmentsInlineFragments <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery( @"query UseInlineFragments { luke: hero(episode: EMPIRE) { ...HumanFragment, ...DroidFragment } r2d2: hero { ...HumanFragment, ...DroidFragment } } fragment HumanFragment on Human { name homePlanet } fragment DroidFragment on Droid { name primaryFunction } " ); Test.DeepEquals(results, @" { luke: { name: 'Luke Skywalker', homePlanet: 'Tatooine' }, r2d2: { name: 'R2-D2', primaryFunction: 'Astromech' } } "); }
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!"" } } "); }
public static void DateTimeFilter <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ accountPaidBy(paid: { year: 2016 month: 1 day: 1 }) { id } }"); Test.DeepEquals(results, "{ accountPaidBy: { id: 1 } }"); }
public static void TypeName <TContext>(GraphQL <TContext> gql) { var results = gql.ExecuteQuery("{ user(id:1) { id, __typename } }"); Test.DeepEquals(results, "{ user: { id: 1, __typename: 'User' } }"); }
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"" } ] } } "); }