Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql variables serialize as a string and not json #1218

Closed
softprops opened this issue Jan 27, 2023 · 6 comments
Closed

graphql variables serialize as a string and not json #1218

softprops opened this issue Jan 27, 2023 · 6 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@softprops
Copy link

softprops commented Jan 27, 2023

Summary

when hurl constructs a request from a graphql query containing variables it correctly generates a json both with "query" and "variables" keys however the value assigned to "variables" is serialized as a string and not as a json object as most graphql servers expect

POST https://example.org/starwars/graphql
```graphql
query Hero($episode: Episode, $withFriends: Boolean!) {
  hero(episode: $episode) {
    name
    friends @include(if: $withFriends) {
      name
    }
  }
}

variables {
  "episode": "JEDI",
  "withFriends": false
}
```

Steps to reproduce

use the graphql example from the readme and run hurl with the --verbose flag capturing the curl example it generates

curl -H 'Content-Type: application/json' --data '{"query":"query Hero($episode: Episode, $withFriends: Boolean!) {\n  hero(episode: $episode) {\n    name\n    friends @include(if: $withFriends) {\n      name\n    }\n  }\n}","variables":"{\n  \"episode\": \"JEDI\",\n  \"withFriends\": false\n}"}' 'https://example.org/starwars/graphql'

note how "variables" is a string and not json "variables":"{\n \"episode\": \"JEDI\",\n \"withFriends\": false\n}"

Example Project

What is the current bug behavior?

"variables" objects in json encoded requests is a string

What is the expected correct behavior?

most servers will expect "variables" as a json object something more like this "variables":{ "episode": "JEDI", "withFriends": false}

this is also what is described by graphql.org https://graphql.org/learn/serving-over-http/#post-request

Relevant logs and/or screenshots

Output of checks

  • Version (hurl --version):
hurl 2.0.0 libcurl/7.77.0 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.42.0
Features (libcurl):  alt-svc AsynchDNS HSTS HTTP2 IPv6 Largefile libz NTLM NTLM_WB SPNEGO SSL UnixSockets
Features (built-in): brotli
  • Operating system and version:

osx

Possible fixes

@softprops softprops added the bug Something isn't working label Jan 27, 2023
@jcamiel
Copy link
Collaborator

jcamiel commented Jan 27, 2023

Good catch and thanks for the detailed issue. We will try to do a quick patch asap.

@softprops
Copy link
Author

softprops commented Jan 27, 2023

@jcamiel thanks and btw, this tool is awesome. My company is going to be looking to invest in usage of this tool for our e2e testing story. So far this looks perfect.

I'm not technically blocked on this issue as I'm able to gracefully degrade back to json requests

GET http://localhost:8080
ApolloGraphql-Client-Name: hurl-test
[Options]
retry: true
{
  "query": "query testMember($id: ID!) { member(id: $id) { name } }",
  "variables": { "id": "{{member_id}}" },
  "operationName": "testMember"
}

HTTP/1.1 200
[Asserts]
jsonpath "$.data.member" exists
duration < 200

but having the new native gql support will make for a much nicer authoring experience for our engineers

@jcamiel jcamiel self-assigned this Jan 29, 2023
@jcamiel jcamiel linked a pull request Jan 29, 2023 that will close this issue
@jcamiel
Copy link
Collaborator

jcamiel commented Jan 29, 2023

@softprops would you be kind to test my fix on this branch https://github.com/Orange-OpenSource/hurl/tree/1218-graphql-variables-serialize-as-a-string-and-not-json master? You'll need to build Hurl (see https://hurl.dev/docs/installation.html#building-from-sources)
No obligation of course, only if you can and have some time! I'm not a GraphQL expert, I want to be sure that variables are ok this time.

@jcamiel jcamiel closed this as completed Jan 30, 2023
@softprops
Copy link
Author

@jcamiel worked like a charm!

POST http://localhost:8080
ApolloGraphql-Client-Name: hurl-test
[Options]
retry: true
```graphql
query testMember($id: ID!) {
  member(id: $id) {
    name
  }
}

variables {
  "id": "{{member_id}}"
}
```
path/to/hurl/target/release/hurl --verbose	 --test --glob "src/e2e-test/**/*.hurl" --variable member_id=xxx

<snip>
*
* Request can be run with the following curl command:
* curl -H 'ApolloGraphql-Client-Name: hurl-test' -H 'Content-Type: application/json' --data '{"query":"query testMember($id: ID!) {\n  member(id: $id) {\n    name\n  }\n}","variables":{"id":"xxx"}}' 'http://localhost:8080'
<snip>

@softprops
Copy link
Author

I'll keep an eye out for the next release. Thanks!

@jcamiel jcamiel added this to the 2.0.1 milestone Feb 1, 2023
@jcamiel
Copy link
Collaborator

jcamiel commented Feb 2, 2023

@softprops We've released a 2.0.1, should be available on the various packet managers soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants