GraphQL片段JSON格式 [英] GraphQL fragment JSON format

查看:95
本文介绍了GraphQL片段JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用其v4(GraphQL)API从GitHub读取一些数据.我编写了一个Java客户端,该客户端可以正常工作,直到我开始用GraphQL片段替换某些查询为止.

我最初使用GraphiQL来测试我的查询,并且在其中添加片段非常简单.但是,当转换为JSON时,我还没有弄清楚正确的格式.我尝试过:

{"query":"{... body_of_query ...} Blob上的片段碎片名{byteSize text}"}

{"query":"{... body_of_query ...},Blob上的片段碎片名{byteSize text}"}

{"query":"{... body_of_query ...}","fragment":"{Blob上的fragname {byteSize text}}"}

添加@Scriptonomy:

  {询问 {搜索(第一个:3,键入:REPOSITORY,查询:\"language:HCL \"){边缘{节点{...在存储库上{名称descriptionHTML对象(表达式:\"master:\"){...在树上{...递归树}}}}光标}pageInfo {endCursorhasNextPage}}}树上的片段recurseTree {条目{名称类型}} 

我很确定 ,继续对此进行随机变化是非常有趣的,而今天早晨,我在搜索各种GraphQL文档和有关片段的博客方面感到非常有趣,甚至我实际上还可以猜出了正确的答案,但parens不匹配(我只是使用硬编码的JSON,直到我知道格式为止-也许不是最明智的选择).

我希望有人可能知道正确的格式并将我设置在正确的路线上,然后再避免使用GraphQL-doc过度曝光.

解决方案

在与查询本身相同的JSON正文属性中发送片段.您可以在此处中看到使用片段的示例./p>

有效的GraphQL请求通常是将查询编码为URL查询参数的GET请求,或者是带有JSON正文的POST请求.JSON正文具有一个必需的键 query 和一个可选字段 variables .在您的情况下,JSON需要看起来像这样:

  {"query":"{\ n查询{\ n搜索(第一个:3,键入:REPOSITORY,查询:\" language:HCL \)){\ n边沿{\ n节点{\ n ...在存储库{\n名称\ n描述HTML \ n对象(表达式:\"master:\"){\ n ...在树上{\ n ... recurseTree \ n} \ n} \ n} \ n} \ n光标\ n} \ n pageInfo {\ n endCursor \ n hasNextPage \ n} \ n} \ n} \ n \ n片段recurseTree树上的树{\ n条目{\ n名称\ n类型\ n} \ n}} 

这是您问题中逐字查询字符串的 JSON.stringify 版本.

我建议您从连接到GitHub GraphQL API的GraphiQL实例运行查询,并调查网络请求.您可以将GraphQL请求复制为cuRL,以查看JSON主体的外观.

如果您仍然获得400,请共享一些代码,因为这意味着您的请求格式错误,因此它可能一开始就不会命中GraphQL解析器.

I'm attempting to read some data out of GitHub with their v4 (GraphQL) API. I've written a Java client that is working fine up until I start replacing some of the query with GraphQL fragments.

I was using GraphiQL to initially test my queries, and adding fragments was pretty simple in there. However, when translating to JSON, I haven't figured out the correct format. I've tried:

{ "query": "{ ... body_of_query ... } fragment fragname on Blob { byteSize text }" }

{ "query": "{ ... body_of_query ... }, fragment fragname on Blob { byteSize text }" }

{ "query": "{ ... body_of_query ... }", "fragment": "{fragname on Blob { byteSize text } }" }

EDIT: Adding for @Scriptonomy:

{
 query {
   search(first:3, type: REPOSITORY, query: \"language:HCL\") {
     edges {
      node {
        ... on Repository {
          name
          descriptionHTML
          object(expression: \"master:\") {
            ... on Tree {
              ...recurseTree
            }
          }
        }
      }
      cursor
    }
    pageInfo {
      endCursor
      hasNextPage
    }
  }
}

fragment recurseTree on Tree {
  entries {
    name
    type
  }
}

I'm sure it would be fun and all to keep throwing random variations on this, and my morning has been huge fun searching various GraphQL docs and blogs on fragments, and I may have even actually guessed the correct answer but had mismatched parens (I'm just using hardcoded JSON until I know the format -- perhaps not the wisest choice looking back on it).

I'm hoping that someone may know the correct format and set me on the correct course before I keel over from GraphQL-doc over-exposure.

解决方案

Fragments are sent in the same property of the JSON body as the query itself. You can see an example for using fragments here.

A valid GraphQL request is usually either a GET request that encodes the query as URL query parameter, or a POST request with a JSON body. The JSON body has one required key, query and one optional field, variables. In your case, the JSON needs to look like this:

{
  "query": "{\n query {\n   search(first:3, type: REPOSITORY, query: \"language:HCL\") {\n     edges {\n      node {\n        ... on Repository {\n          name\n          descriptionHTML\n          object(expression: \"master:\") {\n            ... on Tree {\n              ...recurseTree\n            }\n          }\n        }\n      }\n      cursor\n    }\n    pageInfo {\n      endCursor\n      hasNextPage\n    }\n  }\n}\n\nfragment recurseTree on Tree {\n  entries {\n    name\n    type\n  }\n}"
}

That is the JSON.stringify version of the verbatim query string in your question.

I recommend you to run queries from a GraphiQL instance connected to your GitHub GraphQL API and look into the network request. You can copy the GraphQL request as cuRL to see how the JSON body needs to look like.

If you still obtain a 400, please share some code, because that means your request was malformed, so it probably never hit the GraphQL parser in the first place.

这篇关于GraphQL片段JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