Modules and imports#

Meta: three audiences, in this order so each can stop reading early — single-file users (next section), directory-module users, then people wiring up dang.toml. import/shadowing/cycles follow.

The examples on this page are live: they share one Dang environment, so later snippets use earlier definitions. Each result is computed and baked in by the docs build — edit a snippet and hit Run ▶ to replay the page in your browser. Blocks that show an error are supposed to fail: the build verifies the failure the same way it verifies the results.

A single file is a module#

greeting: String! { `${salutation}, ${audience}!` }   # exported
let salutation = "Hello"                              # private to the module
let audience = "world"

greeting
=> "Hello, world!"

A directory is also a module#

mymod/
  main.dang
  types.dang
  utils.dang

dang.toml#

dang.toml is discovered by walking up from the working directory (stopping at a .git boundary).

dang = "2.5"

[imports.Dagger]
dagger = "v0.19.0"

[imports.MyApi]
schema = "./schema.graphqls"
service = ["go", "run", "./server"]

[imports.Remote]
endpoint = "https://api.example.com/graphql"
authorization = "Bearer ${API_TOKEN}"

The endpoint, authorization, and headers values support ${VAR} environment expansion; $(...) command substitution is not supported.

Importing another Dang module#

An import can point at another Dang module on disk instead of a GraphQL source. Its public surface becomes available under the qualifier, exactly like a GraphQL import.

[imports.Helpers]
path = "./lib/helpers"

Per-module identity#

import declarations#

An import names one of the [imports.<Name>] entries. These docs come with one called Demo, a small GraphQL schema served in-process (see GraphQL interop); each request it receives is printed as → query …:

import Demo

An import exposes both qualified (Demo.User) and unqualified (User) names. That covers types, root Query/Mutation functions, interfaces, unions, enums, scalars, and directives (see GraphQL interop):

[Demo.primaryUser.name, primaryUser.name]
→ query Query {primaryUser{name}} → query Query {primaryUser{name}}
=> ["John Doe", "John Doe"]

Enum values become unqualified too, when no other import has the same name (see Enums and scalars):

[status == ACTIVE, Demo.Status.ACTIVE == Status.ACTIVE]
→ query Query {status}
=> [true, true]

Builtin imports#

Every module also gets Dang's builtin imports — JSON, YAML, TOML, Random and UUID — as imports it doesn't write (see JSON, YAML, and TOML and Standard library reference). They are reached qualified only:

{{encoded: JSON.encode([1, 2]), uuidLength: UUID.v4.length}}
=> {{encoded: "[1,2]", uuidLength: 36}}

Shadowing#

A local declaration shadows an imported name of the same spelling, and the qualified name still reaches the import:

type User {
  name: String!
  origin: String! = "local"
}

let remote: Demo.User! = Demo.primaryUser

[User("Ada").origin, remote.name]
→ query Query {primaryUser{name}}
=> ["local", "John Doe"]

The two Users are different types, so a Demo user doesn't fit the local one. Types match by name, not shape: even a local User with exactly Demo's fields would be rejected.

let mine: User! = primaryUser
Type error: cannot use Demo.User! as User!: - missing field "origin"

Ordering and cycles#

Direct circular variable initializers are caught statically:

left: Int! = right
right: Int! = left
Type error: variables phase failed: circular module variable initializer: left -> right -> left

Cycles hidden behind an auto-called function or constructor default are caught at runtime, when the variable is first read. Run as a script, this reports initialization cycle while evaluating variable "seed":

seed: Int! = grow
grow: Int! { seed * 2 }

What a module exports#

Meta: dang.toml discovery walks up from the file's directory to the nearest .git boundary; the dang CLI runs the resolved module (see CLI reference).