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

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

问题描述

我有一个Json对象,我想将其转换为graphql突变查询,以便调用graphql api. 所以我的要求是我需要创建一个springboot项目并公开POST API,该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直接粘贴到GraphQl中GraphQl中相应review字段中变量:

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": "This is a great movie!" }是您的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天全站免登陆