Commit 2542c4f9 authored by Luke Duncalfe's avatar Luke Duncalfe Committed by Sean McGivern

Add new RuboCop for GraphQL::Types::JSON

This cop backs up our GraphQL API Styleguide advice to avoid the use of
GraphQL::Types::JSON.
parent 966a6aa7
......@@ -336,6 +336,15 @@ Graphql/AuthorizeTypes:
- 'spec/**/*.rb'
- 'ee/spec/**/*.rb'
Graphql/JSONType:
Enabled: true
Include:
- 'app/graphql/types/**/*'
- 'ee/app/graphql/types/**/*'
Exclude:
- 'spec/**/*.rb'
- 'ee/spec/**/*.rb'
RSpec/EnvAssignment:
Enable: true
Include:
......
......@@ -71,7 +71,7 @@ module Types
description: 'Number of events of this alert',
method: :events
field :details,
field :details, # rubocop:disable Graphql/JSONType
GraphQL::Types::JSON,
null: true,
description: 'Alert details'
......
# frozen_string_literal: true
# This cop checks for use of GraphQL::Types::JSON types in GraphQL fields
# and arguments.
#
# @example
#
# # bad
# class AwfulClass
# field :some_field, GraphQL::Types::JSON
# end
#
# # good
# class GreatClass
# field :some_field, GraphQL::STRING_TYPE
# end
module RuboCop
module Cop
module Graphql
class JSONType < RuboCop::Cop::Cop
MSG = 'Avoid using GraphQL::Types::JSON. See: ' \
'https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#json'.freeze
def_node_matcher :has_json_type?, <<~PATTERN
(send nil? {:field :argument}
(sym _)
(const
(const
(const nil? :GraphQL) :Types) :JSON)
(...)?)
PATTERN
def on_send(node)
add_offense(node, location: :expression) if has_json_type?(node)
end
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../../rubocop/cop/graphql/json_type'
RSpec.describe RuboCop::Cop::Graphql::JSONType, type: :rubocop do
include CopHelper
subject(:cop) { described_class.new }
context 'fields' do
it 'adds an offense when GraphQL::Types::JSON is used' do
inspect_source(<<~RUBY.strip)
class MyType
field :some_field, GraphQL::Types::JSON
end
RUBY
expect(cop.offenses.size).to eq(1)
end
it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
inspect_source(<<~RUBY.strip)
class MyType
field :some_field, GraphQL::Types::JSON, null: true, description: 'My description'
end
RUBY
expect(cop.offenses.size).to eq(1)
end
it 'does not add an offense for other types' do
expect_no_offenses(<<~RUBY.strip)
class MyType
field :some_field, GraphQL::STRING_TYPE
end
RUBY
end
end
context 'arguments' do
it 'adds an offense when GraphQL::Types::JSON is used' do
inspect_source(<<~RUBY.strip)
class MyType
argument :some_arg, GraphQL::Types::JSON
end
RUBY
expect(cop.offenses.size).to eq(1)
end
it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
inspect_source(<<~RUBY.strip)
class MyType
argument :some_arg, GraphQL::Types::JSON, null: true, description: 'My description'
end
RUBY
expect(cop.offenses.size).to eq(1)
end
it 'does not add an offense for other types' do
expect_no_offenses(<<~RUBY.strip)
class MyType
argument :some_arg, GraphQL::STRING_TYPE
end
RUBY
end
end
it 'does not add an offense for uses outside of field or argument' do
expect_no_offenses(<<~RUBY.strip)
class MyType
foo :some_field, GraphQL::Types::JSON
end
RUBY
end
end
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