sgqlc-codegen Tool

Generate SGQLC Code

Downloading schema.json

The schema can be downloaded using sgqlc.introspection.

Generating Schema Types

While one can manually write the schema using sgqlc.types, it can be a daunting task. This can be automated if the schema.json is available:

sgqlc-codegen schema --docstrings schema.json my_schema.py

One may omit --docstrings to save space or speed up file loading (however Python’s -OO/$PYTHONOPTIMIZE will drop them).

Generating Operations

If you’re savvy enough to write GraphQL executable documents (aka “queries”) using their Domain Specific Language (DSL), or they exist somehow, they can be used to generate sgqlc.operation.Operation and can be used to serialize and interpret results. The following command will use the schema.json, the my_schema (generated in the previous section) and the my_operations.gql with the GraphQL executable document to generate the my_operations.py. This file can then be imported and the functions called to create the sgqlc.operation.Operation.

sgqlc-codegen operation \
    --schema schema.json \
    my_schema \
    my_operations.py \
    my_operations.gql
See examples:
license:

ISC

sgqlc-codegen Command Line Options

Generate sgqlc-based code

usage: sgqlc-codegen [-h] {schema,operation} ...

Positional Arguments

command

Possible choices: schema, operation

Sub-commands

schema

Generate sgqlc types using GraphQL introspection data

sgqlc-codegen schema [-h] [--schema-name SCHEMA_NAME] [--docstrings]
                     [--exclude-default-types EXCLUDE_DEFAULT_TYPES [EXCLUDE_DEFAULT_TYPES ...]]
                     [--add-scalar-imports ADD_SCALAR_IMPORTS [ADD_SCALAR_IMPORTS ...]]
                     [schema.json] [schema.py]
Positional Arguments
schema.json

The input schema as JSON file. Usually the output from introspection query.

Default: <_io.TextIOWrapper name=’<stdin>’ mode=’r’ encoding=’utf-8’>

schema.py

The output schema as Python file using sgqlc.types. Defaults to the input schema name with .py extension.

Named Arguments
--schema-name, -s

The schema name to use. Defaults to output (or input) basename without extension and invalid python identifiers replaced with “_”.

--docstrings, -d

Include schema descriptions in the generated file as docstrings

Default: False

--exclude-default-types

Exclude the use of sgqlc types in generated client

Default: []

--add-scalar-imports

Specify “ScalarName=import.file” to automatically import “ScalarName” whenever this scalar is used in the schema

Default: []

operation

Generate sgqlc operations using GraphQL (DSL)

sgqlc-codegen operation [-h] [--schema SCHEMA] [--short-names]
                        schema-name [operations.py] [operation.gql ...]
Positional Arguments
schema-name

The schema name to use in the imports. It must be in the form: modname:symbol. It may contain leading . to change the import statement to from . import using that as path. If :symbol is omitted, then modname is used.

operations.py

The output operations as Python file using sgqlc.operation. Defaults to the stdout

Default: <_io.TextIOWrapper name=’<stdout>’ mode=’w’ encoding=’utf-8’>

operation.gql

The input GraphQL (DSL) with operations

Default: [<_io.TextIOWrapper name=’<stdin>’ mode=’r’ encoding=’utf-8’>]

Named Arguments
--schema

The input schema as JSON file. Usually the output from introspection query. If given, the operations will be validated.

--short-names, -s

Use short selection names

Default: False