Skip to content

Instantly share code, notes, and snippets.

@chloerei
Last active October 22, 2023 12:55
Show Gist options
  • Save chloerei/6ff41171c97e618a5674544de4504708 to your computer and use it in GitHub Desktop.
Save chloerei/6ff41171c97e618a5674544de4504708 to your computer and use it in GitHub Desktop.
A simple graphql client without define schema.
require "net/http"
require "json"
# client = Graphql::Client.new("http://localhost:3000/graphql", { "Authorization" => "Bearer token..." })
# response = client.execute("query { ... }", { name: "value" })
class Graphql::Client
def initialize(endpoint, headers = {})
@endpoint = endpoint
@headers = headers
end
def execute(query, variables = {})
data = {
query: query,
variables: variables
}.to_json
headers = {
"Content-Type" => "application/json"
}.merge(@headers)
uri = URI(@endpoint)
response = Net::HTTP.post(uri, data, headers)
JSON.parse(response.body)["data"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment