card-mod-graphql 0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6671fb819df81906a42cfa6b31f917702c45f2d9c21a8db1856f7038c4789a04
4
+ data.tar.gz: af0734bd58e72ab4d48e70384ec75ed8f6a01746f91ee3e436f5dcb4d513ef9b
5
+ SHA512:
6
+ metadata.gz: 12425293668d7cb00b2fec6678ea333d8062d111d4894b3fdde37076b41dd696bcf2a9809af996f76551d438041ac1aa1ad5ede3013eb3302829e5450f856e20
7
+ data.tar.gz: 863b3a60b13f477ee78635e7c83f97919dbb70d8f4ec61d27a3f4926976e2ad0fc0dc409ed7fd1931ed6595fefa2773ae134dacfce1325317f2e2ab4a38abf0c
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ <!--
2
+ # @title README - mod: graphql
3
+ -->
4
+
5
+ # GraphQL mod (alpha)
6
+
7
+ This experimental mod supports simple GraphQL queries of decko sites.
8
+
9
+ ## Setup
10
+
11
+ Once this mod is installed, you will need to add the following to
12
+ `config/routes.rb`:
13
+
14
+ ```
15
+ post "api/graphql", to: "graphql#execute"
16
+ mount GraphiQL::Rails::Engine, at: "api/graphiql", graphql_path: "graphql"
17
+ ```
18
+
19
+ Having done so, you can point your browser to `YOUR_DECK_ROOT/api/graphiql` to
20
+ get a GraphiQL client with which you can make GraphiQL requests.
@@ -0,0 +1,2 @@
1
+ require "graphql"
2
+ require "graphiql/rails"
@@ -0,0 +1,36 @@
1
+ module GraphQL
2
+ class CardSchema < GraphQL::Schema
3
+ GraphQL::Loader.load_mod_queries
4
+ mutation Types::MutationType
5
+ query Types::Query
6
+
7
+ class << self
8
+ # Union and Interface Resolution
9
+ def resolve_type abstract_type, obj, ctx
10
+ # TODO: Implement this function
11
+ # to return the correct object type for `obj`
12
+ raise GraphQL::RequiredImplementationMissingError
13
+ end
14
+
15
+ # Relay-style Object Identification:
16
+
17
+ # Return a string UUID for `object`
18
+ def id_from_object object, type_definition, query_ctx
19
+ # Here's a simple implementation which:
20
+ # - joins the type name & object.id
21
+ # - encodes it with base64:
22
+ # GraphQL::Schema::UniqueWithinType.encode(type_definition.name, object.id)
23
+ end
24
+
25
+ # Given a string UUID, find the object
26
+ def object_from_id id, query_ctx
27
+ # For example, to decode the UUIDs generated above:
28
+ # type_name, item_id = GraphQL::Schema::UniqueWithinType.decode(id)
29
+ #
30
+ # Then, based on `type_name` and `id`
31
+ # find an object in your application
32
+ # ...
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ module GraphQL
2
+ module Loader
3
+ def self.load_mod_queries
4
+ Cardio.config.paths["lib/graph_q_l/types/query.rb"].existent.each { |q| load q }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module GraphQL
2
+ module Mutations
3
+ class BaseMutation < Schema::RelayClassicMutation
4
+ argument_class Types::BaseArgument
5
+ field_class Types::BaseField
6
+ input_object_class Types::BaseInputObject
7
+ object_class Types::BaseObject
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseArgument < Schema::Argument
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseConnection < Types::BaseObject
4
+ # add `nodes` and `pageInfo` fields,
5
+ # as well as `edge_type(...)` and `node_nullable(...)` overrides
6
+ include Types::Relay::ConnectionBehaviors
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseEdge < Types::BaseObject
4
+ # add `node` and `cursor` fields, as well as `node_type(...)` override
5
+ include Types::Relay::EdgeBehaviors
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseEnum < Schema::Enum
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseField < Schema::Field
4
+ argument_class Types::BaseArgument
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseInputObject < Schema::InputObject
4
+ argument_class Types::BaseArgument
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module GraphQL
2
+ module Types
3
+ module BaseInterface
4
+ include Schema::Interface
5
+ edge_type_class Types::BaseEdge
6
+ connection_type_class Types::BaseConnection
7
+
8
+ field_class Types::BaseField
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,9 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseObject < Schema::Object
4
+ edge_type_class Types::BaseEdge
5
+ connection_type_class Types::BaseConnection
6
+ field_class Types::BaseField
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseScalar < Schema::Scalar
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module GraphQL
2
+ module Types
3
+ class BaseUnion < Schema::Union
4
+ edge_type_class Types::BaseEdge
5
+ connection_type_class Types::BaseConnection
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,50 @@
1
+ module GraphQL
2
+ module Types
3
+ class Card < BaseObject
4
+ field :id, Integer, "unique numerical identifier", null: true
5
+ field :type, Card, "card type", null: false
6
+ field :name, String, "name that is unique across all cards", null: false
7
+ field :linkname, String,"url-friendly name variant", null: false
8
+ field :created_at, Types::ISO8601DateTime, "when created", null: true
9
+ field :updated_at, Types::ISO8601DateTime, "when last updated", null: true
10
+ field :creator, Card, "User who created", null: true
11
+ field :updater, Card, "User who last updated", null: true
12
+ field :left, Card, "left name", null: true
13
+ field :right, Card, "right name", null: true
14
+ field :content, String, "core view of card rendered in text format", null: true
15
+
16
+ def type
17
+ object.type_id.card
18
+ end
19
+
20
+ def linkname
21
+ object.name.url_key
22
+ end
23
+
24
+ def left
25
+ object.left_id.card
26
+ end
27
+
28
+ def right
29
+ object.right_id.card
30
+ end
31
+
32
+ def content
33
+ object.format(:text).render_core
34
+ end
35
+
36
+ def creator
37
+ object.creator_id.card
38
+ end
39
+
40
+ def updater
41
+ object.updater_id.card
42
+ end
43
+
44
+ # support methods (move to module?)
45
+ def referers type, field
46
+ ::Card.search type: type, limit: 10, right_plus: [field, refer_to: object.card_id]
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,12 @@
1
+ module GraphQL
2
+ module Types
3
+ class MutationType < Types::BaseObject
4
+ # TODO: remove me
5
+ field :test_field, String, null: false,
6
+ description: "An example field added by the generator"
7
+ def test_field
8
+ "Hello World"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module GraphQL
2
+ module Types
3
+ module NodeType
4
+ include Types::BaseInterface
5
+ # Add the `id` field
6
+ include Types::Relay::NodeBehaviors
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,49 @@
1
+ module GraphQL
2
+ module Types
3
+ class Query < BaseObject
4
+ # Add `node(id: ID!) and `nodes(ids: [ID!]!)`
5
+ # include Types::Relay::HasNodeField
6
+ # include Types::Relay::HasNodesField
7
+
8
+ # Add root-level fields here.
9
+ # They will be entry points for queries on your schema.
10
+
11
+ field :card, Card, null: true do
12
+ argument :name, String, required: false
13
+ argument :id, Integer, required: false
14
+ end
15
+
16
+ field :cards, [Card], null: false do
17
+ argument :name, String, required: false
18
+ argument :type, String, required: false
19
+ end
20
+
21
+ def card **mark
22
+ ok_card nil, **mark
23
+ end
24
+
25
+ def cards name: nil, type: nil, limit: 10, offset: 0
26
+ card_search name, type, limit, offset
27
+ end
28
+
29
+ private
30
+
31
+ def ok_card_of_type type_code, **mark
32
+ card = ok_card(**mark)
33
+ card if card.type_code == type_code
34
+ end
35
+
36
+ def ok_card type_code, name: nil, id: nil
37
+ card = ::Card.fetch name || id
38
+ card if card&.ok?(:read) && (!type_code || card.type_code == type_code)
39
+ end
40
+
41
+ def card_search name, type, limit, offset
42
+ cql = { limit: limit, offset: offset }
43
+ cql[:type] = type if type
44
+ cql[:name] = [:match, name] if name
45
+ ::Card.search cql
46
+ end
47
+ end
48
+ end
49
+ end
data/lib/graph_q_l.rb ADDED
@@ -0,0 +1 @@
1
+ require "card/mod/graphql"
@@ -0,0 +1,58 @@
1
+ # bypasses card controller, executes graphql queries
2
+ class GraphqlController < ActionController::Base
3
+ # If accessing from outside this domain, nullify the session
4
+ # This allows for outside API access while preventing CSRF attacks,
5
+ # but you'll have to authenticate your user separately
6
+ # protect_from_forgery with: :null_session
7
+
8
+ def execute
9
+ variables = prepare_variables params[:variables]
10
+ query = params[:query]
11
+ operation_name = params[:operationName]
12
+ context = {
13
+ # Query context goes here, for example:
14
+ # current_user: current_user,
15
+ }
16
+ result = GraphQL::CardSchema.execute query,
17
+ variables: variables,
18
+ context: context,
19
+ operation_name: operation_name
20
+ render json: result
21
+ rescue StandardError => e
22
+ raise e unless Rails.env.development?
23
+ handle_error_in_development e
24
+ end
25
+
26
+ private
27
+
28
+ # Handle variables in form data, JSON body, or a blank value
29
+ def prepare_variables variables_param
30
+ case variables_param
31
+ when String
32
+ prepare_string_variable variables_param
33
+ when Hash
34
+ variables_param
35
+ when ActionController::Parameters
36
+ variables_param.to_unsafe_hash
37
+ # GraphQL-Ruby will validate name and type of incoming variables.
38
+ when nil
39
+ {}
40
+ else
41
+ raise ArgumentError, "Unexpected parameter: #{variables_param}"
42
+ end
43
+ end
44
+
45
+ def prepare_string_variable string
46
+ return {} unless string.present?
47
+
48
+ JSON.parse(string) || {}
49
+ end
50
+
51
+ def handle_error_in_development e
52
+ logger.error e.message
53
+ logger.error e.backtrace.join("\n")
54
+
55
+ render json: { errors: [{ message: e.message, backtrace: e.backtrace }],
56
+ data: {} }, status: 500
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ require "graphql/rake_task"
2
+
3
+ GraphQL::RakeTask.new schema_name: "GraphQL::CardSchema"
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: card-mod-graphql
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Ethan McCutchen
8
+ - Philipp Kühl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-10-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: card
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: graphql
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.12'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.12'
42
+ - !ruby/object:Gem::Dependency
43
+ name: graphiql-rails
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.7'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.7'
56
+ description: ''
57
+ email:
58
+ - info@decko.org
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - README.md
64
+ - lib/card/mod/graphql.rb
65
+ - lib/graph_q_l.rb
66
+ - lib/graph_q_l/card_schema.rb
67
+ - lib/graph_q_l/loader.rb
68
+ - lib/graph_q_l/mutations/base_mutation.rb
69
+ - lib/graph_q_l/types/base_argument.rb
70
+ - lib/graph_q_l/types/base_connection.rb
71
+ - lib/graph_q_l/types/base_edge.rb
72
+ - lib/graph_q_l/types/base_enum.rb
73
+ - lib/graph_q_l/types/base_field.rb
74
+ - lib/graph_q_l/types/base_input_object.rb
75
+ - lib/graph_q_l/types/base_interface.rb
76
+ - lib/graph_q_l/types/base_object.rb
77
+ - lib/graph_q_l/types/base_scalar.rb
78
+ - lib/graph_q_l/types/base_union.rb
79
+ - lib/graph_q_l/types/card.rb
80
+ - lib/graph_q_l/types/mutation_type.rb
81
+ - lib/graph_q_l/types/node_type.rb
82
+ - lib/graph_q_l/types/query.rb
83
+ - lib/graphql_controller.rb
84
+ - lib/tasks/graphql.rake
85
+ homepage: http://decko.org
86
+ licenses:
87
+ - GPL-3.0
88
+ metadata:
89
+ card-mod: graphql
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '2.5'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.2.28
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: GraphQL for decko cards
109
+ test_files: []