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
阅读 9.3k

推荐阅读
紫色梧桐
用户专栏

心得随笔

1 人关注
6 篇文章
专栏主页