diff --git a/src/index.ts b/src/index.ts index 7ceee71..2551b4d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,15 +7,16 @@ import { Generator } from './types' export { Generator } from './types' export function generateCode(schema: string, generator: Generator | string): string { - if (typeof generator === 'string'){ + if (typeof generator === 'string') { generator = generators[generator] || require(generator).generator if (!generator) { throw new Error(`Generator '${generator}' could not be found. Available generators: ${Object.keys(generators).map(k => `'${k}`).join(', ')}`) } - } + } const document: DocumentNode = parse(schema, { noLocation: true }) + const ast: GraphQLSchema = buildASTSchema(document) // Create types diff --git a/test/createReport.js b/test/createReport.js index 8768d0c..477e1a2 100644 --- a/test/createReport.js +++ b/test/createReport.js @@ -5,14 +5,9 @@ var options = { jsonFile: 'test/report/cucumber_report.json', output: 'test/report/cucumber_report.html', reportSuiteAsScenarios: true, - launchReport: true, + launchReport: false, metadata: { - "App Version":"0.3.2", - "Test Environment": "STAGING", - "Browser": "Chrome 54.0.2840.98", - "Platform": "Windows 10", - "Parallel": "Scenarios", - "Executed": "Remote" + } }; diff --git a/test/features/graphcool-ts.feature b/test/features/graphcool-ts.feature index 93db1d2..2ad02fe 100644 --- a/test/features/graphcool-ts.feature +++ b/test/features/graphcool-ts.feature @@ -2,7 +2,7 @@ Feature: Graphcool Typescript Feature for Graphcool Typescript generator - Scenario: Scenario name + Scenario: Query type only Given a schema looking like this: """ type Query { @@ -49,4 +49,10 @@ Feature for Graphcool Typescript generator posts: (args, info): Promise | null> => super.delegate('query', 'posts', args, {}, info) } } - """ \ No newline at end of file + """ + + Scenario: Query and Mutation type + Given the schema from 'test/testfiles/input/query-mutation.graphql' + And I pick generator 'graphcool-ts' + When I run the generator + Then I expect the output to match 'test/testfiles/output/query-mutation.ts' \ No newline at end of file diff --git a/test/step_definitions/graphcool-ts.steps.ts b/test/step_definitions/graphcool-ts.steps.ts index 36e30a4..23ff785 100644 --- a/test/step_definitions/graphcool-ts.steps.ts +++ b/test/step_definitions/graphcool-ts.steps.ts @@ -1,4 +1,5 @@ -import * as path from 'path' +import * as fs from 'fs'; +import * as path from 'path'; import { defineSupportCode, TableDefinition, World } from 'cucumber' import { generateCode } from '../../src' @@ -7,18 +8,28 @@ defineSupportCode(function({ Given, When, Then }) { this.schema = schema }) + Given(/^the schema from '(.*)'$/, function(filename) { + this.schema = fs.readFileSync(filename, 'utf-8') + }) + Given('I pick generator {string}', function(generator) { this.generator = generator }) When('I run the generator', function() { this.result = generateCode(this.schema, this.generator) - this.attach(this.result, 'application/typescript'); }) Then('I expect the output to be:', function(output) { console.assert(normalizeText(this.result) == normalizeText(output), output) }) + + Then(/^I expect the output to match '(.*)'$/, function(filename) { + const output = fs.readFileSync(filename, 'utf-8') + console.log(output) + console.log(this.result) + console.assert(normalizeText(this.result) == normalizeText(output), output) + }) }) function normalizeText(text) { diff --git a/test/testfiles/input/query-mutation.graphql b/test/testfiles/input/query-mutation.graphql new file mode 100644 index 0000000..eb241cf --- /dev/null +++ b/test/testfiles/input/query-mutation.graphql @@ -0,0 +1,3 @@ +type Query { + posts: [String] +} \ No newline at end of file diff --git a/test/testfiles/output/query-mutation.ts b/test/testfiles/output/query-mutation.ts new file mode 100644 index 0000000..3799e76 --- /dev/null +++ b/test/testfiles/output/query-mutation.ts @@ -0,0 +1,36 @@ +import { Graphcool as BaseGraphcool, BaseGraphcoolOptions } from 'graphcool-binding' +import { GraphQLResolveInfo } from 'graphql' + +const typeDefs = ` +type Query { + posts: [String] +}` + +/* +The `Boolean` scalar type represents `true` or `false`. +*/ +export type Boolean = boolean + +/* +The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text. +*/ +export type String = string + +export interface Schema { + query: Query +} + +export type Query = { + posts: (args: {}, info?: GraphQLResolveInfo | string) => Promise | null> +} + +export class Graphcool extends BaseGraphcool { + + constructor({ endpoint, secret, fragmentReplacements, debug }: BaseGraphcoolOptions) { + super({ typeDefs, endpoint, secret, fragmentReplacements, debug }); + } + + query: Query = { + posts: (args, info): Promise | null> => super.delegate('query', 'posts', args, {}, info) + } +}