5

I built a simple GraphQL API extremely similar to gqlgen's "Getting Started" tutorial. I can query it successfully with curl. But I can't get the curl request for mutation right.

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
}

I've tried:

  • Going through the gqlgen documentation, the GraphQL schema, How to GraphQL, and several examples like this and this. And 1.5 days' worth of googling.

  • Permutating through a lot, a lot of different shapes in my curl request. This one seems the closest:

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

    But it fails with:

    * 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
    

Help?

1 Answer 1

15

The value of query in the JSON payload needs to be a string containing the GraphQL query, not an object like you are using, for example:

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

Note that you need to escape the double quotes within the query string.

6
  • 1
    The payload is JSON, so why would you need to urlencode the double quotes?
    – lmars
    Jan 19, 2019 at 22:04
  • @Imars Following your suggestion, I passed this in: "mutation { createScreenshot(input: { id:\"12345\" username: \"Odour\" url:\"google.com\" description:\"a screenshot\" filename:\"testname\" }) }" But I got this: {"errors":[{"message":"Field \"createScreenshot\" of type \"Screenshot!\" must have a selection of subfields. Did you mean \"createScreenshot { ... }\"?","locations":[{"line":1,"column":12}]}],"data":null}. Also tried commas in between them, and then each field in its own braces. Still wrong. How do I pass in multiple fields?
    – nusantara
    Jan 19, 2019 at 22:13
  • 2
    The input values are fine the way you are specifying them, but as indicated by the error, you need to select some fields on the return value from createScreenshot which is what the { id } part of my example query is doing to select the ID of the resulting Screenshot. So it should be something like: mutation { createScreenshot(input: { ... }) { id } }
    – lmars
    Jan 19, 2019 at 22:44
  • Oh, right! I thought that was optional. Thank you so much! @Imars
    – nusantara
    Jan 19, 2019 at 22:46
  • 2
    @IanS Save the query in a text file (query.graphql, say) and then send it via -d "@query.graphql" Jan 7, 2022 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.