对gqlgen GraphQL API的curl POST请求的正确形状是什么?

nus*_*ara 2 curl go graphql

我建立了一个简单的GraphQL API,与gqlgen的“ 入门 ”教程极为相似。我可以使用curl成功查询它。但是我无法正确获取有关突变的curl请求。

schema.graphql:

type Screenshot {
  id: ID!
  url: String!
  filename: String!
  username: String!
  description: String
}

input NewScreenshot {
  id: ID!
  url: String!
  filename: String!
  username: String!
  description: String
}

type Mutation {
  createScreenshot(input: NewScreenshot!): Screenshot!
  deleteScreenshot(id: ID!): String!
}

type Query {
  screenShots(username: String!): [Screenshot!]!
}
Run Code Online (Sandbox Code Playgroud)

models_gen.go:

type NewScreenshot struct {
    ID          string  `json:"id"`
    URL         string  `json:"url"`
    Filename    string  `json:"filename"`
    Username    string  `json:"username"`
    Description *string `json:"description"`
}

type Screenshot struct {
    ID          string  `json:"id"`
    URL         string  `json:"url"`
    Filename    string  `json:"filename"`
    Username    string  `json:"username"`
    Description *string `json:"description"`
}
Run Code Online (Sandbox Code Playgroud)

resolver.go:

func (r *mutationResolver) CreateScreenshot(ctx context.Context, input NewScreenshot) (Screenshot, error) {
    id, err := uuid.NewV4()
    shot := Screenshot{
        ID:          id.String(),
        Description: input.Description,
        URL:         input.URL,
        Filename:    input.Filename,
        Username:    input.Username,
    }

    return shot, nil
}
Run Code Online (Sandbox Code Playgroud)

我试过了:

  • 通过gqlgen去文档中,GraphQL架构如何GraphQL,和几个例子像这个这个。并进行了1.5天的谷歌搜索。

  • 在我的卷曲请求中排列了很多很多不同的形状。这似乎是最接近的:

    curl -v http://localhost:8080/query
    -H "Content-Type: application/json"
    -d '{ "query":
        { "createScreenshot":
            {"username": "Odour",
             "url": "google.com",
             "description": "just another screenshot",
             "filename": "testimage"
            }
        }
    }'
    
    Run Code Online (Sandbox Code Playgroud)

    但是它失败了:

    * timeout on name lookup is not supported
    *   Trying ::1...
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to localhost (::1) port 8080 (#0)
    > POST /query HTTP/1.1
    > Host: localhost:8080
    > User-Agent: curl/7.47.1
    > Accept: */*
    > Content-Type: application/json
    > Content-Length: 146
    >
    } [146 bytes data]
    * upload completely sent off: 146 out of 146 bytes
    < HTTP/1.1 400 Bad Request
    < Date: Sat, 19 Jan 2019 21:00:15 GMT
    < Content-Length: 149
    < Content-Type: text/plain; charset=utf-8
    <
    { [149 bytes data]
    100   295  100   149  100   146    149    146  0:00:01 --:--:--  0:00:01  145k{"errors":[{"message":"json body could not be decoded: json: cannot unmarshal object into Go struct field params.query of type string"}],"data":null}
    * Connection #0 to host localhost left intact
    
    Run Code Online (Sandbox Code Playgroud)

救命?

lma*_*ars 5

queryJSON有效负载中的值必须是包含GraphQL查询的字符串,而不是您正在使用的对象,例如:

$ curl \
  -H "Content-Type: application/json" \
  -d '{ "query": "mutation { createScreenshot(input: { username: \"Odour\" }) { id } }" }' \
  http://localhost:8080/query
Run Code Online (Sandbox Code Playgroud)

请注意,您需要对查询字符串中的双引号进行转义。

  • 输入值按照您指定的方式很好,但如错误所示,您需要从“createScreenshot”的返回值中选择一些字段,这就是我的示例查询的“{ id }”部分所做的事情选择生成的屏幕截图的 ID。所以它应该是这样的:`mutation { createScreenshot(input: { ... }) { id } }` (2认同)
  • @IanS 将查询保存在文本文件(例如 query.graphql)中,然后通过 -d "@query.graphql" 发送 (2认同)