parent
00fb8a6541
commit
f8d164aee7
|
@ -18,7 +18,7 @@ export const SCALAR_TYPES = {
|
|||
export interface TypegenConfigSourceModule {
|
||||
/**
|
||||
* The module for where to look for the types.
|
||||
* This uses the node resolution algorthm via require.resolve,
|
||||
* This uses the node resolution algorithm via require.resolve,
|
||||
* so if this lives in node_modules, you can just provide the module name
|
||||
* otherwise you should provide the absolute path to the file.
|
||||
*/
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import fs from 'fs'
|
||||
import {
|
||||
GraphQLArgument,
|
||||
GraphQLEnumType,
|
||||
|
@ -159,13 +160,26 @@ export class TypegenPrinter {
|
|||
}
|
||||
}
|
||||
|
||||
eachObj(rootTypings, (val, key) => {
|
||||
if (typeof val !== 'string') {
|
||||
const importPath = (path.isAbsolute(val.path) ? relativePathTo(val.path, outputPath) : val.path)
|
||||
eachObj(rootTypings, (rootType, typeName) => {
|
||||
if (typeof rootType !== 'string') {
|
||||
const rootTypePath = rootType.path
|
||||
|
||||
if (typeof rootTypePath !== 'string' || !path.isAbsolute(rootTypePath)) {
|
||||
throw new Error(
|
||||
`Expected an absolute path for the root typing path of the type ${typeName}, saw ${rootTypePath}`
|
||||
)
|
||||
}
|
||||
|
||||
if (!fs.existsSync(rootTypePath)) {
|
||||
throw new Error(`Root typing path ${rootTypePath} of the type ${typeName} does not exist`)
|
||||
}
|
||||
|
||||
const importPath = relativePathTo(rootType.path, outputPath)
|
||||
.replace(/(\.d)?\.ts/, '')
|
||||
.replace(/\\+/g, '/')
|
||||
|
||||
importMap[importPath] = importMap[importPath] || new Set()
|
||||
importMap[importPath].add(val.alias ? `${val.name} as ${val.alias}` : val.name)
|
||||
importMap[importPath].add(rootType.alias ? `${rootType.name} as ${rootType.alias}` : rootType.name)
|
||||
}
|
||||
})
|
||||
eachObj(importMap, (val, key) => {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import * as path from 'path'
|
||||
import { core, enumType, makeSchema, queryType } from '..'
|
||||
import { core, enumType, makeSchema, objectType, queryType } from '..'
|
||||
import { A, B } from './_types'
|
||||
|
||||
const { TypegenPrinter, TypegenMetadata } = core
|
||||
|
@ -112,4 +112,70 @@ describe('rootTypings', () => {
|
|||
})
|
||||
expect(typegen.print()).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('throws error if root typing path is not an absolute path', async () => {
|
||||
const metadata = new TypegenMetadata({
|
||||
outputs: { typegen: false, schema: false },
|
||||
})
|
||||
const someType = objectType({
|
||||
name: 'SomeType',
|
||||
rootTyping: {
|
||||
name: 'invalid',
|
||||
path: 'fzeffezpokm',
|
||||
},
|
||||
definition(t) {
|
||||
t.id('id')
|
||||
},
|
||||
})
|
||||
|
||||
const schema = makeSchema({
|
||||
types: [someType],
|
||||
outputs: false,
|
||||
})
|
||||
|
||||
const typegenInfo = await metadata.getTypegenInfo(schema)
|
||||
const typegen = new TypegenPrinter(schema, {
|
||||
...typegenInfo,
|
||||
typegenFile: '',
|
||||
})
|
||||
|
||||
expect(() => typegen.print()).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Expected an absolute path for the root typing path of the type SomeType, saw fzeffezpokm"`
|
||||
)
|
||||
})
|
||||
|
||||
it('throws error if root typing path does not exist', async () => {
|
||||
const metadata = new TypegenMetadata({
|
||||
outputs: { typegen: false, schema: false },
|
||||
})
|
||||
const someType = objectType({
|
||||
name: 'SomeType',
|
||||
rootTyping: {
|
||||
name: 'invalid',
|
||||
path: __dirname + '/invalid_path.ts',
|
||||
},
|
||||
definition(t) {
|
||||
t.id('id')
|
||||
},
|
||||
})
|
||||
|
||||
const schema = makeSchema({
|
||||
types: [someType],
|
||||
outputs: false,
|
||||
})
|
||||
|
||||
const typegenInfo = await metadata.getTypegenInfo(schema)
|
||||
const typegen = new TypegenPrinter(schema, {
|
||||
...typegenInfo,
|
||||
typegenFile: '',
|
||||
})
|
||||
|
||||
try {
|
||||
typegen.print()
|
||||
} catch (e) {
|
||||
expect(e.message.replace(__dirname, '')).toMatchInlineSnapshot(
|
||||
`"Root typing path /invalid_path.ts of the type SomeType does not exist"`
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue