/hackathon-graphql-backend

GraphQL backend for the BD hackathon

Primary LanguageScala

hackathon-graphql-backend

Running the server

After starting the server with

sbt run

you can run queries interactively using GraphiQL by opening http://localhost:8080 in a browser or query the /graphql endpoint. It accepts following properties in the JSON body (this follows relay convention):

  • query - String - GraphQL query as a string
  • variables - Object or String containing a JSON object that defines variables for your query (optional)
  • operationName - String - the name of the operation, in case you defined several of them in the query (optional)

Here are some examples of the queries you can make:

$ curl -X POST localhost:8080/graphql \
    -H "Content-Type:application/json" \
    -d '{"query": "{cities {name, country}}"}'

this gives back the cities and their countries:

{
  "data": {
    "cities":[
      {"name":"Assen","country":"NL"},
      {"name":"Groningen","country":"NL"}
      ]
    }
  }
}

Here is another example, which uses variables:

$ curl -X POST localhost:8080/graphql \
    -H "Content-Type:application/json" \
    -d '{"query": "query Test($humanId: String!){human(id: $humanId) {name, homePlanet, friends {name}}}", "variables": {"humanId": "1000"}}'
$ curl -X POST localhost:8080/graphql \
    -H "Content-Type:application/json" \
    -d '{"query": "query Cities($country: Country!){cities(country: $country) {name, id, country, forecast {daysAhead, maxTemp}}}", "variables": {"country": "DE"}}'

query Cities($country: Country) { cities(country: $country) { name id country forecast { daysAhead maxTemp } } }

The result should be something like this:

{
  "data": {
    "cities": [
      {
        "name": "Berlin",
        "id": 2950159,
        "country": "DE",
        "forecast":[
            {"daysAhead":0,"maxTemp":14.2},
            {"daysAhead":1,"maxTemp":12.2},
            {"daysAhead":2,"maxTemp":10.2},
            {"daysAhead":3,"maxTemp":8.2}]
      }
    ]
  }
} 

You can also run GraphiQL using a query with parameters or a query with parameters .