public ExecutionContext(GraphQLSchema graphQLSchema, GraphQLDocument ast) { this.graphQLSchema = graphQLSchema; this.ast = ast; this.fragments = new Dictionary <string, GraphQLFragmentDefinition>(); this.variables = new ExpandoObject(); }
private static void InitializeMutationSchema(GraphQLSchema <MemContext> schema) { var mutate = schema.AddType <MutateMe>(); mutate.AddAllFields(); schema.AddField("mutateMes", new { id = 0 }, (db, args) => db.MutateMes.AsQueryable().FirstOrDefault(a => a.Id == args.id)); schema.AddMutation("mutate", new { id = 0, newVal = 0 }, (db, args) => { var mutateMe = db.MutateMes.First(m => m.Id == args.id); mutateMe.Value = args.newVal; }, (db, args) => db.MutateMes.AsQueryable().FirstOrDefault(a => a.Id == args.id)); schema.AddMutation("addMutate", new { newVal = 0 }, (db, args) => { var newMutate = new MutateMe { Value = args.newVal }; db.MutateMes.Add(newMutate); // simulate Id being set by database newMutate.Id = db.MutateMes.Max(m => m.Id) + 1; return(newMutate.Id); }, (db, args, id) => db.MutateMes.AsQueryable().FirstOrDefault(a => a.Id == id)); }
private static string GetAbcPostField() => "easy as 123"; // mimic an in-memory function private static void InitializeAccountSchema(GraphQLSchema <EfContext> schema) { var account = schema.AddType <Account>(); account.AddField(a => a.Id); account.AddField(a => a.Name); account.AddField(a => a.Paid); account.AddField(a => a.SomeGuid); account.AddField(a => a.ByteArray); account.AddField(a => a.AccountType); account.AddListField(a => a.Users); account.AddListField("activeUsers", (db, a) => a.Users.Where(u => u.Active)); account.AddListField("usersWithActive", new { active = false }, (db, args, a) => a.Users.Where(u => u.Active == args.active)); account.AddField("firstUserWithActive", new { active = false }, (db, args, a) => a.Users.FirstOrDefault(u => u.Active == args.active)); schema.AddField("account", new { id = 0 }, (db, args) => db.Accounts.FirstOrDefault(a => a.Id == args.id)); schema.AddField ("accountPaidBy", new { paid = default(DateTime) }, (db, args) => db.Accounts.AsQueryable().FirstOrDefault(a => a.PaidUtc <= args.paid)); schema.AddListField("accountsByGuid", new { guid = Guid.Empty }, (db, args) => db.Accounts.AsQueryable().Where(a => a.SomeGuid == args.guid)); schema.AddListField("accountsByType", new { accountType = AccountType.None }, (db, args) => db.Accounts.AsQueryable().Where(a => a.AccountType == args.accountType)); schema.AddEnum <AccountType>(prefix: "accountType_"); //add this enum just so it is part of the schema schema.AddEnum <MaterialType>(prefix: "materialType_"); }
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 } }"; }
private static void InitializeMutationSchema(GraphQLSchema <EfContext> schema) { var mutate = schema.AddType <MutateMe>(); mutate.AddAllFields(); schema.AddField("mutateMes", new { id = 0 }, (db, args) => db.MutateMes.AsQueryable().FirstOrDefault(a => a.Id == args.id)); schema.AddMutation("mutate", new { id = 0, newVal = 0 }, (db, args) => { var mutateMe = db.MutateMes.First(m => m.Id == args.id); mutateMe.Value = args.newVal; db.SaveChanges(); }, (db, args) => db.MutateMes.AsQueryable().FirstOrDefault(a => a.Id == args.id)); schema.AddMutation("addMutate", new { newVal = 0 }, (db, args) => { var newMutate = new MutateMe { Value = args.newVal }; db.MutateMes.Add(newMutate); db.SaveChanges(); return(newMutate.Id); }, (db, args, id) => db.MutateMes.AsQueryable().FirstOrDefault(a => a.Id == id)); }
public async Task TestJson() { var organisations = new OrganisationTable("orgs"); var users = new UserTable(); var services = new ServiceCollection() .AddRoscoeSqlServer("Server=localhost;Database=test;"); using (var provider = services.BuildServiceProvider()) { var db = new RoscoeDb(provider); var schema = GraphQLSchema.Create(x => new RootQueryType(x, db)); var result = await schema.ExecuteRequestAsync(@"query { Organisations { id: Id Name UserNames Users { Id Name Organisation { Name } } } }", null); var json = JsonConvert.SerializeObject(result, Formatting.Indented); } }
public void SetUp() { this.schema = new GraphQLSchema(); var rootType = new RootQueryType(); schema.Query(rootType); }
public void PrintSchema_DoesntPrintSchemaOfCommonNames() { this.schema = new GraphQLSchema(); var query = new TestObjectType("Query", this.schema); query.Field("foo", () => default(string)); this.schema.Query(query); var mutation = new TestMutationType("Mutation", this.schema); mutation.Field("foo", () => default(int)); this.schema.Mutation(mutation); var subscription = new TestSubscriptionType("Subscription", this.schema); subscription.Field("foo", () => default(ID)); this.schema.Subscription(subscription); var result = SchemaUtils.PrintSchema(this.schema); this.AreEqual(@" type Mutation { foo: Int! } type Query { foo: String } type Subscription { foo: ID! }", result); }
public void SetUp() { this.schema = new GraphQLSchema(); var rootType = new RootQueryType(); var nestedType = new NestedQueryType(); nestedType.Field("howdy", () => "xzyt"); var anotherSestedType = new AnotherNestedQueryType(); anotherSestedType.Field("stuff", () => "a"); nestedType.Field("anotherNested", () => anotherSestedType); rootType.Field("nested", () => nestedType); var typeWithAccessor = new CustomObject(); typeWithAccessor.Field("Hello", e => e.Hello); typeWithAccessor.Field("Test", e => e.Test); rootType.Field("acessorBasedProp", () => new TestType() { Hello = "world", Test = "stuff" }); this.schema.AddKnownType(rootType); this.schema.AddKnownType(anotherSestedType); this.schema.AddKnownType(nestedType); this.schema.AddKnownType(typeWithAccessor); this.schema.Query(rootType); }
public T1(T2 type2, GraphQLSchema schema) : base("T1", "") { this.Field("a", () => "1"); this.Field("b", () => 2); this.Field("c", () => new int[] { 1, 2, 3 }); this.Field("type2", () => new TestType()); }
public void SetUp() { this.schema = new GraphQLSchema(); var nestedType = new NestedQueryType(this.schema); var rootType = new RootQueryType(nestedType, this.schema); this.schema.Query(rootType); }
public void SetUp() { this.schema = new GraphQLSchema(); var rootType = new QueryRootType(); this.schema.AddKnownType(rootType); this.schema.AddKnownType(new A()); this.schema.Query(rootType); }
public void NoMutationDefined_ShouldReturnNullInIntrospection() { var emptySchema = new GraphQLSchema(); emptySchema.Query(new T1()); var result = emptySchema.Execute("{ __schema { mutationType { name } } }"); Assert.IsNull(result.data.__schema.mutationType); }
private ExecutionManager(GraphQLSchema graphQLSchema, dynamic variables, string clientId, int?subscriptionId) { this.graphQLSchema = graphQLSchema; this.fragments = new Dictionary <string, GraphQLFragmentDefinition>(); this.validationContext = new ValidationContext(); this.variables = variables ?? new ExpandoObject(); this.subscriptionId = subscriptionId; this.clientId = clientId; }
public void SetUp() { this.schema = new GraphQLSchema(); var rootType = new RootQueryType(); schema.Query(rootType); this.singleOperationQuery = "query q1 { a : hello }"; this.multipleOperationQuery = "query q1 { a : hello } query q2 { b : hello }"; }
public RootQueryType(GraphQLSchema schema) : base("RootQueryType", "") { this.Field("nested", (int id) => new TestObject() { Id = id, StringField = "Test with id " + id }); this.Field("withArray", (int[] ids) => ids.Count()); this.Field("isNull", (int?nonMandatory) => !nonMandatory.HasValue); this.Field("withList", (List <int> ids) => ids.Count()); this.Field("withIEnumerable", (IEnumerable <int> ids) => ids.Count()); }
private static void InitializeCharacterSchema(GraphQLSchema <EfContext> schema) { schema.AddType <Character>().AddAllFields(); schema.AddType <Human>().AddAllFields(); schema.AddType <Stormtrooper>().AddAllFields(); schema.AddType <Droid>().AddAllFields(); schema.AddType <Vehicle>().AddAllFields(); schema.AddField("hero", new { id = 0 }, (db, args) => db.Heros.SingleOrDefault(h => h.Id == args.id)); schema.AddListField("heros", db => db.Heros.AsQueryable()); }
public void SetUp() { this.schema = new GraphQLSchema(); var subscriptionRootType = new SubscriptionType(); var mutationType = new MutationType(); this.schema.AddKnownType(subscriptionRootType); this.schema.AddKnownType(mutationType); this.schema.Subscription(subscriptionRootType); this.schema.Mutation(mutationType); }
public async Task TestStrongTypeQuery() { var services = new ServiceCollection() .AddRoscoeSqlServer("Server=localhost;Database=test;"); using (var provider = services.BuildServiceProvider()) { var db = new RoscoeDb(provider); var schema = GraphQLSchema.Create(x => new RootQueryType(x, db)); var result = await schema.QueryAsync(x => new { Organisations = x.Organisations() .Select(y => new { //Foo = y.Id().GetValue(null), Id = y.Id().Value() + "foo", Name = y.Name().Value(), Type = y.Type().Value(), Users = y.Users() .Select(z => new { Org = z.Organisation() .Select(a => new { Name = a.Name().Value(), }), }), }), }); /* * schema.ExecuteQueryAsync((x, dr) => new * { * Organisations = GraphQLSchemaOperations.Select(x.Organisations(), dr, (y, dr2) => new * { * Id = (string)y.Id.GetValue(dr2), * Name = (string)y.Name.GetValue(dr2), * Users = GraphQLSchemaOperations.Select(y.Users(), dr2, (z, dr3) => new * { * Org = GraphQLSchemaOperations.Select(z.Organisation(), dr3, (a, dr4) => new * { * Name = (string)a.Name().GetValue(dr4), * }, * }, * }), * }); * */ var json = JsonConvert.SerializeObject(result, Formatting.Indented); } }
static void GenerateClass(StringBuilder sb, GraphQLSchema schema, GraphQLSchemaType type, int indent) { if (type.Name.StartsWith("__")) { return; } if (type.Kind.Equals("SCALAR")) { return; } EmitComment(ref sb, type.Description, indent); if (type.Kind.Equals("ENUM")) { sb.AppendLine($"{Indent(indent)}[JsonConverter(typeof(StringEnumConverter))]"); } sb.Append($"{Indent(indent)}public {type.EmitTypeDecl()}"); if (type.Interfaces != null && type.Interfaces.Count > 0) { sb.Append(" : "); for (int i = 0; i <= type.Interfaces.Count - 2; i++) { sb.Append($"{type.Interfaces[i].TypeName()}, "); } sb.Append($"{type.Interfaces[type.Interfaces.Count - 1].TypeName()}"); } sb.Append($"\n{Indent(indent)}{{"); switch (type.Kind) { case "OBJECT": EmitFields(ref sb, type.Fields, indent + 1, true); break; case "INPUT_OBJECT": EmitFields(ref sb, type.InputFields, indent + 1, true); break; case "INTERFACE": if (type.PossibleTypes != null) { EmitFields(ref sb, type.Fields, indent + 1, false); } break; case "ENUM": EmitFields(ref sb, type.EnumValues, indent + 1); break; default: System.Diagnostics.Trace.WriteLine("oops"); break; } sb.AppendLine($"{Indent(indent)}}}\n"); }
public void SetUp() { this.schema = new GraphQLSchema(); var rootType = new RootQueryType(); var nestedType = new NestedQueryType(); nestedType.Field("howdy", () => "xzyt"); var anotherSestedType = new AnotherNestedQueryType(); anotherSestedType.Field("stuff", () => "a"); nestedType.Field("anotherNested", () => anotherSestedType); rootType.Field("nested", () => nestedType); var typeWithAccessor = new CustomObject(); typeWithAccessor.Field("Hello", e => e.Hello); typeWithAccessor.Field("Test", e => e.Test); var anotherTypeWithAccessor = new AnotherCustomObject(); anotherTypeWithAccessor.Field("Hello", e => e.Hello); anotherTypeWithAccessor.Field("World", e => e.World); var customInterface = new CustomInterface(); customInterface.Field("hello", e => e.Hello); rootType.Field("acessorBasedProp", () => new TestType() { Hello = "world", Test = "stuff" }); rootType.Field("testTypes", () => new ITestInterface[] { new TestType { Hello = "world", Test = "stuff" }, new AnotherTestType { Hello = "world", World = "hello" } }); this.schema.AddKnownType(new TestEnumType()); this.schema.AddKnownType(rootType); this.schema.AddKnownType(anotherSestedType); this.schema.AddKnownType(nestedType); this.schema.AddKnownType(typeWithAccessor); this.schema.AddKnownType(anotherTypeWithAccessor); this.schema.AddKnownType(customInterface); this.schema.Query(rootType); }
static int Main(string[] args) { try { Console.WriteLine("BuildGraphQLModel -ns <namespace> -e <endpoint> -o <output>"); string endpoint = null; string outputFile = null; string ns = null; for (int i = 0; i < args.Length; i += 2) { switch (args[i].ToLower()) { case "-ns": ns = args[i + 1]; break; case "-e": endpoint = args[i + 1]; break; case "-o": outputFile = args[i + 1]; break; } } if (string.IsNullOrEmpty(endpoint)) { Console.WriteLine("Specify GraphQL endpoint address."); return(-1); } if (string.IsNullOrEmpty(outputFile)) { Console.WriteLine("Specify output file."); return(-1); } if (string.IsNullOrEmpty(ns)) { Console.WriteLine("Specify namespace."); return(-1); } GraphQLClient client = new GraphQLClient(endpoint); GraphQLSchema schema = client.Schema; GenerateSchemaClasses(schema, ns, outputFile); } catch (Exception ex) { Console.WriteLine("An error occured when generating classes."); Console.WriteLine(ex.Message); return(-1); } return(0); }
public void SetUp() { this.schema = new GraphQLSchema(); var graphQLClassBasedModel = new GraphQLClassBasedModel(); var graphQLStructBasedModel = new GraphQLStructBasedModel(); var graphQLEnumBasedModel = new GraphQLEnumBasedModel(); this.schema.AddKnownType(graphQLClassBasedModel); this.schema.AddKnownType(graphQLStructBasedModel); this.schema.AddKnownType(graphQLEnumBasedModel); schema.Query(graphQLClassBasedModel); }
public void SetUp() { this.schema = new GraphQLSchema(); var rootType = new RootQueryType(this.schema); var nestedTypeNonGeneric = new NestedNonGenericQueryType(); var nestedType = new NestedQueryType(nestedTypeNonGeneric); this.schema.AddKnownType(rootType); this.schema.AddKnownType(nestedTypeNonGeneric); this.schema.AddKnownType(nestedType); this.schema.Query(rootType); }
public void Should_Build_Schema_With_Multiple_Interfaces() { var schema = new GraphQLSchema { QueryType = new GraphQLRequestType { Name = "Person" }, Types = new List <GraphQLType> { new GraphQLType { Name = "IPerson1", Kind = GraphQLTypeKind.Interface }, new GraphQLType { Name = "IPerson2", Kind = GraphQLTypeKind.Interface }, new GraphQLType { Name = "IPerson3", Kind = GraphQLTypeKind.Interface }, new GraphQLType { Name = "Person", Interfaces = new List <GraphQLFieldType> { new GraphQLFieldType { Name = "IPerson1" }, new GraphQLFieldType { Name = "IPerson2" }, new GraphQLFieldType { Name = "IPerson3" }, } } } }; var sdl = SDLBuilder.Build(schema); sdl.ShouldBe(Read("interfaces.graphql")); }
public void SetUp() { this.schema = new GraphQLSchema(); var rootType = new RootQueryType(this.schema); var nestedType = new NestedQueryType(); var mutation = new RootMutationType(this.schema); this.schema.AddKnownType(rootType); this.schema.AddKnownType(nestedType); this.schema.AddKnownType(mutation); this.schema.Query(rootType); this.schema.Mutation(mutation); }
/// <summary> /// Initialization of Beer Schema /// </summary> /// <param name="schema"></param> private static void InitializeBeerSchema(GraphQLSchema <BeerContext> schema) { var beer = schema.AddType <Beers>(); beer.AddField(b => b.Id); beer.AddField(b => b.Name); beer.AddField(b => b.AverageRatings); beer.AddListField("beerRatings", (db, b) => b.BeerRatings); beer.AddField(t => t.BeerTypes); beer.AddField("beerType", (db, u) => u.BeerTypes.Name); schema.AddListField("Beers", db => db.Beers); schema.AddField("Beer", new { id = 0 }, (db, args) => db.Beers.FirstOrDefault(u => u.Id == args.id)); schema.AddListField("FindBeer", new { match = string.Empty }, (db, args) => db.Beers.Where(b => b.Name.ToLower().Contains(args.match.ToLower()))); }
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 void SetUp() { this.schema = new GraphQLSchema(); var rootType = new RootQueryType(this.schema); var nestedTypeNonGeneric = new NestedNonGenericQueryType(); var nestedType = new NestedQueryType(nestedTypeNonGeneric); this.schema.AddKnownType(rootType); this.schema.AddKnownType(nestedTypeNonGeneric); this.schema.AddKnownType(nestedType); this.schema.AddKnownType(new InputTestObjectType()); this.schema.AddKnownType(new TestEnumType()); this.schema.AddOrReplaceDirective(new DefaultArgumentDirectiveType()); this.schema.Query(rootType); }
public void SetUp() { this.schema = new GraphQLSchema(); var type2 = new T2(this.schema); var type1 = new T1(type2, this.schema); var t2interface = new T2Interface(this.schema); var rootType = new RootQueryType(type1, t2interface, this.schema); this.schema.AddKnownType(type1); this.schema.AddKnownType(type2); this.schema.AddKnownType(t2interface); this.schema.AddKnownType(rootType); this.schema.AddKnownType(new SampleInputObjectType()); this.schema.Query(rootType); }