ruby里面使用graphql

安装graphql模块

gem install graphql

使用ruby hello.rb运行hello.rb中的代码:

require 'graphql'
module Schemas
  module Types
    class User < GraphQL::Types::Relay::BaseObject
      field :id, String, null: true
      field :title, String, null: true
    end
  end
end

class QueryType < GraphQL::Schema::Object
  graphql_name 'Query'
  field :user,Schemas::Types::User,null:true do
    argument :id, String, required: true
    argument :title, String, required: false
  end
  def user(id)
    puts id
    {
      title: 121,
      id: 1231
    }
  end
end

class Schema < GraphQL::Schema
  query QueryType
end

puts Schema.execute('{ user(id: "123", title: "aa") {title} }').to_json

你可能感兴趣的:(ruby,graphql)