Replace echo function with a resolver

The `GraphQL::Function` has been deprecated in
favor of resolvers.
This commit is contained in:
Brett Walker 2019-08-26 17:43:38 +00:00 committed by Mayra Cabrera
parent 783670c4cd
commit bdd5b5b695
11 changed files with 58 additions and 33 deletions

View File

@ -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]

View File

@ -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)
@ -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)

View File

@ -1,6 +0,0 @@
# frozen_string_literal: true
module Functions
class BaseFunction < GraphQL::Function
end
end

View File

@ -1,15 +0,0 @@
# frozen_string_literal: true
module Functions
class Echo < BaseFunction
argument :text, GraphQL::STRING_TYPE
description "Testing endpoint to validate the API with"
def call(obj, args, ctx)
username = ctx[:current_user]&.username
"#{username.inspect} says: #{args[:text]}"
end
end
end

View File

@ -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

View File

@ -24,6 +24,6 @@ module Types
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

View File

@ -23,7 +23,9 @@ module Gitlab
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

View File

@ -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

View File

@ -30,7 +30,10 @@ describe Gitlab::Graphql::Authorize::AuthorizeFieldService do
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

View File

@ -30,17 +30,20 @@ describe Gitlab::Graphql::MarkdownField do
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

View File

@ -6,9 +6,9 @@ describe 'Multiplexed queries' do
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' } }
]
@ -23,8 +23,8 @@ describe 'Multiplexed queries' do
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' } }
]