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

feature: support ts schema #431

Merged
merged 3 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/graphqlgen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@types/yargs": "12.0.8",
"benchmark": "2.1.4",
"flow-bin": "0.86.0",
"graphql-tag": "^2.10.1",
"jest": "23.6.0",
"ts-jest": "23.10.5",
"ts-node": "8.0.2",
Expand Down
19 changes: 16 additions & 3 deletions packages/graphqlgen/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Ajv from 'ajv'
import * as chalk from 'chalk'
import * as fs from 'fs'
import * as yaml from 'js-yaml'
import { print } from 'graphql'
import { importSchema } from 'graphql-import'

import {
Expand Down Expand Up @@ -83,16 +84,28 @@ export function parseContext(
}

export function parseSchema(schemaPath: string): GraphQLTypes {
if (!fs.existsSync(schemaPath)) {
const [filePath, constName] = schemaPath.split(':')

if (!fs.existsSync(filePath)) {
console.error(
chalk.default.red(`The schema file ${schemaPath} does not exist`),
chalk.default.red(`The schema file ${filePath} does not exist`),
)
process.exit(1)
}

let schema: string | undefined
try {
schema = importSchema(schemaPath)
if (filePath.endsWith('.ts')) {
const loadedSchema = require(filePath)[constName || 'default']

if (typeof loadedSchema === 'string') {
schema = loadedSchema
} else {
schema = print(loadedSchema)
}
} else {
schema = importSchema(filePath)
}
} catch (e) {
console.error(
chalk.default.red(`Error occurred while reading schema: ${e}`),
Expand Down
3 changes: 3 additions & 0 deletions packages/graphqlgen/tests/validation/mocks/gqlSchema/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface PostModel {
id: string
}
12 changes: 12 additions & 0 deletions packages/graphqlgen/tests/validation/mocks/gqlSchema/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gql from 'graphql-tag'

export const typeDefs = gql`
type User {
id: String!
posts: [Post!]!
}

type Post {
id: String!
}
`
3 changes: 3 additions & 0 deletions packages/graphqlgen/tests/validation/mocks/gqlSchema/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface User {
id: string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface PostModel {
id: string
}
10 changes: 10 additions & 0 deletions packages/graphqlgen/tests/validation/mocks/tsSchemaConst/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const typeDefs = `
type User {
id: String!
posts: [Post!]!
}

type Post {
id: String!
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface User {
id: string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface PostModel {
id: string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default `
type User {
id: String!
posts: [Post!]!
}

type Post {
id: String!
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface User {
id: string
}
45 changes: 45 additions & 0 deletions packages/graphqlgen/tests/validation/validateModels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,49 @@ describe('test validateModels()', () => {
false,
)
})

test('import schema ts default', () => {
testValidateModels(
{
schema: relative('./mocks/tsSchemaDefault/schema.ts'),
models: {
files: [relative('./mocks/tsSchemaDefault/types.ts')],
override: {
Post: relative('./mocks/tsSchemaDefault/model.ts:PostModel'),
},
},
},
true,
)
})

test('import schema ts const', () => {
testValidateModels(
{
schema: relative('./mocks/tsSchemaConst/schema.ts:typeDefs'),
models: {
files: [relative('./mocks/tsSchemaConst/types.ts')],
override: {
Post: relative('./mocks/tsSchemaConst/model.ts:PostModel'),
},
},
},
true,
)
})

test('import schema gql default', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('import schema gql default', () => {
test('import schema gql const', () => {

testValidateModels(
{
schema: relative('./mocks/gqlSchema/schema.ts:typeDefs'),
models: {
files: [relative('./mocks/gqlSchema/types.ts')],
override: {
Post: relative('./mocks/gqlSchema/model.ts:PostModel'),
},
},
},
true,
)
})
})
Loading