Ejemplo n.º 1
0
        public static SchemaProvider <DemoContext> MakeSchema()
        {
            // build our schema directly from the DB Context
            var demoSchema = SchemaBuilder.FromObject <DemoContext>();

            demoSchema.AddCustomScalarType(typeof(DateTime), "Date");
            demoSchema.AddCustomScalarType(typeof(DateTime?), "Date");

            // we can extend the schema

            // Add custom root fields
            demoSchema.ReplaceField("actors", new
            {
                filter = ArgumentHelper.EntityQuery <Person>()
            }, (db, param) => db.People.Where(p => p.ActorIn.Any()).WhereWhen(param.filter, param.filter.HasValue), "List of actors");
            demoSchema.AddField("writers", db => db.People.Where(p => p.WriterOf.Any()), "List of writers");
            demoSchema.AddField("directors", db => db.People.Where(p => p.DirectorOf.Any()), "List of directors");

            // Add calculated fields to a type
            demoSchema.Type <Person>().AddField("name", l => $"{l.FirstName} {l.LastName}", "Person's name");
            // really poor example of using services e.g. you should just do below but pretend the service does something crazy like calls an API
            // demoSchema.Type<Person>().AddField("age", l => (int)((DateTime.Now - l.Dob).TotalDays / 365), "Show the person's age");
            // AgeService needs to be added to the ServiceProvider
            demoSchema.Type <Person>().AddField("age", person => ArgumentHelper.WithService((AgeService ageService) => ageService.Calc(person)), "Show the person's age");

            // replace fields. e.g. remove a many-to-many relationships
            demoSchema.Type <Movie>().ReplaceField("actors", m => m.Actors.Select(a => a.Person), "Actors in the movie");
            demoSchema.Type <Movie>().ReplaceField("writers", m => m.Writers.Select(a => a.Person), "Writers in the movie");

            demoSchema.Type <Person>().ReplaceField("writerOf", m => m.WriterOf.Select(a => a.Movie), "Movies they wrote");
            demoSchema.Type <Person>().ReplaceField("actorIn", m => m.ActorIn.Select(a => a.Movie), "Movies they acted in");

            var dto = demoSchema.AddType <PersonPagination>(nameof(PersonPagination), "Actor Pagination");

            dto.AddField("total", x => x.Total, "total records to match search");
            dto.AddField("pageCount", x => x.PageCount, "total pages based on page size");
            dto.AddField("people", x => x.People, "collection of people");

            demoSchema.AddField("actorPager",
                                new { page = 1, pagesize = 10, search = "" },
                                (db, p) => PaginateActors(db, p),
                                "Pagination. [defaults: page = 1, pagesize = 10]",
                                "PersonPagination");

            // add some mutations (always last, or after the types they require have been added)
            demoSchema.AddMutationFrom(new DemoMutations());
            demoSchema.AddInputType <Detail>("Detail", "Detail item").AddAllFields();
            File.WriteAllText("schema.graphql", demoSchema.GetGraphQLSchema());
            return(demoSchema);
        }