Another API question: I think after the API update, my graphql queries aren’t working anymore. I’m sure this is just a syntax issue, but I can’t even run the example queries on the documentation site. Can anyone spot what’s wrong with my query?

Another API question: I think after the API update, my graphql queries aren’t working anymore. I’m sure this is just a syntax issue, but I can’t even run the example queries on the documentation site. Can anyone spot what’s wrong with my query?
I’ve also attached what the example query on the documentation site looks like. Maybe this instance hasn’t been updated to the latest version of the API?

def sf_ca_gql_request(params):
    query = """
        query($city: String, $start_date: DateTime!, $end_date: DateTime!) {
          search(filter: { 
            address: {
              city: $city
              iso_country_code: "CA"
            }
          }){
            safegraph_weekly_patterns(start_date: $start_date end_date: $end_date) {
              places(first: 300000 after: "") {
                edges {
                  node {
                    safegraph_patterns {
                      city
                      raw_visit_counts
                      poi_cbg
                    } 
                  }
                }
              }
            }
          }
        }
    """

    req = requests.post(
        'https://api.safegraph.com/v2/graphql',
        json={
            'query': query,
            'variables': params
        },
        headers=headers
    )

    return req.json()

params = {
            'start_date': '2020-01-07',
            'end_date': '2020-02-04',
            'city': 'Toronto',
        }
ca_toronto_test = sf_gql_request(params)
ca_toronto_test
![sample_2.0|610x500](upload://wqEr2Dd1mkOKOJRwP4iVSdIEaJY.png)

query($city: String, $start_date: DateTime!, $end_date: DateTime!) {
  search(filter: { 
    address: {
      city: $city
      iso_country_code: "CA"
    }
  }){
    weekly_patterns(start_date: $start_date end_date: $end_date) {
      results(first: 500) {
        edges {
          node {
            city
              raw_visit_counts
              poi_cbg
          }
        }
      }
    }
  }
}

hi daniel, we made some changes to make the weekly_patterns call run from the top level so it could process faster. try that query; also pagination is limited to 500 results at a time.

Hi Victor - thanks for the feedback, I’ll give it a shot later today. Do you have any advice for trying to get a list of all the places in a city? or if there’s a way for me to find out the last page in the pagination?

hm, are you thinking something like our pageInfo

query($city: String, $start_date: DateTime!, $end_date: DateTime!) {
  search(filter: { 
    address: {
      city: $city
      iso_country_code: "CA"
    }
  }){
    weekly_patterns(start_date: $start_date end_date: $end_date) {
      results(first: 500) {
        pageInfo { hasNextPage endCursor }
        edges {
          node {
            city
              raw_visit_counts
              poi_cbg
          }
        }
      }
    }
  }
}

this will tell you if there is another page to request and what the endCursor for that pagination

I’m still not getting a response. any suggestions

import requests
import json

def sf_ca_gql_request(params):
    query = """
        query($city: String, $start_date: DateTime!, $end_date: DateTime!) {
          search(filter: { 
            address: {
              city: $city
              iso_country_code: "CA"
            }
          }){
            weekly_patterns(start_date: $start_date end_date: $end_date) {
              results(first: 500) {
                pageInfo { hasNextPage endCursor }
                edges {
                  node {
                    city
                      raw_visit_counts
                      poi_cbg
                  }
                }
              }
            }
          }
        }
    """

    req = requests.post(
        'https://api.safegraph.com/v2/graphql',
        json={
            'query': query,
            'variables': params
        },
        headers=headers
    )

    return req.json()

params = {
            'start_date': '2020-01-07',
            'end_date': '2020-02-04',
            'city': 'Toronto',
        }
ca_toronto_test = sf_gql_request(params)
ca_toronto_test

Response

Out[8]: {'data': None,
 'errors': [{'message': 'The search request could not be completed',
   'path': ['search', 'weekly_patterns', 'results'],
   'locations': [{'line': 9, 'column': 15}]}],
 'extensions': {'row_count': 0,
  'version_date': ['safegraph_core: 1643356807__2022_01',
   'safegraph_geometry: 1643356807__2022_01',
   'safegraph_weekly_patterns2: 2022_01_11',
   'safegraph_monthly_patterns: 2022_01_11']}}

nevermind, I made a careless mistake!

Sorry to bother so much, but do you have any advice on speeding up the querying process for getting all the places in a city? currently i’m doing this sequentially (waiting for a request to finish, see if it has a next page, if so, continue, if not, then stop), and I’m wondering if it’s possible to thread this process or is there some sort of aggregation by location query?

I’m also struggling to get any results for the monthly_patterns query.

import requests
import json

def sf_ca_gql_request(params):
    query = """
        query($city: String, $after: String = "", $start_date: DateTime!, $end_date: DateTime!) {
          search(filter: { 
            address: {
              city: $city
              iso_country_code: "CA"
            }
          }){
            monthly_patterns(start_date: $start_date end_date: $end_date) {
              results(first: 500, after: $after) {
                pageInfo { hasNextPage endCursor }
                edges {
                  node {
                      city
                      raw_visit_counts
                      poi_cbg
                      normalized_visits_by_total_visits
                      normalized_visits_by_state_scaling
                  }
                }
              }
            }
          }
        }
    """

    req = requests.post(
        'https://api.safegraph.com/v2/graphql',
        json={
            'query': query,
            'variables': params
        },
        headers=headers
    )

    return req.json()

params = {
            'start_date': '2019-03-01',
            'end_date': '2019-04-01',
            'city': 'Toronto',
        }
ca_toronto_test = sf_ca_gql_request(params)
ca_toronto_test

Response

Out[37]: {'data': {'search': {'monthly_patterns': {'results': {'pageInfo': {'hasNextPage': False,
      'endCursor': None},
     'edges': []}}}},
 'extensions': {'row_count': 0,
  'version_date': ['safegraph_core: 1643356807__2022_01',
   'safegraph_geometry: 1643356807__2022_01',
   'safegraph_weekly_patterns2: 2022_01_11',
   'safegraph_monthly_patterns: 2022_01_11']}}

you can make concurrent requests as long as the total requests are < 1,000/min. i will not that that there seems to be some issues around how some python libraries handle the asyncio calls that causes some issues.

for canada we only generate weekly_patterns.