Skip to content

Instantly share code, notes, and snippets.

@mczachurski
mczachurski / graphql-status-codes.json
Last active October 10, 2017 07:52
GraphQL - JSON with status codes
{
"data": {
"project": {
"statusCode": "Success",
"errorMessage": null,
"data": {
"id": "projectA",
"name": "Project A",
"visibleBranches": [
"Latest"
{
"data": {
"project": {
"statusCode": "NotFound",
"errorMessage": "Resource not found",
"data": null
},
"projects": {
"statusCode": "Success",
"errorMessage": null,
public class ResponseGraphType<TGraphType> : ObjectGraphType<Response> where TGraphType : GraphType
{
public ResponseGraphType()
{
Name = $"Response{typeof(TGraphType).Name}";
Field(x => x.StatusCode, nullable: true).Description("Status code of the request.");
Field(x => x.ErrorMessage, nullable: true).Description("Error message if requests fails.");
Field<TGraphType>(
public class Response
{
public object Data { get; set; }
public string StatusCode { get; set; }
public string ErrorMessage { get; set; }
public Response(object data)
{
StatusCode = "Success";
Data = data;
public class BranchesResolver : IBranchesResolver
{
private readonly IBranchesService _branchesService;
public BranchesResolver(IBranchesService branchesService)
{
_branchesService = branchesService;
}
public void Resolve(GraphQLQuery graphQLQuery)
public class Resolver
{
public Response Response(object data)
{
return new Response(data);
}
public Response Error(GraphQLError error)
{
return new Response(error.StatusCode, error.ErrorMessage);
public class BranchesResolver : Resolver, IBranchesResolver
{
private readonly IBranchesService _branchesService;
public BranchesResolver(IBranchesService branchesService)
{
_branchesService = branchesService;
}
public void Resolve(GraphQLQuery graphQLQuery)
public class ResponseListGraphType<TGraphType> : ObjectGraphType<Response> where TGraphType : GraphType
{
public ResponseListGraphType()
{
Name = $"ResponseList{typeof(TGraphType).Name}";
Field(x => x.StatusCode, nullable: true).Description("Status code of the request.");
Field(x => x.ErrorMessage, nullable: true).Description("Error message if requests fails.");
Field<ListGraphType<TGraphType>>(
public class BranchesResolver : Resolver, IBranchesResolver
{
private readonly IBranchesService _branchesService;
public BranchesResolver(IBranchesService branchesService)
{
_branchesService = branchesService;
}
public void Resolve(GraphQLQuery graphQLQuery)
public class GraphQLParameter
{
public string Query { get; set; }
}