Skip to content

Commit

Permalink
Merge branch 'bw-move-funtion-to-resolver' into 'master'
Browse files Browse the repository at this point in the history
Upgrade GraphQL gem to 1.8.4 and replace echo function with a resolver

See merge request gitlab-org/gitlab-ce!32016
  • Loading branch information
Mayra Cabrera committed Aug 26, 2019
2 parents 783670c + bdd5b5b commit a7f9a84
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ gem 'grape-entity', '~> 0.7.1'
gem 'rack-cors', '~> 1.0.0', require: 'rack/cors'

# GraphQL API
gem 'graphql', '~> 1.8.0'
gem 'graphql', '= 1.8.4'
gem 'graphiql-rails', '~> 1.4.10'
gem 'apollo_upload_server', '~> 2.0.0.beta3'
gem 'graphql-docs', '~> 1.6.0', group: [:development, :test]
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ GEM
graphiql-rails (1.4.10)
railties
sprockets-rails
graphql (1.8.1)
graphql (1.8.4)
graphql-docs (1.6.0)
commonmarker (~> 0.16)
escape_utils (~> 1.2)
Expand Down Expand Up @@ -1118,7 +1118,7 @@ DEPENDENCIES
grape-path-helpers (~> 1.1)
grape_logging (~> 1.7)
graphiql-rails (~> 1.4.10)
graphql (~> 1.8.0)
graphql (= 1.8.4)
graphql-docs (~> 1.6.0)
grpc (~> 1.19.0)
haml_lint (~> 0.31.0)
Expand Down
6 changes: 0 additions & 6 deletions app/graphql/functions/base_function.rb

This file was deleted.

15 changes: 0 additions & 15 deletions app/graphql/functions/echo.rb

This file was deleted.

14 changes: 14 additions & 0 deletions app/graphql/resolvers/echo_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Resolvers
class EchoResolver < BaseResolver
argument :text, GraphQL::STRING_TYPE, required: true
description 'Testing endpoint to validate the API with'

def resolve(**args)
username = context[:current_user]&.username

"#{username.inspect} says: #{args[:text]}"
end
end
end
2 changes: 1 addition & 1 deletion app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class QueryType < ::Types::BaseObject
resolver: Resolvers::MetadataResolver,
description: 'Metadata about GitLab'

field :echo, GraphQL::STRING_TYPE, null: false, function: Functions::Echo.new
field :echo, GraphQL::STRING_TYPE, null: false, resolver: Resolvers::EchoResolver
end
end
4 changes: 3 additions & 1 deletion lib/gitlab/graphql/present/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def instrument(type, field)
end

presenter = presented_in.presenter_class.new(object, **context.to_h)
wrapped = presented_type.class.new(presenter, context)

# we have to use the new `authorized_new` method, as `new` is protected
wrapped = presented_type.class.authorized_new(presenter, context)

old_resolver.call(wrapped, args, context)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/graphql/resolvers/echo_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'spec_helper'

describe Resolvers::EchoResolver do
include GraphqlHelpers

let(:current_user) { create(:user) }
let(:text) { 'Message test' }

describe '#resolve' do
it 'echoes text and username' do
expect(resolve_echo(text)).to eq %Q("#{current_user.username}" says: #{text})
end

it 'echoes text and nil as username' do
expect(resolve_echo(text, { current_user: nil })).to eq "nil says: #{text}"
end
end

def resolve_echo(text, context = { current_user: current_user })
resolve(described_class, obj: nil, args: { text: text }, ctx: context)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def type_with_field(field_type, field_authorizations = [], resolved_value = 'Res
describe '#authorized_resolve' do
let(:presented_object) { double('presented object') }
let(:presented_type) { double('parent type', object: presented_object) }
subject(:resolved) { service.authorized_resolve.call(presented_type, {}, { current_user: current_user }) }
let(:query_type) { GraphQL::ObjectType.new }
let(:schema) { GraphQL::Schema.define(query: query_type, mutation: nil)}
let(:context) { GraphQL::Query::Context.new(query: OpenStruct.new(schema: schema), values: { current_user: current_user }, object: nil) }
subject(:resolved) { service.authorized_resolve.call(presented_type, {}, context) }

context 'scalar types' do
shared_examples 'checking permissions on the presented object' do
Expand Down
7 changes: 5 additions & 2 deletions spec/lib/gitlab/graphql/markdown_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@
let(:note) { build(:note, note: '# Markdown!') }
let(:thing_with_markdown) { double('markdown thing', object: note) }
let(:expected_markdown) { '<h1 data-sourcepos="1:1-1:11" dir="auto">Markdown!</h1>' }
let(:query_type) { GraphQL::ObjectType.new }
let(:schema) { GraphQL::Schema.define(query: query_type, mutation: nil)}
let(:context) { GraphQL::Query::Context.new(query: OpenStruct.new(schema: schema), values: nil, object: nil) }

it 'renders markdown from the same property as the field name without the `_html` suffix' do
field = class_with_markdown_field(:note_html, null: false).fields['noteHtml']

expect(field.to_graphql.resolve(thing_with_markdown, {}, {})).to eq(expected_markdown)
expect(field.to_graphql.resolve(thing_with_markdown, {}, context)).to eq(expected_markdown)
end

it 'renders markdown from a specific property when a `method` argument is passed' do
field = class_with_markdown_field(:test_html, null: false, method: :note).fields['testHtml']

expect(field.to_graphql.resolve(thing_with_markdown, {}, {})).to eq(expected_markdown)
expect(field.to_graphql.resolve(thing_with_markdown, {}, context)).to eq(expected_markdown)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/requests/api/graphql/multiplexed_queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

it 'returns responses for multiple queries' do
queries = [
{ query: 'query($text: String) { echo(text: $text) }',
{ query: 'query($text: String!) { echo(text: $text) }',
variables: { 'text' => 'Hello' } },
{ query: 'query($text: String) { echo(text: $text) }',
{ query: 'query($text: String!) { echo(text: $text) }',
variables: { 'text' => 'World' } }
]

Expand All @@ -23,8 +23,8 @@

it 'returns error and data combinations' do
queries = [
{ query: 'query($text: String) { broken query }' },
{ query: 'query working($text: String) { echo(text: $text) }',
{ query: 'query($text: String!) { broken query }' },
{ query: 'query working($text: String!) { echo(text: $text) }',
variables: { 'text' => 'World' } }
]

Expand Down

0 comments on commit a7f9a84

Please sign in to comment.