pushrax / graphql-ruby

Ruby implementation of Facebook's GraphQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql

Build Status Gem Version Code Climate Test Coverage built with love

Overview

  • Install the gem:

    # Gemfile
    gem 'graphql'
    $ bundle install
    
  • Declare types & build a schema:

    # Declare a type...
    PostType = GraphQL::ObjectType.new do |t, types, field|
      t.name "Post"
      t.description "A blog post"
      t.fields({
        id:       field.build(type: !types.Int)
        title:    field.build(type: !types.String),
        body:     field.build(type: !types.String),
        comments: field.build(type: types[!CommentType])
      })
    end
    
    # ...and a query root
    QueryType = GraphQL::ObjectType.new do |t, types, field, arg|
      t.name "Query"
      t.description "The query root of this schema"
      t.fields({
        post: GraphQL::Field.new do |f|
          f.arguments(id: arg.build(type: !types.Int))
          f.resolve -> (object, args, context) {
            Post.find(args["id"])
          }
        end
      })
    end
    
    # Then create your schema
    Schema = GraphQL::Schema.new(query: QueryType, mutation: nil)

    See also:

  • Execute queries:

    query = GraphQL::Query.new(Schema, query_string)
    result_hash = query.result
    # {
    #   "data" => {
    #     "post" => {
    #        "id" => 1,
    #        "title" => "GraphQL is nice"
    #     }
    #   }
    # }

    See also:

To Do:

Goals:

  • Implement the GraphQL spec & support a Relay front end
  • Provide idiomatic, plain-Ruby API with similarities to reference implementation where possible
  • Support graphql-rails

About

Ruby implementation of Facebook's GraphQL

License:MIT License


Languages

Language:Ruby 100.0%