我构建了一个简单的GraphQL API,非常类似于gqlgen的"快速入门“教程。我可以用卷发成功地查询它。但我不能正确的要求变异。
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"`
}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
}我试过:
帮助?
发布于 2019-01-19 21:27:45
JSON有效负载中的query值需要是包含GraphQL查询的字符串,而不是您使用的对象,例如:
$ curl \
-H "Content-Type: application/json" \
-d '{ "query": "mutation { createScreenshot(input: { username: \"Odour\" }) { id } }" }' \
http://localhost:8080/query注意,您需要转义查询字符串中的双引号。
https://stackoverflow.com/questions/54271405
复制相似问题