Commit 5ef68579 authored by charlie ablett's avatar charlie ablett

Refactor echo resolver

- use current_user method
- add GraphQL type
- add a field options test
- refactor test
parent 454ba65e
...@@ -2,15 +2,16 @@ ...@@ -2,15 +2,16 @@
module Resolvers module Resolvers
class EchoResolver < BaseResolver class EchoResolver < BaseResolver
type ::GraphQL::STRING_TYPE, null: false
description 'Testing endpoint to validate the API with' description 'Testing endpoint to validate the API with'
argument :text, GraphQL::STRING_TYPE, required: true, argument :text, GraphQL::STRING_TYPE, required: true,
description: 'Text to echo back' description: 'Text to echo back'
def resolve(**args) def resolve(text:)
username = context[:current_user]&.username username = current_user&.username
"#{username.inspect} says: #{args[:text]}" "#{username.inspect} says: #{text}"
end end
end end
end end
...@@ -8,6 +8,13 @@ RSpec.describe Resolvers::EchoResolver do ...@@ -8,6 +8,13 @@ RSpec.describe Resolvers::EchoResolver do
let(:current_user) { create(:user) } let(:current_user) { create(:user) }
let(:text) { 'Message test' } let(:text) { 'Message test' }
specify do
expect(described_class.field_options).to include(
type: eq(::GraphQL::STRING_TYPE),
null: be_falsey
)
end
describe '#resolve' do describe '#resolve' do
it 'echoes text and username' do it 'echoes text and username' do
expect(resolve_echo(text)).to eq %Q("#{current_user.username}" says: #{text}) expect(resolve_echo(text)).to eq %Q("#{current_user.username}" says: #{text})
......
...@@ -4,7 +4,7 @@ require 'spec_helper' ...@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe 'GraphQL' do RSpec.describe 'GraphQL' do
include GraphqlHelpers include GraphqlHelpers
let(:query) { graphql_query_for('echo', 'text' => 'Hello world' ) } let(:query) { graphql_query_for('echo', text: 'Hello world' ) }
context 'logging' do context 'logging' do
shared_examples 'logging a graphql query' do shared_examples 'logging a graphql query' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment