使用ruby 或者rails进行api的管理
首先进行安装gem,使用如下命令直接安装,或者进行githob的安装。在Gemfile文件增加然后$ bundle install也可以。$ rails generate graphql:install
# Gemfile
gem 'graphql'
GitHub - rmosolgo/graphql-ruby: Ruby implementation of GraphQL
gem install graphql
进行尝试,hello.rb
require 'graphql'
class QueryType < GraphQL::Schema::Object
field :hello, String
def hello
"Hello world!"
end
end
class Schema < GraphQL::Schema
query QueryType
end
puts Schema.execute('{ hello }').to_json
同样的 ,使用field,熟悉一下方法 的定义和使用。
require 'graphql'
class QueryType < GraphQL::Schema::Object
field :hero, String
def hero
"R2-D2"
end
end
class Schema < GraphQL::Schema
query QueryType
end
puts Schema.execute('{ hero}').to_json