https://github.com/michallytek/typegraphql-reflection-poc
Proof of concept of an enhanced reflection system for TypeScript
https://github.com/michallytek/typegraphql-reflection-poc
plugin reflection transform typegraphql typescript
Last synced: 9 months ago
JSON representation
Proof of concept of an enhanced reflection system for TypeScript
- Host: GitHub
- URL: https://github.com/michallytek/typegraphql-reflection-poc
- Owner: MichalLytek
- Created: 2019-02-12T20:46:02.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-10T07:11:25.000Z (almost 5 years ago)
- Last Synced: 2025-04-04T21:44:27.638Z (12 months ago)
- Topics: plugin, reflection, transform, typegraphql, typescript
- Language: TypeScript
- Homepage:
- Size: 42 KB
- Stars: 41
- Watchers: 7
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TypeGraphQL reflection (PoC)
Proof of concept of an enhanced reflection system for TypeScript.
## How it works?
I uses a custom transform plugin and TS compiler API with a bit of help from [`ts-morph`](https://github.com/dsherret/ts-morph) to read deeply nested generic types info for GraphQL schema making purposes.
## What does it do?
It can read type definitions of this class declaration:
```ts
@ObjectType
export class Sample {
@Field
dateField!: Date;
@Field
optionalStringField?: string;
@Field
optionalStringArrayField?: string[];
@Field
nullableStringArrayField!: Array;
@Field
nullableStringPromiseField!: Promise;
@Field
nullableStringNullableArrayPromiseField!: Promise | null>;
}
```
And emit that metadata, which then allows to read and transform them into this GraphQL type definition (SDL):
```graphql
type Sample {
dateField: Date!
optionalStringField: String
optionalStringArrayField: [String!]
nullableStringArrayField: [String]!
nullableStringPromiseField: String
nullableStringNullableArrayPromiseField: [String]
}
```
## How to run it?
You can run the example by `npm start` - it will run a `ts-node` with a special `ttypescript` compiler flag (`ts-node -C ttypescript src/example/index.ts`) that allows to apply the plugins defined in `tsconfig.json` during the TS files transpilation.
The transform plugin files are located in `src/transformer` directory. The example code (class source file and reading the metadata) are in `src/example` folder and the related supporting code is in `src/helpers` dir. In `src/sdl` you can find some simple helpers for creating GraphQL type SDL string for this demo purposes.
## Further work
This proof of concept supports only basic subset of types - primitive types, array, nullability.
There is a lot of work to do next, both on the reflection plugin side and the TypeGraphQL core side.
If you like this idea of making TypeGraphQL easier to use without all the additional boilerplate, please consider supporting the project and our development efforts!
[](https://opencollective.com/typegraphql)