Skip to content

Instantly share code, notes, and snippets.

@mykeels
Last active July 8, 2017 22:10
Show Gist options
  • Save mykeels/43035e1eb8689994b610ee658bcc8729 to your computer and use it in GitHub Desktop.
Save mykeels/43035e1eb8689994b610ee658bcc8729 to your computer and use it in GitHub Desktop.
ASP.NET WebAPI Custom Formatter for GraphQL Syntax
using System;
using System.Net.Http.Headers;
using System.Net.Http.Formatting;
using System.IO;
using System.Threading;
using System.Net.Http;
using GraphQL;
using GraphQL.Types;
namespace GraphQLWebAPI.Formatters
{
public class GraphQLFormatter : BufferedMediaTypeFormatter
{
public GraphQLFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/graphql"));
}
public override bool CanReadType(Type type)
{
return type == typeof(string) || type == typeof(ExecutionResult) || type is IObjectGraphType;
}
public override bool CanWriteType(Type type)
{
return type == typeof(string) || type == typeof(ExecutionResult) || type is IObjectGraphType;
}
public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
{
StreamReader reader = new StreamReader(readStream);
string str = reader.ReadToEnd();
return str;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment