Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 19f5bce

Browse files
committed
fix: ts-schema is found via absolute path and parsed
fixes #436
1 parent c6584e7 commit 19f5bce

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

packages/graphqlgen/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"prepublishOnly": "rm -rf example/node_modules && yarn test && yarn build",
1818
"benchmarks": "ts-node benchmarks",
1919
"build": "yarn clean && yarn lint && tsc --declaration",
20-
"watch": "tsc -w",
20+
"watch": "tsc --watch",
2121
"lint": "tslint --project tsconfig.json {src,test}/**/*.ts",
2222
"test": "jest",
2323
"check:types": "yarn tsc --noEmit",

packages/graphqlgen/src/parse.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import * as Ajv from 'ajv'
22
import * as chalk from 'chalk'
33
import * as fs from 'fs'
44
import * as yaml from 'js-yaml'
5+
import * as Path from 'path'
6+
import * as tsNode from 'ts-node'
57
import { print } from 'graphql'
68
import { importSchema } from 'graphql-import'
7-
89
import {
910
GraphQLGenDefinition,
1011
Language,
@@ -84,9 +85,14 @@ export function parseContext(
8485
}
8586

8687
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)
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

Comments
 (0)