Skip to content

Instantly share code, notes, and snippets.

@yuki24
Created Feb 2, 2019
Embed
What would you like to do?
graphql-client vs artemis
# config/graphql.yml
development:
sw_api:
url: "https://example.com/graphql"
schema_path: "path/to/schema.json"
# app/operations/sw_api/hero.graphql
query($episode: Episode) {
hero(episode: $episode) {
name
}
}
# app/operations/sw_api.rb
class SwApi < Artemis::Client
self.default_context = {
headers: { "User-Agent": "My Client" }
}
end
result = SwApi.hero(episode: 4)
result.data.hero.name # => Luke
# Somewhere in your app
require "graphql/client"
require "graphql/client/http"
module SwApi
HTTP = GraphQL::Client::HTTP.new("https://example.com/graphql") do
def headers(context)
{ "User-Agent": "My Client" }
end
end
Schema = GraphQL::Client.load_schema("path/to/schema.json")
Client = GraphQL::Client.new(schema: Schema, execute: HTTP)
HeroQuery = SWAPI::Client.parse <<-'GRAPHQL'
query($episode: Episode) {
hero(episode: $episode) {
name
}
}
GRAPHQL
def self.hero(episode: )
SWAPI::Client.query(SwApi::HeroQuery, variables: { episode: episode })
end
end
result = SwApi.hero(episode: 4)
result.data.hero.name # => Luke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment