Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 53aa5a8

Browse files
Apply in-code documentation to generated GraphQL (#519)
Co-authored-by: Matt Johnson-Pint <[email protected]>
1 parent 966e8e9 commit 53aa5a8

File tree

13 files changed

+502
-45
lines changed

13 files changed

+502
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## UNRELEASED - Runtime
44

5+
- feat: Apply in-code documentation to generated GraphQL [#519](https://github.com/hypermodeinc/modus/pull/519)
56
- feat: Reduce logger output during development [#576](https://github.com/hypermodeinc/modus/pull/576)
67
- chore: Trigger internal release pipeline at the end of the release-runtime workflow [#577](https://github.com/hypermodeinc/modus/pull/577)
78
- feat: Add API explorer to runtime [#578](https://github.com/hypermodeinc/modus/pull/578)

lib/metadata/metadata.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,22 @@ type Metadata struct {
3434
Types TypeMap `json:"types,omitempty"`
3535
}
3636

37+
type Docs struct {
38+
Lines []string `json:"lines"`
39+
}
40+
3741
type Function struct {
3842
Name string `json:"-"`
3943
Parameters []*Parameter `json:"parameters,omitempty"`
4044
Results []*Result `json:"results,omitempty"`
45+
Docs *Docs `json:"docs,omitempty"`
4146
}
4247

4348
type TypeDefinition struct {
4449
Name string `json:"-"`
4550
Id uint32 `json:"id,omitempty"`
4651
Fields []*Field `json:"fields,omitempty"`
52+
Docs *Docs `json:"docs,omitempty"`
4753
}
4854

4955
type Parameter struct {
@@ -60,6 +66,7 @@ type Result struct {
6066
type Field struct {
6167
Name string `json:"name"`
6268
Type string `json:"type"`
69+
Docs *Docs `json:"docs,omitempty"`
6370
}
6471

6572
func (p *Parameter) UnmarshalJSON(data []byte) error {
@@ -118,9 +125,9 @@ func (m *Metadata) SdkVersion() string {
118125
func (m *Metadata) GetTypeDefinition(typ string) (*TypeDefinition, error) {
119126
switch typ {
120127
case "[]byte":
121-
return &TypeDefinition{typ, 1, nil}, nil
128+
return &TypeDefinition{typ, 1, nil, nil}, nil
122129
case "string":
123-
return &TypeDefinition{typ, 2, nil}, nil
130+
return &TypeDefinition{typ, 2, nil, nil}, nil
124131
}
125132

126133
def, ok := m.Types[typ]

runtime/graphql/schemagen/schemagen.go

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,16 @@ func transformTypes(types metadata.TypeMap, lti langsupport.LanguageTypeInfo, fo
118118
continue
119119
}
120120

121-
typeDefs[name] = &TypeDefinition{
121+
typeDef := &TypeDefinition{
122122
Name: name,
123123
Fields: fields,
124124
}
125+
126+
if t.Docs != nil {
127+
typeDef.DocLines = t.Docs.Lines
128+
}
129+
130+
typeDefs[name] = typeDef
125131
}
126132
return typeDefs, errors
127133
}
@@ -131,12 +137,14 @@ type FieldDefinition struct {
131137
Type string
132138
Arguments []*ArgumentDefinition
133139
Function string
140+
DocLines []string
134141
}
135142

136143
type TypeDefinition struct {
137144
Name string
138145
Fields []*FieldDefinition
139146
IsMapType bool
147+
DocLines []string
140148
}
141149

142150
type ArgumentDefinition struct {
@@ -186,6 +194,10 @@ func transformFunctions(functions metadata.FunctionMap, inputTypeDefs, resultTyp
186194
Function: fn.Name,
187195
}
188196

197+
if fn.Docs != nil {
198+
field.DocLines = fn.Docs.Lines
199+
}
200+
189201
if filter(field) {
190202
if isMutation(fn.Name) {
191203
mutationFields = append(mutationFields, field)
@@ -329,14 +341,34 @@ func writeSchema(buf *bytes.Buffer, root *RootObjects, scalarTypes []string, inp
329341
buf.WriteByte('\n')
330342
}
331343
}
332-
333344
// write input types
334345
for _, t := range inputTypeDefs {
335346
buf.WriteByte('\n')
347+
348+
if len(t.DocLines) > 0 {
349+
buf.WriteString("\"\"\"\n")
350+
for _, line := range t.DocLines {
351+
buf.WriteString(line)
352+
buf.WriteByte('\n')
353+
}
354+
buf.WriteString("\"\"\"\n")
355+
}
356+
336357
buf.WriteString("input ")
337358
buf.WriteString(t.Name)
338359
buf.WriteString(" {\n")
339360
for _, f := range t.Fields {
361+
362+
if len(f.DocLines) > 0 {
363+
buf.WriteString(" \"\"\"\n")
364+
for _, line := range f.DocLines {
365+
buf.WriteString(" ")
366+
buf.WriteString(line)
367+
buf.WriteByte('\n')
368+
}
369+
buf.WriteString(" \"\"\"\n")
370+
}
371+
340372
buf.WriteString(" ")
341373
buf.WriteString(f.Name)
342374
buf.WriteString(": ")
@@ -349,10 +381,31 @@ func writeSchema(buf *bytes.Buffer, root *RootObjects, scalarTypes []string, inp
349381
// write result types
350382
for _, t := range resultTypeDefs {
351383
buf.WriteByte('\n')
384+
385+
if len(t.DocLines) > 0 {
386+
buf.WriteString("\"\"\"\n")
387+
for _, line := range t.DocLines {
388+
buf.WriteString(line)
389+
buf.WriteByte('\n')
390+
}
391+
buf.WriteString("\"\"\"\n")
392+
}
393+
352394
buf.WriteString("type ")
353395
buf.WriteString(t.Name)
354396
buf.WriteString(" {\n")
355397
for _, f := range t.Fields {
398+
399+
if len(f.DocLines) > 0 {
400+
buf.WriteString(" \"\"\"\n")
401+
for _, line := range f.DocLines {
402+
buf.WriteString(" ")
403+
buf.WriteString(line)
404+
buf.WriteByte('\n')
405+
}
406+
buf.WriteString(" \"\"\"\n")
407+
}
408+
356409
buf.WriteString(" ")
357410
buf.WriteString(f.Name)
358411
buf.WriteString(": ")
@@ -364,6 +417,17 @@ func writeSchema(buf *bytes.Buffer, root *RootObjects, scalarTypes []string, inp
364417
}
365418

366419
func writeField(buf *bytes.Buffer, field *FieldDefinition) {
420+
421+
if len(field.DocLines) > 0 {
422+
buf.WriteString(" \"\"\"\n")
423+
for _, line := range field.DocLines {
424+
buf.WriteString(" ")
425+
buf.WriteString(line)
426+
buf.WriteByte('\n')
427+
}
428+
buf.WriteString(" \"\"\"\n")
429+
}
430+
367431
buf.WriteString(" ")
368432
buf.WriteString(field.Name)
369433
if len(field.Arguments) > 0 {
@@ -486,10 +550,17 @@ func convertFields(fields []*metadata.Field, lti langsupport.LanguageTypeInfo, t
486550
if err != nil {
487551
return nil, err
488552
}
489-
results[i] = &FieldDefinition{
553+
554+
fieldDef := &FieldDefinition{
490555
Name: f.Name,
491556
Type: t,
492557
}
558+
559+
if f.Docs != nil {
560+
fieldDef.DocLines = f.Docs.Lines
561+
}
562+
563+
results[i] = fieldDef
493564
}
494565
return results, nil
495566
}

sdk/assemblyscript/examples/simple/assembly/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@
66

77
import { Person } from "./person";
88

9-
// This function adds two 32-bit signed integers together, and returns the result.
9+
/**
10+
* Adds two integers together, and returns the result.
11+
*/
1012
export function add(a: i32, b: i32): i32 {
1113
return a + b;
1214
}
1315

14-
// This function takes a first name and a last name, and concatenates them to returns a full name.
16+
/**
17+
* Combines the first and last name of a person, and returns the full name.
18+
*/
1519
export function getFullName(firstName: string, lastName: string): string {
1620
return `${firstName} ${lastName}`;
1721
}
1822

19-
// This function makes a list of people, and returns it.
23+
/**
24+
* Gets a list of people.
25+
*/
2026
export function getPeople(): Person[] {
2127
return [
2228
new Person("Bob", "Smith"),
@@ -25,15 +31,19 @@ export function getPeople(): Person[] {
2531
];
2632
}
2733

28-
// This function returns a random person from the list of people.
34+
/**
35+
* Gets a random person from the list of people.
36+
*/
2937
export function getRandomPerson(): Person {
3038
const people = getPeople();
3139
const index = <i32>Math.floor(Math.random() * people.length);
3240
const person = people[index];
3341
return person;
3442
}
3543

36-
// This function demonstrates various ways to log messages and errors.
44+
/**
45+
* Demonstrates logging error messages at different levels.
46+
*/
3747
export function testErrors(): void {
3848
// This is a simple log message. It has no level.
3949
console.log("This is a simple log message.");

sdk/assemblyscript/examples/simple/assembly/person.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,24 @@
77
// This class is used in the examples.
88
// It is separated just to demonstrated how to use multiple files.
99
// Note, this is just one way to define a class in AssemblyScript.
10+
11+
/**
12+
* A simple object representing a person.
13+
*/
1014
export class Person {
15+
/**
16+
* The first name of the person.
17+
*/
1118
firstName: string;
19+
20+
/**
21+
* The last name of the person.
22+
*/
1223
lastName: string;
24+
25+
/**
26+
* The full name of the person.
27+
*/
1328
fullName: string;
1429

1530
constructor(firstName: string, lastName: string) {

0 commit comments

Comments
 (0)