Skip to content
This repository was archived by the owner on May 10, 2018. It is now read-only.

fix(generators): replaces incompatible characters in schema with sane defaults #51

Merged
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
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import { Generator } from './types'

export { Generator } from './types'

/**
* The schema contains incompatible characters sometimes, e.g.
* data types in comments are emphasized with "`", which represents
* template strings in ES2015 and TypeScript. This function
* replaces those characters with sane defaults.
*
* @param schema {String} The serialized schema
* @returns {String}
*
*/
const sanitizeSchema = (schema: string) =>
schema.replace(/\`/g, '\'')

export function generateCode(schema: string, generator: Generator | string): string {
if (typeof generator === 'string'){
generator = generators[generator] || require(generator).generator
Expand All @@ -15,6 +28,8 @@ ${Object.keys(generators).map(k => `'${k}`).join(', ')}`)
}
}

schema = sanitizeSchema(schema);

const document: DocumentNode = parse(schema, { noLocation: true })
const ast: GraphQLSchema = buildASTSchema(document)

Expand Down
2 changes: 2 additions & 0 deletions test/features/graphcool-ts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Feature for Graphcool Typescript generator
Scenario: Scenario name
Given a schema looking like this:
"""
# The `Query` type
type Query {
posts: [String]
}
Expand All @@ -17,6 +18,7 @@ Feature for Graphcool Typescript generator
import { GraphQLResolveInfo } from 'graphql'

const typeDefs = `
# The 'Query' type
type Query {
posts: [String]
}`
Expand Down
4 changes: 4 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,10 @@ input LocationWhereUniqueInput {
id: ID
}

"""
The \`Long\` scalar type represents non-fractional signed whole numeric values.
Long can represent values between -(2^63) and 2^63 - 1.
"""
scalar Long

type MessageConnection {
Expand Down