Created
August 2, 2019 08:57
-
-
Save ngohungphuc/4b53ec99c45cff82da0325626414859a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Query : ObjectGraphType | |
{ | |
public Query(Repository repository) | |
{ | |
Field<ListGraphType<PostType>>("posts", | |
arguments: new QueryArguments(new List<QueryArgument> | |
{ | |
new QueryArgument<IdGraphType> | |
{ | |
Name = "id" | |
}, | |
new QueryArgument<StringGraphType> | |
{ | |
Name = "title" | |
}, | |
new QueryArgument<StringGraphType> | |
{ | |
Name = "body" | |
}, | |
new QueryArgument<IdGraphType> | |
{ | |
Name = "userId" | |
}, | |
new QueryArgument<ListGraphType<CommentType>> | |
{ | |
Name = "comment" | |
} | |
}), | |
resolve: context => | |
{ | |
var query = repository.GetPostQuery(); | |
var postId = context.GetArgument<int?>("id"); | |
if (postId.HasValue) | |
{ | |
return query.Where(r => r.Id == postId.Value); | |
} | |
var title = context.GetArgument<string>("title"); | |
if (!string.IsNullOrEmpty(title)) | |
{ | |
return query.Where(r => r.Title == title); | |
} | |
var body = context.GetArgument<string>("body"); | |
if (!string.IsNullOrEmpty(body)) | |
{ | |
return query.Where(r => r.Body == body); | |
} | |
var userId = context.GetArgument<int?>("userId"); | |
if (userId.HasValue) | |
{ | |
return query.Where(r => r.UserId == userId); | |
} | |
return query.ToList(); | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment