Based on the example from the gql documentation, I have the below, working query:
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url="https://data.rcsb.org/graphql")
# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Provide a GraphQL query
query = gql(
"""
{
entries(entry_ids: ["4HHB", "5cbg", "3j3Q"]) {
assemblies {
pdbx_struct_assembly {
id
details
}
}
}
}
"""
)
# Execute the query on the transport
result = client.execute(query)
print(result)
which prints, correctly:
{'entries': [{'assemblies': [{'pdbx_struct_assembly': {'id': '1', 'details': 'author_and_software_defined_assembly'}}]}, {'assemblies': [{'pdbx_struct_assembly': {'id': '1', 'details': 'author_and_software_defined_assembly'}}, {'pdbx_struct_assembly': {'id': '2', 'details': 'author_and_software_defined_assembly'}}, {'pdbx_struct_assembly': {'id': '3', 'details': 'author_and_software_defined_assembly'}}]}, {'assemblies': [{'pdbx_struct_assembly': {'id': '1', 'details': 'author_defined_assembly'}}]}]}
But I want to be able to programmatically give input to the endpoint as a list as a variable in place of ["4HHB", "5cbg", "3j3Q"]
. How can I do this?
Based again on the documentation, I came up with the below, but it does not work because GQL does not recognize List!
as valid:
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url="https://data.rcsb.org/graphql")
# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Provide a GraphQL query
query = gql(
"""
query getAssembliesInfo ($ids_list: List) {
entries(entry_ids: $ids_list) {
assemblies {
pdbx_struct_assembly {
id
details
}
}
}
}
"""
)
pdbs = ["4HHB", "5cbg", "3j3Q"]
params = {'ids_list': pdbs}
# Execute the query on the transport
result = client.execute(query, variable_values=params)
print(result)
result:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/nfs/turbo/umms-petefred/jaschwa/HDPRED/lib/python3.8/site-packages/gql/client.py", line 388, in execute
data = loop.run_until_complete(
File "/nfs/turbo/umms-petefred/jaschwa/HDPRED/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/nfs/turbo/umms-petefred/jaschwa/HDPRED/lib/python3.8/site-packages/gql/client.py", line 285, in execute_async
return await session.execute(
File "/nfs/turbo/umms-petefred/jaschwa/HDPRED/lib/python3.8/site-packages/gql/client.py", line 1220, in execute
result = await self._execute(
File "/nfs/turbo/umms-petefred/jaschwa/HDPRED/lib/python3.8/site-packages/gql/client.py", line 1111, in _execute
self.client.validate(document)
File "/nfs/turbo/umms-petefred/jaschwa/HDPRED/lib/python3.8/site-packages/gql/client.py", line 149, in validate
raise validation_errors[0]
graphql.error.graphql_error.GraphQLError: Unknown type 'List'. Did you mean 'Int'?
GraphQL request:2:41
1 |
2 | query getAssembliesInfo ($ids_list: List) {
| ^
3 |
The answer is that I needed to get out of python land and define a typed list with
[String!]!
: