英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论