【发布时间】:2022-01-21 17:54:05
【问题描述】:
我可以从远程 graphQL 服务器获取自省结果,我现在想将该结果(JSON 格式)转换为 SDL 格式。 example 是我需要但用 JavaScript 编写的
谢谢
【问题讨论】:
我可以从远程 graphQL 服务器获取自省结果,我现在想将该结果(JSON 格式)转换为 SDL 格式。 example 是我需要但用 JavaScript 编写的
谢谢
【问题讨论】:
一种方法是使用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)
}
}
【讨论】: