Graphql-dotnet: Passing StringGraphType as QueryArgument

Created on 28 May 2018  路  1Comment  路  Source: graphql-dotnet/graphql-dotnet

I know this isn't so much a graphql-dotnet issue as a NewtonSoft issue but may help someone (or I likely am missing something).

I'm trying to deserialize where the queryargument is a string:

var body = "{\"query\":\"{transactions(term:\"hello\") {transactionId}}\"}"
var request = JsonConvert.DeserializeObject<GraphQLQuery>(body);

But NewtonSoft baulks at the 'h' in 'hello':

"After parsing a value an unexpected character was encountered: h. Path 'query', line 1, position 30."

How do you get around this?

Passing IntGraphType as below works fine.

var body = "{\"query\":\"{documents(typeId:1) {documentId, documentName, description, revision}}\"}

Thanks for any/all responses.

question

Most helpful comment

var body = "{\"query\":\"{transactions(term:\"hello\") {transactionId}}\"}"

That is invalid JSON (which is why you get the parse error). To properly escape the " around hello you have to include the escaped \.

var body = "{\"query\":\"{transactions(term:\\\"hello\\\") {transactionId}}\"}"

You can see the required JSON format by reversing the process:

var data = new GraphQLQuery
{
    Query = "{transactions(term:\"hello\") {transactionId}}"
};
var body2 = JsonConvert.SerializeObject(data);

>All comments

var body = "{\"query\":\"{transactions(term:\"hello\") {transactionId}}\"}"

That is invalid JSON (which is why you get the parse error). To properly escape the " around hello you have to include the escaped \.

var body = "{\"query\":\"{transactions(term:\\\"hello\\\") {transactionId}}\"}"

You can see the required JSON format by reversing the process:

var data = new GraphQLQuery
{
    Query = "{transactions(term:\"hello\") {transactionId}}"
};
var body2 = JsonConvert.SerializeObject(data);
Was this page helpful?
0 / 5 - 0 ratings