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

Commit e3fbbfa

Browse files
maticzavJason Kuhrt
authored andcommitted
feature(gg): allow schema to be read from typescript module (#431)
1 parent d3fa492 commit e3fbbfa

File tree

13 files changed

+980
-199
lines changed

13 files changed

+980
-199
lines changed

packages/graphqlgen/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@types/yargs": "12.0.8",
6767
"benchmark": "2.1.4",
6868
"flow-bin": "0.86.0",
69+
"graphql-tag": "^2.10.1",
6970
"jest": "23.6.0",
7071
"ts-jest": "23.10.5",
7172
"ts-node": "8.0.2",

packages/graphqlgen/src/parse.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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 { print } from 'graphql'
56
import { importSchema } from 'graphql-import'
67

78
import {
@@ -83,16 +84,28 @@ export function parseContext(
8384
}
8485

8586
export function parseSchema(schemaPath: string): GraphQLTypes {
86-
if (!fs.existsSync(schemaPath)) {
87+
const [filePath, constName] = schemaPath.split(':')
88+
89+
if (!fs.existsSync(filePath)) {
8790
console.error(
88-
chalk.default.red(`The schema file ${schemaPath} does not exist`),
91+
chalk.default.red(`The schema file ${filePath} does not exist`),
8992
)
9093
process.exit(1)
9194
}
9295

9396
let schema: string | undefined
9497
try {
95-
schema = importSchema(schemaPath)
98+
if (filePath.endsWith('.ts')) {
99+
const loadedSchema = require(filePath)[constName || 'default']
100+
101+
if (typeof loadedSchema === 'string') {
102+
schema = loadedSchema
103+
} else {
104+
schema = print(loadedSchema)
105+
}
106+
} else {
107+
schema = importSchema(filePath)
108+
}
96109
} catch (e) {
97110
console.error(
98111
chalk.default.red(`Error occurred while reading schema: ${e}`),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface PostModel {
2+
id: string
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import gql from 'graphql-tag'
2+
3+
export const typeDefs = gql`
4+
type User {
5+
id: String!
6+
posts: [Post!]!
7+
}
8+
9+
type Post {
10+
id: String!
11+
}
12+
`
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface User {
2+
id: string
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface PostModel {
2+
id: string
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const typeDefs = `
2+
type User {
3+
id: String!
4+
posts: [Post!]!
5+
}
6+
7+
type Post {
8+
id: String!
9+
}
10+
`
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface User {
2+
id: string
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface PostModel {
2+
id: string
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default `
2+
type User {
3+
id: String!
4+
posts: [Post!]!
5+
}
6+
7+
type Post {
8+
id: String!
9+
}
10+
`

0 commit comments

Comments
 (0)