This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Kh/to avro #58
Merged
Merged
Kh/to avro #58
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
local path = (...):gsub('%.[^%.]+$', '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that you more or less investigated this functions. It would be glab to see brief comments for them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have just copied). I will do further investigations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, (...) stores the string passed to require when the
|
||
local types = require(path .. '.types') | ||
local util = require(path .. '.util') | ||
|
||
local query_util = {} | ||
|
||
local function typeFromAST(node, schema) | ||
local innerType | ||
if node.kind == 'listType' then | ||
innerType = typeFromAST(node.type) | ||
return innerType and types.list(innerType) | ||
elseif node.kind == 'nonNullType' then | ||
innerType = typeFromAST(node.type) | ||
return innerType and types.nonNull(innerType) | ||
else | ||
assert(node.kind == 'namedType', 'Variable must be a named type') | ||
return schema:getType(node.name.value) | ||
end | ||
end | ||
|
||
local function getFieldResponseKey(field) | ||
return field.alias and field.alias.name.value or field.name.value | ||
end | ||
|
||
local function shouldIncludeNode(selection, context) | ||
if selection.directives then | ||
local function isDirectiveActive(key, _type) | ||
local directive = util.find(selection.directives, function(directive) | ||
return directive.name.value == key | ||
end) | ||
|
||
if not directive then return end | ||
|
||
local ifArgument = util.find(directive.arguments, function(argument) | ||
return argument.name.value == 'if' | ||
end) | ||
|
||
if not ifArgument then return end | ||
|
||
return util.coerceValue(ifArgument.value, _type.arguments['if'], context.variables) | ||
end | ||
|
||
if isDirectiveActive('skip', types.skip) then return false end | ||
if isDirectiveActive('include', types.include) == false then return false end | ||
end | ||
|
||
return true | ||
end | ||
|
||
local function doesFragmentApply(fragment, type, context) | ||
if not fragment.typeCondition then return true end | ||
|
||
local innerType = typeFromAST(fragment.typeCondition, context.schema) | ||
|
||
if innerType == type then | ||
return true | ||
elseif innerType.__type == 'Interface' then | ||
local implementors = context.schema:getImplementors(innerType.name) | ||
return implementors and implementors[type] | ||
elseif innerType.__type == 'Union' then | ||
return util.find(innerType.types, function(member) | ||
return member == type | ||
end) | ||
end | ||
end | ||
|
||
function query_util.collectFields(objectType, selections, visitedFragments, result, context) | ||
for _, selection in ipairs(selections) do | ||
if selection.kind == 'field' then | ||
if shouldIncludeNode(selection, context) then | ||
local name = getFieldResponseKey(selection) | ||
result[name] = result[name] or {} | ||
table.insert(result[name], selection) | ||
end | ||
elseif selection.kind == 'inlineFragment' then | ||
if shouldIncludeNode(selection, context) and doesFragmentApply(selection, objectType, context) then | ||
collectFields(objectType, selection.selectionSet.selections, visitedFragments, result, context) | ||
end | ||
elseif selection.kind == 'fragmentSpread' then | ||
local fragmentName = selection.name.value | ||
if shouldIncludeNode(selection, context) and not visitedFragments[fragmentName] then | ||
visitedFragments[fragmentName] = true | ||
local fragment = context.fragmentMap[fragmentName] | ||
if fragment and shouldIncludeNode(fragment, context) and doesFragmentApply(fragment, objectType, context) then | ||
collectFields(objectType, fragment.selectionSet.selections, visitedFragments, result, context) | ||
end | ||
end | ||
end | ||
end | ||
|
||
return result | ||
end | ||
|
||
function query_util.mergeSelectionSets(fields) | ||
local selections = {} | ||
|
||
for i = 1, #fields do | ||
local selectionSet = fields[i].selectionSet | ||
if selectionSet then | ||
for j = 1, #selectionSet.selections do | ||
table.insert(selections, selectionSet.selections[j]) | ||
end | ||
end | ||
end | ||
|
||
return selections | ||
end | ||
|
||
function query_util.buildContext(schema, tree, rootValue, variables, operationName) | ||
local context = { | ||
schema = schema, | ||
rootValue = rootValue, | ||
variables = variables, | ||
operation = nil, | ||
fragmentMap = {} | ||
} | ||
|
||
for _, definition in ipairs(tree.definitions) do | ||
if definition.kind == 'operation' then | ||
if not operationName and context.operation then | ||
error('Operation name must be specified if more than one operation exists.') | ||
end | ||
|
||
if not operationName or definition.name.value == operationName then | ||
context.operation = definition | ||
end | ||
elseif definition.kind == 'fragmentDefinition' then | ||
context.fragmentMap[definition.name.value] = definition | ||
end | ||
end | ||
|
||
if not context.operation then | ||
if operationName then | ||
error('Unknown operation "' .. operationName .. '"') | ||
else | ||
error('Must provide an operation') | ||
end | ||
end | ||
|
||
return context | ||
end | ||
|
||
return query_util |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you moved it from buildContext? The
qcontext
field looks as part of context to build up for me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it just because buildContext since now is uset in to_avro too. And it is not necessary in to_avro. Second, I suppose that we need to pass this variable from tarantool_graphql (compile or/and execute), for example to specify query timeout. What do you think?