Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Feb 5, 2015
0 parents commit 299b6e0
Show file tree
Hide file tree
Showing 22 changed files with 881 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source 'https://rubygems.org'

# Declare your gem's dependencies in graphql_rails.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use a debugger
# gem 'byebug', group: [:development, :test]
82 changes: 82 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
PATH
remote: .
specs:
graphql (0.0.0)
activesupport (>= 4)
parslet (>= 1.6.2)

GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.0)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
ansi (1.4.3)
blankslate (3.1.3)
builder (3.2.2)
celluloid (0.16.0)
timers (~> 4.0.0)
coderay (1.1.0)
ffi (1.9.6)
formatador (0.2.5)
guard (2.10.1)
formatador (>= 0.2.4)
listen (~> 2.7)
lumberjack (~> 1.0)
pry (>= 0.9.12)
thor (>= 0.18.1)
guard-bundler (2.0.0)
bundler (~> 1.0)
guard (~> 2.2)
guard-minitest (2.0.0)
guard (~> 2.0)
minitest (>= 3.0)
hitimes (1.2.2)
i18n (0.7.0)
json (1.8.2)
listen (2.8.3)
celluloid (>= 0.15.2)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
lumberjack (1.0.9)
method_source (0.8.2)
minitest (5.5.1)
minitest-focus (1.1.0)
minitest (>= 4, < 6)
minitest-reporters (1.0.8)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
parslet (1.6.2)
blankslate (>= 2.0, <= 4.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rb-fsevent (0.9.4)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
ruby-progressbar (1.7.0)
slop (3.6.0)
thor (0.19.1)
thread_safe (0.3.4)
timers (4.0.1)
hitimes
tzinfo (1.2.2)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
graphql!
guard
guard-bundler
guard-minitest
minitest
minitest-focus
minitest-reporters
12 changes: 12 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
guard :bundler do
watch('Gemfile')
# Uncomment next line if your Gemfile contains the `gemspec' command.
watch(/^.+\.gemspec/)
end

guard :minitest do
# with Minitest::Spec
watch(%r{^spec/(.*)_spec\.rb})
watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^spec/spec_helper\.rb}) { 'spec' }
end
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2015 Robert Mosolgo

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions graphql.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$:.push File.expand_path("../lib", __FILE__)

require "graphql"

Gem::Specification.new do |s|
s.name = 'graphql'
s.version = GraphQL::VERSION
s.date = '2015-01-30'
s.summary = "GraphQL"
s.description = "A GraphQL adapter for Ruby"
s.homepage = 'http://rubygems.org/gems/graphql'
s.authors = ["Robert Mosolgo"]
s.email = ['[email protected]']
s.license = "MIT"

s.files = Dir["{lib}/**/*", "MIT-LICENSE", "readme.md"]
s.test_files = Dir["spec/**/*"]

s.add_dependency "activesupport", ">= 4"
s.add_dependency "parslet", ">= 1.6.2"

s.add_development_dependency "guard"
s.add_development_dependency "guard-bundler"
s.add_development_dependency "guard-minitest"
s.add_development_dependency "minitest"
s.add_development_dependency "minitest-focus"
s.add_development_dependency "minitest-reporters"
end
26 changes: 26 additions & 0 deletions lib/graphql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "parslet"
require "active_support/core_ext/string/inflections"
require "active_support/core_ext/object/blank"

module GraphQL
VERSION = "0.0.0"

autoload(:CollectionEdge, "graphql/collection_edge")
autoload(:Parser, "graphql/parser")
autoload(:Query, "graphql/query")
autoload(:Node, "graphql/node")
autoload(:Transform, "graphql/transform")

module Syntax
autoload(:Call, "graphql/syntax/call")
autoload(:Edge, "graphql/syntax/edge")
autoload(:Field, "graphql/syntax/field")
autoload(:Node, "graphql/syntax/node")
end

PARSER = Parser.new
TRANSFORM = Transform.new

class FieldNotDefinedError < RuntimeError
end
end
62 changes: 62 additions & 0 deletions lib/graphql/collection_edge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class GraphQL::CollectionEdge
attr_accessor :fields, :edge_class, :calls, :fields

def initialize(items:, edge_class:)
@items = items
@edge_class = edge_class
end

def to_json
json = {}
fields.each do |field|
name = field.identifier
if name == "edges"
json["edges"] = edges(fields: field.fields)
else
json[name] = safe_send(name)
end
end
json
end

def count
@items.count
end

def apply_calls(unfiltered_items, call_hash)
# override this to apply calls to your items
unfiltered_items
end

