gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:25:43 25 4
gpt4 key购买 nike

我构建了一个简单的 GraphQL API,与 gqlgen 的“Getting Started”教程极为相似。我可以用 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!]!
}

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"`
}

解析器.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
}

我试过:

  • 通过 gqlgen documentation , GraphQL schema , How to GraphQL ,还有几个例子,比如 thisthis .以及 1.5 天的谷歌搜索。

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

    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"
    }
    }
    }'

    但它失败了:

    * 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

帮忙吗?

最佳答案

JSON 负载中 query 的值需要是包含 GraphQL 查询的字符串,而不是像您正在使用的对象,例如:

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

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

关于go - 对 gqlgen GraphQL API 的 curl POST 请求的正确形状是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54271405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com