|
| 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' .. ';' .. |
| 8 | + package.path |
| 9 | + |
| 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": { |
| 17 | + "name": "user", |
| 18 | + "type": "record", |
| 19 | + "fields": [ |
| 20 | + { "name": "id", "type": "string" }, |
| 21 | + { "name": "favs", "type": { "type": "array", "items": "string" } } |
| 22 | + ] |
| 23 | + } |
| 24 | +}]]) |
| 25 | + |
| 26 | +local collections = json.decode([[{ |
| 27 | + "user_collection": { |
| 28 | + "schema_name": "user" |
| 29 | +}]]) |
| 30 | + |
| 31 | +local function access_function(parent, collection_name, filter, args) |
| 32 | + --[[ |
| 33 | + print('DEBUG: collection_name: ' .. collection_name) |
| 34 | + print('DEBUG: filter: ' .. json.encode(filter)) |
| 35 | + print('DEBUG: args: ' .. json.encode(args)) |
| 36 | + print('DEBUG: --------') |
| 37 | + --]] |
| 38 | + local obj |
| 39 | + if collection_name == 'user_collection' then |
| 40 | + obj = { |
| 41 | + user_id = 'def', |
| 42 | + favs = {'potato', 'fruit'} |
| 43 | + } |
| 44 | + else |
| 45 | + error('NIY: ' .. collection_name) |
| 46 | + end |
| 47 | + if not utils.is_subtable(obj, filter) then |
| 48 | + return {} |
| 49 | + end |
| 50 | + return { obj } |
| 51 | +end |
| 52 | + |
| 53 | +local accessor = setmetatable({}, { |
| 54 | + __index = { |
| 55 | + select = function(self, parent, collection_name, connection_name, |
| 56 | + filter, args) |
| 57 | + return access_function(parent, collection_name, filter, args) |
| 58 | + end, |
| 59 | + list_args = function(self, connection_type) |
| 60 | + if connection_type == '1:1' then |
| 61 | + return {} |
| 62 | + end |
| 63 | + return { |
| 64 | + { name = 'limit', type = 'int' }, |
| 65 | + { name = 'offset', type = 'long' }, |
| 66 | + -- {name = 'filter', type = ...}, |
| 67 | + } |
| 68 | + end, |
| 69 | + } |
| 70 | +}) |
| 71 | + |
| 72 | +local gql_wrapper = graphql.new({ |
| 73 | +-- class_name:class mapping |
| 74 | + schemas = schemas, |
| 75 | +-- collection_{schema_name=..., connections=...} mapping |
| 76 | + collections = collections, |
| 77 | +-- :select() and :list_args() provider |
| 78 | + accessor = accessor, |
| 79 | +}) |
| 80 | + |
| 81 | +local query_1 = [[ |
| 82 | + query obtainUserFavs($user_id: String) { |
| 83 | + user_collection(user_id: $user_id, type: "type 1", size: 2) { |
| 84 | + id |
| 85 | + favs |
| 86 | + } |
| 87 | + } |
| 88 | +]] |
| 89 | + |
| 90 | +utils.show_trace(function() |
| 91 | + local variables_1 = { user_id = 'def' } |
| 92 | + local gql_query_1 = gql_wrapper:compile(query_1) |
| 93 | + local result = gql_query_1:execute(variables_1) |
| 94 | + print(('RESULT\n%s'):format(yaml.encode(result))) |
| 95 | +end) |
0 commit comments