Last active
October 22, 2023 12:55
-
-
Save chloerei/6ff41171c97e618a5674544de4504708 to your computer and use it in GitHub Desktop.
A simple graphql client without define schema.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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