Last active
July 8, 2017 22:10
-
-
Save mykeels/43035e1eb8689994b610ee658bcc8729 to your computer and use it in GitHub Desktop.
ASP.NET WebAPI Custom Formatter for GraphQL Syntax
This file contains 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
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