Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.59k stars 593 forks source link

How to apply graphql with query variable in android? #377

Closed AAIING closed 3 years ago

AAIING commented 3 years ago

i need know how to apply query/mutation to android

example in playground:

mutation($usuarios: UsuariosInput!){ modificarUsuario(id: 3 usuarios:$usuarios){ id_usuario email contrasena } }

query variable

{ "usuarios": { "email": "testin123@testin.cl", "contrasena": "123456", "nombre":"testin1", "apellido": "test" } }

apply to android how?

ty!

ryber commented 3 years ago

Per the spec (YMMV) GraphQL can be called over HTTP as either a GET or a POST and it's a little different for each. https://graphql.org/learn/serving-over-http/

If it was GET it would probably look something like this:

Unirest.get("http://server/graphql-endpoint")
                .header("Accept", "application/graphql")
                .queryString("query", "{\n" +
                        "\"usuarios\": {\n" +
                        "\"email\": \"testin123@testin.cl\",\n" +
                        "\"contrasena\": \"123456\",\n" +
                        "\"nombre\":\"testin1\",\n" +
                        "\"apellido\": \"test\"\n" +
                        "}\n" +
                        "}")
                .asJson();

or a POST would look like this:

Unirest.post("http://server/graphql-endpoint")
                .header("Accept", "application/graphql")
                .header("Content-Type", "application/json")
                .body(new JSONObject().put("query", "{\n" +
                        "\"usuarios\": {\n" +
                        "\"email\": \"testin123@testin.cl\",\n" +
                        "\"contrasena\": \"123456\",\n" +
                        "\"nombre\":\"testin1\",\n" +
                        "\"apellido\": \"test\"\n" +
                        "}\n" +
                        "}"))
                .asJson();

Note that GraphQL syntax is itself NOT json, it only looks JSON-ish.

ryber commented 3 years ago

@AAIING was your question answered?