Skip to content

✨ Refactor NodeJS SDK generator #4294

Closed

Description

@TomChv

What are you trying to do?

This issue aims to improve the current TS SDK by working on:

Templates system

The current implementation has too many tmpl files that could be
merged together to simplify the maintainability.
That would make the code much easier to follow since we would not have to jump from file to file.

Currently, we have 18 template files with api.ts.tmpl as root.

│   ├── api.ts.tmpl
│   ├── arg.ts.tmpl
│   ├── args.ts.tmpl
│   ├── call_args.ts.tmpl
│   ├── class_comment.ts.tmpl
│   ├── header.ts.tmpl
│   ├── method.ts.tmpl
│   ├── method_comment.ts.tmpl
│   ├── method_solve.ts.tmpl
│   ├── object.ts.tmpl
│   ├── object_comment.ts.tmpl
│   ├── objects.ts.tmpl
│   ├── return.ts.tmpl
│   ├── return_solve.ts.tmpl
│   ├── type.ts.tmpl
│   ├── type_field_comment.ts.tmpl
│   └── types.ts.tmpl

Templates to merge

  • types.ts.tpml and type.ts.tpml: The only purpose of types.ts.tpml is to isolate the for loop.
    I feel it's not worth to have two files for this, the template will still be readable if we merge
    those files.

  • objects.ts.tpml and object.ts.tpml: Same as above, files are not big enough to justify a split.

  • args.ts.tpml and arg.ts.tpml: Same as above. (Done in NodeJS: use ID only when it makes sense #4313)

  • return.ts.tpml and method.ts.tpml / return_solve.ts.tpml and method_solve.ts.tpml: Return
    files are so small that we directly use the template instead of creating a different file for this.
    I wonder if it's possible to add the {{- with . }} in a one-liner.

Other files are complex to follow and are not worth merging.
However, to improve the maintainability, we can work on the documentation.

Duplication

I saw that class_comments.ts.tpml and object_comment.ts.tpml are precisely the same.

diff class_comment.ts.tmpl object_comment.ts.tmpl 
1c1
< {{ define "class_comment" -}}
---
> {{ define "object_comment" -}}

I think we can use object_comment.ts.tpml only since a class can be considered as an object.

⚠️ This change only makes sense if we do not plan to add special documentation or annotation that
would make those templates differ.

Documentation

When I started looking at the generator, it was tough for me to figure out how the
beast works.
I understood how the NodeSDK generator works when @dolanor explained it to me during a call.
I was not an expert in Go template (and I'm still not), so that may be the reason, but it's
sometimes hard to enter in the codebase by just reading the code.

To open contributions to anyone, we shall work on its documentation and demystify it.

I have two ideas about that:

Code comments

I think we should write a "header comments" on each template file that explain what this template
do. So even someone who does not understand the Go template can figure out what's happening.
Those comments should be brief, just a couple of lines.

For instance, with api.ts.tpml, we could add the following header comment.

This template is the entry point of the NodeJS SDK generator. 
It designs the skeleton of the SDK.

First, it inserts a static header defined in `header.ts.tpml`.
Second, it inserts a typescript representation of all GraphQL types defined by Dagger GQL API.
Finally, it generates a bunch of classes to interact with this API.

Write a README.md

Another approach that can replace the one above or be a complement is to write a README that explains
how the generator works.
It should explain how each piece of template are mixed together, what functions.go is made for,
how tests work, and how the GraphQL schema is converted to a text file.

I remember having a hard time writing a test because I did not know what to write as input, I
reversed the GQL schema to finally find out some GQL types that were not tests (especially when
I worked on the integration of GQL input type).
Basically this could have been solve by just looking at GraphQL spec if it was written in a doc.

Again, that should be brief but accurate.

💡 I'm against massive documentation because it's painful to update when we make changes.
It should stick to the essentials so a new contributor can have all the context and information
required.

/cc @dolanor @slumbering

Activity

  1. added
    kind/refactor
    on Jan 4, 2023
  2. self-assigned this
    on Jan 4, 2023
  3. dolanor commented on Jan 11, 2023

    @dolanor
    Contributor

    Totally agree with this. With the deadline, I haven't been able to correctly document this, but it needs to be done. I created an issue on this: #4033

    @TomChv and I had a talk on what to do. I started a PR: #4360 so we can work on it.

  4. linked a pull request that will close this issueRefactor NodeJS codegen #4360on Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/refactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions