GraphQL using a for loop in within the search query
Question:
Because Yelp limits the output of businesses to 50, I’m trying to loop through the Census block centroid longitudes and latitudes of San Diego to get all the different businesses (which I want for a project). However, the query does not seem to accept anything that is not strictly numeric. Am I missing something?
Here is my code:
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
yelp_api_key = 'xxx'
#define headers
header = {'Authorization': 'bearer {}'.format(yelp_api_key),
'Content-Type': 'application/json'}
#url = "https://api.yelp.com/v3/businesses/search?location=san%2520diego&sort_by=best_match&limit=20"
append_data = []
for i in range(0,len(bcx)):
#PARAMETERS = {'limit': 50,
# 'offset': 0,
# 'longitude': bcx[i],
# 'latitude': bcy[i]
# }
#build request framework
transport = RequestsHTTPTransport(url = 'https://api.yelp.com/v3/graphql', headers = header, use_json = True) #params = PARAMETERS
# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Provide a GraphQL query
query_bus = gql("""
{
sd_businesses: search(location: "san diego", limit: 50,
offset: 0,
longitude: bcx[i],
latitude: bcy[i]){
total
business {
name
coordinates{
latitude
longitude
}
}
}
}
""")
# Execute the query on the transport
result_bus = client.execute(query_bus)
append_data.append(result_bus)
print(append_data)
Answers:
You’re attempting to do python variable substitution inside the GraphQL query string and the GraphQL interpreter can’t deal with that. You need to use variables to define the parameters of your query.
params = {
"location": "san diego",
"limit": 50,
"offset": 0,
"longitude": bcx[i],
"latitude": bcy[i]
}
query_bus = gql("""
query sd_businesses($location: String, $limit: Number, $offset: Int, $longitude: Float, $latitude: Float) {
search(location: $location, limit: $limit, offset: $offset, longitude: $longitude, latitude: $latitude) {
total
business {
name
coordinates {
latitude
longitude
}
}
}
}
""")
# Execute the query on the transport
result = client.execute(query, variable_values=params)
Because Yelp limits the output of businesses to 50, I’m trying to loop through the Census block centroid longitudes and latitudes of San Diego to get all the different businesses (which I want for a project). However, the query does not seem to accept anything that is not strictly numeric. Am I missing something?
Here is my code:
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
yelp_api_key = 'xxx'
#define headers
header = {'Authorization': 'bearer {}'.format(yelp_api_key),
'Content-Type': 'application/json'}
#url = "https://api.yelp.com/v3/businesses/search?location=san%2520diego&sort_by=best_match&limit=20"
append_data = []
for i in range(0,len(bcx)):
#PARAMETERS = {'limit': 50,
# 'offset': 0,
# 'longitude': bcx[i],
# 'latitude': bcy[i]
# }
#build request framework
transport = RequestsHTTPTransport(url = 'https://api.yelp.com/v3/graphql', headers = header, use_json = True) #params = PARAMETERS
# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Provide a GraphQL query
query_bus = gql("""
{
sd_businesses: search(location: "san diego", limit: 50,
offset: 0,
longitude: bcx[i],
latitude: bcy[i]){
total
business {
name
coordinates{
latitude
longitude
}
}
}
}
""")
# Execute the query on the transport
result_bus = client.execute(query_bus)
append_data.append(result_bus)
print(append_data)
You’re attempting to do python variable substitution inside the GraphQL query string and the GraphQL interpreter can’t deal with that. You need to use variables to define the parameters of your query.
params = {
"location": "san diego",
"limit": 50,
"offset": 0,
"longitude": bcx[i],
"latitude": bcy[i]
}
query_bus = gql("""
query sd_businesses($location: String, $limit: Number, $offset: Int, $longitude: Float, $latitude: Float) {
search(location: $location, limit: $limit, offset: $offset, longitude: $longitude, latitude: $latitude) {
total
business {
name
coordinates {
latitude
longitude
}
}
}
}
""")
# Execute the query on the transport
result = client.execute(query, variable_values=params)