neomatrixcode / Diana.jl

GraphQL for Julia
https://diana.nicepage.io/
MIT License
115 stars 16 forks source link

Add client side mutations example #19

Closed mattborghi closed 3 years ago

mattborghi commented 3 years ago

Hi, first of all awesome package. I really like using GraphQL in my projects and Diana.jl is really useful.

When I started reading the documentation, I thought mutations on the client side couldn't be made but, surprisingly, I could!

So, I propose adding explicitly this functionality for better understanding.

Here is a small example using SpaceX API

using Random
using Diana
using Test

GET_EMPTY_USER = """
query(\$name: String!){
  users(where: {name: {_eq: \$name}}) {
    name
    rocket
  }
}
"""

my_random_name = randstring(12)

r = Queryclient("https://api.spacex.land/graphql/", GET_EMPTY_USER, vars=Dict("name" => my_random_name))

@test r.Info.status == 200
@test r.Data == "{\"data\":{\"users\":[]}}\n"

CREATE_USER = """
mutation(\$name: String!, \$rocket: String! ) {
  insert_users(objects: {name: \$name, rocket: \$rocket}) {
    returning {
      name
      rocket
    }
  }
}
"""

my_random_rocket = randstring(12)

r1 = Queryclient("https://api.spacex.land/graphql/", CREATE_USER, vars=Dict("name" => my_random_name, "rocket" => my_random_rocket))

@test r1.Info.status == 200
@test r1.Data == "{\"data\":{\"insert_users\":{\"returning\":[{\"name\":\"$my_random_name\",\"rocket\":\"$my_random_rocket\"}]}}}\n"

new_random_rocket = randstring(12)

UPDATE_USER = """
mutation(\$name: String!, \$rocket: String!) {
  update_users(where: {name: {_eq: \$name}}, _set: {rocket: \$rocket}) {
    returning {
      name
      rocket
    }
  }
}
"""

r2 = Queryclient("https://api.spacex.land/graphql/", UPDATE_USER, vars=Dict("name" => my_random_name, "rocket" => new_random_rocket))

@test r2.Info.status == 200
@test r2.Data == "{\"data\":{\"update_users\":{\"returning\":[{\"name\":\"$my_random_name\",\"rocket\":\"$new_random_rocket\"}]}}}\n"
neomatrixcode commented 3 years ago

hi @mattborghi, sorry to say I forgot to add the client side mutations in the docs; your example is very good and I will add it to the documentation. Thank you very much.