Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow use of enums with any values
fix: nullable/wrapper issues
- Loading branch information
1 parent
1fda153
commit f8cd7e4
Showing
15 changed files
with
401 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import 'jest'; | ||
import ObjectType from '../decorators/ObjectType'; | ||
import Field from '../decorators/Field'; | ||
import InputObjectType from '../decorators/InputObjectType'; | ||
import InputField from '../decorators/InputField'; | ||
import nullable from '../wrappers/nullable'; | ||
import { getInputType, getNamedType, getOutputType, TSGraphQLString } from '..'; | ||
import InterfaceType from '../decorators/InterfaceType'; | ||
import { | ||
isEnumType, | ||
isInputObjectType, isInterfaceType, | ||
isListType, | ||
isNonNullType, | ||
isObjectType, | ||
isScalarType, | ||
isUnionType, | ||
} from 'graphql'; | ||
import { getType } from '../typeHelpers'; | ||
import list from '../wrappers/list'; | ||
import TSGraphQLEnumType from '../wrappers/TSGraphQLEnumType'; | ||
import TSGraphQLUnionType from '../wrappers/TSGraphQLUnionType'; | ||
|
||
@ObjectType() | ||
class AnObjectType { | ||
@Field() | ||
foo!: string; | ||
} | ||
|
||
@ObjectType() | ||
class AnotherObjectType { | ||
@Field() | ||
bar!: string; | ||
} | ||
|
||
@InputObjectType() | ||
class AnInputObjectType { | ||
@InputField() | ||
foo!: string; | ||
} | ||
|
||
@InterfaceType() | ||
class AnInterfaceType { | ||
@Field() | ||
foo!: string; | ||
} | ||
|
||
enum AnEnum { | ||
Foo, | ||
} | ||
|
||
const AnEnumType = new TSGraphQLEnumType(AnEnum, { name: 'AnEnumType' }) | ||
|
||
const AUnionType = new TSGraphQLUnionType<AnObjectType | AnotherObjectType>({ | ||
name: 'AUnionType', | ||
types: [AnObjectType, AnotherObjectType] | ||
}) | ||
|
||
const ANullableType = nullable(TSGraphQLString); | ||
const AListType = list(TSGraphQLString); | ||
|
||
describe('typeHelpers', () => { | ||
describe('getType', () => { | ||
it('should never return GraphQLNonNull if nonNull is false', () => { | ||
expect(isNonNullType(getType(AnObjectType))).toBeFalsy(); | ||
expect(isNonNullType(getType(AnInputObjectType))).toBeFalsy(); | ||
expect(isNonNullType(getType(AnInterfaceType))).toBeFalsy(); | ||
expect(isNonNullType(getType(ANullableType))).toBeFalsy(); | ||
expect(isNonNullType(getType(TSGraphQLString))).toBeFalsy(); | ||
}); | ||
|
||
it('should return GraphQLNonNull if nonNull true and type not nullable', () => { | ||
expect(isNonNullType(getType(ANullableType, true))).toBeFalsy(); | ||
|
||
expect(isNonNullType(getType(AnObjectType, true))).toBeTruthy(); | ||
expect(isNonNullType(getType(AnInputObjectType, true))).toBeTruthy(); | ||
expect(isNonNullType(getType(AnInterfaceType, true))).toBeTruthy(); | ||
expect(isNonNullType(getType(TSGraphQLString, true))).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('getInputType', () => { | ||
it('should correctly return input types', () => { | ||
expect(isEnumType(getInputType(AnEnumType))).toBeTruthy(); | ||
expect(isScalarType(getInputType(TSGraphQLString))).toBeTruthy(); | ||
expect(isInputObjectType(getInputType(AnInputObjectType))).toBeTruthy(); | ||
expect(isListType(getInputType(AListType))).toBeTruthy(); | ||
expect(isNonNullType(getInputType(TSGraphQLString, true))).toBeTruthy(); | ||
}); | ||
|
||
it('should throw if not an input type', () => { | ||
expect(() => getInputType(AnObjectType)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('getOutputType', () => { | ||
it('should correctly return output types', () => { | ||
expect(isEnumType(getOutputType(AnEnumType))).toBeTruthy(); | ||
expect(isScalarType(getOutputType(TSGraphQLString))).toBeTruthy(); | ||
expect(isObjectType(getOutputType(AnObjectType))).toBeTruthy(); | ||
expect(isListType(getOutputType(AListType))).toBeTruthy(); | ||
expect(isInterfaceType(getOutputType(AnInterfaceType))).toBeTruthy(); | ||
expect(isNonNullType(getOutputType(TSGraphQLString, true))).toBeTruthy(); | ||
}); | ||
|
||
it('should throw if not an output type', () => { | ||
expect(() => getOutputType(AnInputObjectType)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('getNamedType', () => { | ||
it('should correctly return named types', () => { | ||
expect(isEnumType(getNamedType(AnEnumType))).toBeTruthy(); | ||
expect(isScalarType(getNamedType(TSGraphQLString))).toBeTruthy(); | ||
expect(isObjectType(getNamedType(AnObjectType))).toBeTruthy(); | ||
expect(isInputObjectType(getNamedType(AnInputObjectType))).toBeTruthy(); | ||
expect(isUnionType(getNamedType(AUnionType))).toBeTruthy(); | ||
expect(isInterfaceType(getNamedType(AnInterfaceType))).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,14 @@ | ||
import 'jest'; | ||
import { exec } from 'child_process'; | ||
import { readdir } from 'fs'; | ||
import pify from 'pify'; | ||
import path from 'path'; | ||
|
||
// Would be better to do this programmatically, didn't look straightforward though. | ||
const typeCheckFiles = async (files: string[], expectError?: boolean) => { | ||
await Promise.all(files.map(file => new Promise((resolve, reject) => { | ||
const tscArgs = '--strict --experimentalDecorators --emitDecoratorMetadata --noEmit --lib esnext'; | ||
exec(`$(npm bin)/tsc ${tscArgs} ${file}`, (err) => { | ||
if ((err && expectError) || (!err && !expectError)) { | ||
resolve(); | ||
} else { | ||
reject(new Error(expectError | ||
? 'Types valid, expected invalid' | ||
: 'Types invalid, expected valid', | ||
)); | ||
} | ||
}); | ||
}))); | ||
}; | ||
|
||
const getFiles = async (directory: string): Promise<string[]> => { | ||
const files = await pify(readdir)(path.join(__dirname, directory)); | ||
return files.map((file: string) => path.join(__dirname, directory, file)); | ||
} | ||
import { getFilesInDir, typeCheckFiles } from '../../../typeChecking'; | ||
|
||
describe('Decorator type validation', () => { | ||
it('passes for valid files', async () => { | ||
const validFiles = await getFiles('valid'); | ||
const validFiles = await getFilesInDir(__dirname, 'valid'); | ||
await typeCheckFiles(validFiles); | ||
}, 30000); | ||
|
||
it('fails for invalid files', async () => { | ||
const invalidFiles = await getFiles('invalid'); | ||
const invalidFiles = await getFilesInDir(__dirname, 'invalid'); | ||
await typeCheckFiles(invalidFiles, true); | ||
}, 30000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.