Skip to content

Instantly share code, notes, and snippets.

@yuki24
Created February 2, 2019 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuki24/6f29cf95c3b39799e55a855b2d1f0a16 to your computer and use it in GitHub Desktop.
Save yuki24/6f29cf95c3b39799e55a855b2d1f0a16 to your computer and use it in GitHub Desktop.
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