|
| 1 | +import { buildSchema } from 'graphql'; |
| 2 | +import '@graphql-codegen/testing'; |
| 3 | +import { plugin } from '../src/index.js'; |
| 4 | + |
| 5 | +describe('customDirectives.sematicNonNull', () => { |
| 6 | + it('allowSemanticNonNull - should build strict type if annotated by @semanticNonNull directive', async () => { |
| 7 | + const testingSchema = buildSchema(/* GraphQL */ ` |
| 8 | + directive @semanticNonNull(levels: [Int] = [0]) on FIELD_DEFINITION |
| 9 | +
|
| 10 | + type TestingType { |
| 11 | + field: String @semanticNonNull |
| 12 | + fieldLevel0: String @semanticNonNull(levels: [0]) |
| 13 | + fieldLevel1: String @semanticNonNull(levels: [1]) |
| 14 | + fieldBothLevels: String @semanticNonNull(levels: [0, 1]) |
| 15 | + list: [String] @semanticNonNull |
| 16 | + listLevel0: [String] @semanticNonNull(levels: [0]) |
| 17 | + listLevel1: [String] @semanticNonNull(levels: [1]) |
| 18 | + listBothLevels: [String] @semanticNonNull(levels: [0, 1]) |
| 19 | + nonNullableList: [String]! @semanticNonNull |
| 20 | + nonNullableListLevel0: [String]! @semanticNonNull(levels: [0]) |
| 21 | + nonNullableListLevel1: [String]! @semanticNonNull(levels: [1]) |
| 22 | + nonNullableListBothLevels: [String]! @semanticNonNull(levels: [0, 1]) |
| 23 | + listWithNonNullableItem: [String!] @semanticNonNull |
| 24 | + listWithNonNullableItemLevel0: [String!] @semanticNonNull(levels: [0]) |
| 25 | + listWithNonNullableItemLevel1: [String!] @semanticNonNull(levels: [1]) |
| 26 | + listWithNonNullableItemBothLevels: [String!] @semanticNonNull(levels: [0, 1]) |
| 27 | + nonNullableListWithNonNullableItem: [String!]! @semanticNonNull |
| 28 | + nonNullableListWithNonNullableItemLevel0: [String!]! @semanticNonNull(levels: [0]) |
| 29 | + nonNullableListWithNonNullableItemLevel1: [String!]! @semanticNonNull(levels: [1]) |
| 30 | + nonNullableListWithNonNullableItemBothLevels: [String!]! @semanticNonNull(levels: [0, 1]) |
| 31 | + } |
| 32 | + `); |
| 33 | + |
| 34 | + const result = await plugin( |
| 35 | + testingSchema, |
| 36 | + [], |
| 37 | + { |
| 38 | + customDirectives: { semanticNonNull: true }, |
| 39 | + }, |
| 40 | + { outputFile: '' } |
| 41 | + ); |
| 42 | + |
| 43 | + expect(result.content).toBeSimilarStringTo(` |
| 44 | + export type TestingTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['TestingType'] = ResolversParentTypes['TestingType']> = { |
| 45 | + field?: Resolver<ResolversTypes['String'], ParentType, ContextType>; |
| 46 | + fieldLevel0?: Resolver<ResolversTypes['String'], ParentType, ContextType>; |
| 47 | + fieldLevel1?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>; |
| 48 | + fieldBothLevels?: Resolver<ResolversTypes['String'], ParentType, ContextType>; |
| 49 | + list?: Resolver<Array<Maybe<ResolversTypes['String']>>, ParentType, ContextType>; |
| 50 | + listLevel0?: Resolver<Array<Maybe<ResolversTypes['String']>>, ParentType, ContextType>; |
| 51 | + listLevel1?: Resolver<Maybe<Array<ResolversTypes['String']>>, ParentType, ContextType>; |
| 52 | + listBothLevels?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 53 | + nonNullableList?: Resolver<Array<Maybe<ResolversTypes['String']>>, ParentType, ContextType>; |
| 54 | + nonNullableListLevel0?: Resolver<Array<Maybe<ResolversTypes['String']>>, ParentType, ContextType>; |
| 55 | + nonNullableListLevel1?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 56 | + nonNullableListBothLevels?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 57 | + listWithNonNullableItem?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 58 | + listWithNonNullableItemLevel0?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 59 | + listWithNonNullableItemLevel1?: Resolver<Maybe<Array<ResolversTypes['String']>>, ParentType, ContextType>; |
| 60 | + listWithNonNullableItemBothLevels?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 61 | + nonNullableListWithNonNullableItem?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 62 | + nonNullableListWithNonNullableItemLevel0?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 63 | + nonNullableListWithNonNullableItemLevel1?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 64 | + nonNullableListWithNonNullableItemBothLevels?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>; |
| 65 | + __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>; |
| 66 | + }; |
| 67 | + `); |
| 68 | + }); |
| 69 | +}); |
0 commit comments