Json 到 Graphql 的转换 [英] Json to Graphql conversion

查看:21
本文介绍了Json 到 Graphql 的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Json 对象,我想将它转换为 graphql 突变查询以便调用 graphql api.所以我的要求是我需要创建一个 springboot 项目并公开一个 POST API,它将输入作为 json 并将其转换为 graphql 突变查询格式,然后我需要使用带有突变查询的 graphql api.任何人都可以提供任何指向它的指针,因为我无法继续.

I have a Json object and I want to convert it graphql mutation query inorder to invoke a graphql api. So my requirement is I need to create a springboot project and expose a POST API which will take input as a json and convert it into graphql mutation query format, then I need to consume a graphql api with the mutation query. Can any one please provide any pointer to it as I am not able to proceed.

推荐答案

将您的对象作为变量传递.您可以将其以 JSON 格式放入 Graphql 变量定义中.

Pass your object as a variable. You are able to put it in JSON format in the Graphql variables definitions.

如果你看一下关于突变的官方教程,在查询中,他们通过ReviewInput 作为变量,$review:

If you look at the official tutorial on mutations, in the query, they pass the ReviewInput as a variable, $review:

mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
  createReview(episode: $ep, review: $review) {
    stars
    commentary
  }
}

由于 ReviewInput 至少在我的用例中是模型中的标准对象,用于 REST 和 GraphQl,因此可以将 JSON 直接粘贴到相应 review GraphQl 变量中的字段:

As the ReviewInput is, at least in my use case, a standard object from the model, used for both REST and GraphQl, the JSON can be directly pasted in the GraphQl in the corresponding review field in the GraphQl variables:

{
  "ep": "JEDI",
  "review": {
    "stars": 5,
    "commentary": "This is a great movie!"
  }
}

{ "stars": 5, "commentary": "这是一部很棒的电影!"} 是你的 JSON.

所以,简而言之,不需要转换,您只需要您的 API 将 JSON 放入正确的变异模板中即可.

So, in short, no conversion is required, you just need your API to put the JSON in the correct mutation template.

这篇关于Json 到 Graphql 的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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