graphql-persisted_documents 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4705b7ffafbf43fd6313d0dc28eab8bec977b5ce
4
+ data.tar.gz: 9a90cfbd553bcc4d473afe7cd194bb0959519f9f
5
+ SHA512:
6
+ metadata.gz: be981aae45332255e1b938344c8612b3178a16c497d3c33ad788c7d788d6cb7125dec48093c353b1637ca7e48e4cef8dba5aaec609df1d634a636441fd383490
7
+ data.tar.gz: df1dec550f99cc3231aadd5dcca47148da219a43ab97bdc66492ea8a6eec596d2d03013d67ad04278fd8d6aa17a3252e34023751550b810049262aad665abed5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at mgiroux0@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in graphql-persisted_documents.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Graphql::PersistedDocuments
2
+
3
+ ### What is a GraphQL Document ?
4
+
5
+ From the GraphQL spec:
6
+
7
+ > A GraphQL document is defined as a syntactic grammar where terminal symbols are tokens (indivisible lexical units). These tokens are defined in a lexical grammar which matches patterns of source characters (defined by a double‐colon ::)
8
+
9
+ ### Persisted Documents
10
+
11
+ In most client side applications, the GraphQL requests are static. There is a limited set of them in an app and they chances are they wont change during the lifetime of the app. If they are not going to change, why not cache them server side, and simply call them by an id instead of sending a potentially huge query server side each time ?
12
+
13
+ This is what Graphql::PersistedDocuments allows you to do. At build time / deploy time, a client side can send all it's documents to a special endpoint, called `/persist` in this case. The GraphQL backend simply returns a `document_id` which allows every client side running application to call the server using an id instead of a full query_stirng.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'graphql-persisted_documents'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install graphql-persisted_documents
30
+
31
+ ## Usage
32
+
33
+ `GraphQL::PersistedDocuments` is simply a Rack Middleware defined as `Graphql::PersistedDocuments::Rack`. To use it in Rails for example:
34
+
35
+ ```ruby
36
+ module YourApp
37
+ class Application < Rails::Application
38
+
39
+ # ...
40
+
41
+ # Rails 3/4
42
+ config.middleware.insert_before 0, "Graphql::PersistedDocuments::Rack"
43
+
44
+ # Rails 5
45
+ config.middleware.insert_before 0, Rack::Cors
46
+ end
47
+ end
48
+ ```
49
+
50
+ ## Configuring
51
+
52
+ The middleware currently required two things to be defined in the config:
53
+
54
+ - `config.schema`: A GraphQL Schema, defined by the `graphql` gem.
55
+ - `config.persit_validated_document`: An object that responds to `call`, which returns an id in exchange of a parsed GraphQL Document.
56
+
57
+ To configure, for example in a Rails app:
58
+
59
+ ```ruby
60
+ Graphql::PersistedDocuments.configure do |config|
61
+ config.schema = ShopSchema
62
+ config.persist_validated_document = lambda do |document|
63
+ uuid = SecureRandom.uuid
64
+ Rails.cache.write(uuid, document)
65
+ uuid
66
+ end
67
+ end
68
+ ```
69
+
70
+ ## Using with Rails
71
+
72
+ When using with `rails` and `graphql` gems. We can simply accept a `document_id` as a param, as well as the standard `query` param.
73
+
74
+ If we receive a `document_id`, we can retrieve that id from a store, cache, database, depending on how you implemented `persist_validated_document`.
75
+
76
+ Example controller:
77
+
78
+
79
+ ```ruby
80
+ class GraphqlController < ApplicationController
81
+ def execute
82
+ query_string = params[:query]
83
+ document_id = params[:document_id]
84
+ variables = params[:variables]
85
+
86
+ raise SomeGraphQLError unless query_string || document_id
87
+
88
+ if document_id
89
+ result = execute_from_persisted_document(document_id, variables)
90
+ else
91
+ result = execute_from_query_string(query_string, variables)
92
+ end
93
+
94
+ render json: result
95
+ end
96
+
97
+ private
98
+
99
+ def execute_from_query_string(query_string, variables)
100
+ MySchema.execute(query_string, variables: variables)
101
+ end
102
+
103
+ def execute_from_persisted_document(document_id, variables)
104
+ persisted_document = get_persisted_document(document_id)
105
+ MySchema.execute(document: persisted_document, variables: variables)
106
+ end
107
+
108
+ def get_persisted_document(document_id)
109
+ Rails.cache.fetch(document_id)
110
+ end
111
+ ```
112
+
113
+ ## Development
114
+
115
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
116
+
117
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
118
+
119
+ ## Contributing
120
+
121
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xuorig/graphql-persisted_documents. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
122
+
123
+ ## TODO
124
+
125
+ - [ ] Better error messages
126
+ - [ ] Allow custom path
127
+ - [ ] Add unit tests for persister
128
+
129
+
130
+ ## License
131
+
132
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
133
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "graphql/persisted_documents"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'graphql/persisted_documents/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "graphql-persisted_documents"
8
+ spec.version = Graphql::PersistedDocuments::VERSION
9
+ spec.authors = ["Marc-Andre Giroux"]
10
+ spec.email = ["mgiroux0@gmail.com"]
11
+
12
+ spec.summary = %q{Persisted GraphQL documents for Rack applications}
13
+ spec.description = %q{Rack middleware enabling an endpoint to persist GraphQL documents}
14
+ spec.homepage = "http://mgiroux.me"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "graphql", "~> 0.17.2"
23
+
24
+ spec.add_development_dependency "rspec", "~> 3.5.0"
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rack-test", "~> 0.6.3"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
@@ -0,0 +1,21 @@
1
+ require "graphql"
2
+
3
+ require "graphql/persisted_documents/configuration"
4
+ require "graphql/persisted_documents/version"
5
+ require "graphql/persisted_documents/rack"
6
+ require "graphql/persisted_documents/persister"
7
+
8
+ module Graphql
9
+ module PersistedDocuments
10
+ @configuration = Configuration.new
11
+
12
+ class << self
13
+ attr_accessor :configuration
14
+
15
+ def configure
16
+ @configuration ||= Configuration.new
17
+ yield(configuration) if block_given?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module Graphql
2
+ module PersistedDocuments
3
+ class Configuration
4
+ DEFAULT_PATH = '/persist'
5
+
6
+ attr_accessor(
7
+ :schema,
8
+ :persist_validated_document,
9
+ :path
10
+ )
11
+
12
+ def initialize
13
+ @path = DEFAULT_PATH
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,46 @@
1
+ module Graphql
2
+ module PersistedDocuments
3
+ class Persister
4
+ InvalidDocument = Class.new(StandardError)
5
+ MissingPersistValidatedDocument = Class.new(StandardError)
6
+ ParseError = Class.new(StandardError)
7
+
8
+ attr_reader :query_string, :errors
9
+
10
+ def initialize(query_string)
11
+ @query_string = query_string
12
+ @errors = []
13
+ end
14
+
15
+ def persist!
16
+ document = GraphQL.parse(query_string)
17
+ query = GraphQL::Query.new(config.schema, document: document)
18
+ perform_validation(query)
19
+
20
+ raise InvalidDocument unless errors.empty?
21
+
22
+ user_persister = config.persist_validated_document
23
+ return user_persister.call(document) if user_persister
24
+
25
+ raise MissingPersistValidatedDocument
26
+ rescue GraphQL::ParseError
27
+ raise ParseError
28
+ end
29
+
30
+ private
31
+
32
+ def perform_validation(query)
33
+ validation_result = validator.validate(query)
34
+ @errors = validation_result[:errors]
35
+ end
36
+
37
+ def config
38
+ PersistedDocuments.configuration
39
+ end
40
+
41
+ def validator
42
+ GraphQL::StaticValidation::Validator.new(schema: config.schema)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,47 @@
1
+ module Graphql
2
+ module PersistedDocuments
3
+ class Rack
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ if env['PATH_INFO'] == PersistedDocuments.configuration.path
10
+ request = ::Rack::Request.new(env)
11
+ document = request.params['document']
12
+
13
+ unless document
14
+ return error_response(422, ['Missing required argument document'])
15
+ end
16
+
17
+ persister = Persister.new(document)
18
+ document_id = persister.persist!
19
+
20
+ [
21
+ '200',
22
+ {'Content-Type' => 'application/json'},
23
+ [ { document_id: document_id }.to_json ]
24
+ ]
25
+ else
26
+ @app.call(env)
27
+ end
28
+ rescue Persister::ParseError
29
+ error_response(422, ['Unable to parse document'])
30
+ rescue Persister::InvalidDocument
31
+ error_response(422, persister.errors)
32
+ rescue Persister::MissingPersistValidatedDocument
33
+ error_response(400, ['persist_validated_document must be defined in config'])
34
+ end
35
+
36
+ private
37
+
38
+ def error_response(code, errors)
39
+ [
40
+ code,
41
+ {'Content-Type' => 'application/json'},
42
+ [ { errors: errors }.to_json ]
43
+ ]
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Graphql
2
+ module PersistedDocuments
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-persisted_documents
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marc-Andre Giroux
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphql
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.17.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.17.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.5.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.6.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.6.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: Rack middleware enabling an endpoint to persist GraphQL documents
84
+ email:
85
+ - mgiroux0@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - CODE_OF_CONDUCT.md
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - graphql-persisted_documents.gemspec
99
+ - lib/graphql/persisted_documents.rb
100
+ - lib/graphql/persisted_documents/configuration.rb
101
+ - lib/graphql/persisted_documents/persister.rb
102
+ - lib/graphql/persisted_documents/rack.rb
103
+ - lib/graphql/persisted_documents/version.rb
104
+ homepage: http://mgiroux.me
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Persisted GraphQL documents for Rack applications
128
+ test_files: []