【问题标题】:GraphQL Query syntax errorGraphQL 查询语法错误
【发布时间】:2017-07-27 21:59:40
【问题描述】:

我正在使用这个GraphQl nuget 使用以下代码在我的系统上实现一般查询

        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"});
        }

执行查询的样子

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;
    }
}

还有像这样的主要 GraqhQL 组件

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");

        }
    }

我使用了不同的 graphql 查询字符串,这些字符串不起作用示例

query MediaSearch    
        {

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

我总是出错

"{\"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\"}]}"

知道如何解决这个问题

【问题讨论】:

    标签: c# .net graphql graphql-dotnet


    【解决方案1】:

    这是一个常见的错误,它是使用 GraphQL 的“操作名称”,它更像是一个装饰性的函数名称,作为查询字段。这是一个获取您要求的数据的查询:

    query MyQueryName {
      MediaSearch {
        MediaTitle
        MediaIdentifier
      }
    }
    

    下面是你传递参数的方式——它们在场上进行:

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

    如果您想了解有关 GraphQL 的更多信息,我建议您快速浏览网站上的“学习”部分,包括查询和模式:http://graphql.org/learn/

    请注意,上面的MyQueryName 可以是任何东西,根本不会影响查询的结果。它仅用于服务器端日志记录,以便您可以轻松识别此查询。

    编辑 - 受这个问题的启发,我写了一篇关于查询所有不同部分的博客文章! https://dev-blog.apollodata.com/the-anatomy-of-a-graphql-query-6dffa9e9e747#.lf93twh8x

    【讨论】:

    • 非常感谢,但我尝试查询 MyQueryName {mediaSearch{ mediaTitle mediaIdentifier } } 收到此错误消息 "{\"data\":null,\"errors\":[{\"message\" :\"尝试解析 mediaSearch 时出错。\",\"locations\":[{\"Line\":1,\"Column\":20}]}]}" 任何建议
    • 您的代码中似乎存在运行时错误,但查询有效。
    • 我会查看您的ExecuteQuery 方法的result 中的内部错误。
    猜你喜欢
    • 2019-04-18
    • 2020-09-12
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多