Skip to main content

Cognite GraphQL Python Generation SDK

Project description

Cognite GraphQL Python Generator

build GitHub Code style: black

This is the Cognite GraphQL Python Generator, pygen. The purpose of this package is to help developers to work with Cognite Data Fusion's (CDF) Data Models (DM) in Python.

[DISCLAIMER!] This project is in a highly experimental state and no guarantees are made for consistency between versions. The project may also become deprecated if the experimentation turns out to be a dead end.

The core functionality is to provide a Python client that matches a data model. This enables the developer for the following benefits

  • Client side validation of the data before writing it to CDF.
  • Autocompletion is matching the data model in the integrated developer environment (IDE). This is important as it enables:
    • Discoverability of a data model through Python.
    • Reduced typing errors in development.
  • Keeping the language domain specific for the developer. Instead of working with generic concepts such as instances, nodes, edges, the developer can work with the concepts in the data model.

Installation

Without any optional dependencies

To install this package without CLI support:

pip install cognite-pygen

With optional dependencies

  • cli This includes CLI support such that you can run the package from the command line.
pip install cognite-pygen[cli]

Usage

The goal of the package is to have representations of all the types in a given data model with API calls to .list(), .apply(), .delete(), and .retrieve() individuals for each type.

image

With CLI

You can specify the data models either as a .graphql schema or a pydantic classes in a .py file. Then, you can use the CLI to automatically generate the other representation as well as the client.py file which creates the API and the convenience method get_[client_name]_client().

To generate from a .graphql schema you use the following command.

pygen topython 'PATH_TO_SCHEMA'

This will create a schema.py and a client.py file in the directory you are running the command.

To generate from schema.py use the following command

pygen togql 'PATH_TO_FILE'

This will load the python module and create a schema.graphql file in the directory you are running the command.

PATH_TO_FILE can be either a path to a .py file or a Python dot-notation to a package (e.g. my_project.schema_module make sure that the package in which case the module must be in Python path).

Note the schema.py file must follow a specific structure, see examples/cinematography_domain for an example. The overall structure is as follows:

  1. Instantiate a new schema with the line, myschema: Schema[DomainModel] = Schema()
  2. Register all you Types with @myschema.register_type
  3. Close the schema with myschema.close()

To get a concrete example is available in examples/cinematography_domain, it consists of four files.

  • schema.graphql The schema defined in GraphQL language.
  • schema.py The schema defined in pydantic classes.
  • client.py Which sets up the client for the data model.
  • usage.py Demonstrates the usage of the client.

Without CLI

You can run this package directly in Python code. This can be useful, for example, a notebook. When you run the function to_client_sdk you get a PythonSDK object back which has the pydantic classes in a PythonSDK.schema and the client in the PythonSDK.client.

from cognite.pygen import to_client_sdk
my_schema = """type Case {
  scenario: Scenario
  start_time: String!
  end_time: String!
 }

type Scenario {
  name: String!
}
"""
sdk = to_client_sdk(my_schema, "MyClient", "my_schema")
print(sdk.schema)

Settings File

gypen togql and pygen topython take their defaults form settings.toml if present. See settings.toml for an example, section [local] is relevant for togql and topython commands.

DM Non-GraphQl API

The API developed is based on the non-GraphQL endpoints in Data Model API v3. There is a simplified wrapper which is available in cognite.pygen.dm_clients.

See dm_clients/README.md for more details.__

Changelog

Wondering about previous changes to the SDK? Take a look at the CHANGELOG.

Contributing

Want to contribute? Check out CONTRIBUTING.

Project details


Release history Release notifications | RSS feed

This version

0.9.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cognite_pygen-0.9.1.tar.gz (36.9 kB view details)

Uploaded Source

Built Distribution

cognite_pygen-0.9.1-py3-none-any.whl (45.4 kB view details)

Uploaded Python 3

File details

Details for the file cognite_pygen-0.9.1.tar.gz.

File metadata

  • Download URL: cognite_pygen-0.9.1.tar.gz
  • Upload date:
  • Size: 36.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for cognite_pygen-0.9.1.tar.gz
Algorithm Hash digest
SHA256 3c88d5858c2c6722c2a3fc9920127cc524795c2725be3fb98cdebcf9d3b7b9ae
MD5 7bcf8be7e3c90d2fab5f55af94a38b54
BLAKE2b-256 ce027ecd0882d447246a79ac11eff331b1c4e7890eb9fd3e85568a959aef9aa9

See more details on using hashes here.

File details

Details for the file cognite_pygen-0.9.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cognite_pygen-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f6f386573b1e554f9ca7d0f74e4df58ed4f557c22c641a58ef02a6903142a05e
MD5 166e7636533de41e4dd5dcb8567c21d4
BLAKE2b-256 76a8be1151dc15f3613cbc07a464e59dd79b6c200dfeb1056154f1de2c3ddefc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page