Skip to content

Commit 51b3bd9

Browse files
no1semanDifferentialOrange
authored andcommitted
Improve descriptions support
Fix using kind with object injections. Support descriptions in InputObject fields. Include descriptions in args introspection. Based on PR #22 by @no1seman
1 parent 7d2e9c9 commit 51b3bd9

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

graphql/execute.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ local function getFieldEntry(objectType, object, fields, context)
260260

261261
local arguments = util.map(fieldType.arguments or {}, function(argument, name)
262262
local supplied = argumentMap[name] and argumentMap[name].value
263+
if argument.kind then argument = argument.kind end
263264
return util.coerceValue(supplied, argument, context.variables, {
264265
strict_non_null = true,
265266
defaultValues = defaultValues,

graphql/introspection.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local __Schema, __Directive, __DirectiveLocation, __Type, __Field, __InputValue,
77
local function resolveArgs(field)
88
local function transformArg(arg, name)
99
if arg.__type then
10-
return { kind = arg, name = name }
10+
return { kind = arg, name = name, description = arg.description }
1111
elseif arg.name then
1212
return arg
1313
else

graphql/rules.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ local function isVariableTypesValid(argument, argumentType, context,
516516
if hasDefault and variableType.__type ~= 'NonNull' then
517517
variableType = types.nonNull(variableType)
518518
end
519+
if argumentType.kind then argumentType = argumentType.kind end
519520

520521
if not isTypeSubTypeOf(variableType, argumentType, context) then
521522
return false, ('Variable "%s" type mismatch: the variable type "%s" ' ..

graphql/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ function types.inputObject(config)
224224
fields[fieldName] = {
225225
name = fieldName,
226226
kind = field.kind,
227+
description = field.description,
227228
}
228229
end
229230

test/integration/graphql_test.lua

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,3 +1509,82 @@ function g.test_specifiedBy_directive()
15091509
t.assert_equals(data, { test_A = { url = "http://localhost", value = "1" } })
15101510
t.assert_equals(errors, nil)
15111511
end
1512+
1513+
function g.test_descriptions()
1514+
local function callback(_, _)
1515+
return nil
1516+
end
1517+
1518+
local query_schema = {
1519+
['test_query'] = {
1520+
kind = types.string.nonNull,
1521+
arguments = {
1522+
arg = types.string,
1523+
arg_described = {
1524+
kind = types.object({
1525+
name = 'test_object',
1526+
fields = {
1527+
object_arg_described = {
1528+
kind = types.string,
1529+
description = 'object argument'
1530+
},
1531+
object_arg = types.string,
1532+
},
1533+
kind = types.string,
1534+
}),
1535+
description = 'described query argument',
1536+
}
1537+
},
1538+
resolve = callback,
1539+
description = 'test query',
1540+
}
1541+
}
1542+
1543+
local mutation_schema = {
1544+
['test_mutation'] = {
1545+
kind = types.string.nonNull,
1546+
arguments = {
1547+
mutation_arg = types.string,
1548+
mutation_arg_described = {
1549+
kind = types.inputObject({
1550+
name = 'test_input_object',
1551+
fields = {
1552+
input_object_arg_described = {
1553+
kind = types.string,
1554+
description = 'input object argument'
1555+
},
1556+
input_object_arg = types.string,
1557+
},
1558+
kind = types.string,
1559+
}),
1560+
description = 'described mutation argument',
1561+
},
1562+
},
1563+
resolve = callback,
1564+
description = 'test mutation',
1565+
}
1566+
}
1567+
1568+
local data, errors = check_request(introspection.query, query_schema, mutation_schema)
1569+
t.assert_equals(errors, nil)
1570+
1571+
local test_query = util.find_by_name(data.__schema.types, 'Query')
1572+
t.assert_equals(test_query.fields[1].description, 'test query')
1573+
1574+
local arg_described = util.find_by_name(test_query.fields[1].args, 'arg_described')
1575+
t.assert_equals(arg_described.description, 'described query argument')
1576+
1577+
local test_object = util.find_by_name(data.__schema.types, 'test_object')
1578+
local object_arg_described = util.find_by_name(test_object.fields, 'object_arg_described')
1579+
t.assert_equals(object_arg_described.description, 'object argument')
1580+
1581+
local test_mutation = util.find_by_name(data.__schema.types, 'Mutation')
1582+
t.assert_equals(test_mutation.fields[1].description, 'test mutation')
1583+
1584+
local mutation_arg_described = util.find_by_name(test_mutation.fields[1].args, 'mutation_arg_described')
1585+
t.assert_equals(mutation_arg_described.description, 'described mutation argument')
1586+
1587+
local test_input_object = util.find_by_name(data.__schema.types, 'test_input_object')
1588+
local input_object_arg_described = util.find_by_name(test_input_object.inputFields, 'input_object_arg_described')
1589+
t.assert_equals(input_object_arg_described.description, 'input object argument')
1590+
end

0 commit comments

Comments
 (0)