@@ -2,9 +2,10 @@ import * as Ajv from 'ajv'
22import * as chalk from 'chalk'
33import * as fs from 'fs'
44import * as yaml from 'js-yaml'
5+ import * as Path from 'path'
6+ import * as tsNode from 'ts-node'
57import { print } from 'graphql'
68import { importSchema } from 'graphql-import'
7-
89import {
910 GraphQLGenDefinition ,
1011 Language ,
@@ -84,9 +85,14 @@ export function parseContext(
8485}
8586
8687export 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 )
8894
89- if ( ! fs . existsSync ( filePath ) ) {
95+ if ( ! fs . existsSync ( absoluteFilePath ) ) {
9096 console . error (
9197 chalk . default . red ( `The schema file ${ filePath } does not exist` ) ,
9298 )
@@ -96,7 +102,11 @@ export function parseSchema(schemaPath: string): GraphQLTypes {
96102 let schema : string | undefined
97103 try {
98104 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 ]
100110
101111 if ( typeof loadedSchema === 'string' ) {
102112 schema = loadedSchema
0 commit comments