diff options
author | Douwe Maan <douwe@gitlab.com> | 2018-06-06 13:03:34 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-06-06 13:03:34 +0300 |
commit | af9cc234f2bf854de38e9730266a411f261918da (patch) | |
tree | bf25d7590d728ee1daa6a179ad43db87301475b8 /app/controllers | |
parent | 7334b8556ba2ee06397293fd90ea04be8f3fccd2 (diff) | |
parent | 9b65d4bb417fb4939289eab94487c894f0a62db6 (diff) |
Merge branch 'bvl-graphql-start-34754' into 'master'
GraphQL setup: Basic Project and Merge request endpoint
Closes #34754
See merge request gitlab-org/gitlab-ce!19008
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/graphql_controller.rb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb new file mode 100644 index 00000000000..0a1cf169aca --- /dev/null +++ b/app/controllers/graphql_controller.rb @@ -0,0 +1,45 @@ +class GraphqlController < ApplicationController + # Unauthenticated users have access to the API for public data + skip_before_action :authenticate_user! + + before_action :check_graphql_feature_flag! + + def execute + variables = Gitlab::Graphql::Variables.new(params[:variables]).to_h + query = params[:query] + operation_name = params[:operationName] + context = { + current_user: current_user + } + result = GitlabSchema.execute(query, variables: variables, context: context, operation_name: operation_name) + render json: result + end + + rescue_from StandardError do |exception| + log_exception(exception) + + render_error("Internal server error") + end + + rescue_from Gitlab::Graphql::Variables::Invalid do |exception| + render_error(exception.message, status: :unprocessable_entity) + end + + private + + # Overridden from the ApplicationController to make the response look like + # a GraphQL response. That is nicely picked up in Graphiql. + def render_404 + render_error("Not found!", status: :not_found) + end + + def render_error(message, status: 500) + error = { errors: [message: message] } + + render json: error, status: status + end + + def check_graphql_feature_flag! + render_404 unless Feature.enabled?(:graphql) + end +end |