Providing Vertex Id in Vertex parameter

Hi,
I want to know how to provide Vertex id in GSQL.
For eg, CREATE QUERY get_org(VERTEX O) FOR GRAPH Companies
This is my query. When I run this in GraphStudio, I get Enter Query parameters window like, q_parameters .
Here, Org i.e Vertex Type I have provided in the Query(VERTEX). I need to know, how can I provide Vertex id in the Query, so that when I run the Query, the vertex type would default as “Org” and vertex id would default as “myvalue”.

Hey,

First, I’m pretty sure that “Org” is already specified as the default vertex type because your query argument is specifically VERTEX < Org > and not just any VERTEX type.

For providing default vertex ID, simply supply the default value in the query declaration as follows:

CREATE QUERY name(VERTEX<Org> vertex_input = "myvalue") FOR GRAPH graphname { ...

1 Like

Hi,
Thanks for the help. I have tried the query declaration as you have mentioned,
CREATE QUERY get_org(VERTEX <Org> vertex_input = "881" O) FOR GRAPH Companies {
Start={O};
person=SELECT tgt FROM Start:src-(has:h)->Person:tgt;
PRINT person;
}

But I get error, “Error: extraneous input ‘O’ expecting ‘)’”.

When I try this,

CREATE QUERY get_org(VERTEX <Org> vertex_input = "881") FOR GRAPH Companies {
Start={O};
person=SELECT tgt FROM Start:src-(has:h)->Person:tgt;
PRINT person;
}

I get error “Error: Vertex or set type argument vertex_input cannot have a default value”.

Can you please tell me where I am making mistake?

Hi Karan,

Sorry about the trouble, you are not making a mistake. It appears that GSQL simply does not allow vertex arguments to be assigned default values.

One option is to instead provide the primary ID of your vertex to the function as a STRING optional argument and then use to_vertex(STRING id, STRING v_type) to convert it as desired. Here is an example:

CREATE QUERY test(STRING inString = "54749779") FOR GRAPH name { 
  VERTEX inPerson;
  
  inPerson = to_vertex(inString, "Person");
  
  PRINT inPerson;
  //PRINT inPerson.Name; # CANNOT DO THIS!
}

However, a key limitation of this function is that inPerson must be a default VERTEX type (and not the desired VERTEX < Person >), which means you cannot access its attributes or call functions on it. What I would instead recommend is doing SELECT statement filtering, which can get you properly typed variables within your query (VERTEX < Person >) as both a singleton set and as an individual vertex. Make sure you also specify your PRIMARY ID to be callable as an attribute.

CREATE QUERY test(STRING inString = "54749779") FOR GRAPH name { 
  VERTEX<Person> inPerson;
  start = {Person.*};
  
  singleton = SELECT s FROM start:s
              WHERE s.ID == inString
              ACCUM inPerson = s;
  
  PRINT inPerson;
  PRINT inPerson.Name;
  
  PRINT singleton;
}