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

Commit

Permalink
fix: ts-schema is found via absolute path and parsed
Browse files Browse the repository at this point in the history
fixes #436
  • Loading branch information
jasonkuhrt committed Feb 7, 2019
1 parent c6584e7 commit 19f5bce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/graphqlgen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"prepublishOnly": "rm -rf example/node_modules && yarn test && yarn build",
"benchmarks": "ts-node benchmarks",
"build": "yarn clean && yarn lint && tsc --declaration",
"watch": "tsc -w",
"watch": "tsc --watch",
"lint": "tslint --project tsconfig.json {src,test}/**/*.ts",
"test": "jest",
"check:types": "yarn tsc --noEmit",
Expand Down
18 changes: 14 additions & 4 deletions packages/graphqlgen/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import * as Ajv from 'ajv'
import * as chalk from 'chalk'
import * as fs from 'fs'
import * as yaml from 'js-yaml'
import * as Path from 'path'
import * as tsNode from 'ts-node'
import { print } from 'graphql'
import { importSchema } from 'graphql-import'

import {
GraphQLGenDefinition,
Language,
Expand Down Expand Up @@ -84,9 +85,14 @@ export function parseContext(
}

export function parseSchema(schemaPath: string): GraphQLTypes {
const [filePath, constName] = schemaPath.split(':')
const [filePath, exportName = 'default'] = schemaPath.split(':')

// We can assume absolute path is cwd prefixed because
// gg currently only works when run in a directory with the
// graphqlgen manifest.
const absoluteFilePath = Path.join(process.cwd(), filePath)

if (!fs.existsSync(filePath)) {
if (!fs.existsSync(absoluteFilePath)) {
console.error(
chalk.default.red(`The schema file ${filePath} does not exist`),
)
Expand All @@ -96,7 +102,11 @@ export function parseSchema(schemaPath: string): GraphQLTypes {
let schema: string | undefined
try {
if (filePath.endsWith('.ts')) {
const loadedSchema = require(filePath)[constName || 'default']
tsNode.register({
transpileOnly: true,
})
const schemaModule = require(absoluteFilePath)
const loadedSchema = schemaModule[exportName]

if (typeof loadedSchema === 'string') {
schema = loadedSchema
Expand Down

0 comments on commit 19f5bce

Please sign in to comment.