Skip to main content

Typegate

Typegate version

meta dev
note

You can launch the embedded typegate via two subcommands, meta dev and meta typegate. Check the Embedded Typegate for more info.

The typegate instance runs on port 7890 by default. You can check if the typegate node is running by accessing http://localhost:7890 in your browser.

Using docker (Not Recommended)

Install Docker and use the following compose.yml to launch a typegate node. For multi-instance production workloads, Redis and an S3 object store provider are required but the typegate will run using in-memory stores if no SYNC_* environment variable is detected. More details can be found here. In practice you might also want to add a database or other systems that the typegate can connect to.

services:
typegate:
image: ghcr.io/metatypedev/typegate:latest
ports:
- "7890:7890"
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
# only for dev, generate secure values for production
TG_SECRET: "a4lNi0PbEItlFZbus1oeH/+wyIxi9uH6TpL8AIqIaMBNvp7SESmuUBbfUwC0prxhGhZqHw8vMDYZAGMhSZ4fLw=="
TG_ADMIN_PASSWORD: password
DEBUG: "true"
# launch the containers
docker compose up --detach

# watch the typegate logs
docker compose logs typegate --follow

Internal APIs

Most of the internal APIs are still unstable, and may change without notice. If you still want to experiment with them, you can use the GraphQL introspection to discover them.

  • /typegate
  • /prisma-migration

The typegate nodes - or typegates - are the central components of the ecosystems. They build and type check typegraphs, and expose them through a HTTP/GraphQL interface. They enforce the type safety of the data flows, connect to all the runtimes and orchestrate the execution of incoming requests.

Request lifecycle

When a new request fires a trigger, the typegate orchestrates the following stages:

  1. extract the secure request context from custom authentication or JSON Web Token (JWT)
  2. retrieve cached execution plan or compute a new one
    • traverse the typegraph to create a DAG of the required types
    • optimize the DAG to reduce the number of calls to the runtimes
    • pre-compute all structural elements and data resolutions
  3. execute the plan
    • type check the arguments
    • verify lazily policies on the need
    • run the DAG execution
    • enforce the rate-limiting
    • type check the response
  4. manage metadata of the request

HTTP/GraphQL interface

For now, the typegate nodes are only accessible through HTTP/1.1 and HTTP/2. More protocols could be supported in the future. Typegates expose a GraphQL interface which is the result of a typegraph projected onto corresponding GraphQL types. While this reduces the type safety of the data flowing, it makes more interoperable thanks to the many high-quality and well-known GraphQL tooling already available. The underlying types are also exposed in order for API clients to verify the underlying types.

GraphQL, being a query language, offers a great asset for Metatype's philosophy:

  1. Efficient querying: the client can specify exactly what data it needs, reducing the amount of over- or under-fetching
  2. Flexibility: allows for retrieving multiple resources in a single request, unlike REST, which often requires multiple ones
  3. Typing: GraphQL has a built-in type system that allows for better documentation and stronger validation of the requests
  4. Improved tooling: tools and libraries around GraphQL are rapidly growing and great a development experience

Configuration

Environment variables.

The following environment variables can be used to configure the typegate. SYNC_* variables have special semantics which you can read about here.

Environment variablesDescDefaultExamples
HOSTNAMEHostname that typegate is deployed on.getHostname() result.typegate-123
TG_PORTTcp port to serve typegate APIs at.78907891
TG_ADMIN_PASSWORDPassword use by the CLI/SDK to configure the typegate.RequiredMy5up3r53cr37
TG_SECRETSymmetric key used to encrypt cookies and other things (64-byte binary string encoded in base64).Required.0p64wJCpQCTiPqPOEze92HLBieszD3sGLtnx6tWm67kqo1tCYcNZ17rjFjEzMl7HJ/SOFZsTVWU0fUjndMrhsQ==
TMP_DIRTop-level temporary directory.$PWD/tmp/tmp/typegate-tmp-dir
DEBUGEnable debug output and other development paths.falsetrue
TIMER_MAX_TIMEOUT_MSTimeout for custom runtime functions and other proccesses.30005000
TIMER_POLICY_EVAL_RETRIESNumber of retries when evaluating policies that have timed out13
TIMER_DESTROY_RESOURCESForce abort and attempt to restart operations that did not respond after multiple retriestruefalse
JWT_MAX_DURATION_SECThe lifetime of generated JWT access tokens.30 * 24 * 3600604800
JWT_REFRESH_DURATION_SECThe lifetime of generated JWT refresh tokens.5 * 60600
SENTRY_DSNData source name for sentrynullhttps://[email protected]/1
SENTRY_SAMPLE_RATEThe rate of error events to be sent to Sentry (between 0.0 and 1.0)1.00.5
SENTRY_TRACES_SAMPLE_RATEThe rate of transactions be sent to Sentry (between 0.0 and 1.0)1.00.2
TRUST_PROXYWhether to accept proxy headers when resolving request contexts.falsetrue
TRUST_HEADER_IPThe header key on which to resolve request origin addresses.X-Forwarded-ForX-Forwarded-For
DENO_V8_FLAGSFlags for tuning the v8 javascript engine. Use the --help flag here to see what options are available.--stack-size=1968
SYNC_REDIS_URLURL to the Redis database. Must include the database number.**Required (sync mode)http://:password@localhost:6379/0
SYNC_S3_HOSTHostname of the S3 store.**Required (sync mode)play.min.io:9000
SYNC_S3_REGIONS3 region.Required (sync mode)us-west-2
SYNC_S3_ACCESS_KEYAccess key for the S3 store credentials.Required (sync mode)user
SYNC_S3_SECRET_KEYAccess key secret for the S3 store credentials.Required (sync mode)password
SYNC_S3_PATH_STYLEtrue or false, force path style if true.falsetrue
SYNC_S3_BUCKETThe bucket to be used for the system (dedicated).Required (sync mode)mybucket
SUBSTANTIAL_POLL_INTERVAL_SECRate at which new schedules are read.1.00.6
SUBSTANTIAL_LEASE_LIFESPAN_SECLease duration associated to a workflow run2.06
SUBSTANTIAL_MAX_ACQUIRE_PER_TICKMax amount of new acquired replay requests per tick35