-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle enum type fields in subgraph entities
- Loading branch information
Showing
5 changed files
with
129 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// Copyright 2021 Vulcanize, Inc. | ||
// | ||
|
||
{{#each types as | type |}} | ||
export enum {{type.name}} { | ||
{{#each type.values as | value |}} | ||
{{value}} = '{{value}}', | ||
{{/each}} | ||
} | ||
{{/each}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// Copyright 2021 Vulcanize, Inc. | ||
// | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import Handlebars from 'handlebars'; | ||
import { Writable } from 'stream'; | ||
|
||
const TEMPLATE_FILE = './templates/types-template.handlebars'; | ||
|
||
export class Types { | ||
_types: Array<any>; | ||
_templateString: string; | ||
|
||
constructor () { | ||
this._types = []; | ||
this._templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString(); | ||
} | ||
|
||
/** | ||
* Writes the generated types files from a template to a stream. | ||
* @param outStream A writable output stream to write the types file to. | ||
*/ | ||
exportTypes (outStream: Writable): void { | ||
const template = Handlebars.compile(this._templateString); | ||
const obj = { | ||
types: this._types | ||
}; | ||
const database = template(obj); | ||
outStream.write(database); | ||
} | ||
|
||
addSubgraphTypes (subgraphSchemaDocument: any): void { | ||
const subgraphTypeDefs = subgraphSchemaDocument.definitions; | ||
|
||
subgraphTypeDefs.forEach((def: any) => { | ||
if (def.kind !== 'EnumTypeDefinition') { | ||
return; | ||
} | ||
|
||
console.log('def.values', def.values); | ||
|
||
const typeObject: any = { | ||
name: def.name.value, | ||
values: def.values.map((value: any) => value.name.value) | ||
}; | ||
|
||
this._types.push(typeObject); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters