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 static void Create <TContext>(GraphQLSchema <TContext> schema, Func <TContext, IQueryable <ICharacter> > herosProviderFunc) { schema.AddEnum <EpisodeEnum>(); var characterInterface = schema.AddInterfaceType <ICharacter>(); characterInterface.AddAllFields(); var humanType = schema.AddType <Human>(); humanType.AddAllFields(); humanType.AddInterface(characterInterface); var droidType = schema.AddType <Droid>(); droidType.AddAllFields(); droidType.AddInterface(characterInterface); var starshipType = schema.AddType <Starship>(); starshipType.AddAllFields(); schema.AddUnionType("SearchResult", new[] { typeof(Droid), typeof(Human), typeof(Starship) }); schema.AddField( "hero", new { episode = EpisodeEnum.NEWHOPE }, (db, args) => args.episode == EpisodeEnum.EMPIRE // Luke is the hero of Episode V. ? herosProviderFunc(db).FirstOrDefault(h => h.Id == "1000") // Artoo is the hero otherwise. : herosProviderFunc(db).FirstOrDefault(h => h.Id == "2001")); schema.AddField("human", new { id = "" }, (db, args) => herosProviderFunc(db).OfType <Human>().FirstOrDefault(c => c.Id == args.id)); schema.AddField("droid", new { id = "" }, (db, args) => herosProviderFunc(db).OfType <Droid>().FirstOrDefault(c => c.Id == args.id)); schema.AddField("search", new { text = "" }, (db, args) => args.text == "starship" ? new Starship() as object : (args.text == "droid" ? new Droid() as object : new Human() as object)) .WithReturnType("SearchResult"); }