AMH
AMH

Reputation: 6451

GraphQL Query syntax error

am using this GraphQl nuget to achieve a general QUery at my system using the following code

        if (!string.IsNullOrEmpty(query))
        {
            var urlDecode = HttpUtility.UrlDecode(query);

                var result =
                    await (_searchRepository.ExecuteQuery(urlDecode.Replace("\t", "").Replace(@"\", string.Empty))).ConfigureAwait(false);
                response = Request.CreateResponse(HttpStatusCode.OK,
                    new GeneralOutput() {HasError = false, Data = result});

        }
        else
        {
            response = Request.CreateResponse(HttpStatusCode.OK,
                    new GeneralOutput() { HasError = true, Data = null , ErrorType = ErrorType.ValidationError , Message = "Empty Query String"});
        }

where Execcute query looks like

public async Task<string> ExecuteQuery(string querystring)
{
    try
    {
        var result = await new DocumentExecuter().ExecuteAsync(_ =>
        {
            _.Schema = new Schema { Query = new ViewerType() };
            _.Query = querystring;
        }).ConfigureAwait(false);

        var json = new DocumentWriter(Formatting.None, null).Write(result);
        return json;
    }
    catch (Exception e)
    {
        var logInstance = LogManager.GetCurrentClassLogger();
        logInstance?.Error(e,
            $" {MethodBase.GetCurrentMethod().Name} => {e.Message}{Environment.NewLine}{e.StackTrace}");
        return null;
    }
}

And main GraqhQL components like this

public class ViewerType : ObjectGraphType
{
    public ViewerType()
    {
        Name = "Viewer";
        Field<QueryResult>("MediaSearch",
            arguments: new QueryArguments(
                new QueryArgument<StringGraphType> { Name = "userTwelveDigits", DefaultValue = null},
                new QueryArgument<DateGraphType> { Name = "fromcreationDate", DefaultValue = null },
                new QueryArgument<DateGraphType> { Name = "tocreationDate", DefaultValue = null } ,
                new QueryArgument<StringGraphType> { Name = "mediaId", DefaultValue = null },
                new QueryArgument<IntGraphType> { Name = "versionTypeId", DefaultValue = null },
                new QueryArgument<StringGraphType> { Name = "keyword", DefaultValue = null }

                ),
            resolve: context => (new BaseService<MediaQuery_Result>()).Context.MediaQuery(
                context.GetArgument<string>("userTwelveDigits"),
                context.GetArgument<DateTime?>("fromcreationDate", null),
                context.GetArgument<DateTime?>("tocreationDate", null),
                context.GetArgument<string>("mediaId"),
                context.GetArgument<int?>("versionTypeId", null),
                context.GetArgument<string>("keyword")
            ));
    }

}


  public class QueryResult :  ObjectGraphType<MediaQuery_Result>
    {
        public QueryResult()
        {
            Field(m => m.MediaId).Description("The Media Id");
            Field(m => m.MediaTitle).Description("The Media Title");
            Field(m => m.MediaIdentifier).Description("The Media Identifier");
            Field(m => m.MP4Path).Description("The Media mp4 Path");
            Field(m => m.DataS3Path).Description("The Media S3 Path");
            Field(m => m.InSync.Value).Description("The Media In Sync or not").Name("InSync");
            Field(m => m.IsLinked.Value).Description("The media (video) if linked before or not ").Name("IsLinked");
            Field(m => m.IsPublished.Value).Description("The Media If is published or not ").Name("IsPublished");

        }
    }

I used different graphql query strings which doesn't work example

query MediaSearch    
        {

            MediaTitle
            MediaIdentifier
        }
query MediaSearch(userTwelveDigits:"12345678",fromcreationDate:null,tocreationDate:null,mediaId:null,versionTypeId:null,keyword:null)    {  MediaTitle  MediaIdentifier       }

I always get error

"{\"errors\":[{\"message\":\"Syntax Error GraphQL (1:19) Expected $, found Name \\\"userTwelveDigits\\\"\\n1: query MediaSearch(userTwelveDigits:\\\"12345678\\\",fromcreationDate:null,tocreationDate:null,mediaId:null,versionTypeId:null,keyword:null)  { MediaTitle MediaIdentifier   }\\n                     ^\\n\"}]}"

Any idea how to fix that

Upvotes: 1

Views: 4314

Answers (1)

stubailo
stubailo

Reputation: 6147

This is a common error, which is using the "Operation name" of GraphQL, which is more like a cosmetic function name, as a query field. Here's a query that gets the data you are asking for:

query MyQueryName {
  MediaSearch {
    MediaTitle
    MediaIdentifier
  }
}

And here is how you pass in arguments - they go on the field:

query MyQueryName {
  MediaSearch(mediaId: "asdf") {
    MediaTitle
    MediaIdentifier
  }
}

If you want to learn more about GraphQL, I recommend quickly going through the "learn" section on the website, both about queries and schemas: http://graphql.org/learn/

Note that MyQueryName above can be anything, and doesn't affect the result of the query at all. It's just for server-side logging, so that you can easily identify this query.

Edit - I've written up a blog post about all the different parts of a query, inspired by this question! https://dev-blog.apollodata.com/the-anatomy-of-a-graphql-query-6dffa9e9e747#.lf93twh8x

Upvotes: 2

Related Questions