Comments (4)
Does this work:
First, do this https://graphql.org/graphql-js/running-an-express-graphql-server/
Then:
library(crul)
con <- HttpClient$new("http://localhost:4000/graphql", headers=list(`Content-Type`="application/json"))
res <- con$post(body = '{"query": "{hello}"}', encode="json")
res$parse("UTF-8")
#> [1] "{\"data\":{\"hello\":\"Hello world!\"}}"
from crul.
I've tried some simple queries on the server, and if a token is provided in the header, they all work, e.g.
client <- HttpClient$new(link, headers=list(`Content-Type`="application/json"))
client$post(body = "{\"query\": \"{ newsArticles { totalCount } }\"}", encode = "json")
gives me a 200 even without authorization in the header, the correct data when authorized.
While testing out some queries, I found out that strings in graphQL need somewhat of a different escape to not throw a'bad request' error, e.g. here (note the triple \ ):
client$post(body = "{\"query\": \"{ newsArticle(id: \\\"101\\\") { id } }\"}", encode = "json")
However, this does not resolve the issue I'm having with the mutation. I've also tried a somewhat simplified call (even though the above curl call works in cmd), resembling the graphQL muation more closely.
The graphQL mutation is, in graphQL syntax:
mutation MyMutation {
login(input: {email: "email", password: "password"}) {
token
}
}
which I made into:
client$post(body = "{\"mutation\": \"{ login(input: { email: \\\"email\\\", password: \\\"password\\\" }) { token } }\" }", encode = "json")
This gives me the same error ('400 Bad Request') as the above queries, no matter how I escape the strings "password" and "email". I've also tried the longer query with specially escaped characters, encoded as json, but with the same results.
Since standard queries work, I'm thinking it might have to do something with the mutation, but I'm not sure what the issue is here.
from crul.
I've figured out my mistake. GraphQL seems to expect that a query is called as a mutation only if specifid within the query, while http is expecting the query field. Standard queries do not require this specification, however. Hence
client$post(body = "{\"query\": \"mutation { login(input: { email: \\\"email\\\", password: \\\"password\\\" }) { token } }\" }", encode = "json")
is the way to go.
Thanks for looking into it. I hope somebody will find this toubleshooting useful!
from crul.
Nice work, thanks for sharing the solution
from crul.
Related Issues (20)
- funky times headers should warn instead of error HOT 6
- Add HttpClient tests for new number formatting
- Update docs telling users about number formatting for query params
- How do you avoid exceeding the rate limit when making API calls? HOT 11
- S3 method for async ``$responses` method
- consider making `AsyncQueue` non-blocking
- How to retry requests for AsyncQueue? HOT 4
- Add support for pagination for Async calls HOT 3
- How to parse .xlsx content from crul response? HOT 4
- `Error in nchar(hh) : invalid multibyte string` when response has Latin1 headers HOT 1
- Async$get fails with non default disc param HOT 3
- server certificate verification failed. CAfile: none CRLfile: none HOT 2
- Integrate progress bars for Async requests HOT 3
- Number of parallel requests with AsyncVaried HOT 9
- use cookies from browser HOT 1
- tweak tests/egs to make cran happy HOT 1
- rgbif users reporting that "HTTP/2 stream was not closed cleanly before end of the underlying stream" HOT 4
- Fix errors in cran checks
- Add _PACKAGE thingy for pkg level doc
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crul.