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

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.0.tar.gz (36.9 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: cognite_pygen-0.9.0.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.0.tar.gz
Algorithm Hash digest
SHA256 6d39c2ca6af62669bf006d15335e11fa1aaa457a6d300f3f8dd1c0bdce28e007
MD5 09ff10ef5a8574d57896dcc27891b207
BLAKE2b-256 001d45e130588e398bb316a98f0e6793ef0d61220b74c7d14db5213e3f9f977e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cognite_pygen-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 572cfa781ef7bb71da59418825652e0adafca13d1fe3b01cfdad178c299ed241
MD5 378f4b1533274c53e4157c4a782f9d7d
BLAKE2b-256 f9d87d1cb95b224b2dd1ea7f327be0c5c6b015cfbc0c093978c08cb260eeb4a5

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