def edges(fields:)
filtered_items = apply_calls(items, calls)
filtered_items.map do |item|
node = edge_class.new(item)
json = {}
fields.each do |field|
name = field.identifier
if name == "node" # it's magic
node.fields = field.fields
json[name] = node.to_json
else
json[name] = node.safe_send(name)
end
end
json
end
end

def safe_send(identifier)
if respond_to?(identifier)
public_send(identifier)
else
raise GraphQL::FieldNotDefinedError, "#{self.class.name}##{identifier} was requested, but it isn't defined."
end
end

private

def items
@items
end
end
61 changes: 61 additions & 0 deletions lib/graphql/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class GraphQL::Node
attr_accessor :fields

def initialize(target=nil)
# DONT EXPOSE Node#target! otherwise you might be able to access it
@target = target
end

def safe_send(identifier)
if respond_to?(identifier)
public_send(identifier)
else
raise GraphQL::FieldNotDefinedError, "#{self.class.name}##{identifier} was requested, but it isn't defined."
end
end

def to_json
json = {}
fields.each do |field|
name = field.identifier
if field.is_a?(GraphQL::Syntax::Field)
json[name] = safe_send(name)
elsif field.is_a?(GraphQL::Syntax::Edge)
edge = safe_send(field.identifier)
edge.calls = field.call_hash
edge.fields = field.fields
json[name] = edge.to_json
end
end
json
end


def self.call(argument)
raise NotImplementedError, "Implement #{name}#call(argument) to use this node as a call"
end

def self.field_reader(*field_names)
field_names.each do |field_name|
define_method(field_name) do
@target.public_send(field_name)
end
end
end

def self.edges(field_name, collection_class_name:, edge_class_name:)
define_method(field_name) do
collection_items = @target.send(field_name)
collection_class = Object.const_get(collection_class_name)
edge_class = Object.const_get(edge_class_name)
collection = collection_class.new(items: collection_items, edge_class: edge_class)
end
end


def self.cursor(field_name)
define_method "cursor" do
safe_send(field_name).to_s
end
end
end
20 changes: 20 additions & 0 deletions lib/graphql/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class GraphQL::Parser < Parslet::Parser
root(:node)

rule(:node) { space? >> call >> space? >> fields.as(:fields) }

rule(:fields) { str("{") >> space? >> ((edge | field) >> str(",").maybe >> space?).repeat(1) >> space? >> str("}") >> space?}

rule(:edge) { call_chain >> space? >> fields.as(:fields) }
rule(:call_chain) { identifier >> (dot >> call).repeat(0).as(:calls) }

rule(:call) { identifier >> str("(") >> name.maybe.as(:argument) >> str(")") }
rule(:dot) { str(".") }

rule(:field) { identifier }

rule(:identifier) { name.as(:identifier) }
rule(:name) { match('\w').repeat(1) }
rule(:space) { match('[\s\n]+').repeat(1) }
rule(:space?) { space.maybe }
end
41 changes: 41 additions & 0 deletions lib/graphql/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class GraphQL::Query
attr_reader :query_string, :root, :namespace
def initialize(query_string, namespace: Object)
if !query_string.is_a?(String) || query_string.length == 0
raise "You must send a query string, not a #{query_string.class.name}"
end
@query_string = query_string
@root = parse(query_string)
@namespace = namespace
end

def to_json
root_node = make_call(nil, root.identifier, root.argument)
raise "Couldn't find root for #{root.identifier}(#{root.argument})" if root.nil?

root_node.fields = root.fields
{
root_node.cursor => root_node.to_json
}
end

def get_node(identifier)
name = "#{identifier}_node"
namespace.const_get(name.camelize)
end

def make_call(context, name, *arguments)
if context.nil?
context = get_node(name)
name = "call"
end
context.send(name, *arguments)
end

private

def parse(query_string)
parsed_hash = GraphQL::PARSER.parse(query_string)
root_node = GraphQL::TRANSFORM.apply(parsed_hash)
end
end
17 changes: 17 additions & 0 deletions lib/graphql/syntax/call.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class GraphQL::Syntax::Call
attr_reader :identifier, :argument, :calls
def initialize(identifier:, argument: nil, calls: [])
@identifier = identifier
@argument = argument
@calls = calls
end

def execute!(query)
node_class = query.get_node(identifier)
node_class.call(argument)
end

def to_query
(["#{identifier}(#{argument})"] + calls.map(&:to_query)).join(".")
end
end
Loading

0 comments on commit 299b6e0

Please sign in to comment.