Python sgqlc client library
This guide explains how to get started with the Simple GraphQL Client Python library (sgqlc).
Open a terminal application and install sgqlc with the following command:
Choose your approach
Client libraries can interact with the GraphQL server in two ways:
- directly with hand-written string queries, mutations, and subscriptions,
- indirectly through objects generated for types defined in a GraphQL schema.
Although you can use both approaches to make API calls we highly recommend that you avoid hand-writing the operations. It is mainly because:
- writing queries or mutations and later interpreting the results can be cumbersome and error-prone,
- dealing with changes introduced in the schema or by user needs is more costly if done by hand.
Hand-written queries
To use a hand-written query in the string format open the text editor of choice and run the following code:
Once the query is complete, the data
variable will contain JSON-encoded descriptions of urban models, such as:
Object queries
To write queries as objects you need a Python module, which includes classes corresponding to the Urban API schema. Since converting the existing schema to the Python module manually is time-consuming and error prone, sgqlc library automates this process in two steps.
Use the introspection query to fetch the Urban API schema as JSON:
Use dark colors for code blocks Copy Use the sgqlc code generator, which outputs a Python module straight from JSON:
Use dark colors for code blocks Copy
Now that the Urban API schema is described in urban_
, you can write the query as an object. Each object query is converted to a string form behind the scenes.
As a result, the actual API call is exactly the same as when using hand-written queries.
The above code snippet will print information about the Urban Models returned from the query, and will look similar to:
For more information about sgqlc check the GitHub repository or read the documentation.
Keep learning
If you're just getting started with the Urban API we recommend exploring the code samples to find specific examples of the the API can do.