From 39a3e14b1f445cc710767d5391feffb95372f1e0 Mon Sep 17 00:00:00 2001 From: Oleg Babin Date: Fri, 11 Dec 2020 23:51:08 +0300 Subject: [PATCH] prepare to public release --- .gitignore | 28 ++++++ .gitmodules | 3 + CMakeLists.txt | 28 ++++++ FindTarantool.cmake | 45 +++++++++ README.md | 165 +++++++++++++++++++++++++++++++ ckit/CMakeLists.txt | 13 --- ckit/init.lua | 41 -------- libgraphqlparser | 1 + luagraphqlparser-scm-1.rockspec | 49 +++++++++ luagraphqlparser/CMakeLists.txt | 13 +++ luagraphqlparser/init.lua | 1 + {ckit => luagraphqlparser}/lib.c | 19 +--- test/definitions_test.lua | 23 +++++ test/directives_test.lua | 20 ++++ test/fragment_test.lua | 106 ++++++++++++++++++++ test/misc_test.lua | 54 ++++++++++ test/selections_test.lua | 15 +++ test/values_test.lua | 127 ++++++++++++++++++++++++ test/variables_test.lua | 54 ++++++++++ 19 files changed, 736 insertions(+), 69 deletions(-) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 FindTarantool.cmake create mode 100644 README.md delete mode 100644 ckit/CMakeLists.txt delete mode 100644 ckit/init.lua create mode 160000 libgraphqlparser create mode 100644 luagraphqlparser-scm-1.rockspec create mode 100644 luagraphqlparser/CMakeLists.txt create mode 100644 luagraphqlparser/init.lua rename {ckit => luagraphqlparser}/lib.c (96%) create mode 100755 test/definitions_test.lua create mode 100755 test/directives_test.lua create mode 100755 test/fragment_test.lua create mode 100755 test/misc_test.lua create mode 100755 test/selections_test.lua create mode 100755 test/values_test.lua create mode 100755 test/variables_test.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1a845cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +CMakeFiles/ +obj-*/ +CMakeCache.txt +Makefile +cmake_*.cmake +install_manifest.txt +*.a +*.cbp +*.d +*.dylib +*.gcno +*.gcda +*.user +*.o +*.reject +*.so +*.snap* +*.xlog* +*~ +.gdb_history +./build +/build +VERSION +CTestTestfile.cmake +Testing +cmake-build-debug/ +build.luarocks/ +.idea/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1dbfe20 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libgraphqlparser"] + path = libgraphqlparser + url = https://github.com/graphql/libgraphqlparser diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ea10761 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 2.8 FATAL_ERROR) + +project(luagraphqlparser C CXX) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE RelWithDebInfo) +endif() +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" ${CMAKE_MODULE_PATH}) + +# Find Tarantool and Lua dependencies +set(TARANTOOL_FIND_REQUIRED ON) +find_package(Tarantool) +include_directories(${TARANTOOL_INCLUDE_DIRS}) + +# Find other dependencies + +add_subdirectory(libgraphqlparser EXCLUDE_FROM_ALL) +include_directories("${CMAKE_SOURCE_DIR}/libgraphqlparser" "${CMAKE_BINARY_DIR}/libgraphqlparser") + +# Set CFLAGS +# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wextra -Werror") +# Set CXXFLAGS +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -Werror") + +# Build module +add_subdirectory(luagraphqlparser) diff --git a/FindTarantool.cmake b/FindTarantool.cmake new file mode 100644 index 0000000..a938158 --- /dev/null +++ b/FindTarantool.cmake @@ -0,0 +1,45 @@ +# Define GNU standard installation directories +include(GNUInstallDirs) + +macro(extract_definition name output input) + string(REGEX MATCH "#define[\t ]+${name}[\t ]+\"([^\"]*)\"" + _t "${input}") + string(REGEX REPLACE "#define[\t ]+${name}[\t ]+\"(.*)\"" "\\1" + ${output} "${_t}") +endmacro() + +find_path(TARANTOOL_INCLUDE_DIR tarantool/module.h + HINTS ${TARANTOOL_DIR} ENV TARANTOOL_DIR + PATH_SUFFIXES include +) + +if(TARANTOOL_INCLUDE_DIR) + set(_config "-") + file(READ "${TARANTOOL_INCLUDE_DIR}/tarantool/module.h" _config0) + string(REPLACE "\\" "\\\\" _config ${_config0}) + unset(_config0) + extract_definition(PACKAGE_VERSION TARANTOOL_VERSION ${_config}) + extract_definition(INSTALL_PREFIX _install_prefix ${_config}) + unset(_config) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(TARANTOOL + REQUIRED_VARS TARANTOOL_INCLUDE_DIR VERSION_VAR TARANTOOL_VERSION) +if(TARANTOOL_FOUND) + set(TARANTOOL_INCLUDE_DIRS "${TARANTOOL_INCLUDE_DIR}" + "${TARANTOOL_INCLUDE_DIR}/tarantool/" + CACHE PATH "Include directories for Tarantool") + set(TARANTOOL_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/tarantool" + CACHE PATH "Directory for storing Lua modules written in Lua") + set(TARANTOOL_INSTALL_LUADIR "${CMAKE_INSTALL_DATADIR}/tarantool" + CACHE PATH "Directory for storing Lua modules written in C") + + if (NOT TARANTOOL_FIND_QUIETLY AND NOT FIND_TARANTOOL_DETAILS) + set(FIND_TARANTOOL_DETAILS ON CACHE INTERNAL "Details about TARANTOOL") + message(STATUS "Tarantool LUADIR is ${TARANTOOL_INSTALL_LUADIR}") + message(STATUS "Tarantool LIBDIR is ${TARANTOOL_INSTALL_LIBDIR}") + endif () +endif() +mark_as_advanced(TARANTOOL_INCLUDE_DIRS TARANTOOL_INSTALL_LIBDIR + TARANTOOL_INSTALL_LUADIR) diff --git a/README.md b/README.md new file mode 100644 index 0000000..9e680e7 --- /dev/null +++ b/README.md @@ -0,0 +1,165 @@ +### Luagraphqlparser + +Lua-binding of [libgraphqlparser](https://github.com/graphql/libgraphqlparser) +compatible with [graphql-lua](https://github.com/bjornbytes/graphql-lua). + +Differences from graphql-lua parser: + * Improved error messages + * Null support + +Usage: +```lua +parse = require('luagraphqlparser').parse +parse([[ + query HeroComparison($first: Int = 3) { + leftComparison: hero(episode: EMPIRE) { + ...comparisonFields + } + rightComparison: hero(episode: JEDI) { + ...comparisonFields + } + } + + fragment comparisonFields on Character { + name + friendsConnection(first: $first) { + totalCount + edges { + node { + name + } + } + } + } +]]) +--- +- definitions: + - operation: query + kind: operation + variableDefinitions: + - type: + name: + kind: name + value: Int + kind: namedType + kind: variableDefinition + variable: + name: + kind: name + value: first + kind: variable + defaultValue: + kind: int + value: '3' + name: + kind: name + value: HeroComparison + selectionSet: + selections: + - selectionSet: + selections: + - name: + kind: name + value: comparisonFields + kind: fragmentSpread + kind: selectionSet + kind: field + alias: + name: + kind: name + value: leftComparison + kind: alias + name: + kind: name + value: hero + arguments: + - kind: argument + name: + kind: name + value: episode + value: + kind: enum + value: EMPIRE + - selectionSet: + selections: + - name: + kind: name + value: comparisonFields + kind: fragmentSpread + kind: selectionSet + kind: field + alias: + name: + kind: name + value: rightComparison + kind: alias + name: + kind: name + value: hero + arguments: + - kind: argument + name: + kind: name + value: episode + value: + kind: enum + value: JEDI + kind: selectionSet + - typeCondition: + name: + kind: name + value: Character + kind: namedType + selectionSet: + selections: + - name: + kind: name + value: name + kind: field + - selectionSet: + selections: + - name: + kind: name + value: totalCount + kind: field + - selectionSet: + selections: + - selectionSet: + selections: + - name: + kind: name + value: name + kind: field + kind: selectionSet + name: + kind: name + value: node + kind: field + kind: selectionSet + name: + kind: name + value: edges + kind: field + kind: selectionSet + arguments: + - kind: argument + name: + kind: name + value: first + value: + name: + kind: name + value: first + kind: variable + name: + kind: name + value: friendsConnection + kind: field + kind: selectionSet + name: + kind: name + value: comparisonFields + kind: fragmentDefinition + kind: document +... +``` diff --git a/ckit/CMakeLists.txt b/ckit/CMakeLists.txt deleted file mode 100644 index 0c73555..0000000 --- a/ckit/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -if (APPLE) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined suppress -flat_namespace") -endif(APPLE) - -# Add C library -add_library(lib SHARED lib.c) -add_dependencies(lib graphqlparser) -target_link_libraries(lib graphqlparser) -set_target_properties(lib PROPERTIES PREFIX "" OUTPUT_NAME "lib") - -# Install module -install(FILES init.lua DESTINATION ${TARANTOOL_INSTALL_LUADIR}/${PROJECT_NAME}/) -install(TARGETS lib LIBRARY DESTINATION ${TARANTOOL_INSTALL_LIBDIR}/${PROJECT_NAME}/) diff --git a/ckit/init.lua b/ckit/init.lua deleted file mode 100644 index ab85edf..0000000 --- a/ckit/init.lua +++ /dev/null @@ -1,41 +0,0 @@ --------------------------------------------------------------------------------- ---- Example of a Lua module for Tarantool --------------------------------------------------------------------------------- - --- --- Dependencies --- - -local log = require('log') -- some other Tarantool module - --- C library -local clib = require('ckit.lib') --- Now you can use exported C functions from 'ckit/lib.c' submodule in your code - --- --- Constants --- - --- local variables are only visible from this file -local SOME_CONSTANT = 10 - --- --- Internal functions --- - --- Some internal function -local function func(a, b) - log.info("func() called with a=%s b=%s", a, b) - return a + b -end - --- --- Exported functions --- - --- result returned from require('ckit') -return { - func = func; -- pure Lua function - parse = clib.parse; -- C function -} --- vim: ts=4 sts=4 sw=4 et diff --git a/libgraphqlparser b/libgraphqlparser new file mode 160000 index 0000000..f8928a3 --- /dev/null +++ b/libgraphqlparser @@ -0,0 +1 @@ +Subproject commit f8928a3d2b4234534937c1fb47810d6ae583d83a diff --git a/luagraphqlparser-scm-1.rockspec b/luagraphqlparser-scm-1.rockspec new file mode 100644 index 0000000..6d5e3a8 --- /dev/null +++ b/luagraphqlparser-scm-1.rockspec @@ -0,0 +1,49 @@ +-- name of the package to be published +package = 'luagraphqlparser' + +-- version of the package; it's mandatory, but we don't use it in Tarantool; +-- instead, provide below a specific branch in the package's repository at +-- GitHub and set version to some stub value, e.g. 'scm-1' +version = 'scm-1' + +-- url and branch of the package's repository at GitHub +source = { + url = 'git://github.com/olegrok/luagraphqlparser.git'; + branch = 'master'; +} + +-- general information about the package; +-- for a Tarantool package, we require three fields (summary, homepage, license) +-- and more package information is always welcome +description = { + summary = "Lua graphql parser"; + detailed = [[ + Lua port of libgraphqlparser + ]]; + homepage = 'https://github.com/olegrok/luagraphqlparser.git'; + maintainer = "Oleg Babin "; + license = 'BSD2'; +} + +-- Lua version and other packages on which this one depends; +-- Tarantool currently supports strictly Lua 5.1 +dependencies = { + 'lua == 5.1'; +} + +-- build options and paths for the package; +-- this package distributes modules in C, so the build type = 'cmake'; +-- also, specify here all variables required for build: +-- CMAKE_BUILD_TYPE = Tarantool build type (default is RelWithDebInfo) +-- TARANTOOL_INSTALL_LIBDIR = path to all C header files within the package, +-- TARANTOOL_INSTALL_LUADIR = path to all Lua source files within the package +build = { + type = 'cmake'; + variables = { + CMAKE_BUILD_TYPE="RelWithDebInfo"; + TARANTOOL_DIR="$(TARANTOOL_DIR)"; + TARANTOOL_INSTALL_LIBDIR="$(LIBDIR)"; + TARANTOOL_INSTALL_LUADIR="$(LUADIR)"; + }; +} +-- vim: syntax=lua ts=4 sts=4 sw=4 et diff --git a/luagraphqlparser/CMakeLists.txt b/luagraphqlparser/CMakeLists.txt new file mode 100644 index 0000000..5a60ee5 --- /dev/null +++ b/luagraphqlparser/CMakeLists.txt @@ -0,0 +1,13 @@ +if (APPLE) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined suppress -flat_namespace") +endif(APPLE) + +# Add C library +add_library(luagraphqlparser SHARED lib.c) +add_dependencies(luagraphqlparser graphqlparser) +target_link_libraries(luagraphqlparser graphqlparser) +set_target_properties(luagraphqlparser PROPERTIES PREFIX "" OUTPUT_NAME "lib") + +# Install module +install(TARGETS luagraphqlparser LIBRARY DESTINATION ${TARANTOOL_INSTALL_LIBDIR}/${PROJECT_NAME}/) +install(FILES init.lua DESTINATION ${TARANTOOL_INSTALL_LUADIR}/${PROJECT_NAME}/) diff --git a/luagraphqlparser/init.lua b/luagraphqlparser/init.lua new file mode 100644 index 0000000..4ae3714 --- /dev/null +++ b/luagraphqlparser/init.lua @@ -0,0 +1 @@ +return require('luagraphqlparser.lib') \ No newline at end of file diff --git a/ckit/lib.c b/luagraphqlparser/lib.c similarity index 96% rename from ckit/lib.c rename to luagraphqlparser/lib.c index 7c63798..4c73183 100644 --- a/ckit/lib.c +++ b/luagraphqlparser/lib.c @@ -36,7 +36,6 @@ end_visit_document(const struct GraphQLAstDocument *def, void *arg) static int visit_operation_definition(const struct GraphQLAstOperationDefinition *def, void *arg) { - printf("visit_operation_definition\n"); struct lua_State *L = arg; // len = #definitions @@ -84,12 +83,9 @@ end_visit_operation_definition(const struct GraphQLAstOperationDefinition *def, static int visit_selection_set(const struct GraphQLAstSelectionSet *def, void *arg) { + (void)def; struct lua_State *L = arg; - int size = GraphQLAstSelectionSet_get_selections_size(def); - - printf("visit_selection_set %d\n", size); - // selectionSet: { lua_newtable(L); @@ -109,7 +105,6 @@ static void end_visit_selection_set(const struct GraphQLAstSelectionSet *def, void *arg) { int size = GraphQLAstSelectionSet_get_selections_size(def); - printf("end_visit_selection_set %d\n", size); struct lua_State *L = arg; @@ -224,7 +219,6 @@ end_visit_name(const struct GraphQLAstName *def, void *arg) static int visit_variable_definition(const struct GraphQLAstVariableDefinition *def, void *arg) { - printf("visit_variable_definition\n"); struct lua_State *L = arg; lua_getfield(L, -1, "variableDefinitions"); @@ -343,10 +337,7 @@ end_visit_argument(const struct GraphQLAstArgument *def, void *arg) static int visit_variable(const struct GraphQLAstVariable *def, void *arg) { - const struct GraphQLAstName *name_node = GraphQLAstVariable_get_name(def); - const char *name = GraphQLAstName_get_value(name_node); - - printf("visit_variable %s\n", name); + (void)def; struct lua_State *L = arg; lua_newtable(L); @@ -436,7 +427,6 @@ end_visit_object_field(const struct GraphQLAstObjectField *def, void *arg) static int \ visit_##type##_value(const struct GraphQLAst##snake_type##Value *def, void *arg) \ { \ - printf("visit_"#type"_value\n"); \ struct lua_State *L = arg; \ lua_newtable(L); \ \ @@ -816,11 +806,10 @@ graphql_parse(struct lua_State *L) return 1; } -/* exported function */ LUA_API int -luaopen_ckit_lib(lua_State *L) +luaopen_luagraphqlparser_lib(lua_State *L) { - /* result returned from require('ckit.lib') */ + /* result returned from require('luagraphqlparser.lib') */ lua_newtable(L); static const struct luaL_Reg meta [] = { {"parse", graphql_parse}, diff --git a/test/definitions_test.lua b/test/definitions_test.lua new file mode 100755 index 0000000..99f0dc0 --- /dev/null +++ b/test/definitions_test.lua @@ -0,0 +1,23 @@ +local t = require('luatest') +local g = t.group() + +local parse = require('luagraphqlparser').parse +local cartridge_parse = require('cartridge.graphql.parse').parse + +function g.test_several_definitions() + local query = [[ + query { test(a: 123) } + mutation { test(a: 123) } + mutation { test2(a: 123) } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_several_definitions_with_aliases() + local query = [[ + query { a1: test(a: 123) } + mutation { test(a: 123) } + mutation { a3: test2(a: 123) } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end diff --git a/test/directives_test.lua b/test/directives_test.lua new file mode 100755 index 0000000..60fa150 --- /dev/null +++ b/test/directives_test.lua @@ -0,0 +1,20 @@ +local t = require('luatest') +local g = t.group() + +local parse = require('luagraphqlparser').parse +local cartridge_parse = require('cartridge.graphql.parse').parse + +function g.test_field() + local query = [[ query { friends @include(if: $withFriends) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_subselections() + local query = [[ + query { + friends @include(if: $withFriends val: 2) @exclude { name { v } } + friends2 @include(if: $withFriends val: 2) @exclude { name { v } } + } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end diff --git a/test/fragment_test.lua b/test/fragment_test.lua new file mode 100755 index 0000000..47dfbbd --- /dev/null +++ b/test/fragment_test.lua @@ -0,0 +1,106 @@ +local t = require('luatest') +local g = t.group() + +local parse = require('luagraphqlparser').parse +local cartridge_parse = require('cartridge.graphql.parse').parse + +function g.test_simple_fragment_definition() + local query = [[ + fragment f on Obj { k } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_simple_fragment_definition_with_alias() + local query = [[ + fragment f on Obj { a: k } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_fragment_definition() + local query = [[ + fragment f on Obj { + key + value { nested_obj { nested_field } } + } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_fragment_spread() + local query = [[ + { query { ...spread } } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_fragment_complex() + local query = [[ + { + leftComparison: hero(episode: EMPIRE) { + ...comparisonFields + } + rightComparison: hero(episode: JEDI) { + ...comparisonFields + } + } + + fragment comparisonFields on Character { + name + appearsIn + friends { + name + } + } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_fragment_with_variables() + local query = [[ + query HeroComparison($first: Int = 3) { + leftComparison: hero(episode: EMPIRE) { + ...comparisonFields + } + rightComparison: hero(episode: JEDI) { + ...comparisonFields + } + } + + fragment comparisonFields on Character { + name + friendsConnection(first: $first) { + totalCount + edges { + node { + name + } + } + } + } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_fragment_inline_simple() + local query = [[ { value { ... on Human { height } } } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_fragment_inline_complex() + local query = [[ + query HeroForEpisode($ep: Episode!) { + hero(episode: $ep) { + name + ... on Droid { + primaryFunction + } + ... on Human { + height + } + } + } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end diff --git a/test/misc_test.lua b/test/misc_test.lua new file mode 100755 index 0000000..ed10dfb --- /dev/null +++ b/test/misc_test.lua @@ -0,0 +1,54 @@ +local t = require('luatest') +local g = t.group() + +local parse = require('luagraphqlparser').parse +local cartridge_parse = require('cartridge.graphql.parse').parse + +function g.test_operation_name() + local query = [[ + query HeroNameAndFriends { + hero { + name + friends { + name + } + } + } +]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_comment() + local query = [[ + query HeroNameAndFriends { # Comment + hero { # Comment + name + friends { # Comment + name + } + } + } + # Comment +]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_metafields() + local query = [[ + { + search(text: "an") { + __typename + ... on Human { + name + } + ... on Droid { + name + } + ... on Starship { + name + } + } + } +]] + t.assert_equals(parse(query), cartridge_parse(query)) +end diff --git a/test/selections_test.lua b/test/selections_test.lua new file mode 100755 index 0000000..8b514ab --- /dev/null +++ b/test/selections_test.lua @@ -0,0 +1,15 @@ +local t = require('luatest') +local g = t.group() + +local parse = require('luagraphqlparser').parse +local cartridge_parse = require('cartridge.graphql.parse').parse + +function g.test_select_field() + local query = [[ { test { a } } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_select_set() + local query = [[ { test { a { b { c d } e } } } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end diff --git a/test/values_test.lua b/test/values_test.lua new file mode 100755 index 0000000..9a62717 --- /dev/null +++ b/test/values_test.lua @@ -0,0 +1,127 @@ +local t = require('luatest') +local g = t.group() + +local parse = require('luagraphqlparser').parse +local cartridge_parse = require('cartridge.graphql.parse').parse + +function g.test_int() + local query = [[ query { test(a: 123) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_bool() + local query = [[ query { test(a: bool) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_float() + local query = [[ query { test(a: 2.5) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_enum() + local query = [[ query { test(a: en) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_string() + local query = [[ query { test(a: "str") } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_null() + local query = [[ query { test(a: null) } ]] + local expected = { + definitions = { + { + kind = "operation", + operation = "query", + selectionSet = { + kind = "selectionSet", + selections = { + { + arguments = { + { + kind = "argument", + name = {kind = "name", value = "a"}, + value = {kind = "null", value = "null"}, + }, + }, + kind = "field", + name = {kind = "name", value = "test"}, + }, + }, + }, + }, + }, + kind = "document", + } + t.assert_equals(parse(query), expected) +end + +function g.test_empty_list() + local query = [[ query { test(a: []) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_list_simple() + local query = [[ query { test(a: [1, "a", 2.5, false]) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_empty_object() + local query = [[ query { test(a: {}) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_object_simple() + local query = [[ query { test(a: {i: 1 f: 2.5 s: "s" e: en b: true}) } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_nesting() + local query = [==[ query { test(a: {l: [1, 2, {c: [true, false, {c: {d: {e: 5}}}]}, [1]]}) } ]==] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_value_with_slash() + local query = [[ query { a(id: "\\\\a\\\\") } ]] + t.assert_equals(parse(query), { + definitions = { + { + kind = "operation", + operation = "query", + selectionSet = { + kind = "selectionSet", + selections = { + { + arguments = { + { + kind = "argument", + name = {kind = "name", value = "id"}, + value = {kind = "string", value = "\\\\a\\\\"}, + }, + }, + kind = "field", + name = {kind = "name", value = "a"}, + }, + }, + }, + }, + }, + kind = "document", + }) +end + +function g.test_example() + local query = [[ + { + human(id: "1000") { + name + height + } + } + ]] + + t.assert_equals(parse(query), cartridge_parse(query)) +end diff --git a/test/variables_test.lua b/test/variables_test.lua new file mode 100755 index 0000000..737e23b --- /dev/null +++ b/test/variables_test.lua @@ -0,0 +1,54 @@ +local t = require('luatest') +local g = t.group() + +local parse = require('luagraphqlparser').parse +local cartridge_parse = require('cartridge.graphql.parse').parse + +function g.test_int() + local query = [[ query($val: Int) { test } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_string() + local query = [[ query($val: String) { test } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_long_boolean() + local query = [[ query($l: Long $b: Boolean) { test } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_non_null() + local query = [[ query($l: Long! $b: Boolean!) { test } ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_list() + local query = [=[ query($l: [Long!] $b: [Boolean] $s: [String]! $l: [[Int]]) { test } ]=] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_custom_type() + local query = [=[ query($ct: CustomType) { test } ]=] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_default() + local query = [=[ query($ct: String = [{a: 5}, "test", true, [1]]) { test(a: "String", b: "str") } ]=] + t.assert_equals(parse(query), cartridge_parse(query)) +end + +function g.test_complex() + local query = [[ + query HeroNameAndFriends($episode: Episode) { + hero(episode: $episode) { + name + friends { + name + } + } + } + ]] + t.assert_equals(parse(query), cartridge_parse(query)) +end