Skip to content

Commit

Permalink
Enable linting for graphql (#830)
Browse files Browse the repository at this point in the history
Also fix existing warnings

Co-authored-by: Ilya Konyukhov <[email protected]>
  • Loading branch information
rosik and RunsFor authored May 6, 2020
1 parent 441481a commit c94918c
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 27 deletions.
2 changes: 0 additions & 2 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ include_files = {
}
exclude_files = {
'.rocks',
'cartridge/graphql.lua',
'cartridge/graphql/*.lua',
}
new_read_globals = {
box = { fields = {
Expand Down
4 changes: 2 additions & 2 deletions cartridge/graphql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ local function add_callback_prefix(prefix, doc)
local obj = {
kind = kind,
arguments = {},
resolve = function(self, args)
resolve = function()
return {}
end,
description = doc,
Expand All @@ -79,7 +79,7 @@ local function add_mutation_prefix(prefix, doc)
local obj = {
kind = kind,
arguments = {},
resolve = function(self, args)
resolve = function()
return {}
end,
description = doc,
Expand Down
4 changes: 2 additions & 2 deletions cartridge/graphql/execute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ local function mergeSelectionSets(fields)
return selections
end

local function defaultResolver(object, arguments, info)
local function defaultResolver(object, _, info)
return object[info.fieldASTs[1].name.value]
end

Expand Down Expand Up @@ -253,7 +253,7 @@ local function getFieldEntry(objectType, object, fields, context)
end)

--[[
Make arguments ordered map using metatble.
Make arguments ordered map using metatable.
This way callback can use positions to access argument values.
For example buisness logic depends on argument positions to choose
appropriate storage iteration.
Expand Down
2 changes: 1 addition & 1 deletion cartridge/graphql/introspection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ __Type = types.object({

possibleTypes = {
kind = types.list(types.nonNull(__Type)),
resolve = function(kind, arguments, context)
resolve = function(kind, _, context)
if kind.__type == 'Interface' or kind.__type == 'Union' then
return context.schema:getPossibleTypes(kind)
end
Expand Down
34 changes: 29 additions & 5 deletions cartridge/graphql/parse.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
_G._ENV = rawget(_G, "_ENV") -- to get lulpeg work under strict mode
local lpeg = require 'lulpeg'
local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V
local C, Ct, Cmt, Cg, Cc, Cf, Cmt = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cc, lpeg.Cf, lpeg.Cmt
local C, Ct, Cmt = lpeg.C, lpeg.Ct, lpeg.Cmt

local line
local lastLinePos
Expand Down Expand Up @@ -261,8 +261,22 @@ local graphQL = P {
definition = _'operation' + _'fragmentDefinition',

operationType = C(P'query' + P'mutation'),
operation = (_'operationType' * ws * maybe(name) * maybe('variableDefinitions') * maybe('directives') * _'selectionSet' + _'selectionSet') / cOperation,
fragmentDefinition = 'fragment' * ws * fragmentName * ws * _'typeCondition' * ws * _'selectionSet' / cFragmentDefinition,
operation = (_'operationType'
* ws
* maybe(name)
* maybe('variableDefinitions')
* maybe('directives')
* _'selectionSet'
+ _'selectionSet'
) / cOperation,
fragmentDefinition = 'fragment'
* ws
* fragmentName
* ws
* _'typeCondition'
* ws
* _'selectionSet'
/ cFragmentDefinition,

selectionSet = ws * '{' * ws * list('selection') * ws * '}' / cSelectionSet,
selection = ws * (_'field' + _'fragmentSpread' + _'inlineFragment'),
Expand All @@ -278,10 +292,20 @@ local graphQL = P {
directive = '@' * name * maybe('arguments') / cDirective,
directives = ws * list('directive', 1) * ws,

variableDefinition = ws * variable * ws * ':' * ws * _'type' * (ws * '=' * _'value') ^ -1 * comma * ws / cVariableDefinition,
variableDefinition = ws
* variable
* ws
* ':'
* ws
* _'type'
* (ws * '=' * _'value') ^ -1
* comma
* ws
/ cVariableDefinition,
variableDefinitions = ws * '(' * list('variableDefinition', 1) * ')',

value = ws * (variable + _'objectValue' + _'listValue' + enumValue + stringValue + booleanValue + floatValue + intValue),
value = ws
* (variable + _'objectValue' + _'listValue' + enumValue + stringValue + booleanValue + floatValue + intValue),
listValue = '[' * list('value') * ']' / cList,
objectFieldValue = ws * C(rawName) * ws * ':' * ws * _'value' * comma / cObjectField,
objectValue = '{' * ws * list('objectFieldValue') * ws * '}' / cObject,
Expand Down
1 change: 0 additions & 1 deletion cartridge/graphql/query_util.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local path = (...):gsub('%.[^%.]+$', '')
local types = require(path .. '.types')
local util = require(path .. '.util')

local function typeFromAST(node, schema)
local innerType
Expand Down
6 changes: 3 additions & 3 deletions cartridge/graphql/rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function rules.unambiguousSelections(node, context)
validateSelectionSet(node, context.objects[#context.objects])
end

function rules.uniqueArgumentNames(node, context)
function rules.uniqueArgumentNames(node, _)
if node.arguments then
local arguments = {}
for _, argument in ipairs(node.arguments) do
Expand Down Expand Up @@ -226,7 +226,7 @@ function rules.requiredArgumentsPresent(node, context)
end
end

function rules.uniqueFragmentNames(node, context)
function rules.uniqueFragmentNames(node, _)
local fragments = {}
for _, definition in ipairs(node.definitions) do
if definition.kind == 'fragmentDefinition' then
Expand Down Expand Up @@ -348,7 +348,7 @@ function rules.fragmentSpreadIsPossible(node, context)
end
end

function rules.uniqueInputObjectFields(node, context)
function rules.uniqueInputObjectFields(node, _)
local function validateValue(value)
if value.kind == 'listType' or value.kind == 'nonNullType' then
return validateValue(value.type)
Expand Down
9 changes: 6 additions & 3 deletions cartridge/graphql/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ end

types.int = types.scalar({
name = 'Int',
description = "The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values from -(2^31) to 2^31 - 1, inclusive.",
description = "The `Int` scalar type represents non-fractional signed whole numeric values. " ..
"Int can represent values from -(2^31) to 2^31 - 1, inclusive.",
serialize = coerceInt,
parseValue = coerceInt,
parseLiteral = function(node)
Expand All @@ -309,7 +310,8 @@ types.int = types.scalar({

types.long = types.scalar({
name = 'Long',
description = "The `Long` scalar type represents non-fractional signed whole numeric values. Long can represent values from -(2^52) to 2^52 - 1, inclusive.",
description = "The `Long` scalar type represents non-fractional signed whole numeric values. " ..
"Long can represent values from -(2^52) to 2^52 - 1, inclusive.",
serialize = coerceLong,
parseValue = coerceLong,
parseLiteral = function(node)
Expand All @@ -336,7 +338,8 @@ types.float = types.scalar({

types.string = types.scalar({
name = 'String',
description = "The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.",
description = "The `String` scalar type represents textual data, represented as UTF-8 character sequences. " ..
"The String type is most often used by GraphQL to represent free-form human-readable text.",
serialize = tostring,
parseValue = tostring,
parseLiteral = function(node)
Expand Down
16 changes: 8 additions & 8 deletions cartridge/graphql/validate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local visitors = {
end
end,

children = function(node, context)
children = function(node, _)
return node.definitions
end,

Expand All @@ -41,7 +41,7 @@ local visitors = {
context.variableReferences = {}
end,

exit = function(node, context)
exit = function(_, context)
table.remove(context.objects)
context.currentOperation = nil
context.variableReferences = nil
Expand Down Expand Up @@ -85,7 +85,7 @@ local visitors = {
end
end,

exit = function(node, context)
exit = function(_, context)
table.remove(context.objects)
end,

Expand Down Expand Up @@ -135,11 +135,11 @@ local visitors = {
table.insert(context.objects, kind)
end,

exit = function(node, context)
exit = function(_, context)
table.remove(context.objects)
end,

children = function(node, context)
children = function(node, _)
if node.selectionSet then
return {node.selectionSet}
end
Expand Down Expand Up @@ -205,7 +205,7 @@ local visitors = {
end
end,

exit = function(node, context)
exit = function(_, context)
table.remove(context.objects)
end,

Expand All @@ -223,7 +223,7 @@ local visitors = {
table.insert(context.objects, kind)
end,

exit = function(node, context)
exit = function(_, context)
table.remove(context.objects)
end,

Expand Down Expand Up @@ -293,7 +293,7 @@ local visitors = {
},

directive = {
children = function(node, context)
children = function(node, _)
return node.arguments
end
}
Expand Down

0 comments on commit c94918c

Please sign in to comment.