Git Product home page Git Product logo

Comments (4)

sckott avatar sckott commented on July 30, 2024

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.

TimBMK avatar TimBMK commented on July 30, 2024

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.

TimBMK avatar TimBMK commented on July 30, 2024

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.

sckott avatar sckott commented on July 30, 2024

Nice work, thanks for sharing the solution

from crul.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.