将Go中的内省结果转换为SDL格式

huangapple go评论92阅读模式
英文:

Convert introspection result to SDL format in Go

问题

我可以从远程的 GraphQL 服务器获取内省结果,现在我想将这个结果(JSON 格式)转换为 SDL 格式。
这是一个示例(链接:https://www.apollographql.com/blog/backend/schema-design/three-ways-to-represent-your-graphql-schema/),展示了我需要的内容,但是是用 JavaScript 编写的。

谢谢。

英文:

I can fetch the result of introspection from remote graphQL server, I want now to transform that result (JSON format) to SDL format.
An example of what I need but written in JavaScript

thanks

答案1

得分: 1

一种方法是使用https://github.com/CDThomas/graphql-json-to-sdl。

由于它是一个Node.js应用程序,你可以设置、安装等,然后像这样从Go中运行它:

import (
	"bytes"
	"fmt"
	"log"
	"os/exec"
)

func main() {
	var stdout, stderr bytes.Buffer
	jsonFilePath := "./schema.json"
	outputFilePath := "./schema.graphql"
	comm := fmt.Sprintf("graphql-json-to-sdl %s %s", jsonFilePath, outputFilePath)
	sourcePath := "~/.bashrc"
	cmd := exec.Command("/bin/sh", "-c", "source "+sourcePath+" ; "+comm)
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
	fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
	if err != nil {
		log.Fatalf("cmd.Run() failed with %s\n", err)
	}
}
英文:

One way to do it is with https://github.com/CDThomas/graphql-json-to-sdl

Since it's a node JS application, you could set it up, install it, etc then run it from go like this

import (
	"bytes"
	"fmt"
	"log"
	"os/exec"
)

func main() {
	var stdout, stderr bytes.Buffer
	jsonFilePath := "./schema.json"
	outputFilePath := "./schema.graphql"
	comm := fmt.Sprintf("graphql-json-to-sdl %s %s", jsonFilePath, outputFilePath)
	sourcePath := "~/.bashrc"
	cmd := exec.Command("/bin/sh", "-c", "source "+sourcePath+" ; "+comm)
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
	fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
	if err != nil {
		log.Fatalf("cmd.Run() failed with %s\n", err)
	}
}

huangapple
  • 本文由 发表于 2021年12月20日 08:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/70416321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定