Skip to content

Commit 631abbd

Browse files
committed
Add unit test for readOnly
1 parent f89eb27 commit 631abbd

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
});

packages/utils/graphql-codegen-testing/src/operations.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import type { Types } from '@graphql-codegen/plugin-helpers';
44
export const createTestDocument = ({
55
location,
66
document,
7+
type = 'standard',
78
}: {
89
location: string;
910
document: DocumentNode;
11+
type?: 'standard' | 'read-only';
1012
}): Types.DocumentFile => {
1113
return {
1214
location,
1315
document,
1416
hash: 'mocked',
15-
type: 'standard',
17+
type,
1618
};
1719
};

0 commit comments

Comments
 (0)