Skip to content

Enums

Preface

The concept of Enum or enumerated type is present both in GraphQL and in some SQL databases (like PostgreSQL or MySQL).

An Enum in GraphQL is defined like the following (from graphql docs):

enum Episode {
  NEWHOPE
  EMPIRE
  JEDI
}

An Enum in PostgreSQL could be defined and used like the following (from postgresql docs):

CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
    name text,
    current_mood mood
);
INSERT INTO person VALUES ('Moe', 'happy');
SELECT * FROM person WHERE current_mood = 'happy';
 name | current_mood
------+--------------
 Moe  | happy
(1 row)

SQLAlchemy also has support for Enum types as the following (from sqlalchemy docs):

import enum
from sqlalchemy import Enum


class MyEnum(enum.Enum):
    one = 1
    two = 2
    three = 3


t = Table("data", MetaData(), Column("value", Enum(MyEnum)))

connection.execute(t.insert(), {"value": MyEnum.two})
assert connection.scalar(t.select()) is MyEnum.two

But currently during graphql schema parsing the enum types are just ignored, so they are not transfered in the generated "complete" graphql schema, not used in models and the codegen would just fail overall when using an enum in the input graphql schema.

Subject

Add full support for enum types. Create a respective class in the internal schema model and facilities for encoding into and decoding from the graphql schema definition language. Extend the codegen to generate a respective sqlalchemy Enum model and use it as data type in proper columns of other models.