apollo-codegen-typescript
Advanced tools
Comparing version 0.27.1 to 0.27.3
@@ -1,3 +0,3 @@ | ||
import 'babel-types'; | ||
declare module 'babel-types' { | ||
import "babel-types"; | ||
declare module "babel-types" { | ||
interface StringLiteralTypeAnnotation { | ||
@@ -11,3 +11,3 @@ value: string; | ||
variance?: { | ||
kind: 'plus' | 'minus'; | ||
kind: "plus" | "minus"; | ||
}; | ||
@@ -14,0 +14,0 @@ optional: boolean; |
@@ -1,8 +0,8 @@ | ||
declare module '@babel/types' { | ||
export * from 'babel-types'; | ||
declare module "@babel/types" { | ||
export * from "babel-types"; | ||
} | ||
declare module '@babel/generator' { | ||
export * from 'babel-generator'; | ||
import Generator from 'babel-generator'; | ||
declare module "@babel/generator" { | ||
export * from "babel-generator"; | ||
import Generator from "babel-generator"; | ||
export default Generator; | ||
} |
@@ -1,4 +0,4 @@ | ||
import { GraphQLType } from 'graphql'; | ||
import * as t from '@babel/types'; | ||
import { CompilerOptions } from 'apollo-codegen-core/lib/compiler'; | ||
import { GraphQLType } from "graphql"; | ||
import * as t from "@babel/types"; | ||
import { CompilerOptions } from "apollo-codegen-core/lib/compiler"; | ||
export declare function createTypeFromGraphQLTypeFunction(compilerOptions: CompilerOptions): (graphQLType: GraphQLType, typeName?: string) => t.TSType; |
@@ -10,3 +10,3 @@ "use strict"; | ||
[graphql_1.GraphQLBoolean.name]: t.TSBooleanKeyword(), | ||
[graphql_1.GraphQLID.name]: t.TSStringKeyword(), | ||
[graphql_1.GraphQLID.name]: t.TSStringKeyword() | ||
}; | ||
@@ -17,3 +17,5 @@ function createTypeFromGraphQLTypeFunction(compilerOptions) { | ||
const elementType = typeFromGraphQLType(graphQLType.ofType, typeName); | ||
return t.TSArrayType(t.isTSUnionType(elementType) ? t.TSParenthesizedType(elementType) : elementType); | ||
return t.TSArrayType(t.isTSUnionType(elementType) | ||
? t.TSParenthesizedType(elementType) | ||
: elementType); | ||
} | ||
@@ -26,3 +28,3 @@ else if (graphQLType instanceof graphql_1.GraphQLScalarType) { | ||
else if (compilerOptions.passthroughCustomScalars) { | ||
return t.TSTypeReference(t.identifier((compilerOptions.customScalarsPrefix || '') + graphQLType.name)); | ||
return t.TSTypeReference(t.identifier((compilerOptions.customScalarsPrefix || "") + graphQLType.name)); | ||
} | ||
@@ -29,0 +31,0 @@ else { |
@@ -1,1 +0,1 @@ | ||
export { generateSource, generateLocalSource, generateGlobalSource } from './codeGeneration'; | ||
export { generateSource, generateLocalSource, generateGlobalSource } from "./codeGeneration"; |
@@ -1,4 +0,4 @@ | ||
import { GraphQLEnumType, GraphQLInputObjectType, GraphQLType } from 'graphql'; | ||
import { CompilerOptions } from 'apollo-codegen-core/lib/compiler'; | ||
import * as t from '@babel/types'; | ||
import { GraphQLEnumType, GraphQLInputObjectType, GraphQLType } from "graphql"; | ||
import { CompilerOptions } from "apollo-codegen-core/lib/compiler"; | ||
import * as t from "@babel/types"; | ||
export declare type ObjectProperty = { | ||
@@ -5,0 +5,0 @@ name: string; |
@@ -19,6 +19,8 @@ "use strict"; | ||
if (description) { | ||
typeAlias.leadingComments = [{ | ||
type: 'CommentBlock', | ||
typeAlias.leadingComments = [ | ||
{ | ||
type: "CommentBlock", | ||
value: printing_1.commentBlockContent(description) | ||
}]; | ||
} | ||
]; | ||
} | ||
@@ -30,4 +32,3 @@ return typeAlias; | ||
const fieldMap = inputObjectType.getFields(); | ||
const fields = Object.keys(inputObjectType.getFields()) | ||
.map((fieldName) => { | ||
const fields = Object.keys(inputObjectType.getFields()).map((fieldName) => { | ||
const field = fieldMap[fieldName]; | ||
@@ -43,6 +44,8 @@ return { | ||
if (description) { | ||
inputType.leadingComments = [{ | ||
type: 'CommentBlock', | ||
inputType.leadingComments = [ | ||
{ | ||
type: "CommentBlock", | ||
value: printing_1.commentBlockContent(description) | ||
}]; | ||
} | ||
]; | ||
} | ||
@@ -54,8 +57,11 @@ return inputType; | ||
const propertySignatureType = t.TSPropertySignature(t.identifier(name), t.TSTypeAnnotation(type)); | ||
propertySignatureType.optional = keyInheritsNullability && this.isNullableType(type); | ||
propertySignatureType.optional = | ||
keyInheritsNullability && this.isNullableType(type); | ||
if (description) { | ||
propertySignatureType.leadingComments = [{ | ||
type: 'CommentBlock', | ||
propertySignatureType.leadingComments = [ | ||
{ | ||
type: "CommentBlock", | ||
value: printing_1.commentBlockContent(description) | ||
}]; | ||
} | ||
]; | ||
} | ||
@@ -77,15 +83,12 @@ return propertySignatureType; | ||
nameFromScopeStack(scope) { | ||
return scope.join('_'); | ||
return scope.join("_"); | ||
} | ||
makeNullableType(type) { | ||
return t.TSUnionType([ | ||
type, | ||
t.TSNullKeyword() | ||
]); | ||
return t.TSUnionType([type, t.TSNullKeyword()]); | ||
} | ||
isNullableType(type) { | ||
return t.isTSUnionType(type) && type.types.some(type => t.isTSNullKeyword(type)); | ||
return (t.isTSUnionType(type) && type.types.some(type => t.isTSNullKeyword(type))); | ||
} | ||
import(types, source) { | ||
return t.importDeclaration(types.map((type) => t.importSpecifier(t.identifier(type.toString()), t.identifier(type.toString()))), t.stringLiteral(source)); | ||
return t.importDeclaration(types.map(type => t.importSpecifier(t.identifier(type.toString()), t.identifier(type.toString()))), t.stringLiteral(source)); | ||
} | ||
@@ -92,0 +95,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import * as t from '@babel/types'; | ||
import * as t from "@babel/types"; | ||
declare type Printable = t.Node | string; | ||
@@ -3,0 +3,0 @@ export default class Printer { |
@@ -9,5 +9,4 @@ "use strict"; | ||
print() { | ||
return this.printQueue | ||
.reduce((document, printable) => { | ||
if (typeof printable === 'string') { | ||
return (this.printQueue.reduce((document, printable) => { | ||
if (typeof printable === "string") { | ||
return document + printable; | ||
@@ -19,8 +18,8 @@ } | ||
} | ||
}, '') + '\n'; | ||
}, "") + "\n"); | ||
} | ||
enqueue(printable) { | ||
if (this.printQueue.length > 0) { | ||
this.printQueue.push('\n'); | ||
this.printQueue.push('\n'); | ||
this.printQueue.push("\n"); | ||
this.printQueue.push("\n"); | ||
} | ||
@@ -35,5 +34,3 @@ this.printQueue.push(printable); | ||
indentComments(documentPart) { | ||
const lines = documentPart | ||
.split('\n') | ||
.filter(Boolean); | ||
const lines = documentPart.split("\n").filter(Boolean); | ||
let currentLine = 0; | ||
@@ -44,3 +41,3 @@ const newDocumentParts = []; | ||
const currentLineContents = lines[currentLine]; | ||
const commentColumn = currentLineContents.indexOf('//'); | ||
const commentColumn = currentLineContents.indexOf("//"); | ||
if (commentColumn > 0) { | ||
@@ -50,5 +47,5 @@ if (maxCommentColumn < commentColumn) { | ||
} | ||
const [contents, comment] = currentLineContents.split('//'); | ||
const [contents, comment] = currentLineContents.split("//"); | ||
newDocumentParts.push({ | ||
main: contents.replace(/\s+$/g, ''), | ||
main: contents.replace(/\s+$/g, ""), | ||
comment: comment ? comment.trim() : null | ||
@@ -71,3 +68,3 @@ }); | ||
const spacesBetween = maxCommentColumn - main.length; | ||
line = `${main}${' '.repeat(spacesBetween)} // ${comment.trim()}`; | ||
line = `${main}${" ".repeat(spacesBetween)} // ${comment.trim()}`; | ||
} | ||
@@ -77,8 +74,5 @@ else { | ||
} | ||
return [ | ||
...memo, | ||
line | ||
]; | ||
return [...memo, line]; | ||
}, []) | ||
.join('\n'); | ||
.join("\n"); | ||
} | ||
@@ -85,0 +79,0 @@ } |
{ | ||
"name": "apollo-codegen-typescript", | ||
"description": "TypeScript generator module for Apollo Codegen", | ||
"version": "0.27.1", | ||
"main": "./lib/index.js", | ||
"version": "0.27.3", | ||
"author": "Apollo GraphQL <opensource@apollographql.com>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/apollographql/apollo-cli.git" | ||
}, | ||
"homepage": "https://github.com/apollographql/apollo-cli", | ||
"bugs": "https://github.com/apollographql/apollo-cli/issues", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"scripts": { | ||
"clean": "rm -rf lib", | ||
"prebuild": "npm run clean", | ||
"build": "tsc -p .", | ||
"watch": "tsc -w -p .", | ||
"test": "./node_modules/.bin/jest", | ||
"prepack": "npm run build" | ||
"build": "tsc", | ||
"prepare": "npm run build" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "apollographql/apollo-cli" | ||
}, | ||
"author": "Martijn Walraven <martijn@martijnwalraven.com>", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=6.0", | ||
"npm": ">=5.0" | ||
"node": ">=8", | ||
"npm": ">=6" | ||
}, | ||
"devDependencies": { | ||
"@types/graphql": "^0.13.1", | ||
"@types/inflected": "^1.1.29", | ||
"graphql": "^0.13.1", | ||
"jest": "^22.0.3", | ||
"jest-matcher-utils": "^22.0.3", | ||
"lerna": "^2.11.0", | ||
"ts-jest": "^22.0.0", | ||
"typescript": "^2.6.2" | ||
}, | ||
"dependencies": { | ||
"@babel/types": "7.0.0-beta.38", | ||
"apollo-codegen-core": "^0.27.0", | ||
"apollo-codegen-core": "0.27.3", | ||
"change-case": "^3.0.1", | ||
@@ -66,3 +57,4 @@ "inflected": "^2.0.3" | ||
} | ||
} | ||
}, | ||
"gitHead": "e8d31998beae239ea92e140c670f45412ca0ffd4" | ||
} |
@@ -1,6 +0,10 @@ | ||
import { parse } from 'graphql'; | ||
import { parse } from "graphql"; | ||
import { loadSchema } from 'apollo-codegen-core/lib/loading'; | ||
const schema = loadSchema(require.resolve('../../../common-test/fixtures/starwars/schema.json')); | ||
const miscSchema = loadSchema(require.resolve('../../../common-test/fixtures/misc/schema.json')); | ||
import { loadSchema } from "apollo-codegen-core/lib/loading"; | ||
const schema = loadSchema( | ||
require.resolve("../../../common-test/fixtures/starwars/schema.json") | ||
); | ||
const miscSchema = loadSchema( | ||
require.resolve("../../../common-test/fixtures/misc/schema.json") | ||
); | ||
@@ -10,6 +14,10 @@ import { | ||
CompilerOptions, | ||
CompilerContext, | ||
} from 'apollo-codegen-core/lib/compiler'; | ||
CompilerContext | ||
} from "apollo-codegen-core/lib/compiler"; | ||
import { generateSource, generateLocalSource, generateGlobalSource } from '../codeGeneration'; | ||
import { | ||
generateSource, | ||
generateLocalSource, | ||
generateGlobalSource | ||
} from "../codeGeneration"; | ||
@@ -21,3 +29,3 @@ function compile( | ||
addTypename: true | ||
}, | ||
} | ||
): CompilerContext { | ||
@@ -33,3 +41,3 @@ const document = parse(source); | ||
addTypename: true | ||
}, | ||
} | ||
): CompilerContext { | ||
@@ -40,4 +48,4 @@ const document = parse(source); | ||
describe('Typescript codeGeneration', () => { | ||
test('multiple files', () => { | ||
describe("Typescript codeGeneration", () => { | ||
test("multiple files", () => { | ||
const context = compile(` | ||
@@ -69,15 +77,14 @@ query HeroName($episode: Episode) { | ||
`); | ||
context.operations["HeroName"].filePath = '/some/file/ComponentA.js'; | ||
context.operations["SomeOther"].filePath = '/some/file/ComponentB.js'; | ||
context.fragments['someFragment'].filePath = '/some/file/ComponentB.js'; | ||
context.operations["HeroName"].filePath = "/some/file/ComponentA.js"; | ||
context.operations["SomeOther"].filePath = "/some/file/ComponentB.js"; | ||
context.fragments["someFragment"].filePath = "/some/file/ComponentB.js"; | ||
const output = generateSource(context); | ||
expect(output).toBeInstanceOf(Object); | ||
Object.keys(output) | ||
.forEach((filePath) => { | ||
expect(filePath).toMatchSnapshot(); | ||
expect(output[filePath]).toMatchSnapshot(); | ||
}); | ||
Object.keys(output).forEach(filePath => { | ||
expect(filePath).toMatchSnapshot(); | ||
expect(output[filePath]).toMatchSnapshot(); | ||
}); | ||
}); | ||
test('simple hero query', () => { | ||
test("simple hero query", () => { | ||
const context = compile(` | ||
@@ -96,3 +103,3 @@ query HeroName($episode: Episode) { | ||
test('simple mutation', () => { | ||
test("simple mutation", () => { | ||
const context = compile(` | ||
@@ -111,3 +118,3 @@ mutation ReviewMovie($episode: Episode, $review: ReviewInput) { | ||
test('simple fragment', () => { | ||
test("simple fragment", () => { | ||
const context = compile(` | ||
@@ -123,3 +130,3 @@ fragment SimpleFragment on Character{ | ||
test('fragment with fragment spreads', () => { | ||
test("fragment with fragment spreads", () => { | ||
const context = compile(` | ||
@@ -140,3 +147,3 @@ fragment simpleFragment on Character { | ||
test('fragment with fragment spreads with inline fragment', () => { | ||
test("fragment with fragment spreads with inline fragment", () => { | ||
const context = compile(` | ||
@@ -161,3 +168,3 @@ fragment simpleFragment on Character { | ||
test('query with fragment spreads', () => { | ||
test("query with fragment spreads", () => { | ||
const context = compile(` | ||
@@ -180,3 +187,3 @@ fragment simpleFragment on Character { | ||
test('inline fragment', () => { | ||
test("inline fragment", () => { | ||
const context = compile(` | ||
@@ -195,5 +202,5 @@ query HeroInlineFragment($episode: Episode) { | ||
expect(output).toMatchSnapshot(); | ||
}) | ||
}); | ||
test('inline fragment on type conditions', () => { | ||
test("inline fragment on type conditions", () => { | ||
const context = compile(` | ||
@@ -222,3 +229,3 @@ query HeroName($episode: Episode) { | ||
test('inline fragment on type conditions with differing inner fields', () => { | ||
test("inline fragment on type conditions with differing inner fields", () => { | ||
const context = compile(` | ||
@@ -251,3 +258,3 @@ query HeroName($episode: Episode) { | ||
test('fragment spreads with inline fragments', () => { | ||
test("fragment spreads with inline fragments", () => { | ||
const context = compile(` | ||
@@ -284,3 +291,3 @@ query HeroName($episode: Episode) { | ||
test('handles multiline graphql comments', () => { | ||
test("handles multiline graphql comments", () => { | ||
const context = compileMisc(` | ||
@@ -299,4 +306,4 @@ query CustomScalar { | ||
describe('Typescript codeGeneration local / global', () => { | ||
test('simple hero query', () => { | ||
describe("Typescript codeGeneration local / global", () => { | ||
test("simple hero query", () => { | ||
const context = compile(` | ||
@@ -311,8 +318,8 @@ query HeroName($episode: Episode) { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -323,3 +330,3 @@ expect(output).toMatchSnapshot(); | ||
test('simple mutation', () => { | ||
test("simple mutation", () => { | ||
const context = compile(` | ||
@@ -334,8 +341,8 @@ mutation ReviewMovie($episode: Episode, $review: ReviewInput) { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -346,3 +353,3 @@ expect(output).toMatchSnapshot(); | ||
test('simple fragment', () => { | ||
test("simple fragment", () => { | ||
const context = compile(` | ||
@@ -354,8 +361,8 @@ fragment SimpleFragment on Character{ | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -366,3 +373,3 @@ expect(output).toMatchSnapshot(); | ||
test('fragment with fragment spreads', () => { | ||
test("fragment with fragment spreads", () => { | ||
const context = compile(` | ||
@@ -379,8 +386,8 @@ fragment simpleFragment on Character { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -391,3 +398,3 @@ expect(output).toMatchSnapshot(); | ||
test('fragment with fragment spreads with inline fragment', () => { | ||
test("fragment with fragment spreads with inline fragment", () => { | ||
const context = compile(` | ||
@@ -408,8 +415,8 @@ fragment simpleFragment on Character { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -420,3 +427,3 @@ expect(output).toMatchSnapshot(); | ||
test('query with fragment spreads', () => { | ||
test("query with fragment spreads", () => { | ||
const context = compile(` | ||
@@ -435,8 +442,8 @@ fragment simpleFragment on Character { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -447,3 +454,3 @@ expect(output).toMatchSnapshot(); | ||
test('inline fragment', () => { | ||
test("inline fragment", () => { | ||
const context = compile(` | ||
@@ -460,14 +467,14 @@ query HeroInlineFragment($episode: Episode) { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
expect(output).toMatchSnapshot(); | ||
expect(generateGlobalSource(context)).toMatchSnapshot(); | ||
}) | ||
}); | ||
test('inline fragment on type conditions', () => { | ||
test("inline fragment on type conditions", () => { | ||
const context = compile(` | ||
@@ -493,8 +500,8 @@ query HeroName($episode: Episode) { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -505,3 +512,3 @@ expect(output).toMatchSnapshot(); | ||
test('inline fragment on type conditions with differing inner fields', () => { | ||
test("inline fragment on type conditions with differing inner fields", () => { | ||
const context = compile(` | ||
@@ -530,8 +537,8 @@ query HeroName($episode: Episode) { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -542,3 +549,3 @@ expect(output).toMatchSnapshot(); | ||
test('fragment spreads with inline fragments', () => { | ||
test("fragment spreads with inline fragments", () => { | ||
const context = compile(` | ||
@@ -572,8 +579,8 @@ query HeroName($episode: Episode) { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -584,3 +591,3 @@ expect(output).toMatchSnapshot(); | ||
test('handles multiline graphql comments', () => { | ||
test("handles multiline graphql comments", () => { | ||
const context = compileMisc(` | ||
@@ -594,8 +601,8 @@ query CustomScalar { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -606,3 +613,3 @@ expect(output).toMatchSnapshot(); | ||
test('multiple nested non-null list enum', () => { | ||
test("multiple nested non-null list enum", () => { | ||
const context = compileMisc(` | ||
@@ -616,8 +623,8 @@ query nesting { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -628,3 +635,3 @@ expect(output).toMatchSnapshot(); | ||
test('multiple nested list enum', () => { | ||
test("multiple nested list enum", () => { | ||
const context = compileMisc(` | ||
@@ -638,8 +645,8 @@ query nesting { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -650,3 +657,3 @@ expect(output).toMatchSnapshot(); | ||
test('duplicates', () => { | ||
test("duplicates", () => { | ||
const context = compileMisc(` | ||
@@ -661,8 +668,8 @@ mutation duplicates($a: EnumCommentTestCase!, $b: EnumCommentTestCase!, $c: Duplicate!) { | ||
const output = generateLocalSource(context).map((f) => ({ | ||
const output = generateLocalSource(context).map(f => ({ | ||
...f, | ||
content: f.content({ | ||
outputPath: '/some/file/ComponentA.tsx', | ||
globalSourcePath: '/__generated__/globalTypes.ts' | ||
}), | ||
outputPath: "/some/file/ComponentA.tsx", | ||
globalSourcePath: "/__generated__/globalTypes.ts" | ||
}) | ||
})); | ||
@@ -669,0 +676,0 @@ expect(output).toMatchSnapshot(); |
@@ -9,8 +9,8 @@ import { | ||
GraphQLList, | ||
GraphQLScalarType, | ||
} from 'graphql'; | ||
GraphQLScalarType | ||
} from "graphql"; | ||
import * as t from '@babel/types'; | ||
import * as t from "@babel/types"; | ||
import { createTypeFromGraphQLTypeFunction } from '../helpers'; | ||
import { createTypeFromGraphQLTypeFunction } from "../helpers"; | ||
@@ -22,55 +22,37 @@ const typeFromGraphQLType = createTypeFromGraphQLTypeFunction({ | ||
function nullableType(type: t.TSType) { | ||
return t.TSUnionType([ | ||
type, | ||
t.TSNullKeyword() | ||
]) | ||
return t.TSUnionType([type, t.TSNullKeyword()]); | ||
} | ||
describe('Typescript typeAnnotationFromGraphQLType', () => { | ||
test('String', () => { | ||
expect(typeFromGraphQLType(GraphQLString)) | ||
.toMatchObject( | ||
nullableType( | ||
t.TSStringKeyword() | ||
) | ||
); | ||
describe("Typescript typeAnnotationFromGraphQLType", () => { | ||
test("String", () => { | ||
expect(typeFromGraphQLType(GraphQLString)).toMatchObject( | ||
nullableType(t.TSStringKeyword()) | ||
); | ||
}); | ||
test('Int', () => { | ||
expect(typeFromGraphQLType(GraphQLInt)) | ||
.toMatchObject( | ||
nullableType( | ||
t.TSNumberKeyword() | ||
) | ||
); | ||
test("Int", () => { | ||
expect(typeFromGraphQLType(GraphQLInt)).toMatchObject( | ||
nullableType(t.TSNumberKeyword()) | ||
); | ||
}); | ||
test('Float', () => { | ||
expect(typeFromGraphQLType(GraphQLFloat)) | ||
.toMatchObject( | ||
nullableType( | ||
t.TSNumberKeyword() | ||
) | ||
); | ||
test("Float", () => { | ||
expect(typeFromGraphQLType(GraphQLFloat)).toMatchObject( | ||
nullableType(t.TSNumberKeyword()) | ||
); | ||
}); | ||
test('Boolean', () => { | ||
expect(typeFromGraphQLType(GraphQLBoolean)) | ||
.toMatchObject( | ||
nullableType( | ||
t.TSBooleanKeyword() | ||
) | ||
); | ||
test("Boolean", () => { | ||
expect(typeFromGraphQLType(GraphQLBoolean)).toMatchObject( | ||
nullableType(t.TSBooleanKeyword()) | ||
); | ||
}); | ||
test('ID', () => { | ||
expect(typeFromGraphQLType(GraphQLID)) | ||
.toMatchObject( | ||
nullableType( | ||
t.TSStringKeyword() | ||
) | ||
); | ||
test("ID", () => { | ||
expect(typeFromGraphQLType(GraphQLID)).toMatchObject( | ||
nullableType(t.TSStringKeyword()) | ||
); | ||
}); | ||
test('String!', () => { | ||
test("String!", () => { | ||
expect( | ||
@@ -81,15 +63,15 @@ typeFromGraphQLType(new GraphQLNonNull(GraphQLString)) | ||
test('Int!', () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(GraphQLInt)) | ||
).toMatchObject(t.TSNumberKeyword()); | ||
test("Int!", () => { | ||
expect(typeFromGraphQLType(new GraphQLNonNull(GraphQLInt))).toMatchObject( | ||
t.TSNumberKeyword() | ||
); | ||
}); | ||
test('Float!', () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(GraphQLFloat)) | ||
).toMatchObject(t.TSNumberKeyword()); | ||
test("Float!", () => { | ||
expect(typeFromGraphQLType(new GraphQLNonNull(GraphQLFloat))).toMatchObject( | ||
t.TSNumberKeyword() | ||
); | ||
}); | ||
test('Boolean!', () => { | ||
test("Boolean!", () => { | ||
expect( | ||
@@ -100,6 +82,6 @@ typeFromGraphQLType(new GraphQLNonNull(GraphQLBoolean)) | ||
test('ID!', () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(GraphQLID)) | ||
).toMatchObject(t.TSStringKeyword()); | ||
test("ID!", () => { | ||
expect(typeFromGraphQLType(new GraphQLNonNull(GraphQLID))).toMatchObject( | ||
t.TSStringKeyword() | ||
); | ||
}); | ||
@@ -109,276 +91,161 @@ | ||
test('[String]', () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(GraphQLString)) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSStringKeyword() | ||
) | ||
) | ||
) | ||
) | ||
test("[String]", () => { | ||
expect(typeFromGraphQLType(new GraphQLList(GraphQLString))).toMatchObject( | ||
nullableType( | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSStringKeyword()))) | ||
) | ||
); | ||
}); | ||
test('[Int]', () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(GraphQLInt)) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSNumberKeyword() | ||
) | ||
) | ||
) | ||
) | ||
test("[Int]", () => { | ||
expect(typeFromGraphQLType(new GraphQLList(GraphQLInt))).toMatchObject( | ||
nullableType( | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSNumberKeyword()))) | ||
) | ||
); | ||
}); | ||
test('[Float]', () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(GraphQLFloat)) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSNumberKeyword() | ||
) | ||
) | ||
) | ||
) | ||
test("[Float]", () => { | ||
expect(typeFromGraphQLType(new GraphQLList(GraphQLFloat))).toMatchObject( | ||
nullableType( | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSNumberKeyword()))) | ||
) | ||
); | ||
}); | ||
test('[Boolean]', () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(GraphQLBoolean)) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSBooleanKeyword() | ||
) | ||
) | ||
) | ||
) | ||
test("[Boolean]", () => { | ||
expect(typeFromGraphQLType(new GraphQLList(GraphQLBoolean))).toMatchObject( | ||
nullableType( | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSBooleanKeyword()))) | ||
) | ||
); | ||
}); | ||
test('[ID]', () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(GraphQLID)) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSStringKeyword() | ||
) | ||
) | ||
) | ||
) | ||
test("[ID]", () => { | ||
expect(typeFromGraphQLType(new GraphQLList(GraphQLID))).toMatchObject( | ||
nullableType( | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSStringKeyword()))) | ||
) | ||
}) | ||
); | ||
}); | ||
test('[String]!', () => { | ||
test("[String]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(GraphQLString))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSStringKeyword() | ||
) | ||
) | ||
) | ||
) | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSStringKeyword()))) | ||
); | ||
}); | ||
test('[Int]!', () => { | ||
test("[Int]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(GraphQLInt))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSNumberKeyword() | ||
) | ||
) | ||
) | ||
) | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSNumberKeyword()))) | ||
); | ||
}); | ||
test('[Float]!', () => { | ||
test("[Float]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(GraphQLFloat))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSNumberKeyword() | ||
) | ||
) | ||
) | ||
) | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSNumberKeyword()))) | ||
); | ||
}); | ||
test('[Boolean]!', () => { | ||
test("[Boolean]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(GraphQLBoolean))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSBooleanKeyword() | ||
) | ||
) | ||
) | ||
) | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSBooleanKeyword()))) | ||
); | ||
}); | ||
test('[ID]!', () => { | ||
test("[ID]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(GraphQLID))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSStringKeyword() | ||
) | ||
) | ||
) | ||
) | ||
t.TSArrayType(t.TSParenthesizedType(nullableType(t.TSStringKeyword()))) | ||
); | ||
}); | ||
test('[String!]', () => { | ||
test("[String!]", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(new GraphQLNonNull(GraphQLString))) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSStringKeyword() | ||
) | ||
) | ||
) | ||
).toMatchObject(nullableType(t.TSArrayType(t.TSStringKeyword()))); | ||
}); | ||
test('[Int!]', () => { | ||
test("[Int!]", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(new GraphQLNonNull((GraphQLInt)))) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSNumberKeyword() | ||
) | ||
) | ||
) | ||
}) | ||
typeFromGraphQLType(new GraphQLList(new GraphQLNonNull(GraphQLInt))) | ||
).toMatchObject(nullableType(t.TSArrayType(t.TSNumberKeyword()))); | ||
}); | ||
test('[Float!]', () => { | ||
test("[Float!]", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(new GraphQLNonNull(GraphQLFloat))) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSNumberKeyword() | ||
) | ||
) | ||
) | ||
).toMatchObject(nullableType(t.TSArrayType(t.TSNumberKeyword()))); | ||
}); | ||
test('[Boolean!]', () => { | ||
test("[Boolean!]", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(new GraphQLNonNull(GraphQLBoolean))) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSBooleanKeyword() | ||
) | ||
) | ||
) | ||
).toMatchObject(nullableType(t.TSArrayType(t.TSBooleanKeyword()))); | ||
}); | ||
test('[ID!]', () => { | ||
test("[ID!]", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(new GraphQLNonNull(GraphQLID))) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSStringKeyword() | ||
) | ||
) | ||
) | ||
).toMatchObject(nullableType(t.TSArrayType(t.TSStringKeyword()))); | ||
}); | ||
test('[String!]!', () => { | ||
test("[String!]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString)))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSStringKeyword() | ||
) | ||
typeFromGraphQLType( | ||
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString))) | ||
) | ||
).toMatchObject(t.TSArrayType(t.TSStringKeyword())); | ||
}); | ||
test('[Int!]!', () => { | ||
test("[Int!]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLInt)))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSNumberKeyword() | ||
) | ||
typeFromGraphQLType( | ||
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLInt))) | ||
) | ||
).toMatchObject(t.TSArrayType(t.TSNumberKeyword())); | ||
}); | ||
test('[Float!]!', () => { | ||
test("[Float!]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLFloat)))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSNumberKeyword() | ||
) | ||
typeFromGraphQLType( | ||
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLFloat))) | ||
) | ||
).toMatchObject(t.TSArrayType(t.TSNumberKeyword())); | ||
}); | ||
test('[Boolean!]!', () => { | ||
test("[Boolean!]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLBoolean)))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSBooleanKeyword() | ||
) | ||
typeFromGraphQLType( | ||
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLBoolean))) | ||
) | ||
).toMatchObject(t.TSArrayType(t.TSBooleanKeyword())); | ||
}); | ||
test('[ID!]!', () => { | ||
test("[ID!]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID)))) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSStringKeyword() | ||
) | ||
typeFromGraphQLType( | ||
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID))) | ||
) | ||
).toMatchObject(t.TSArrayType(t.TSStringKeyword())); | ||
}); | ||
test('[[String]]', () => { | ||
test("[[String]]", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLList(new GraphQLList(GraphQLString))) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSStringKeyword() | ||
) | ||
) | ||
) | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType(nullableType(t.TSStringKeyword())) | ||
) | ||
@@ -389,18 +256,16 @@ ) | ||
) | ||
); | ||
}); | ||
test('[[String]]!', () => { | ||
test("[[String]]!", () => { | ||
expect( | ||
typeFromGraphQLType(new GraphQLNonNull(new GraphQLList(new GraphQLList(GraphQLString)))) | ||
typeFromGraphQLType( | ||
new GraphQLNonNull(new GraphQLList(new GraphQLList(GraphQLString))) | ||
) | ||
).toMatchObject( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSStringKeyword() | ||
) | ||
) | ||
) | ||
t.TSArrayType( | ||
t.TSParenthesizedType( | ||
nullableType( | ||
t.TSArrayType( | ||
t.TSParenthesizedType(nullableType(t.TSStringKeyword())) | ||
) | ||
@@ -410,23 +275,20 @@ ) | ||
) | ||
); | ||
}); | ||
test('Custom Scalar', () => { | ||
test("Custom Scalar", () => { | ||
const OddType = new GraphQLScalarType({ | ||
name: 'Odd', | ||
name: "Odd", | ||
serialize(value) { | ||
return value % 2 === 1 ? value : null | ||
return value % 2 === 1 ? value : null; | ||
} | ||
}); | ||
expect( | ||
typeFromGraphQLType(OddType) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSAnyKeyword() | ||
) | ||
) | ||
expect(typeFromGraphQLType(OddType)).toMatchObject( | ||
nullableType(t.TSAnyKeyword()) | ||
); | ||
}); | ||
}); | ||
describe('passthrough custom scalars', () => { | ||
describe("passthrough custom scalars", () => { | ||
let getTypeAnnotation: Function; | ||
@@ -440,21 +302,17 @@ | ||
test('Custom Scalar', () => { | ||
test("Custom Scalar", () => { | ||
const OddType = new GraphQLScalarType({ | ||
name: 'Odd', | ||
name: "Odd", | ||
serialize(value) { | ||
return value % 2 === 1 ? value : null | ||
return value % 2 === 1 ? value : null; | ||
} | ||
}); | ||
expect( | ||
getTypeAnnotation(OddType) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSTypeReference(t.identifier('Odd')) | ||
) | ||
) | ||
expect(getTypeAnnotation(OddType)).toMatchObject( | ||
nullableType(t.TSTypeReference(t.identifier("Odd"))) | ||
); | ||
}); | ||
}); | ||
describe('passthrough custom scalars with custom scalar prefix', () => { | ||
describe("passthrough custom scalars with custom scalar prefix", () => { | ||
let getTypeAnnotation: Function; | ||
@@ -465,23 +323,18 @@ | ||
passthroughCustomScalars: true, | ||
customScalarsPrefix: "Foo$", | ||
customScalarsPrefix: "Foo$" | ||
}); | ||
}); | ||
test('Custom Scalar', () => { | ||
test("Custom Scalar", () => { | ||
const OddType = new GraphQLScalarType({ | ||
name: 'Odd', | ||
name: "Odd", | ||
serialize(value) { | ||
return value % 2 === 1 ? value : null | ||
return value % 2 === 1 ? value : null; | ||
} | ||
}); | ||
expect( | ||
getTypeAnnotation(OddType) | ||
).toMatchObject( | ||
nullableType( | ||
t.TSTypeReference(t.identifier('Foo$Odd')) | ||
) | ||
) | ||
expect(getTypeAnnotation(OddType)).toMatchObject( | ||
nullableType(t.TSTypeReference(t.identifier("Foo$Odd"))) | ||
); | ||
}); | ||
}); | ||
@@ -10,8 +10,8 @@ import { | ||
GraphQLString, | ||
GraphQLType, | ||
} from 'graphql' | ||
GraphQLType | ||
} from "graphql"; | ||
import * as t from '@babel/types'; | ||
import * as t from "@babel/types"; | ||
import { CompilerOptions } from 'apollo-codegen-core/lib/compiler'; | ||
import { CompilerOptions } from "apollo-codegen-core/lib/compiler"; | ||
@@ -23,4 +23,4 @@ const builtInScalarMap = { | ||
[GraphQLBoolean.name]: t.TSBooleanKeyword(), | ||
[GraphQLID.name]: t.TSStringKeyword(), | ||
} | ||
[GraphQLID.name]: t.TSStringKeyword() | ||
}; | ||
@@ -30,14 +30,23 @@ export function createTypeFromGraphQLTypeFunction( | ||
): (graphQLType: GraphQLType, typeName?: string) => t.TSType { | ||
function nonNullableTypeFromGraphQLType(graphQLType: GraphQLType, typeName?: string): t.TSType { | ||
function nonNullableTypeFromGraphQLType( | ||
graphQLType: GraphQLType, | ||
typeName?: string | ||
): t.TSType { | ||
if (graphQLType instanceof GraphQLList) { | ||
const elementType = typeFromGraphQLType(graphQLType.ofType, typeName); | ||
return t.TSArrayType( | ||
t.isTSUnionType(elementType) ? t.TSParenthesizedType(elementType) : elementType | ||
t.isTSUnionType(elementType) | ||
? t.TSParenthesizedType(elementType) | ||
: elementType | ||
); | ||
} else if (graphQLType instanceof GraphQLScalarType) { | ||
const builtIn = builtInScalarMap[typeName || graphQLType.name] | ||
const builtIn = builtInScalarMap[typeName || graphQLType.name]; | ||
if (builtIn != null) { | ||
return builtIn; | ||
} else if (compilerOptions.passthroughCustomScalars) { | ||
return t.TSTypeReference(t.identifier((compilerOptions.customScalarsPrefix || '') + graphQLType.name)); | ||
return t.TSTypeReference( | ||
t.identifier( | ||
(compilerOptions.customScalarsPrefix || "") + graphQLType.name | ||
) | ||
); | ||
} else { | ||
@@ -54,3 +63,6 @@ return t.TSAnyKeyword(); | ||
function typeFromGraphQLType(graphQLType: GraphQLType, typeName?: string): t.TSType { | ||
function typeFromGraphQLType( | ||
graphQLType: GraphQLType, | ||
typeName?: string | ||
): t.TSType { | ||
if (graphQLType instanceof GraphQLNonNull) { | ||
@@ -57,0 +69,0 @@ return nonNullableTypeFromGraphQLType(graphQLType.ofType, typeName); |
@@ -1,1 +0,5 @@ | ||
export { generateSource, generateLocalSource, generateGlobalSource } from './codeGeneration'; | ||
export { | ||
generateSource, | ||
generateLocalSource, | ||
generateGlobalSource | ||
} from "./codeGeneration"; |
@@ -1,26 +0,18 @@ | ||
import { | ||
GraphQLEnumType, | ||
GraphQLInputObjectType, | ||
GraphQLType | ||
} from 'graphql'; | ||
import { GraphQLEnumType, GraphQLInputObjectType, GraphQLType } from "graphql"; | ||
import { | ||
CompilerOptions | ||
} from 'apollo-codegen-core/lib/compiler'; | ||
import { CompilerOptions } from "apollo-codegen-core/lib/compiler"; | ||
import { commentBlockContent } from 'apollo-codegen-core/lib/utilities/printing'; | ||
import { commentBlockContent } from "apollo-codegen-core/lib/utilities/printing"; | ||
import { | ||
sortEnumValues | ||
} from 'apollo-codegen-core/lib/utilities/graphql'; | ||
import { sortEnumValues } from "apollo-codegen-core/lib/utilities/graphql"; | ||
import { createTypeFromGraphQLTypeFunction, } from './helpers'; | ||
import { createTypeFromGraphQLTypeFunction } from "./helpers"; | ||
import * as t from '@babel/types'; | ||
import * as t from "@babel/types"; | ||
export type ObjectProperty = { | ||
name: string, | ||
description?: string | null | undefined, | ||
type: t.TSType | ||
} | ||
name: string; | ||
description?: string | null | undefined; | ||
type: t.TSType; | ||
}; | ||
@@ -32,4 +24,4 @@ export interface TypescriptCompilerOptions extends CompilerOptions { | ||
export default class TypescriptGenerator { | ||
options: TypescriptCompilerOptions | ||
typeFromGraphQLType: Function | ||
options: TypescriptCompilerOptions; | ||
typeFromGraphQLType: Function; | ||
@@ -39,3 +31,5 @@ constructor(compilerOptions: TypescriptCompilerOptions) { | ||
this.typeFromGraphQLType = createTypeFromGraphQLTypeFunction(compilerOptions); | ||
this.typeFromGraphQLType = createTypeFromGraphQLTypeFunction( | ||
compilerOptions | ||
); | ||
} | ||
@@ -46,13 +40,7 @@ | ||
const enumMembers = sortEnumValues(type.getValues()).map(({ value }) => { | ||
return t.TSEnumMember( | ||
t.identifier(value), | ||
t.stringLiteral(value) | ||
); | ||
return t.TSEnumMember(t.identifier(value), t.stringLiteral(value)); | ||
}); | ||
const typeAlias = t.exportNamedDeclaration( | ||
t.TSEnumDeclaration( | ||
t.identifier(name), | ||
enumMembers | ||
), | ||
t.TSEnumDeclaration(t.identifier(name), enumMembers), | ||
[] | ||
@@ -62,6 +50,8 @@ ); | ||
if (description) { | ||
typeAlias.leadingComments = [{ | ||
type: 'CommentBlock', | ||
value: commentBlockContent(description) | ||
} as t.CommentBlock]; | ||
typeAlias.leadingComments = [ | ||
{ | ||
type: "CommentBlock", | ||
value: commentBlockContent(description) | ||
} as t.CommentBlock | ||
]; | ||
} | ||
@@ -76,20 +66,26 @@ | ||
const fieldMap = inputObjectType.getFields(); | ||
const fields: ObjectProperty[] = Object.keys(inputObjectType.getFields()) | ||
.map((fieldName: string) => { | ||
const field = fieldMap[fieldName]; | ||
return { | ||
name: fieldName, | ||
type: this.typeFromGraphQLType(field.type) | ||
} | ||
}); | ||
const fields: ObjectProperty[] = Object.keys( | ||
inputObjectType.getFields() | ||
).map((fieldName: string) => { | ||
const field = fieldMap[fieldName]; | ||
return { | ||
name: fieldName, | ||
type: this.typeFromGraphQLType(field.type) | ||
}; | ||
}); | ||
const inputType = t.exportNamedDeclaration(this.interface(name, fields, { | ||
keyInheritsNullability: true | ||
}), []); | ||
const inputType = t.exportNamedDeclaration( | ||
this.interface(name, fields, { | ||
keyInheritsNullability: true | ||
}), | ||
[] | ||
); | ||
if (description) { | ||
inputType.leadingComments = [{ | ||
type: 'CommentBlock', | ||
value: commentBlockContent(description) | ||
} as t.CommentBlock] | ||
inputType.leadingComments = [ | ||
{ | ||
type: "CommentBlock", | ||
value: commentBlockContent(description) | ||
} as t.CommentBlock | ||
]; | ||
} | ||
@@ -100,9 +96,11 @@ | ||
public typesForProperties(fields: ObjectProperty[], { | ||
keyInheritsNullability = false | ||
} : { | ||
keyInheritsNullability?: boolean | ||
} = {}) { | ||
return fields.map(({name, description, type}) => { | ||
public typesForProperties( | ||
fields: ObjectProperty[], | ||
{ | ||
keyInheritsNullability = false | ||
}: { | ||
keyInheritsNullability?: boolean; | ||
} = {} | ||
) { | ||
return fields.map(({ name, description, type }) => { | ||
const propertySignatureType = t.TSPropertySignature( | ||
@@ -114,9 +112,12 @@ t.identifier(name), | ||
// TODO: Check if this works | ||
propertySignatureType.optional = keyInheritsNullability && this.isNullableType(type); | ||
propertySignatureType.optional = | ||
keyInheritsNullability && this.isNullableType(type); | ||
if (description) { | ||
propertySignatureType.leadingComments = [{ | ||
type: 'CommentBlock', | ||
value: commentBlockContent(description) | ||
} as t.CommentBlock] | ||
propertySignatureType.leadingComments = [ | ||
{ | ||
type: "CommentBlock", | ||
value: commentBlockContent(description) | ||
} as t.CommentBlock | ||
]; | ||
} | ||
@@ -128,8 +129,11 @@ | ||
public interface(name: string, fields: ObjectProperty[], { | ||
keyInheritsNullability = false | ||
}: { | ||
keyInheritsNullability?: boolean | ||
} = {}) { | ||
public interface( | ||
name: string, | ||
fields: ObjectProperty[], | ||
{ | ||
keyInheritsNullability = false | ||
}: { | ||
keyInheritsNullability?: boolean; | ||
} = {} | ||
) { | ||
return t.TSInterfaceDeclaration( | ||
@@ -151,5 +155,3 @@ t.identifier(name), | ||
undefined, | ||
t.TSUnionType( | ||
members | ||
) | ||
t.TSUnionType(members) | ||
); | ||
@@ -163,14 +165,13 @@ } | ||
public nameFromScopeStack(scope: string[]) { | ||
return scope.join('_'); | ||
return scope.join("_"); | ||
} | ||
public makeNullableType(type: t.TSType) { | ||
return t.TSUnionType([ | ||
type, | ||
t.TSNullKeyword() | ||
]) | ||
return t.TSUnionType([type, t.TSNullKeyword()]); | ||
} | ||
public isNullableType(type: t.TSType) { | ||
return t.isTSUnionType(type) && type.types.some(type => t.isTSNullKeyword(type)); | ||
return ( | ||
t.isTSUnionType(type) && type.types.some(type => t.isTSNullKeyword(type)) | ||
); | ||
} | ||
@@ -180,6 +181,8 @@ | ||
return t.importDeclaration( | ||
types.map((type) => t.importSpecifier( | ||
t.identifier(type.toString()), | ||
t.identifier(type.toString()), | ||
)), | ||
types.map(type => | ||
t.importSpecifier( | ||
t.identifier(type.toString()), | ||
t.identifier(type.toString()) | ||
) | ||
), | ||
t.stringLiteral(source) | ||
@@ -186,0 +189,0 @@ ); |
@@ -1,3 +0,3 @@ | ||
import * as t from '@babel/types'; | ||
import generate from '@babel/generator'; | ||
import * as t from "@babel/types"; | ||
import generate from "@babel/generator"; | ||
@@ -7,17 +7,15 @@ type Printable = t.Node | string; | ||
export default class Printer { | ||
private printQueue: Printable[] = [] | ||
private printQueue: Printable[] = []; | ||
public print(): string { | ||
return this.printQueue | ||
.reduce( | ||
(document: string, printable) => { | ||
if (typeof printable === 'string') { | ||
return document + printable; | ||
} else { | ||
const documentPart = generate(printable).code; | ||
return document + this.indentComments(documentPart); | ||
} | ||
}, | ||
'' | ||
) + '\n'; | ||
return ( | ||
this.printQueue.reduce((document: string, printable) => { | ||
if (typeof printable === "string") { | ||
return document + printable; | ||
} else { | ||
const documentPart = generate(printable).code; | ||
return document + this.indentComments(documentPart); | ||
} | ||
}, "") + "\n" | ||
); | ||
} | ||
@@ -27,4 +25,4 @@ | ||
if (this.printQueue.length > 0) { | ||
this.printQueue.push('\n'); | ||
this.printQueue.push('\n'); | ||
this.printQueue.push("\n"); | ||
this.printQueue.push("\n"); | ||
} | ||
@@ -41,5 +39,3 @@ this.printQueue.push(printable); | ||
private indentComments(documentPart: string) { | ||
const lines = documentPart | ||
.split('\n') | ||
.filter(Boolean); // filter out lines that have no content | ||
const lines = documentPart.split("\n").filter(Boolean); // filter out lines that have no content | ||
@@ -54,3 +50,3 @@ let currentLine = 0; | ||
const currentLineContents = lines[currentLine]; | ||
const commentColumn = currentLineContents.indexOf('//'); | ||
const commentColumn = currentLineContents.indexOf("//"); | ||
if (commentColumn > 0) { | ||
@@ -61,5 +57,5 @@ if (maxCommentColumn < commentColumn) { | ||
const [contents, comment] = currentLineContents.split('//'); | ||
const [contents, comment] = currentLineContents.split("//"); | ||
newDocumentParts.push({ | ||
main: contents.replace(/\s+$/g, ''), | ||
main: contents.replace(/\s+$/g, ""), | ||
comment: comment ? comment.trim() : null | ||
@@ -79,6 +75,3 @@ }); | ||
.reduce((memo: string[], part) => { | ||
const { | ||
main, | ||
comment | ||
} = part; | ||
const { main, comment } = part; | ||
@@ -88,3 +81,3 @@ let line; | ||
const spacesBetween = maxCommentColumn - main.length; | ||
line = `${main}${' '.repeat(spacesBetween)} // ${comment.trim()}` | ||
line = `${main}${" ".repeat(spacesBetween)} // ${comment.trim()}`; | ||
} else { | ||
@@ -94,9 +87,6 @@ line = main; | ||
return [ | ||
...memo, | ||
line | ||
]; | ||
return [...memo, line]; | ||
}, []) | ||
.join('\n'); | ||
.join("\n"); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
32
1
156465
2362
1
Updatedapollo-codegen-core@0.27.3