Created
February 2, 2019 03:56
-
-
Save yuki24/6f29cf95c3b39799e55a855b2d1f0a16 to your computer and use it in GitHub Desktop.
graphql-client vs artemis
This file contains 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
# 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 |
This file contains 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
# 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