From 97fa5a61b724696304cd8e5c837c267b4ae90042 Mon Sep 17 00:00:00 2001 From: Flavian Desverne Date: Sat, 22 Dec 2018 06:03:42 +0100 Subject: [PATCH] test: actually compile the code generated by tests (#275) --- .circleci/config.yml | 2 +- package.json | 1 + packages/graphqlgen/package.json | 8 +- .../src/generators/flow-generator.ts | 22 ++- packages/graphqlgen/src/index.ts | 6 +- packages/graphqlgen/src/source-helper.ts | 2 +- .../src/tests/fixtures/context/flow-types.js | 6 +- .../src/tests/fixtures/context/types.ts | 6 +- .../src/tests/fixtures/prisma/flow-types.js | 38 ++-- .../flow/__snapshots__/basic.test.ts.snap | 22 +-- .../__snapshots__/large-schema.test.ts.snap | 2 +- .../graphqlgen/src/tests/flow/basic.test.ts | 46 ++--- .../src/tests/flow/large-schema.test.ts | 10 +- packages/graphqlgen/src/tests/generation.ts | 165 +++++++++++++++++- .../__snapshots__/basic.test.ts.snap | 18 +- .../__snapshots__/large-schema.test.ts.snap | 2 +- .../src/tests/typescript/basic.test.ts | 53 +++--- .../src/tests/typescript/large-schema.test.ts | 11 +- yarn.lock | 5 + 19 files changed, 306 insertions(+), 119 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4868d59d..5a0e1b1b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,5 +6,5 @@ jobs: steps: - checkout - run: yarn install - - run: yarn test + - run: yarn test:ci # - run: npx semantic-release diff --git a/package.json b/package.json index b1a3be3c..1adc21e0 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "scripts": { "build": "cd packages/graphqlgen-json-schema && yarn build && cd ../graphqlgen && yarn build", "test": "yarn build && cd packages/graphqlgen && yarn test", + "test:ci": "yarn build && cd packages/graphqlgen && yarn test:ci", "fix:prettier": "yarn prettier --write './**/*.{md,ts,js,graphql,yaml}'" }, "workspaces": [ diff --git a/packages/graphqlgen/package.json b/packages/graphqlgen/package.json index 0ce2b090..c65a3b7e 100644 --- a/packages/graphqlgen/package.json +++ b/packages/graphqlgen/package.json @@ -17,8 +17,9 @@ "build": "npm run lint && tsc --declaration", "watch": "tsc -w", "lint": "tslint --project tsconfig.json {src,test}/**/*.ts", - "t": "jest", - "test": "npm run lint && npm run t", + "test": "jest", + "test:watch": "jest --watch", + "test:ci": "npm run lint && jest --maxWorkers 4", "gen": "ts-node --files src/index.ts" }, "repository": { @@ -58,7 +59,8 @@ "@types/node": "10.12.18", "@types/prettier": "1.15.2", "@types/rimraf": "2.0.2", - "@types/yargs": "12.0.2", + "@types/yargs": "12.0.1", + "flow-bin": "^0.86.0", "jest": "23.6.0", "ts-jest": "23.10.5", "ts-node": "7.0.1", diff --git a/packages/graphqlgen/src/generators/flow-generator.ts b/packages/graphqlgen/src/generators/flow-generator.ts index eafdfcee..3c733b7c 100644 --- a/packages/graphqlgen/src/generators/flow-generator.ts +++ b/packages/graphqlgen/src/generators/flow-generator.ts @@ -2,7 +2,11 @@ import * as os from 'os' import * as prettier from 'prettier' import { GenerateArgs, ModelMap, ContextDefinition } from '../types' -import { GraphQLTypeField, GraphQLTypeObject } from '../source-helper' +import { + GraphQLTypeField, + GraphQLTypeObject, + GraphQLTypeArgument, +} from '../source-helper' import { upperFirst } from '../utils' import { getContextName, @@ -197,7 +201,7 @@ function renderInputArgInterface( ${field.arguments .map( arg => - `${arg.name}: ${printFieldLikeType( + `${arg.name}: ${getArgTypePrefix(type, arg)}${printFieldLikeType( arg as GraphQLTypeField, modelMap, )}`, @@ -207,6 +211,20 @@ function renderInputArgInterface( ` } +const getArgTypePrefix = ( + type: GraphQLTypeObject, + fieldArg: GraphQLTypeArgument, +): string => { + if ( + fieldArg.type.isScalar || + // Object type includes GQL ID + fieldArg.type.isObject || + fieldArg.type.isEnum + ) + return '' + return upperFirst(type.name) + '_' +} + function renderResolverFunctionInterfaces( type: GraphQLTypeObject, modelMap: ModelMap, diff --git a/packages/graphqlgen/src/index.ts b/packages/graphqlgen/src/index.ts index f2b3acc6..6021d6aa 100644 --- a/packages/graphqlgen/src/index.ts +++ b/packages/graphqlgen/src/index.ts @@ -114,7 +114,7 @@ export function generateCode( return { generatedTypes, generatedResolvers } } -function writeTypes(types: string, config: GraphQLGenDefinition): void { +export function writeTypes(types: string, config: GraphQLGenDefinition): void { // Create generation target folder, if it does not exist // TODO: Error handling around this mkdirp.sync(path.dirname(config.output)) @@ -135,7 +135,7 @@ function writeTypes(types: string, config: GraphQLGenDefinition): void { ) } -function writeResolversScaffolding( +export function writeResolversScaffolding( resolvers: CodeFileLike[], config: GraphQLGenDefinition, ) { @@ -172,8 +172,6 @@ function writeResolversScaffolding( }) console.log(chalk.green(`Resolvers scaffolded at ${outputResolversDir}`)) - - process.exit(0) } function bootstrapYamlFile() { diff --git a/packages/graphqlgen/src/source-helper.ts b/packages/graphqlgen/src/source-helper.ts index 467541b7..5e51cda0 100644 --- a/packages/graphqlgen/src/source-helper.ts +++ b/packages/graphqlgen/src/source-helper.ts @@ -49,7 +49,7 @@ export type GraphQLType = GraphQLTypeDefinition & { isRequired: boolean } -type GraphQLTypeArgument = { +export type GraphQLTypeArgument = { name: string type: GraphQLType defaultValue?: unknown diff --git a/packages/graphqlgen/src/tests/fixtures/context/flow-types.js b/packages/graphqlgen/src/tests/fixtures/context/flow-types.js index 1c41e32d..1b8e43ef 100644 --- a/packages/graphqlgen/src/tests/fixtures/context/flow-types.js +++ b/packages/graphqlgen/src/tests/fixtures/context/flow-types.js @@ -1,13 +1,13 @@ // @flow -interface Data { +export interface Data { users: User[]; } -interface Context { +export interface Context { data: Data; } -interface User { +export interface User { id: string; } diff --git a/packages/graphqlgen/src/tests/fixtures/context/types.ts b/packages/graphqlgen/src/tests/fixtures/context/types.ts index afd5c4a3..d5929cfa 100644 --- a/packages/graphqlgen/src/tests/fixtures/context/types.ts +++ b/packages/graphqlgen/src/tests/fixtures/context/types.ts @@ -1,11 +1,11 @@ -interface Data { +export interface Data { users: User[] } -interface Context { +export interface Context { data: Data } -interface User { +export interface User { id: string } diff --git a/packages/graphqlgen/src/tests/fixtures/prisma/flow-types.js b/packages/graphqlgen/src/tests/fixtures/prisma/flow-types.js index 66857ab6..f724abbc 100644 --- a/packages/graphqlgen/src/tests/fixtures/prisma/flow-types.js +++ b/packages/graphqlgen/src/tests/fixtures/prisma/flow-types.js @@ -31,7 +31,7 @@ export interface Review { checkIn: number; cleanliness: number; communication: number; - createdAt: undefined; + createdAt: string; id: string; location: number; stars: number; @@ -119,7 +119,7 @@ export interface Amenities { export interface User { bookings: any[]; - createdAt: undefined; + createdAt: string; email: string; firstName: string; hostingExperiences: any[]; @@ -136,22 +136,22 @@ export interface User { responseRate: number | null; responseTime: number | null; sentMessages: any[]; - updatedAt: undefined; + updatedAt: string; } export interface Booking { id: string; - createdAt: undefined; + createdAt: string; bookee: any; place: any; - startDate: undefined; - endDate: undefined; + startDate: any; + endDate: any; payment: any; } export interface Payment { booking: any; - createdAt: undefined; + createdAt: string; id: string; paymentMethod: any; serviceFee: number; @@ -159,7 +159,7 @@ export interface Payment { export interface PaymentAccount { id: string; - createdAt: undefined; + createdAt: string; type: any | null; user: any; payments: any[]; @@ -170,7 +170,7 @@ export interface PaymentAccount { export interface PAYMENT_PROVIDER {} export interface PaypalInformation { - createdAt: undefined; + createdAt: string; email: string; id: string; paymentAccount: any; @@ -179,7 +179,7 @@ export interface PaypalInformation { export interface CreditCardInformation { cardNumber: string; country: string; - createdAt: undefined; + createdAt: string; expiresOnMonth: number; expiresOnYear: number; firstName: string; @@ -191,10 +191,10 @@ export interface CreditCardInformation { } export interface Notification { - createdAt: undefined; + createdAt: string; id: string; link: string; - readDate: undefined; + readDate: any; type: any | null; user: any; } @@ -202,10 +202,10 @@ export interface Notification { export interface NOTIFICATION_TYPE {} export interface Message { - createdAt: undefined; - deliveredAt: undefined; + createdAt: string; + deliveredAt: any; id: string; - readAt: undefined; + readAt: any; } export interface Pricing { @@ -242,21 +242,21 @@ export interface Policies { checkInEndTime: number; checkInStartTime: number; checkoutTime: number; - createdAt: undefined; + createdAt: string; id: string; - updatedAt: undefined; + updatedAt: string; } export interface HouseRules { additionalRules: string | null; - createdAt: undefined; + createdAt: string; id: string; partiesAndEventsAllowed: boolean | null; petsAllowed: boolean | null; smokingAllowed: boolean | null; suitableForChildren: boolean | null; suitableForInfants: boolean | null; - updatedAt: undefined; + updatedAt: string; } export interface Reservation { diff --git a/packages/graphqlgen/src/tests/flow/__snapshots__/basic.test.ts.snap b/packages/graphqlgen/src/tests/flow/__snapshots__/basic.test.ts.snap index 124e009d..7871fa13 100644 --- a/packages/graphqlgen/src/tests/flow/__snapshots__/basic.test.ts.snap +++ b/packages/graphqlgen/src/tests/flow/__snapshots__/basic.test.ts.snap @@ -5,7 +5,7 @@ exports[`basic enum 1`] = ` // Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import type { GraphQLResolveInfo } from \\"graphql\\"; -import type { User } from \\"../../../fixtures/enum/types-flow\\"; +import type { User } from \\"../../fixtures/enum/types-flow\\"; type Context = any; type EnumAnnotation = \\"EDITOR\\" | \\"COLLABORATOR\\"; @@ -164,7 +164,7 @@ exports[`basic scalar 1`] = ` // Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import type { GraphQLResolveInfo } from \\"graphql\\"; -import type { AddMemberPayload } from \\"../../../fixtures/scalar/flow-types\\"; +import type { AddMemberPayload } from \\"../../fixtures/scalar/flow-types\\"; type Context = any; // Types for Mutation @@ -176,7 +176,7 @@ export interface Mutation_AddMemberData { } export interface Mutation_Args_AddMember { - data: AddMemberData; + data: Mutation_AddMemberData; } export type Mutation_AddMember_Resolver = ( @@ -276,7 +276,7 @@ exports[`basic schema 1`] = ` // Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import type { GraphQLResolveInfo } from \\"graphql\\"; -import type { Number } from \\"../../../fixtures/basic/types-flow\\"; +import type { Number } from \\"../../fixtures/basic/types-flow\\"; type Context = any; // Types for Query @@ -614,11 +614,7 @@ exports[`basic union 1`] = ` // Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import type { GraphQLResolveInfo } from \\"graphql\\"; -import type { - User, - Student, - Professor -} from \\"../../../fixtures/union/flow-types\\"; +import type { User, Student, Professor } from \\"../../fixtures/union/flow-types\\"; type Context = any; // Types for User @@ -791,8 +787,8 @@ exports[`context 1`] = ` // Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import type { GraphQLResolveInfo } from \\"graphql\\"; -import type { User } from \\"../../../fixtures/context/flow-types\\"; -import type { Context } from \\"../../../fixtures/context/flow-types\\"; +import type { User } from \\"../../fixtures/context/flow-types\\"; +import type { Context } from \\"../../fixtures/context/flow-types\\"; // Types for Query export const Query_defaultResolvers = {}; @@ -894,7 +890,7 @@ exports[`defaultName 1`] = ` // Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import type { GraphQLResolveInfo } from \\"graphql\\"; -import type { NumberNode } from \\"../../../fixtures/defaultName/flow-types\\"; +import type { NumberNode } from \\"../../fixtures/defaultName/flow-types\\"; type Context = any; // Types for Query @@ -1232,7 +1228,7 @@ exports[`subscription 1`] = ` // Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import type { GraphQLResolveInfo } from \\"graphql\\"; -import type { User } from \\"../../../fixtures/subscription/flow-types\\"; +import type { User } from \\"../../fixtures/subscription/flow-types\\"; type Context = any; // Types for Subscription diff --git a/packages/graphqlgen/src/tests/flow/__snapshots__/large-schema.test.ts.snap b/packages/graphqlgen/src/tests/flow/__snapshots__/large-schema.test.ts.snap index 58d2d337..568dc06f 100644 --- a/packages/graphqlgen/src/tests/flow/__snapshots__/large-schema.test.ts.snap +++ b/packages/graphqlgen/src/tests/flow/__snapshots__/large-schema.test.ts.snap @@ -34,7 +34,7 @@ import type { Message, AuthPayload, MutationResult -} from \\"../../../fixtures/prisma/flow-types\\"; +} from \\"../../fixtures/prisma/flow-types\\"; type Context = any; type PLACE_SIZES = diff --git a/packages/graphqlgen/src/tests/flow/basic.test.ts b/packages/graphqlgen/src/tests/flow/basic.test.ts index e54ded15..6781a6db 100644 --- a/packages/graphqlgen/src/tests/flow/basic.test.ts +++ b/packages/graphqlgen/src/tests/flow/basic.test.ts @@ -1,56 +1,58 @@ import { testGeneration } from '../generation' import { join } from 'path' -const language = 'flow' const relative = (p: string) => join(__dirname, p) +const typesPath = relative('./generated-basic/graphqlgen.js') +const resolversDir = relative('./generated-basic/tmp-resolvers/') +const language = 'flow' test('basic schema', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/basic/schema.graphql'), models: { files: [relative('../fixtures/basic/types-flow.js')], }, - output: relative('./generated/basic/graphqlgen.js'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/basic/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('basic enum', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/enum/schema.graphql'), models: { files: [relative('../fixtures/enum/types-flow.js')], }, - output: relative('./generated/enum/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/enum/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('basic union', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/union/schema.graphql'), models: { files: [relative('../fixtures/union/flow-types.js')], }, - output: relative('./generated/union/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/union/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('defaultName', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/defaultName/schema.graphql'), models: { @@ -61,55 +63,55 @@ test('defaultName', async () => { }, ], }, - output: relative('./generated/defaultName/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/scalar/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('basic scalar', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/scalar/schema.graphql'), models: { files: [relative('../fixtures/scalar/flow-types.js')], }, - output: relative('./generated/scalar/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/scalar/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('context', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/context/schema.graphql'), context: relative('../fixtures/context/flow-types.js:Context'), models: { files: [relative('../fixtures/context/flow-types.js')], }, - output: relative('./generated/context/graphqlgen.js'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/input/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('subscription', () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/subscription/schema.graphql'), models: { files: [relative('../fixtures/subscription/flow-types.js')], }, - output: relative('./generated/subscription/graphqlgen.js'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/input/'), + output: resolversDir, layout: 'file-per-type', }, }) diff --git a/packages/graphqlgen/src/tests/flow/large-schema.test.ts b/packages/graphqlgen/src/tests/flow/large-schema.test.ts index a63110d3..5b13bd7b 100644 --- a/packages/graphqlgen/src/tests/flow/large-schema.test.ts +++ b/packages/graphqlgen/src/tests/flow/large-schema.test.ts @@ -1,19 +1,21 @@ import { testGeneration } from '../generation' import { join } from 'path' -const language = 'flow' const relative = (p: string) => join(__dirname, p) +const typesDir = relative('./generated-large/graphqlgen.js') +const resolversDir = relative('./generated-large/tmp-resolvers/') +const language = 'flow' test('large schema', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/prisma/schema.graphql'), models: { files: [relative('../fixtures/prisma/flow-types.js')], }, - output: relative('./generated/prisma/graphqlgen.js'), + output: typesDir, ['resolver-scaffolding']: { - output: relative('./tmp/prisma/'), + output: resolversDir, layout: 'file-per-type', }, }) diff --git a/packages/graphqlgen/src/tests/generation.ts b/packages/graphqlgen/src/tests/generation.ts index 55cf8260..98bf4f9d 100644 --- a/packages/graphqlgen/src/tests/generation.ts +++ b/packages/graphqlgen/src/tests/generation.ts @@ -1,9 +1,131 @@ -import { GraphQLGenDefinition } from 'graphqlgen-json-schema' -import { parseModels, parseSchema } from '../parse' +import * as ts from 'typescript' +import { EOL } from 'os' +import * as rimraf from 'rimraf' +import * as path from 'path' +import { execFile } from 'child_process' +import { writeFileSync } from 'fs' +import chalk from 'chalk' +import { File, GraphQLGenDefinition } from 'graphqlgen-json-schema' +import { getPath, parseModels, parseSchema } from '../parse' import { validateConfig } from '../validation' -import { generateCode } from '../index' +import { generateCode, writeResolversScaffolding, writeTypes } from '../index' +const flow = require('flow-bin') -export function testGeneration(config: GraphQLGenDefinition) { +class ExecError extends Error { + constructor( + public message: string, + public stdout: string, + public stderr: string, + ) { + super(message) + // https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work + Object.setPrototypeOf(this, new.target.prototype) + } +} + +const exec = (command: string, args: string[]): Promise => { + return new Promise(resolve => { + return execFile(command, args, (err, stdout, stderr) => { + if (err) { + resolve(new ExecError(err.message, stdout, stderr)) + } else { + resolve(stdout) + } + }) + }) +} + +function printTypescriptErrors(diagnotics: ReadonlyArray) { + diagnotics.forEach(diagnostic => { + if (diagnostic.file) { + const { line, character } = diagnostic.file.getLineAndCharacterOfPosition( + diagnostic.start!, + ) + const message = ts.flattenDiagnosticMessageText( + diagnostic.messageText, + '\n', + ) + console.log( + `${diagnostic.file.fileName} (${line + 1},${character + + 1}): ${message}`, + ) + } else { + console.log( + `${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`, + ) + } + }) +} + +function compileTypescript(fileNames: string[], compiledOutputDir: string) { + const errors = ts + .createProgram(fileNames, { + sourceMap: false, + noEmitOnError: true, + target: ts.ScriptTarget.ESNext, + module: ts.ModuleKind.CommonJS, + outDir: compiledOutputDir, + }) + .emit().diagnostics + + if (errors.length > 0) { + printTypescriptErrors(errors) + } + + expect(errors.length).toEqual(0) +} + +async function compileFlow(includeFiles: File[], typesPath: string) { + const flowConfig = ` +[ignore] + +[include] +${includeFiles.map(file => getPath(file)).join(EOL)} + +[libs] + +[lints] + +[options] + +[strict] + ` + + const flowConfigName = `.flowconfig-${Math.random()}` + const flowConfigPath = path.join(path.dirname(typesPath), flowConfigName) + + writeFileSync(flowConfigPath, flowConfig) + + const result = await exec(flow, [ + 'check', + '--flowconfig-name', + flowConfigName, + path.resolve(path.dirname(typesPath)), + ]) + + if (result instanceof ExecError) { + const errorDelimiter = + 'Error ----------------------------------------------------------------' + + const errors = result.stdout + .split(errorDelimiter) + // Do not take into account error from 'import type { GraphQLResolveInfo } from "graphql"' + .filter( + error => + error.length !== 0 && + !error.includes('Cannot resolve module `graphql`'), + ) + + if (errors.length > 0) { + const message = errors + .map(error => `${chalk.red(errorDelimiter) + EOL}${error}`) + .join(EOL) + throw new Error(message) + } + } +} + +export async function testGeneration(config: GraphQLGenDefinition) { const schema = parseSchema(config.schema) expect(validateConfig(config, schema)).toBe(true) @@ -24,4 +146,39 @@ export function testGeneration(config: GraphQLGenDefinition) { expect(generatedTypes).toMatchSnapshot() expect(generatedResolvers).toMatchSnapshot() + + const restoreLog = console.log + + // Mock console.log + console.log = jest.fn() + + writeTypes(generatedTypes, config) + writeResolversScaffolding(generatedResolvers, config) + + // Restore console log to print errors if there are any + console.log = restoreLog + + const outputResolversDir = config['resolver-scaffolding']!.output + + const fileNames = [ + ...generatedResolvers.map(resolver => + path.join(outputResolversDir, resolver.path), + ), + config.output, + ] + + const compiledOutputDir = path.join( + path.dirname(config.output), + String(Math.random()), + ) + + if (config.language === 'typescript') { + compileTypescript(fileNames, compiledOutputDir) + } + + if (config.language === 'flow') { + await compileFlow(config.models.files!, config.output) + } + + rimraf.sync(path.dirname(config.output)) } diff --git a/packages/graphqlgen/src/tests/typescript/__snapshots__/basic.test.ts.snap b/packages/graphqlgen/src/tests/typescript/__snapshots__/basic.test.ts.snap index b9d6ba47..e4a17ff0 100644 --- a/packages/graphqlgen/src/tests/typescript/__snapshots__/basic.test.ts.snap +++ b/packages/graphqlgen/src/tests/typescript/__snapshots__/basic.test.ts.snap @@ -4,7 +4,7 @@ exports[`basic enum 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { User } from \\"../../../fixtures/enum/types\\"; +import { User } from \\"../../fixtures/enum/types\\"; type Context = any; type EnumAnnotation = \\"EDITOR\\" | \\"COLLABORATOR\\"; @@ -167,7 +167,7 @@ exports[`basic input 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { AddMemberPayload } from \\"../../../fixtures/input/types\\"; +import { AddMemberPayload } from \\"../../fixtures/input/types\\"; type Context = any; export namespace MutationResolvers { @@ -340,7 +340,7 @@ exports[`basic scalar 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { AddMemberPayload } from \\"../../../fixtures/scalar/types\\"; +import { AddMemberPayload } from \\"../../fixtures/scalar/types\\"; type Context = any; export namespace MutationResolvers { @@ -456,7 +456,7 @@ exports[`basic schema 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { Number } from \\"../../../fixtures/basic\\"; +import { Number } from \\"../../fixtures/basic\\"; type Context = any; export namespace QueryResolvers { @@ -798,7 +798,7 @@ exports[`basic union 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { User, Student, Professor } from \\"../../../fixtures/union/types\\"; +import { User, Student, Professor } from \\"../../fixtures/union/types\\"; type Context = any; export namespace UserResolvers { @@ -975,8 +975,8 @@ exports[`context 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { User } from \\"../../../fixtures/context/types\\"; -import { Context } from \\"../../../fixtures/context/types\\"; +import { User } from \\"../../fixtures/context/types\\"; +import { Context } from \\"../../fixtures/context/types\\"; export namespace QueryResolvers { export const defaultResolvers = {}; @@ -1082,7 +1082,7 @@ exports[`defaultName 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { NumberNode } from \\"../../../fixtures/defaultName\\"; +import { NumberNode } from \\"../../fixtures/defaultName\\"; type Context = any; export namespace QueryResolvers { @@ -1766,7 +1766,7 @@ exports[`subscription 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { User } from \\"../../../fixtures/subscription/types\\"; +import { User } from \\"../../fixtures/subscription/types\\"; type Context = any; export namespace SubscriptionResolvers { diff --git a/packages/graphqlgen/src/tests/typescript/__snapshots__/large-schema.test.ts.snap b/packages/graphqlgen/src/tests/typescript/__snapshots__/large-schema.test.ts.snap index a217cfa8..9ed15c4b 100644 --- a/packages/graphqlgen/src/tests/typescript/__snapshots__/large-schema.test.ts.snap +++ b/packages/graphqlgen/src/tests/typescript/__snapshots__/large-schema.test.ts.snap @@ -33,7 +33,7 @@ import { Message, AuthPayload, MutationResult -} from \\"../../../fixtures/prisma/types\\"; +} from \\"../../fixtures/prisma/types\\"; type Context = any; type PLACE_SIZES = diff --git a/packages/graphqlgen/src/tests/typescript/basic.test.ts b/packages/graphqlgen/src/tests/typescript/basic.test.ts index 0b5eb31e..22c7303a 100644 --- a/packages/graphqlgen/src/tests/typescript/basic.test.ts +++ b/packages/graphqlgen/src/tests/typescript/basic.test.ts @@ -1,56 +1,59 @@ import { testGeneration } from '../generation' import { join } from 'path' -const language = 'typescript' const relative = (p: string) => join(__dirname, p) +const typesPath = relative('./generated-basic/graphqlgen.ts') +const resolversDir = relative('./generated-basic/tmp-resolvers/') +const language = 'typescript' + test('basic schema', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/basic/schema.graphql'), models: { files: [relative('../fixtures/basic/index.ts')], }, - output: relative('./generated/basic/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/basic/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('basic enum', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/enum/schema.graphql'), models: { files: [relative('../fixtures/enum/types.ts')], }, - output: relative('./generated/enum/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/enum/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('basic union', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/union/schema.graphql'), models: { files: [relative('../fixtures/union/types.ts')], }, - output: relative('./generated/union/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/union/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('defaultName', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/defaultName/schema.graphql'), models: { @@ -61,70 +64,70 @@ test('defaultName', async () => { }, ], }, - output: relative('./generated/defaultName/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/scalar/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('basic scalar', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/scalar/schema.graphql'), models: { files: [relative('../fixtures/scalar/types.ts')], }, - output: relative('./generated/scalar/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/scalar/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('basic input', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/input/schema.graphql'), models: { files: [relative('../fixtures/input/types.ts')], }, - output: relative('./generated/input/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/input/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('context', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/context/schema.graphql'), context: relative('../fixtures/context/types.ts:Context'), models: { files: [relative('../fixtures/context/types.ts')], }, - output: relative('./generated/context/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/input/'), + output: resolversDir, layout: 'file-per-type', }, }) }) test('subscription', () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/subscription/schema.graphql'), models: { files: [relative('../fixtures/subscription/types.ts')], }, - output: relative('./generated/subscription/graphqlgen.ts'), + output: typesPath, ['resolver-scaffolding']: { - output: relative('./tmp/input/'), + output: resolversDir, layout: 'file-per-type', }, }) diff --git a/packages/graphqlgen/src/tests/typescript/large-schema.test.ts b/packages/graphqlgen/src/tests/typescript/large-schema.test.ts index d37cf63c..a66cab01 100644 --- a/packages/graphqlgen/src/tests/typescript/large-schema.test.ts +++ b/packages/graphqlgen/src/tests/typescript/large-schema.test.ts @@ -1,20 +1,23 @@ import { testGeneration } from '../generation' import { join } from 'path' -const language = 'typescript' const relative = (p: string) => join(__dirname, p) +const typesDir = relative('./generated-large/graphqlgen.ts') +const resolversDir = relative('./generated-large/tmp-resolvers/') +const language = 'typescript' + describe('large schema tests', () => { test('large schema', async () => { - testGeneration({ + return testGeneration({ language, schema: relative('../fixtures/prisma/schema.graphql'), models: { files: [relative('../fixtures/prisma/types.ts')], }, - output: relative('./generated/prisma/graphqlgen.ts'), + output: typesDir, ['resolver-scaffolding']: { - output: relative('./tmp/prisma/'), + output: resolversDir, layout: 'file-per-type', }, }) diff --git a/yarn.lock b/yarn.lock index 9ba0dd8a..c9adb050 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1612,6 +1612,11 @@ find-up@^3.0.0: dependencies: locate-path "^3.0.0" +flow-bin@^0.86.0: + version "0.86.0" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.86.0.tgz#153a28722b4dc13b7200c74b644dd4d9f4969a11" + integrity sha512-ulRvFH3ewGIYwg+qPk/OJXoe3Nhqi0RyR0wqgK0b1NzUDEC6O99zU39MBTickXvlrr6iwRO6Wm4lVGeDmnzbew== + for-in@^1.0.1, for-in@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"