graphiti-openapi 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +10 -0
  3. data/.gitignore +20 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +7 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +16 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +45 -0
  10. data/Rakefile +20 -0
  11. data/app/assets/packs/api.js +27 -0
  12. data/app/controllers/graphiti/open_api/application_controller.rb +7 -0
  13. data/app/controllers/graphiti/open_api/specifications_controller.rb +21 -0
  14. data/app/helpers/graphiti/open_api/application_helper.rb +6 -0
  15. data/app/models/graphiti/open_api/action.rb +130 -0
  16. data/app/models/graphiti/open_api/attribute.rb +40 -0
  17. data/app/models/graphiti/open_api/endpoint.rb +72 -0
  18. data/app/models/graphiti/open_api/functions.rb +16 -0
  19. data/app/models/graphiti/open_api/generator.rb +174 -0
  20. data/app/models/graphiti/open_api/parameters.rb +30 -0
  21. data/app/models/graphiti/open_api/relationship.rb +65 -0
  22. data/app/models/graphiti/open_api/resource.rb +322 -0
  23. data/app/models/graphiti/open_api/schema.rb +32 -0
  24. data/app/models/graphiti/open_api/source.rb +27 -0
  25. data/app/models/graphiti/open_api/struct.rb +12 -0
  26. data/app/models/graphiti/open_api/type.rb +38 -0
  27. data/app/models/graphiti/open_api/types.rb +10 -0
  28. data/app/views/graphiti/open_api/specifications/index.html.erb +6 -0
  29. data/app/views/graphiti/open_api/specifications/index.yaml.erb +1 -0
  30. data/app/views/layouts/graphiti/open_api/application.html.erb +12 -0
  31. data/bin/console +6 -0
  32. data/bin/rails +26 -0
  33. data/bin/setup +8 -0
  34. data/bin/webpack +20 -0
  35. data/config/openapi.yml +66 -0
  36. data/config/routes.rb +5 -0
  37. data/graphiti-openapi.gemspec +48 -0
  38. data/lib/graphiti-openapi.rb +1 -0
  39. data/lib/graphiti/open_api.rb +15 -0
  40. data/lib/graphiti/open_api/engine.rb +16 -0
  41. data/lib/graphiti/open_api/version.rb +5 -0
  42. data/lib/tasks/graphiti_openapi.rake +33 -0
  43. data/lib/templates/installer.rb +20 -0
  44. metadata +353 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cc40de5f023004fe21eb92c3383093e90399e5eb1e92d67a59649f95e6a67863
