public ContactType(Data.Context.AppDbContext appDbContext) { Field(p => p.Id); Field(p => p.FirstName); Field(p => p.LastName); Field(p => p.PhoneNumber); Field(p => p.Email); Field <ContactTypeEnumType>("ContactType"); Field(p => p.WebAddress); Field(p => p.Notes); Field <IntGraphType>( name: "CountOfAddress", resolve: context => { return(appDbContext.Addresses.Where(p => p.Contact.Id == context.Source.Id & p.IsDeleted == false).Count()); }); Field <ListGraphType <AddressType> >( name: "Addresses", resolve: context => { return(appDbContext.Addresses.Where(p => p.Contact.Id == context.Source.Id & p.IsDeleted == false).ToList()); }); Field(p => p.CreateDateTime); Field(p => p.UpdateDateTime); }
public RootQuery(Data.Context.AppDbContext appDbContext) { Name = nameof(RootQuery); Description = "Root Query"; Field <ListGraphType <Type.ContactType> >( name: "contacts", description: "Get all contacts", resolve: context => { return(appDbContext.Contacts.Where(p => p.IsDeleted == false).OrderBy(p => p.FirstName).ToList()); }); Field <Type.ContactType>( name: "contact", description: "Get contact by contactId", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "id" } ), resolve: context => { var id = context.GetArgument <int>("id"); var foundContact = appDbContext.Contacts.FirstOrDefault(p => p.Id == id & p.IsDeleted == false); if (foundContact == null) { context.Errors.Add(new GraphQL.ExecutionError("Contact not found.")); return(null); } return(foundContact); }); Field <Type.AddressType>( name: "address", description: "Get address by addressId", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "id" } ), resolve: context => { var id = context.GetArgument <int>("id"); var foundAddress = appDbContext.Addresses.FirstOrDefault(p => p.Id == id & p.IsDeleted == false); if (foundAddress == null) { context.Errors.Add(new GraphQL.ExecutionError("Address not found.")); return(null); } return(foundAddress); }); }
public RootMutation(Data.Context.AppDbContext appDbContext) { Name = nameof(RootMutation); Description = "Root Mutation"; Field <Query.Type.ContactType>( name: "addContact", description: "Add new contact", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <Type.ContactInputType> > { Name = "contact" } ), resolve: context => { var contact = context.GetArgument <Data.Models.Contact>("contact"); try { var foundContact = appDbContext.Contacts.Where(p => p.Email == contact.Email).FirstOrDefault(); if (foundContact != null) { throw new Exception("This email has been used by another contact."); } appDbContext.Contacts.Add(contact); appDbContext.SaveChanges(); return(contact); } catch (Exception ex) { context.Errors.Add(new GraphQL.ExecutionError(ex.Message, ex)); return(null); } }); Field <Query.Type.ContactType>( name: "updateContact", description: "Update contact specified by contactId", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "contactId" }, new QueryArgument <NonNullGraphType <Type.ContactInputType> > { Name = "contact" } ), resolve: context => { var contactId = context.GetArgument <int>("contactId"); var contact = context.GetArgument <Data.Models.Contact>("contact"); try { var foundContact = appDbContext.Contacts.Where(p => p.Id == contactId && p.IsDeleted == false).FirstOrDefault(); if (foundContact == null) { throw new Exception("Contact not found."); } foundContact.FirstName = contact.FirstName; foundContact.LastName = contact.LastName; foundContact.PhoneNumber = contact.PhoneNumber; foundContact.Email = contact.Email; foundContact.ContactType = contact.ContactType; foundContact.WebAddress = contact.WebAddress; foundContact.Notes = contact.Notes; foundContact.UpdateDateTime = DateTime.Now; appDbContext.SaveChanges(); return(foundContact); } catch (Exception ex) { context.Errors.Add(new GraphQL.ExecutionError(ex.Message, ex)); return(null); } }); Field <BooleanGraphType>( name: "deleteContact", description: "Delete contact specified by contactId", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "contactId" } ), resolve: context => { var contactId = context.GetArgument <int>("contactId"); try { var foundContact = appDbContext.Contacts.Where(p => p.Id == contactId && p.IsDeleted == false).FirstOrDefault(); if (foundContact == null) { throw new Exception("Contact not found."); } foundContact.IsDeleted = true; //appDbContext.Contacts.Remove(foundContact); appDbContext.SaveChanges(); return(true); } catch (Exception ex) { context.Errors.Add(new GraphQL.ExecutionError(ex.Message, ex)); return(false); } }); Field <Query.Type.AddressType>( name: "addAddress", description: "Add new address", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "contactId" }, new QueryArgument <NonNullGraphType <Type.AddressInputType> > { Name = "address" } ), resolve: context => { var address = context.GetArgument <Data.Models.Address>("address"); var contactId = context.GetArgument <int>("contactId"); try { var foundContact = appDbContext.Contacts.Where(p => p.Id == contactId & p.IsDeleted == false).FirstOrDefault(); address.Contact = foundContact ?? throw new Exception("Contact not found."); appDbContext.Addresses.Add(address); appDbContext.SaveChanges(); return(address); } catch (Exception ex) { context.Errors.Add(new GraphQL.ExecutionError(ex.Message, ex)); return(null); } }); Field <Query.Type.AddressType>( name: "updateAddress", description: "Update address specified by addressId", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "addressId" }, new QueryArgument <NonNullGraphType <Type.AddressInputType> > { Name = "address" } ), resolve: context => { var addressId = context.GetArgument <int>("addressId"); var address = context.GetArgument <Data.Models.Address>("address"); try { var foundAddress = appDbContext.Addresses.Where(p => p.Id == addressId && p.IsDeleted == false).FirstOrDefault(); if (foundAddress == null) { throw new Exception("Address not found."); } foundAddress.Name = address.Name; foundAddress.Country = address.Country; foundAddress.City = address.City; foundAddress.PostalCode = address.PostalCode; foundAddress.AddressLine = address.AddressLine; foundAddress.UpdateDateTime = DateTime.Now; appDbContext.SaveChanges(); return(foundAddress); } catch (Exception ex) { context.Errors.Add(new GraphQL.ExecutionError(ex.Message, ex)); return(null); } }); Field <BooleanGraphType>( name: "deleteAddress", description: "Delete address specified by addressId", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "addressId" } ), resolve: context => { var addressId = context.GetArgument <int>("addressId"); try { var foundAddress = appDbContext.Addresses.Where(p => p.Id == addressId & p.IsDeleted == false).FirstOrDefault(); if (foundAddress == null) { throw new Exception("Address not found."); } foundAddress.IsDeleted = true; //appDbContext.Addresses.Remove(foundAddress); appDbContext.SaveChanges(); return(true); } catch (Exception ex) { context.Errors.Add(new GraphQL.ExecutionError(ex.Message, ex)); return(false); } }); }