Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature/graphql #3325

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
464a5d1
Bootstrap GraphQL api extension
bricesanchez Oct 25, 2017
3a361ae
Register it to refinerycms main app
bricesanchez Oct 25, 2017
9b3d4e0
Fix to be able to start the app
bricesanchez Oct 25, 2017
7113607
Add PoC to find a page by id
bricesanchez Oct 25, 2017
83b28fb
Improve Page and PagePart ObjectType
bricesanchez Oct 27, 2017
2ebf980
Remove unused specs
bricesanchez Oct 30, 2017
6ec5fc0
Refactor Pages module
bricesanchez Oct 30, 2017
b3218ef
Add basic pages field spec
bricesanchez Oct 30, 2017
a47a95c
Add basic specs for page and pages fields queries
bricesanchez Nov 17, 2017
4c8a93e
field parts is now ordered by position in grapnel
bricesanchez Nov 17, 2017
061c300
Add 'api' as friendly_id_reserved_words in pages configuration
bricesanchez Nov 17, 2017
c39fb29
WIP: Add mutations for pages
bricesanchez Nov 17, 2017
234c399
Update travis config to support api env
bricesanchez Nov 21, 2017
38142e2
We now use Admin controller for mutations
bricesanchez Nov 21, 2017
34b576c
context is now a private method in GrahqlController
bricesanchez Nov 30, 2017
e77abf2
Add page mutations specs
bricesanchez Nov 30, 2017
3c996b8
WIP: Refactor graphql ruby with class name style
bricesanchez Jul 25, 2018
97ceaf3
Remove unused ApiGenerator
bricesanchez Jul 25, 2018
dc423d0
Move GraphQL controller outside Admin namespace
bricesanchez Jul 25, 2018
c123659
Update GraphQL gem dependency version
bricesanchez Jul 25, 2018
30b09c5
WIP: Update to GraphQL 1.8
bricesanchez Sep 5, 2018
59e6b25
WIP: Update to GraphQL 1.8
bricesanchez Sep 5, 2018
5aa4562
Remove focus
bricesanchez Sep 5, 2018
b0635ee
Fix bugs on mutations
bricesanchez Sep 7, 2018
e55a471
Remove forgotten byebug
bricesanchez Sep 7, 2018
7dee17a
Fix mutations for 1.8
bricesanchez Sep 12, 2018
29b6310
Fix specs old ruby versions
bricesanchez Sep 12, 2018
ae6452b
WIP: Add authorization to the API
bricesanchez Sep 18, 2018
9e7e88f
Remove graphiql-rails
bricesanchez Dec 18, 2019
5a7f647
Update graphql gem to 1.9.17
bricesanchez Dec 18, 2019
be07589
Remove GraphiQL requires and routes
bricesanchez Dec 18, 2019
2f6e2f5
Move graphql api route to /api/graphql
bricesanchez Dec 18, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ env:
- DB=postgresql EXTENSION=pages
- DB=postgresql EXTENSION=images
- DB=postgresql EXTENSION=resources
- DB=postgresql EXTENSION=api
- DB=mysql EXTENSION=core
- DB=mysql EXTENSION=dragonfly
- DB=mysql EXTENSION=pages
- DB=mysql EXTENSION=images
- DB=mysql EXTENSION=resources
- DB=mysql EXTENSION=api
notifications:
email: true
webhooks:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ path "./" do
gem "refinerycms-images"
gem "refinerycms-pages"
gem "refinerycms-resources"
gem "refinerycms-api"
end

gem 'bootsnap', require: false
Expand Down
55 changes: 55 additions & 0 deletions api/app/controllers/refinery/api/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Refinery
module Api
class GraphqlController < ::ApplicationController
include Refinery::Admin::BaseController

def execute
variables = ensure_hash(params[:variables])
query = params[:query]
operation_name = params[:operationName]

result = Refinery::Api::GraphqlSchema.execute(
query, variables: variables, context: context, operation_name: operation_name
)

render json: result
rescue => e
raise e unless Rails.env.development?
handle_error_in_development e
end

private

def context
{
current_user: current_refinery_user
}
end

# Handle form data, JSON body, or a blank value
def ensure_hash(ambiguous_param)
case ambiguous_param
when String
if ambiguous_param.present?
ensure_hash(JSON.parse(ambiguous_param))
else
{}
end
when Hash, ActionController::Parameters
ambiguous_param
when nil
{}
else
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
end
end

def handle_error_in_development(e)
logger.error e.message
logger.error e.backtrace.join("\n")

