在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):github/graphql-client开源软件地址(OpenSource Url):https://github.com/github/graphql-client开源编程语言(OpenSource Language):Ruby 99.7%开源软件介绍(OpenSource Introduction):graphql-clientGraphQL Client is a Ruby library for declaring, composing and executing GraphQL queries. UsageInstallationAdd # Gemfile
gem 'graphql-client' ConfigurationSample configuration for a GraphQL Client to query from the SWAPI GraphQL Wrapper. require "graphql/client"
require "graphql/client/http"
# Star Wars API example wrapper
module SWAPI
# Configure GraphQL endpoint using the basic HTTP network adapter.
HTTP = GraphQL::Client::HTTP.new("https://example.com/graphql") do
def headers(context)
# Optionally set any HTTP headers
{ "User-Agent": "My Client" }
end
end
# Fetch latest schema on init, this will make a network request
Schema = GraphQL::Client.load_schema(HTTP)
# However, it's smart to dump this to a JSON file and load from disk
#
# Run it from a script or rake task
# GraphQL::Client.dump_schema(SWAPI::HTTP, "path/to/schema.json")
#
# Schema = GraphQL::Client.load_schema("path/to/schema.json")
Client = GraphQL::Client.new(schema: Schema, execute: HTTP)
end Defining QueriesIf you haven't already, familiarize yourself with the GraphQL query syntax. Queries are declared with the same syntax inside of a This client library encourages all GraphQL queries to be declared statically and assigned to a Ruby constant. HeroNameQuery = SWAPI::Client.parse <<-'GRAPHQL'
query {
hero {
name
}
}
GRAPHQL Queries can reference variables that are passed in at query execution time. HeroFromEpisodeQuery = SWAPI::Client.parse <<-'GRAPHQL'
query($episode: Episode) {
hero(episode: $episode) {
name
}
}
GRAPHQL Fragments are declared similarly. HumanFragment = SWAPI::Client.parse <<-'GRAPHQL'
fragment on Human {
name
homePlanet
}
GRAPHQL To include a fragment in a query, reference the fragment by constant. HeroNameQuery = SWAPI::Client.parse <<-'GRAPHQL'
{
luke: human(id: "1000") {
...HumanFragment
}
leia: human(id: "1003") {
...HumanFragment
}
}
GRAPHQL This works for namespaced constants. module Hero
Query = SWAPI::Client.parse <<-'GRAPHQL'
{
luke: human(id: "1000") {
...Human::Fragment
}
leia: human(id: "1003") {
...Human::Fragment
}
}
GRAPHQL
end
Executing queriesPass the reference of a parsed query definition to result = SWAPI::Client.query(Hero::Query)
# The raw data is Hash of JSON values
# result["data"]["luke"]["homePlanet"]
# The wrapped result allows to you access data with Ruby methods
result.data.luke.home_planet
result = SWAPI::Client.query(Hero::HeroFromEpisodeQuery, variables: {episode: "JEDI"}, context: {user_id: current_user_id}) Rails ERB integrationIf you're using Ruby on Rails ERB templates, theres a ERB extension that allows static queries to be defined in the template itself. In standard Ruby you can simply assign queries and fragments to constants and they'll be available throughout the app. However, the contents of an ERB template is compiled into a Ruby method, and methods can't assign constants. So a new ERB tag was extended to declare static sections that include a GraphQL query. <%# app/views/humans/human.html.erb %>
<%graphql
fragment HumanFragment on Human {
name
homePlanet
}
%>
<p><%= human.name %> lives on <%= human.home_planet %>.</p> These
Examplesgithub/github-graphql-rails-example is an example application using this library to implement views on the GitHub GraphQL API. InstallationAdd gem 'graphql-client' See Also
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论