【问题标题】:Convert introspection result to SDL format in Go在 Go 中将自省结果转换为 SDL 格式
【发布时间】:2022-01-21 17:54:05
【问题描述】:

我可以从远程 graphQL 服务器获取自省结果,我现在想将该结果(JSON 格式)转换为 SDL 格式。 example 是我需要但用 JavaScript 编写的

谢谢

【问题讨论】:

    标签: go graphql


    【解决方案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)
        }
    }
    

    【讨论】:

    • 感谢您的回答,它符合预期,但不是我想要的。我仅限于纯 Golang 代码。
    猜你喜欢
    • 2020-11-22
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多