|
| 1 | +#!/usr/bin/env tarantool |
| 2 | + |
| 3 | +local fio = require('fio') |
| 4 | + |
| 5 | +-- require in-repo version of graphql/ sources despite current working directory |
| 6 | +package.path = fio.abspath(debug.getinfo(1).source:match("@?(.*/)") |
| 7 | + :gsub('/./', '/'):gsub('/+$', '')) .. '/../../?.lua' .. ';' .. package.path |
| 8 | + |
| 9 | +local graphql = require('graphql') |
| 10 | +local testdata = require('test.testdata.common_testdata') |
| 11 | +local utils = require('graphql.utils') |
| 12 | +local yaml = require('yaml') |
| 13 | + |
| 14 | +-- init box, upload test data and acquire metadata |
| 15 | +-- ----------------------------------------------- |
| 16 | + |
| 17 | +-- init box and data schema |
| 18 | +box.cfg{background = false} |
| 19 | +testdata.init_spaces() |
| 20 | + |
| 21 | +-- upload test data |
| 22 | +testdata.fill_test_data() |
| 23 | + |
| 24 | +-- acquire metadata |
| 25 | +local metadata = testdata.get_test_metadata() |
| 26 | +local schemas = metadata.schemas |
| 27 | +local collections = metadata.collections |
| 28 | +local service_fields = metadata.service_fields |
| 29 | +local indexes = metadata.indexes |
| 30 | + |
| 31 | +-- build accessor and graphql schemas |
| 32 | +-- ---------------------------------- |
| 33 | + |
| 34 | +local accessor = graphql.accessor_space.new({ |
| 35 | + schemas = schemas, |
| 36 | + collections = collections, |
| 37 | + service_fields = service_fields, |
| 38 | + indexes = indexes, |
| 39 | +}) |
| 40 | + |
| 41 | +local gql_wrapper = graphql.new({ |
| 42 | + schemas = schemas, |
| 43 | + collections = collections, |
| 44 | + accessor = accessor, |
| 45 | +}) |
| 46 | + |
| 47 | +-- run queries |
| 48 | +-- ----------- |
| 49 | + |
| 50 | +local query_1 = [[ |
| 51 | + query user_by_order($first_name: String, $description: String, $include: Boolean) { |
| 52 | + order_collection(description: $description) { |
| 53 | + order_id |
| 54 | + description |
| 55 | + user_connection @include(if: $include, first_name: $first_name) { |
| 56 | + user_id |
| 57 | + last_name |
| 58 | + first_name |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + ]] |
| 63 | + |
| 64 | +local gql_query_1 = gql_wrapper:compile(query_1) |
| 65 | + |
| 66 | +-- should match 1 user |
| 67 | +utils.show_trace(function() |
| 68 | + local variables_1_1 = { |
| 69 | + first_name = 'Ivan', |
| 70 | + description = 'first order of Ivan', |
| 71 | + include = true |
| 72 | + } |
| 73 | + local result = gql_query_1:execute(variables_1_1) |
| 74 | + print(('RESULT\n%s'):format(yaml.encode(result))) |
| 75 | +end) |
| 76 | + |
| 77 | +utils.show_trace(function() |
| 78 | + local variables_1_2 = { |
| 79 | + first_name = 'Ivan', |
| 80 | + description = 'first order of Ivan', |
| 81 | + include = false |
| 82 | + } |
| 83 | + local result = gql_query_1:execute(variables_1_2) |
| 84 | + print(('RESULT\n%s'):format(yaml.encode(result))) |
| 85 | +end) |
| 86 | + |
| 87 | +local query_2 = [[ |
| 88 | + query user_by_order($first_name: String, $description: String, $skip: Boolean) { |
| 89 | + order_collection(description: $description) { |
| 90 | + order_id |
| 91 | + description |
| 92 | + user_connection @skip(if: $skip, first_name: $first_name) { |
| 93 | + user_id |
| 94 | + last_name |
| 95 | + first_name |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + ]] |
| 100 | + |
| 101 | +local gql_query_2 = gql_wrapper:compile(query_2) |
| 102 | + |
| 103 | +utils.show_trace(function() |
| 104 | + local variables_2_1 = { |
| 105 | + first_name = 'Ivan', |
| 106 | + description = 'first order of Ivan', |
| 107 | + skip = true |
| 108 | + } |
| 109 | + local result = gql_query_2:execute(variables_2_1) |
| 110 | + print(('RESULT\n%s'):format(yaml.encode(result))) |
| 111 | +end) |
| 112 | + |
| 113 | +utils.show_trace(function() |
| 114 | + local variables_2_2 = { |
| 115 | + first_name = 'Ivan', |
| 116 | + description = 'first order of Ivan', |
| 117 | + skip = false |
| 118 | + } |
| 119 | + local result = gql_query_2:execute(variables_2_2) |
| 120 | + print(('RESULT\n%s'):format(yaml.encode(result))) |
| 121 | +end) |
| 122 | +-- clean up |
| 123 | +-- -------- |
| 124 | + |
| 125 | +testdata.drop_spaces() |
| 126 | + |
| 127 | +os.exit() |
0 commit comments