2
votes

Would someone walk me through the syntax on how to use the runQuery method to preform a gqlQuery using the Google Cloud Datastore REST API v1. I only need help in understanding the query structure of the REST API and do not need help with Google OAUTH or setting up Cloud Datatore. I have included a link to the documentation, example gqlQuery to run and Cloud Datastore structure below.

Method: projects.runQuery https://cloud.google.com/datastore/docs/reference/rest/v1/projects/runQuery

Example gqlQuery = ("Select * From Customer")

Sample Google DataStore Structure

   id = "78090499534213120" 
   Address = "888 Fake St"
   City = "Fake City"
   FirstName = "Name"
   LastName = "Name"
   State = "CT"
2

2 Answers

2
votes
POST https://datastore.googleapis.com/v1/projects/{YOUR_PROJECT_ID}:runQuery?key={YOUR_API_KEY}

Where {YOUR_PROJECT_ID} can be found on the Cloud Console home page for your project and is alphanumeric.

The body of the message will be a JSON string with the details of the query, so in your case:

{
 "gqlQuery":{"queryString": "select * from customer"}
}

If you want to include conditionals, you can do this as well by using parameter binding. The below example shows how to use positional binding to achieve this:

{
   "gqlQuery":
   {
      "queryString": "select * from Customers where State = @1 AND FirstName = @2",
      "positionalBindings": [
         {"value": {"stringValue": "CT"}}.
         {"value": {"stringValue": "Name"}}
      ]
   }
}

Instead of positional binding, you could also do named bindings:

{
   "gqlQuery":
   {
      "queryString": "select * from Customers where State = @State AND FirstName = @FirstName",
      "namedBindings": {
          "State": {"value": {"stringValue": "CT"}},
          "FirstName": {"value": {"stringValue": "Name"}}
      }
   }
}

Lastly, and not recommended as it can lead to query injection attacks, you can include literals in the query string itself by setting the appropriate flag:

{
   "gqlQuery":
   {
      "queryString": "select * from Customers where State = 'CT' AND FirstName = 'Name'",
      "allowLiterals": true
   }
}
0
votes

Do you get a proper response back when you make this request? I get the following 200 response, but don't see any of my expected rows of data

{ "batch": { "entityResultType": "PROJECTION", "endCursor": "CgA=", "moreResults": "NO_MORE_RESULTS" } }