render json: { error: { message: e.message, backtrace: e.backtrace }, data: {} }, status: 500
end
end
end
end
11 changes: 11 additions & 0 deletions api/app/graph/refinery/api/graphql_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Refinery
module Api
class GraphqlSchema < GraphQL::Schema
mutation Types::MutationType
query Types::QueryType
use GraphQL::Guard.new
end
end
end
8 changes: 8 additions & 0 deletions api/app/graph/refinery/api/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Refinery
module Api
module Mutations
class Mutations::BaseMutation < GraphQL::Schema::RelayClassicMutation
end
end
end
end
37 changes: 37 additions & 0 deletions api/app/graph/refinery/api/mutations/pages/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Refinery
module Api
module Mutations
module Pages
class Create < Mutations::BaseMutation
graphql_name 'CreatePage'
description 'Create a Page'

guard ->(_obj, _args, ctx) { ctx[:current_user].has_role?(:refinery) }

argument :page, Types::Pages::PageAttributes, required: true

field :page, Types::Pages::PageType, null: true
field :errors, [String], null: false

def resolve(page:)
page = Refinery::Page.create!(page.to_h)

if page.errors.empty?
{
page: page,
errors: []
}
else
{
page: nil,
errors: page.errors.full_messages
}
end
end
end
end
end
end
end
37 changes: 37 additions & 0 deletions api/app/graph/refinery/api/mutations/pages/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Refinery
module Api
module Mutations
module Pages
class Delete < Mutations::BaseMutation
graphql_name 'DeletePage'
description 'Delete a Page'

guard ->(_obj, _args, ctx) { ctx[:current_user].has_role?(:refinery) }

argument :id, ID, required: true

field :page, Types::Pages::PageType, null: true
field :errors, [String], null: false

def resolve(id:)
page = Refinery::Page.destroy(id)

if page.errors.empty?
{
page: page,
errors: []
}
else
{
page: nil,
errors: page.errors.full_messages
}
end
end
end
end
end
end
end
38 changes: 38 additions & 0 deletions api/app/graph/refinery/api/mutations/pages/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module Refinery
module Api
module Mutations
module Pages
class Update < Mutations::BaseMutation
graphql_name 'UpdatePage'
description 'Update a Page'

guard ->(_obj, _args, ctx) { ctx[:current_user].has_role?(:refinery) }

argument :id, ID, required: true
argument :page, Types::Pages::PageAttributes, required: true

field :page, Types::Pages::PageType, null: false
field :errors, [String], null: false

def resolve(id:, page:)
page = Refinery::Page.update(id, page.to_h)

if page.errors.empty?
{
page: page,
errors: []
}
else
{
page: nil,
errors: page.errors.full_messages
}
end
end
end
end
end
end
end
30 changes: 30 additions & 0 deletions api/app/graph/refinery/api/queries/pages/page_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Refinery
module Api
module Queries
module Pages
class PageQuery < GraphQL::Field.define do
name 'Page'
description 'Find a page by ID'

type Types::Pages::PageType
argument :id, !types.ID

resolve -> (obj, args, ctx) {
Refinery::Page.find_by_id(args[:id])
}
end
end
end
end
end

# frozen_string_literal: true

field :page, Types::Pages::PageType,
null: true, resolve: Resolvers::Pages::PageResolver::ById do
description "Find page by id"
guard ->(_obj, _args, ctx) { ctx[:current_user].has_plugin?('refinery_pages') }
argument :id, ID, required: true
end
Empty file.
31 changes: 31 additions & 0 deletions api/app/graph/refinery/api/resolvers/pages/page_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Refinery
module Api
module Resolvers
module Pages
class PageResolver
class All
def self.call(_obj, args, ctx)
if ctx[:current_user].has_role?(:refinery)
Refinery::Page.all
else
Refinery::Page.live
end
end
end

class ById
def self.call(_obj, args, ctx)
if ctx[:current_user].has_role?(:refinery)
Refinery::Page.find_by_id(args[:id])
else
Refinery::Page.live.find_by_id(args[:id])
end
end
end
end
end
end
end
end
10 changes: 10 additions & 0 deletions api/app/graph/refinery/api/types/base_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
class BaseEnum < GraphQL::Schema::Enum
end
end
end
end
10 changes: 10 additions & 0 deletions api/app/graph/refinery/api/types/base_input_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
class BaseInputObject < GraphQL::Schema::InputObject
end
end
end
end
11 changes: 11 additions & 0 deletions api/app/graph/refinery/api/types/base_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
class BaseInterface
include GraphQL::Schema::Interface
end
end
end
end
10 changes: 10 additions & 0 deletions api/app/graph/refinery/api/types/base_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
class BaseObject < GraphQL::Schema::Object
end
end
end
end
10 changes: 10 additions & 0 deletions api/app/graph/refinery/api/types/base_union.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
class BaseUnion < GraphQL::Schema::Union
end
end
end
end
16 changes: 16 additions & 0 deletions api/app/graph/refinery/api/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
class MutationType < Types::BaseObject
graphql_name 'Mutation'
description 'The mutation root for this schema'

field :createPage, mutation: Mutations::Pages::Create
field :updatePage, mutation: Mutations::Pages::Update
field :deletePage, mutation: Mutations::Pages::Delete
end
end
end
end
Loading