From 2d095aaf06c4880d7070e09d1954a2441767f28f Mon Sep 17 00:00:00 2001 From: Johannes Terblanche Date: Tue, 22 Oct 2024 11:15:53 +0200 Subject: [PATCH 1/2] executeGql wrapper --- src/services/Agent.jl | 33 ++++++------------------------ src/services/BlobEntry.jl | 42 ++++++++++----------------------------- src/services/Common.jl | 15 ++++++++++++++ 3 files changed, 32 insertions(+), 58 deletions(-) diff --git a/src/services/Agent.jl b/src/services/Agent.jl index 0ab7a1e..c3fad0d 100644 --- a/src/services/Agent.jl +++ b/src/services/Agent.jl @@ -15,13 +15,7 @@ function getAgent(client::NavAbilityClient, label::Symbol) T = Vector{NvaNode{Agent}} - response = GQL.execute( - client.client, - QUERY_GET_AGENT, - T; - variables, - throw_on_execution_error = true, - ) + response = executeGql(client, QUERY_GET_AGENT, variables, T) return handleQuery(response, "agents", label) end @@ -55,13 +49,7 @@ function addAgent!(client::NavAbilityClient, label::Symbol, agent = nothing; age # AgentRemoteResponse T = @NamedTuple{agents::Vector{NvaNode{Agent}}} - response = GQL.execute( - client.client, - GQL_ADD_AGENTS, - T; - variables, - throw_on_execution_error = true, - ) + response = executeGql(client, GQL_ADD_AGENTS, variables, T) return handleMutate(response, "addAgents", :agents)[1] end @@ -81,13 +69,7 @@ function listAgents(client::NavAbilityClient) T = Vector{Dict{String, Vector{@NamedTuple{label::Symbol}}}} - response = GQL.execute( - client.client, - QUERY_LIST_AGENTS, - T; - variables, - throw_on_execution_error = true, - ) + response = executeGql(client, QUERY_LIST_AGENTS, variables, T) return last.(handleQuery(response, "orgs", Symbol(client.id))["agents"]) end @@ -102,12 +84,9 @@ query getAgentMetadata($id: ID!) { function DFG.getAgentMetadata(fgclient::NavAbilityDFG) variables = (id = getId(fgclient.agent),) - response = GQL.execute( - fgclient.client.client, - QUERY_GET_AGENT_METADATA; - variables, - throw_on_execution_error = true, - ) + + response = executeGql(fgclient, QUERY_GET_AGENT_METADATA, variables, Any) + b64data = handleQuery(response, "agents", fgclient.agent.label)["metadata"] if isnothing(b64data) return Dict{Symbol, DFG.SmallDataTypes}() diff --git a/src/services/BlobEntry.jl b/src/services/BlobEntry.jl index b8b53eb..24ed0cd 100644 --- a/src/services/BlobEntry.jl +++ b/src/services/BlobEntry.jl @@ -8,13 +8,7 @@ function getBlobEntry(fgclient::NavAbilityDFG, variableLabel::Symbol, label::Sym T = Vector{DFG.BlobEntry} - response = GQL.execute( - fgclient.client.client, - GQL_GET_BLOBENTRY, - T; - variables=(id=id,), - throw_on_execution_error = true, - ) + response = executeGql(fgclient, GQL_GET_BLOBENTRY, (id=id,), T) return handleQuery(response, "blobEntries", label) end @@ -202,14 +196,8 @@ function addBlobEntries!( T = @NamedTuple{blobEntries::Vector{BlobEntry}} - response = GQL.execute( - fgclient.client.client, - GQL_ADD_BLOBENTRIES, - T; #FIXME BlobEntryResponse - # BlobEntryResponse; - variables = (blobEntries=input,), - throw_on_execution_error = true, - ) + response = executeGql(fgclient, GQL_ADD_BLOBENTRIES, (blobEntries=input,), T) + return handleMutate(response, "addBlobEntries", :blobEntries) end @@ -230,33 +218,25 @@ function DFG.listGraphBlobEntries(fgclient::NavAbilityDFG) T = Vector{Dict{String, Vector{@NamedTuple{label::Symbol}}}} - response = GQL.execute( - fgclient.client.client, - GQL_LIST_FACTORGRAPH_BLOBENTRIES, - T; - variables, - throw_on_execution_error = true, - ) + response = executeGql(fgclient, GQL_LIST_FACTORGRAPH_BLOBENTRIES, variables, T) return last.(handleQuery(response, "factorgraphs", fgclient.fg.label)["blobEntries"]) end function DFG.listAgentBlobEntries(fgclient::NavAbilityDFG) + getAgentBlobEntries(fgclient.client, fgclient.agent) +end - variables = (id=getId(fgclient.agent),) +function DFG.listAgentBlobEntries(client::NavAbilityClient, agent::NvaNode{Agent}) + + variables = (id=getId(agent),) T = Vector{Dict{String, Vector{@NamedTuple{label::Symbol}}}} - response = GQL.execute( - fgclient.client.client, - GQL_LIST_AGENT_BLOBENTRIES, - T; - variables, - throw_on_execution_error = true, - ) + response = executeGql(client, GQL_LIST_AGENT_BLOBENTRIES, variables, T) - return last.(handleQuery(response, "agents", fgclient.agent.label)["blobEntries"]) + return last.(handleQuery(response, "agents", agent.label)["blobEntries"]) end diff --git a/src/services/Common.jl b/src/services/Common.jl index 56ec30c..ec27eee 100644 --- a/src/services/Common.jl +++ b/src/services/Common.jl @@ -6,6 +6,21 @@ function getCommonProperties(::Type{T}, from::F, exclude = Symbol[]) where {T, F return (k => getproperty(from, k) for k in commonfields) end +#TODO update all GQL.execute calls to use this +function executeGql(cfg::NavAbilityDFG, query::AbstractString, variables, T::Type; kwargs...) + executeGql(cfg.client, query, variables, T; kwargs...) +end + +function executeGql(client::NavAbilityClient, query::AbstractString, variables, T::Type; throw_on_execution_error = true, kwargs...) + return GQL.execute( + client.client, + query, + T; + variables, + throw_on_execution_error, + ) +end + function handleQuery(response, nodeName::String, label::Symbol) res = isnothing(response.data) ? nothing : get(response.data, nodeName, nothing) if isnothing(res) From a95d9ef97aa29cc38e56f6c018b9755dedd06d4e Mon Sep 17 00:00:00 2001 From: Johannes Terblanche Date: Tue, 22 Oct 2024 11:45:52 +0200 Subject: [PATCH 2/2] use dedicated listVariables endpoint --- src/graphql/Variable/Variable.jl | 8 ++--- src/services/Variable.jl | 50 ++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/graphql/Variable/Variable.jl b/src/graphql/Variable/Variable.jl index 4c396ab..850fdfb 100644 --- a/src/graphql/Variable/Variable.jl +++ b/src/graphql/Variable/Variable.jl @@ -112,12 +112,8 @@ mutation sdk_add_variables(\$variablesToCreate: [VariableCreateInput!]!) { """ GQL_LIST_VARIABLES = GQL.gql""" -query list_variables($fgId: ID!, $varwhere: VariableWhere = {}) { - factorgraphs(where: { id: $fgId }) { - variables (where: $varwhere){ - label - } - } +query list_variables($fgId: ID!, $varwhere: ListWhere = {}) { + listVariables(fgId: $fgId, where: $varwhere) } """ diff --git a/src/services/Variable.jl b/src/services/Variable.jl index 92b4b15..738e7e2 100644 --- a/src/services/Variable.jl +++ b/src/services/Variable.jl @@ -187,38 +187,50 @@ end function listVariables(fgclient::NavAbilityDFG, regexFilter::Union{Nothing, Regex} = nothing; tags::Vector{Symbol} = Symbol[], - solvable::Int = 0 + solvable::Union{Int, Nothing} = nothing, + solvableFilter::Union{Nothing, Base.Fix2} = isnothing(solvable) ? nothing : >=(solvable), + typeFilter::Union{Nothing, Type{<:InferenceVariable}} = nothing, ) + #TODO deprecate solvable "solvable::Int is deprecated, use solvableFilter = >=(solvable) instead" + !isnothing(typeFilter) && @warn("typeFilter is not implemented yet") + fgId = NvaSDK.getId(fgclient.fg) - variables = Dict( "fgId" => fgId, - "varwhere" => Dict{String,Union{Int, Symbol}}( - "solvable_GTE" => solvable, + "varwhere" => Dict{String,Union{Int, Vector{Int}, Symbol}}( ), ) if !isempty(tags) - @assert length(tags) == 1 "Only one tag is supported in tags filter" + @assert length(tags) == 1 "Only one tag is currently supported in tags filter" variables["varwhere"]["tags_INCLUDES"]=tags[1] end - T = Vector{Dict{String, Vector{@NamedTuple{label::Symbol}}}} + if !isnothing(solvableFilter) + if solvableFilter.f == >= + variables["varwhere"]["solvable_GTE"] = solvableFilter.x + elseif solvableFilter.f == > + variables["varwhere"]["solvable_GT"] = solvableFilter.x + elseif solvableFilter.f == <= + variables["varwhere"]["solvable_LTE"] = solvableFilter.x + elseif solvableFilter.f == < + variables["varwhere"]["solvable_LT"] = solvableFilter.x + elseif solvableFilter.f == == + variables["varwhere"]["solvable"] = solvableFilter.x + elseif solvableFilter.f == in + variables["varwhere"]["solvable_IN"] = solvableFilter.x + else + error("Unsupported solvableFilter function: $(solvableFilter.f)") + end + end - response = GQL.execute( - fgclient.client.client, - GQL_LIST_VARIABLES, - T; - variables, - throw_on_execution_error = true, - ) + response = executeGql(fgclient, GQL_LIST_VARIABLES_DIRECT, variables, Vector{Symbol}) + labels = handleQuery(response, "listVariables") - labels = last.(handleQuery(response, "factorgraphs", fgclient.fg.label)["variables"]) - if isnothing(regexFilter) - return labels - else - return filter!(x -> occursin(regexFilter, string(x)), labels) - end + !isnothing(regexFilter) && + filter!(x -> occursin(regexFilter, string(x)), labels) + + return labels end function getVariable(fgclient::NavAbilityDFG{VT, <:AbstractDFGFactor}, label::Symbol) where VT