@@ -2,9 +2,10 @@ import * as Ajv from 'ajv'
2
2
import * as chalk from 'chalk'
3
3
import * as fs from 'fs'
4
4
import * as yaml from 'js-yaml'
5
+ import * as Path from 'path'
6
+ import * as tsNode from 'ts-node'
5
7
import { print } from 'graphql'
6
8
import { importSchema } from 'graphql-import'
7
-
8
9
import {
9
10
GraphQLGenDefinition ,
10
11
Language ,
@@ -84,9 +85,14 @@ export function parseContext(
84
85
}
85
86
86
87
export function parseSchema ( schemaPath : string ) : GraphQLTypes {
87
- const [ filePath , constName ] = schemaPath . split ( ':' )
88
+ const [ filePath , exportName = 'default' ] = schemaPath . split ( ':' )
89
+
90
+ // We can assume absolute path is cwd prefixed because
91
+ // gg currently only works when run in a directory with the
92
+ // graphqlgen manifest.
93
+ const absoluteFilePath = Path . join ( process . cwd ( ) , filePath )
88
94
89
- if ( ! fs . existsSync ( filePath ) ) {
95
+ if ( ! fs . existsSync ( absoluteFilePath ) ) {
90
96
console . error (
91
97
chalk . default . red ( `The schema file ${ filePath } does not exist` ) ,
92
98
)
@@ -96,7 +102,11 @@ export function parseSchema(schemaPath: string): GraphQLTypes {
96
102
let schema : string | undefined
97
103
try {
98
104
if ( filePath . endsWith ( '.ts' ) ) {
99
- const loadedSchema = require ( filePath ) [ constName || 'default' ]
105
+ tsNode . register ( {
106
+ transpileOnly : true ,
107
+ } )
108
+ const schemaModule = require ( absoluteFilePath )
109
+ const loadedSchema = schemaModule [ exportName ]
100
110
101
111
if ( typeof loadedSchema === 'string' ) {
102
112
schema = loadedSchema
0 commit comments