|
| 1 | +import { buildSchema, parse } from 'graphql'; |
| 2 | +import { mergeOutputs } from '@graphql-codegen/plugin-helpers'; |
| 3 | +import { createTestDocument } from '@graphql-codegen/testing'; |
| 4 | +import { plugin } from '../src/index.js'; |
| 5 | + |
| 6 | +describe('TypeScript Operations Plugin - documentsReadOnly', () => { |
| 7 | + it('uses read-only document file as reference, without generating types for it', async () => { |
| 8 | + const schema = buildSchema(/* GraphQL */ ` |
| 9 | + type Query { |
| 10 | + user(id: ID!): User |
| 11 | + } |
| 12 | +
|
| 13 | + type User { |
| 14 | + id: ID! |
| 15 | + name: String! |
| 16 | + role: UserRole! |
| 17 | + } |
| 18 | +
|
| 19 | + enum UserRole { |
| 20 | + ADMIN |
| 21 | + CUSTOMER |
| 22 | + } |
| 23 | + `); |
| 24 | + const query = parse(/* GraphQL */ ` |
| 25 | + query User { |
| 26 | + user(id: "100") { |
| 27 | + id |
| 28 | + ...UserFragment |
| 29 | + } |
| 30 | + } |
| 31 | + `); |
| 32 | + |
| 33 | + const fragment = parse(/* GraphQL */ ` |
| 34 | + fragment UserFragment on User { |
| 35 | + id |
| 36 | + name |
| 37 | + } |
| 38 | + `); |
| 39 | + |
| 40 | + const result = mergeOutputs([ |
| 41 | + await plugin( |
| 42 | + schema, |
| 43 | + [ |
| 44 | + createTestDocument({ location: '', document: query, type: 'standard' }), |
| 45 | + createTestDocument({ location: '', document: fragment, type: 'standard' }), |
| 46 | + ], |
| 47 | + {}, |
| 48 | + { outputFile: '' }, |
| 49 | + ), |
| 50 | + ]); |
| 51 | + |
| 52 | + expect(result).toMatchInlineSnapshot(` |
| 53 | + "export type UserQueryVariables = Exact<{ [key: string]: never; }>; |
| 54 | +
|
| 55 | +
|
| 56 | + export type UserQuery = { __typename?: 'Query', user?: { __typename?: 'User', id: string, name: string } | null }; |
| 57 | + " |
| 58 | + `); |
| 59 | + |
| 60 | + // FIXME: cannot call `validateTs` until next major version |
| 61 | + // https://github.com/dotansimha/graphql-code-generator/pull/10496/changes |
| 62 | + // validateTs(result, undefined, undefined, undefined, undefined, true); |
| 63 | + }); |
| 64 | +}); |
0 commit comments