|
| 1 | +import { buildSchema, parse } from 'graphql'; |
| 2 | +import * as prettier from 'prettier'; |
| 3 | +import { plugin } from '../src/index.js'; |
| 4 | + |
| 5 | +const schema = buildSchema(/* GraphQL */ ` |
| 6 | + directive @semanticNonNull(levels: [Int] = [0]) on FIELD_DEFINITION |
| 7 | +
|
| 8 | + type Query { |
| 9 | + me: User |
| 10 | + } |
| 11 | +
|
| 12 | + type User { |
| 13 | + field: String @semanticNonNull |
| 14 | + fieldLevel0: String @semanticNonNull(levels: [0]) |
| 15 | + fieldLevel1: String @semanticNonNull(levels: [1]) |
| 16 | + fieldBothLevels: String @semanticNonNull(levels: [0, 1]) |
| 17 | + list: [String] @semanticNonNull |
| 18 | + listLevel0: [String] @semanticNonNull(levels: [0]) |
| 19 | + listLevel1: [String] @semanticNonNull(levels: [1]) |
| 20 | + listBothLevels: [String] @semanticNonNull(levels: [0, 1]) |
| 21 | + nonNullableList: [String]! @semanticNonNull |
| 22 | + nonNullableListLevel0: [String]! @semanticNonNull(levels: [0]) |
| 23 | + nonNullableListLevel1: [String]! @semanticNonNull(levels: [1]) |
| 24 | + nonNullableListBothLevels: [String]! @semanticNonNull(levels: [0, 1]) |
| 25 | + listWithNonNullableItem: [String!] @semanticNonNull |
| 26 | + listWithNonNullableItemLevel0: [String!] @semanticNonNull(levels: [0]) |
| 27 | + listWithNonNullableItemLevel1: [String!] @semanticNonNull(levels: [1]) |
| 28 | + listWithNonNullableItemBothLevels: [String!] @semanticNonNull(levels: [0, 1]) |
| 29 | + nonNullableListWithNonNullableItem: [String!]! @semanticNonNull |
| 30 | + nonNullableListWithNonNullableItemLevel0: [String!]! @semanticNonNull(levels: [0]) |
| 31 | + nonNullableListWithNonNullableItemLevel1: [String!]! @semanticNonNull(levels: [1]) |
| 32 | + nonNullableListWithNonNullableItemBothLevels: [String!]! @semanticNonNull(levels: [0, 1]) |
| 33 | + } |
| 34 | +`); |
| 35 | + |
| 36 | +const document = parse(/* GraphQL */ ` |
| 37 | + query { |
| 38 | + me { |
| 39 | + field |
| 40 | + fieldLevel0 |
| 41 | + fieldLevel1 |
| 42 | + fieldBothLevels |
| 43 | + list |
| 44 | + listLevel0 |
| 45 | + listLevel1 |
| 46 | + listBothLevels |
| 47 | + nonNullableList |
| 48 | + nonNullableListLevel0 |
| 49 | + nonNullableListLevel1 |
| 50 | + nonNullableListBothLevels |
| 51 | + listWithNonNullableItem |
| 52 | + listWithNonNullableItemLevel0 |
| 53 | + listWithNonNullableItemLevel1 |
| 54 | + listWithNonNullableItemBothLevels |
| 55 | + nonNullableListWithNonNullableItem |
| 56 | + nonNullableListWithNonNullableItemLevel0 |
| 57 | + nonNullableListWithNonNullableItemLevel1 |
| 58 | + nonNullableListWithNonNullableItemBothLevels |
| 59 | + } |
| 60 | + } |
| 61 | +`); |
| 62 | + |
| 63 | +describe('TypeScript Operations Plugin - nullability', () => { |
| 64 | + it('converts semanticNonNull to nonNull when nullability.errorHandlingClient=true', async () => { |
| 65 | + const result = await plugin(schema, [{ document }], { |
| 66 | + nullability: { |
| 67 | + errorHandlingClient: true, |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + const formattedContent = prettier.format(result.content, { parser: 'typescript' }); |
| 72 | + expect(formattedContent).toMatchInlineSnapshot(` |
| 73 | + "export type Unnamed_1_QueryVariables = Exact<{ [key: string]: never }>; |
| 74 | +
|
| 75 | + export type Unnamed_1_Query = { |
| 76 | + __typename?: "Query"; |
| 77 | + me?: { |
| 78 | + __typename?: "User"; |
| 79 | + field: string; |
| 80 | + fieldLevel0: string; |
| 81 | + fieldLevel1?: string | null; |
| 82 | + fieldBothLevels: string; |
| 83 | + list: Array<string | null>; |
| 84 | + listLevel0: Array<string | null>; |
| 85 | + listLevel1?: Array<string> | null; |
| 86 | + listBothLevels: Array<string>; |
| 87 | + nonNullableList: Array<string | null>; |
| 88 | + nonNullableListLevel0: Array<string | null>; |
| 89 | + nonNullableListLevel1: Array<string>; |
| 90 | + nonNullableListBothLevels: Array<string>; |
| 91 | + listWithNonNullableItem: Array<string>; |
| 92 | + listWithNonNullableItemLevel0: Array<string>; |
| 93 | + listWithNonNullableItemLevel1?: Array<string> | null; |
| 94 | + listWithNonNullableItemBothLevels: Array<string>; |
| 95 | + nonNullableListWithNonNullableItem: Array<string>; |
| 96 | + nonNullableListWithNonNullableItemLevel0: Array<string>; |
| 97 | + nonNullableListWithNonNullableItemLevel1: Array<string>; |
| 98 | + nonNullableListWithNonNullableItemBothLevels: Array<string>; |
| 99 | + } | null; |
| 100 | + }; |
| 101 | + " |
| 102 | + `); |
| 103 | + }); |
| 104 | + |
| 105 | + it('does not convert nullability to nonNull when nullability.errorHandlingClient=false', async () => { |
| 106 | + const result = await plugin(schema, [{ document }], { |
| 107 | + nullability: { |
| 108 | + errorHandlingClient: false, |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + const formattedContent = prettier.format(result.content, { parser: 'typescript' }); |
| 113 | + expect(formattedContent).toMatchInlineSnapshot(` |
| 114 | + "export type Unnamed_1_QueryVariables = Exact<{ [key: string]: never }>; |
| 115 | +
|
| 116 | + export type Unnamed_1_Query = { |
| 117 | + __typename?: "Query"; |
| 118 | + me?: { |
| 119 | + __typename?: "User"; |
| 120 | + field?: string | null; |
| 121 | + fieldLevel0?: string | null; |
| 122 | + fieldLevel1?: string | null; |
| 123 | + fieldBothLevels?: string | null; |
| 124 | + list?: Array<string | null> | null; |
| 125 | + listLevel0?: Array<string | null> | null; |
| 126 | + listLevel1?: Array<string | null> | null; |
| 127 | + listBothLevels?: Array<string | null> | null; |
| 128 | + nonNullableList: Array<string | null>; |
| 129 | + nonNullableListLevel0: Array<string | null>; |
| 130 | + nonNullableListLevel1: Array<string | null>; |
| 131 | + nonNullableListBothLevels: Array<string | null>; |
| 132 | + listWithNonNullableItem?: Array<string> | null; |
| 133 | + listWithNonNullableItemLevel0?: Array<string> | null; |
| 134 | + listWithNonNullableItemLevel1?: Array<string> | null; |
| 135 | + listWithNonNullableItemBothLevels?: Array<string> | null; |
| 136 | + nonNullableListWithNonNullableItem: Array<string>; |
| 137 | + nonNullableListWithNonNullableItemLevel0: Array<string>; |
| 138 | + nonNullableListWithNonNullableItemLevel1: Array<string>; |
| 139 | + nonNullableListWithNonNullableItemBothLevels: Array<string>; |
| 140 | + } | null; |
| 141 | + }; |
| 142 | + " |
| 143 | + `); |
| 144 | + }); |
| 145 | +}); |
0 commit comments