4 simple ways to call a GraphQL API
Sashko Stubailo
3564

Ruby GraphQL Client

Just wrote this Ruby client:

class GraphqlClient
def initialize(url, token)
@url = url
@token = token
end

def execute(query:, variables: nil)
HTTParty.post(
@url,
headers: {
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{@token}"
},
body: {
query: query,
variables: variables
}.to_json
).with_indifferent_access
end
end
...
client.execute(
query: %{
mutation Hello($foo: String, $bar: String) {
greeting (
foo: $foo,
bar: $bar
) {
greeting_result
}
}
},
variables: {
foo: "Hello",
bar: "Ruby"
}
)