4
+ data.tar.gz: 132360a5661e4d95c240f2959458ebe35e624f63bd2f7789d2875587f2849cd8
5
+ SHA512:
6
+ metadata.gz: 31932fcb30dc90a2d9213f5fd7b3fa3a11bdf638daab93043325bcf8b134fb9ab7a791ddbedfeb92dd74e1f188beaf012034db60bf41c2ed84739af72465f45a
7
+ data.tar.gz: 99020af2ba89588ddabccaefdc02762d86253260ab710f3d4b908eb2486ee8e040ee2b2c2c98ff731e7724b2a4584bba78f5d291baf569d93d7a41873a79f8c3
@@ -0,0 +1,10 @@
1
+ [*]
2
+ end_of_line = lf
3
+ charset = utf-8
4
+ trim_trailing_whitespace = true
5
+ insert_final_newline = true
6
+ indent_style = space
7
+ indent_size = 2
8
+
9
+ [*.{diff,md}]
10
+ trim_trailing_whitespace = false
@@ -0,0 +1,20 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /log/*.log
10
+ /spec/dummy/log/*.log
11
+ /spec/dummy/node_modules/
12
+ /spec/dummy/yarn-error.log
13
+ /spec/dummy/tmp/
14
+ /spec/dummy/public/packs/
15
+ /spec/dummy/public/packs-test/
16
+
17
+ # rspec failure tracking
18
+ /spec/examples.txt
19
+ Gemfile.lock
20
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alex@semyonov.us. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in graphiti-openapi.gemspec
4
+ gemspec
5
+
6
+ gem "webpacker"
7
+ gem "responders"
8
+ gem "kaminari"
9
+ gem "graphiti"
10
+
11
+ group :development, :test do
12
+ gem "rspec-rails"
13
+ gem "rspec-its"
14
+ gem "factory_bot_rails"
15
+ gem "database_cleaner"
16
+ end
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Alex Semyonov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # Graphiti::OpenAPI
2
+
3
+ OpenAPI 3.0 specification for your ([Graphiti](https://github.com/graphiti-api/graphiti)) JSON:API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your Rails application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'graphiti-openapi'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle
17
+ ```
18
+
19
+ Set up environment running
20
+
21
+ ```bash
22
+ bin/rails graphiti:openapi:install
23
+ ```
24
+
25
+ Configure OpenAPI template in `config/openapi.yml` and Graphiti::OpenAPI will pick your resources schema and
26
+ translate it to OpenAPI 3.0 JSON and YAML files, providing Swagger UI with live API documentation.
27
+
28
+ ## Usage
29
+
30
+ Edit template in `config/openapi.yml` to customize your OpenAPI output. This file will be used as base for resulting
31
+ document.
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ 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).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alsemyonov/graphiti-openapi. 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.
42
+
43
+ ## Code of Conduct
44
+
45
+ Everyone interacting in the Graphiti::OpenAPI project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alsemyonov/graphiti-openapi/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,20 @@
1
+ require "bundler/setup"
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+ require 'rdoc/task'
5
+
6
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
7
+ load 'rails/tasks/engine.rake'
8
+ task environment: "app:yarn:install"
9
+ load 'rails/tasks/statistics.rake'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Graphiti::OpenAPI'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ RSpec::Core::RakeTask.new(:spec)
20
+ task default: :spec
@@ -0,0 +1,27 @@
1
+ /* eslint no-console:0 */
2
+ // This file is automatically compiled by Webpack, along with any other files
3
+ // present in this directory. You're encouraged to place your actual application logic in
4
+ // a relevant structure within app/javascript and only use these pack files to reference
5
+ // that code so it'll be compiled.
6
+ //
7
+ // To reference this file, add <%= javascript_pack_tag 'api' %> to the appropriate
8
+ // layout file, like app/views/layouts/application.html.erb
9
+
10
+ import SwaggerUI, { plugins, presets } from "swagger-ui"
11
+ import "swagger-ui/dist/swagger-ui.css"
12
+
13
+ window.addEventListener("load", function () {
14
+ SwaggerUI({
15
+ url: `/api/v1/openapi.json`,
16
+ dom_id: "#GraphitiOpenAPI",
17
+ presets: [
18
+ presets.apis,
19
+ SwaggerUI.SwaggerUIStandalonePreset
20
+ ],
21
+ plugins: [
22
+ plugins.SpecIndex,
23
+ plugins.LayoutIndex
24
+ ],
25
+ deepLinking: true,
26
+ });
27
+ });
@@ -0,0 +1,7 @@
1
+ module Graphiti
2
+ module OpenAPI
3
+ class ApplicationController < ::ApplicationController
4
+ # protect_from_forgery with: :exception
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ require_dependency "graphiti/open_api/application_controller"
2
+
3
+ module Graphiti::OpenAPI
4
+ class SpecificationsController < ApplicationController
5
+ def index
6
+ respond_to do |format|
7
+ format.html
8
+ format.json { render json: generator.to_openapi }
9
+ format.yaml { render text: generator.to_openapi(format: :yaml) }
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def generator
16
+ @generator ||= Generator.new
17
+ end
18
+
19
+ helper_method :generator
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ module Graphiti
2
+ module OpenAPI
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,130 @@
1
+ require "graphiti/open_api"
2
+ require_relative "struct"
3
+ require_relative "parameters"
4
+
5
+ module Graphiti::OpenAPI
6
+ class ActionData < Struct
7
+ attribute :resource, Types::String
8
+ end
9
+
10
+ class Action < ActionData
11
+ include Parameters
12
+
13
+ METHODS_MAP = Hash.new do |hash, key|
14
+ hash[key] = key
15
+ end.merge(
16
+ index: :get,
17
+ show: :get,
18
+ create: :post,
19
+ update: :put,
20
+ destroy: :delete,
21
+ )
22
+
23
+ SUMMARIES_MAP = Hash.new do |hash, key|
24
+ hash[key] = "#{key.capitalize} resource"
25
+ end.merge(
26
+ index: "List resources",
27
+ show: "Fetch resource",
28
+ create: "Create resource",
29
+ update: "Update resource",
30
+ destroy: "Destroy resource",
31
+ )
32
+
33
+ OPERATIONS_MAP = Hash.new do |hash, key|
34
+ hash[key] = key
35
+ end.merge(
36
+ index: :list,
37
+ show: :get,
38
+ destroy: :delete,
39
+ )
40
+
41
+ attribute :endpoint, Types::Any
42
+ attribute :action, Types::Symbol
43
+
44
+ def_instance_delegators :endpoint, :schema
45
+
46
+ def resource
47
+ schema.resources[attributes[:resource]]
48
+ end
49
+
50
+ def_instance_delegators :resource, :type, :model_name
51
+
52
+ def collection?
53
+ %i[index create].include?(action)
54
+ end
55
+
56
+ def resource?
57
+ !collection?
58
+ end
59
+
60
+ def read?
61
+ %i[index show].include?(action)
62
+ end
63
+
64
+ def modify?
65
+ %i[create update].include?(action)
66
+ end
67
+
68
+ def method
69
+ METHODS_MAP[action]
70
+ end
71
+
72
+ def summary
73
+ SUMMARIES_MAP[action].gsub(/\bresources\b/, type.humanize).gsub(/\bresource\b/, type.singularize.humanize)
74
+ end
75
+
76
+ def operation_id
77
+ "#{OPERATIONS_MAP[action]}_#{action == :index ? model_name.plural : model_name.singular}".camelize(:lower)
78
+ end
79
+
80
+ def operation
81
+ {method => operation_description}
82
+ end
83
+
84
+ def operation_description
85
+ {
86
+ operationId: operation_id,
87
+ summary: summary,
88
+ tags: tags,
89
+ responses: responses,
90
+ }.tap do |desc|
91
+ desc[:requestBody] = {'$ref': "#/components/requestBodies/#{resource.type}"} if modify?
92
+ end
93
+ end
94
+
95
+ def delete?
96
+ action == :destroy
97
+ end
98
+
99
+ memoize :collection?, :method, :resource, :operation
100
+
101
+ private
102
+
103
+ def tags
104
+ [type]
105
+ end
106
+
107
+ def responses
108
+ {}.tap do |result|
109
+ result[200] = {'$ref': "#/components/responses/#{resource.type}_200#{"_collection" if collection?}"} if %i[index show update].include?(action)
110
+ result[200] = {'$ref': "#/components/responses/200"} if delete?
111
+ result[201] = {'$ref': "#/components/responses/#{resource.type}_201"} if action == :create
112
+ result[202] = {'$ref': "#/components/responses/202"} if modify? || delete?
113
+ result[204] = {'$ref': "#/components/responses/204"} if modify? || delete?
114
+ result[401] = {"$ref": "#/components/responses/401"} # if authentication?
115
+ result[403] = {"$ref": "#/components/responses/403"} # if authorization?
116
+ result[404] = {"$ref": "#/components/responses/404"} unless collection?
117
+ result[403] = {"$ref": "#/components/responses/409"} if modify?
118
+ result[422] = {"$ref": "#/components/responses/422"} if modify?
119
+ end
120
+ end
121
+ end
122
+
123
+ class Actions < Hash
124
+ def self.load(endpoint, data: endpoint.__attributes__[:actions])
125
+ data.map do |action, data|
126
+ Action.new(data.to_hash.merge(endpoint: endpoint, action: action))
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,40 @@
1
+ require "graphiti/open_api"
2
+ require_relative "struct"
3
+
4
+ module Graphiti::OpenAPI
5
+ class AttributeData < Struct
6
+ attribute :type, Types::String
7
+ attribute :readable, Types::Bool
8
+ attribute :writable, Types::Bool
9
+ attribute :description, Types::String.optional
10
+ end
11
+
12
+ class Attribute < AttributeData
13
+ attribute :name, Types::Symbol
14
+ attribute :resource, Types::Any
15
+
16
+ def_instance_delegators :resource, :schema
17
+
18
+ def type
19
+ schema.types[__attributes__[:type].to_sym]
20
+ end
21
+
22
+ def to_property
23
+ return {} unless readable || writable
24
+
25
+ definition = type.to_schema
26
+ definition[:description] = description if description
27
+ definition[:readOnly] = readable unless writable
28
+ definition[:writeOnly] = writable unless readable
29
+ {name => definition}
30
+ end
31
+ end
32
+
33
+ class Attributes < Hash
34
+ def self.load(resource, data = resource.__attributes__[:attributes])
35
+ data.each_with_object(new) do |(name, data), result|
36
+ result[name] = Attribute.new(data.to_hash.merge(name: name, resource: resource))
37
+ end
38
+ end
39
+ end
40
+ end