GraphQL Mutations در ASP.NET Core ( عملیات POST, PUT, DELETE )
نویسنده: ابوالفضل روشن ضمیر
تاریخ: ۱۳۹۸/۰۴/۲۱ ۲۱:۴۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class OwnerInputType : InputObjectGraphType
{
public OwnerInputType()
{
Name = "ownerInput";
Field<NonNullGraphType<StringGraphType>>("name");
Field<NonNullGraphType<StringGraphType>>("address");
}
} public class AppMutation : ObjectGraphType
{
public AppMutation()
{
}
} public class AppSchema : Schema
{
public AppSchema(IDependencyResolver resolver)
:base(resolver)
{
Query = resolver.Resolve<AppQuery>();
Mutation = resolver.Resolve<AppMutation>();
}
} public interface IOwnerRepository
{
...
Owner CreateOwner(Owner owner);
}
public class OwnerRepository : IOwnerRepository
{
...
public Owner CreateOwner(Owner owner)
{
owner.Id = Guid.NewGuid();
_context.Add(owner);
_context.SaveChanges();
return owner;
}
} public class AppMutation : ObjectGraphType
{
// Add
public AppMutation(IOwnerRepository repository)
{
Field<OwnerType>(
"createOwner",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<OwnerInputType>> { Name = "owner" }),
resolve: context =>
{
var owner = context.GetArgument<Owner>("owner");
return repository.CreateOwner(owner);
}
);
}
} mutation($owner:ownerInput!){
createOwner(owner:$owner){
id,
name,
address
}
} {
"owner":{
"name":"Abolfazl-Roshanzamir",
"address":"Address - User 4"
}
}
public interface IOwnerRepository
{
...
Owner UpdateOwner(Owner dbOwner, Owner owner);
}
public class OwnerRepository : IOwnerRepository
{
...
public Owner UpdateOwner(Owner dbOwner, Owner owner)
{
dbOwner.Name = owner.Name;
dbOwner.Address = owner.Address;
_context.SaveChanges();
return dbOwner;
}
} public class AppMutation : ObjectGraphType
{
public AppMutation(IOwnerRepository repository)
{
...
// Update
Field<OwnerType>(
"updateOwner",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<OwnerInputType>> { Name = "owner" },
new QueryArgument<NonNullGraphType<IdGraphType>> { Name = "ownerId" }),
resolve: context =>
{
var owner = context.GetArgument<Owner>("owner");
var ownerId = context.GetArgument<Guid>("ownerId");
var dbOwner = repository.GetById(ownerId);
if (dbOwner == null)
{
context.Errors.Add(new ExecutionError("Couldn't find owner in db."));
return null;
}
return repository.UpdateOwner(dbOwner, owner);
}
);
}
} mutation($owner:ownerInput!,$ownerId:ID!){
updateOwner(owner:$owner,ownerId:$ownerId){
id,
name,
address
}
} {
"owner":{
"name":"Andy Madaidan",
"address":"Address - User 1"
},
"ownerId": "53270061-3ba1-4aa6-b937-1f6bc57d04d2"
} public interface IOwnerRepository
{
...
void DeleteOwner(Owner owner);
}
public class OwnerRepository : IOwnerRepository
{
...
public void DeleteOwner(Owner owner)
{
_context.Remove(owner);
_context.SaveChanges();
}
} public class AppMutation : ObjectGraphType
{
public AppMutation(IOwnerRepository repository)
{
...
//Delete
Field<StringGraphType>(
"deleteOwner",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<IdGraphType>> { Name = "ownerId" }),
resolve: context =>
{
var ownerId = context.GetArgument<Guid>("ownerId");
var owner = repository.GetById(ownerId);
if (owner == null)
{
context.Errors.Add(new ExecutionError("Couldn't find owner in db."));
return null;
}
repository.DeleteOwner(owner);
return $"The owner with the id: {ownerId} has been successfully deleted from db.";
}
);
}
} mutation($ownerId:ID!){
deleteOwner(ownerId:$ownerId)
} {
"ownerId": "6f513773-be46-4001-8adc-2e7f17d52d83"
}