|
| 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 | +box.cfg{background = false} |
| 10 | +local json = require('json') |
| 11 | +local yaml = require('yaml') |
| 12 | +local graphql = require('graphql') |
| 13 | +local utils = require('graphql.utils') |
| 14 | + |
| 15 | +local schemas = json.decode([[{ |
| 16 | + "user_collection": { |
| 17 | + "name": "user_collection", |
| 18 | + "type": "record", |
| 19 | + "fields": [ |
| 20 | + { "name": "user_id", "type": "string" }, |
| 21 | + { "name": "name", "type": "string" }, |
| 22 | + { "name": "stuff", "type": [ |
| 23 | + "null", |
| 24 | + "string", |
| 25 | + {"name": "balances", |
| 26 | + "type": {"type": "array", "items": "string" } |
| 27 | + } |
| 28 | + ] |
| 29 | + } |
| 30 | + ] |
| 31 | + } |
| 32 | +}]]) |
| 33 | + |
| 34 | +local collections = json.decode([[{ |
| 35 | + "user_collection": { |
| 36 | + "schema_name": "user_collection", |
| 37 | + "connections": [] |
| 38 | + } |
| 39 | +}]]) |
| 40 | + |
| 41 | +local service_fields = { |
| 42 | + user_collection = { |
| 43 | + {name = 'expires_on', type = 'long', default = 0} |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +local indexes = { |
| 48 | + user_collection = { |
| 49 | + user_id_index = { |
| 50 | + service_fields = {}, |
| 51 | + fields = {'user_id'}, |
| 52 | + index_type = 'tree', |
| 53 | + unique = true, |
| 54 | + primary = true, |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +local USER_ID = 2 |
| 60 | + |
| 61 | +box.schema.create_space('user_collection') |
| 62 | +box.space.user_collection:create_index('user_id_index', |
| 63 | + {type = 'tree', unique = true, parts = { USER_ID, 'string' }} |
| 64 | +) |
| 65 | + |
| 66 | +box.space.user_collection:replace( |
| 67 | + {1827767717, 'user_id_1', 'Ivan', 'Stuffy string'}) |
| 68 | + |
| 69 | +box.space.user_collection:replace( |
| 70 | + {1827767717, 'user_id_2', 'Bob', {'Union stuff str1', 'Union stuff str2'}}) |
| 71 | + |
| 72 | +box.space.user_collection:replace( |
| 73 | + {1827767717, 'user_id_3', 'Alice'}) |
| 74 | + |
| 75 | +local accessor = graphql.accessor_space.new({ |
| 76 | + schemas = schemas, |
| 77 | + collections = collections, |
| 78 | + service_fields = service_fields, |
| 79 | + indexes = indexes, |
| 80 | +}) |
| 81 | + |
| 82 | +local gql_wrapper = graphql.new({ |
| 83 | + schemas = schemas, |
| 84 | + collections = collections, |
| 85 | + accessor = accessor, |
| 86 | +}) |
| 87 | + |
| 88 | +--- note that inside mapBox there is an auto-generated name for |
| 89 | +--- avro-schema no-named map (it is necessary as we can not |
| 90 | +--- access no-named fields in GQL) |
| 91 | +local query_1 = [[ |
| 92 | + query user_collection { |
| 93 | + user_collection { |
| 94 | + user_id |
| 95 | + name |
| 96 | + stuff { |
| 97 | + ... on stringBox { |
| 98 | + string |
| 99 | + } |
| 100 | + ... on balancesBox { |
| 101 | + balances |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +]] |
| 107 | + |
| 108 | +utils.show_trace(function() |
| 109 | + |
| 110 | + --- extract GQL schema from gql_wrapper.state.nullable_collection_types |
| 111 | + --- and compare them with expected GQL schema |
| 112 | + local variables_1 = { } |
| 113 | + local gql_query_1 = gql_wrapper:compile(query_1) |
| 114 | + local result = gql_query_1:execute(variables_1) |
| 115 | + --- Validate result with initial avro-schema |
| 116 | + print(('RESULT\n%s'):format(yaml.encode(result))) |
| 117 | +end) |
| 118 | + |
| 119 | +box.space.user_collection:drop() |
0 commit comments