Skip to content

Commit 8f4ba67

Browse files
ikusakoveddeee888
authored andcommitted
Implement documentsReadOnly for typescript-operations
- Flow documentsReadOnly to typescript-operations - Set up documents-readonly dev-test - Update dev-tests output - Merge standard and read-only documents - Fix Source vs Types.DocumentFile usage for type correctness
1 parent 3005df7 commit 8f4ba67

File tree

15 files changed

+242
-111
lines changed

15 files changed

+242
-111
lines changed

dev-test/codegen.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,19 @@ const config: CodegenConfig = {
265265
},
266266
},
267267
},
268+
// #region documentsReadOnly
269+
'./dev-test/documents-readonly/app/types.generated.ts': {
270+
schema: './dev-test/documents-readonly/schema.graphqls',
271+
documents: ['./dev-test/documents-readonly/app/*.graphql.ts'],
272+
documentsReadOnly: ['./dev-test/documents-readonly/lib/*.graphql.ts'],
273+
plugins: ['typescript-operations'],
274+
},
275+
'./dev-test/documents-readonly/lib/types.generated.ts': {
276+
schema: './dev-test/documents-readonly/schema.graphqls',
277+
documents: ['./dev-test/documents-readonly/lib/*.graphql.ts'],
278+
plugins: ['typescript-operations'],
279+
},
280+
// #endregion
268281
},
269282
};
270283

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* GraphQL */ `
2+
query User($id: ID!) {
3+
user(id: $id) {
4+
id
5+
...UserFragment
6+
}
7+
}
8+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type UserQueryVariables = Exact<{
2+
id: Scalars['ID']['input'];
3+
}>;
4+
5+
export type UserQuery = {
6+
__typename?: 'Query';
7+
user?: { __typename?: 'User'; id: string; name: string; role: UserRole } | null;
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* GraphQL */ `
2+
fragment UserFragment on User {
3+
id
4+
name
5+
role
6+
}
7+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type UserFragmentFragment = { __typename?: 'User'; id: string; name: string; role: UserRole };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type Query {
2+
user(id: ID!): User
3+
}
4+
5+
type User {
6+
id: ID!
7+
name: String!
8+
role: UserRole!
9+
}
10+
11+
enum UserRole {
12+
ADMIN
13+
CUSTOMER
14+
}

packages/graphql-codegen-cli/src/codegen.ts

Lines changed: 115 additions & 73 deletions
Large diffs are not rendered by default.

packages/graphql-codegen-cli/src/config.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createRequire } from 'module';
44
import { resolve } from 'path';
55
import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
66
import { GraphQLSchema, GraphQLSchemaExtensions, print } from 'graphql';
7-
import { GraphQLConfig } from 'graphql-config';
7+
import { GraphQLConfig, type Source } from 'graphql-config';
88
import { createJiti } from 'jiti';
99
import { env } from 'string-env-interpolation';
1010
import yaml from 'yaml';
@@ -16,6 +16,7 @@ import {
1616
Profiler,
1717
Types,
1818
} from '@graphql-codegen/plugin-helpers';
19+
import type { UnnormalizedTypeDefPointer } from '@graphql-tools/load';
1920
import { findAndLoadGraphQLConfig } from './graphql-config.js';
2021
import {
2122
defaultDocumentsLoadOptions,
@@ -464,18 +465,22 @@ export class CodegenContext {
464465
return addHashToSchema(loadSchema(pointer, config));
465466
}
466467

467-
async loadDocuments(pointer: Types.OperationDocument[]): Promise<Types.DocumentFile[]> {
468+
async loadDocuments(
469+
pointer: UnnormalizedTypeDefPointer,
470+
type: 'standard' | 'read-only',
471+
): Promise<Types.DocumentFile[]> {
468472
const config = this.getConfig(defaultDocumentsLoadOptions);
469473
if (this._graphqlConfig) {
470474
// TODO: pointer won't work here
471475
return addHashToDocumentFiles(
472476
this._graphqlConfig
473477
.getProject(this._project)
474478
.loadDocuments(pointer, { ...config, ...config.config }),
479+
type,
475480
);
476481
}
477482

478-
return addHashToDocumentFiles(loadDocuments(pointer, config));
483+
return addHashToDocumentFiles(loadDocuments(pointer, config), type);
479484
}
480485
}
481486

@@ -502,24 +507,27 @@ function addHashToSchema(schemaPromise: Promise<GraphQLSchema>): Promise<GraphQL
502507
});
503508
}
504509

505-
function hashDocument(doc: Types.DocumentFile) {
506-
if (doc.rawSDL) {
507-
return hashContent(doc.rawSDL);
508-
}
510+
async function addHashToDocumentFiles(
511+
documentFilesPromise: Promise<Source[]>,
512+
type: 'standard' | 'read-only',
513+
): Promise<Types.DocumentFile[]> {
514+
function hashDocument(doc: Source) {
515+
if (doc.rawSDL) {
516+
return hashContent(doc.rawSDL);
517+
}
509518

510-
if (doc.document) {
511-
return hashContent(print(doc.document));
512-
}
519+
if (doc.document) {
520+
return hashContent(print(doc.document));
521+
}
513522

514-
return null;
515-
}
523+
return null;
524+
}
516525

517-
function addHashToDocumentFiles(
518-
documentFilesPromise: Promise<Types.DocumentFile[]>,
519-
): Promise<Types.DocumentFile[]> {
520526
return documentFilesPromise.then(documentFiles =>
521-
documentFiles.map(doc => {
527+
// Note: `doc` here is technically `Source`, but by the end of the funciton it's `Types.DocumentFile`. This re-declaration makes TypeScript happy.
528+
documentFiles.map((doc: Types.DocumentFile): Types.DocumentFile => {
522529
doc.hash = hashDocument(doc);
530+
doc.type = type;
523531

524532
return doc;
525533
}),

packages/graphql-codegen-cli/src/load.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { extname, join } from 'path';
22
import { GraphQLError, GraphQLSchema } from 'graphql';
3+
import type { Source } from 'graphql-config';
34
import { Types } from '@graphql-codegen/plugin-helpers';
45
import { ApolloEngineLoader } from '@graphql-tools/apollo-engine-loader';
56
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
@@ -69,7 +70,7 @@ export async function loadSchema(
6970
export async function loadDocuments(
7071
documentPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
7172
config: Types.Config,
72-
): Promise<Types.DocumentFile[]> {
73+
): Promise<Source[]> {
7374
const loaders = [
7475
new CodeFileLoader({
7576
pluckConfig: {

packages/graphql-codegen-cli/tests/codegen.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,10 +1553,10 @@ describe('Codegen Executor', () => {
15531553
expect(capturedDocumentsReadOnly).toHaveLength(1);
15541554

15551555
const documentNames = capturedDocuments.flatMap(
1556-
d => d.document?.definitions.map((def: any) => def.name?.value) ?? []
1556+
d => d.document?.definitions.map((def: any) => def.name?.value) ?? [],
15571557
);
15581558
const readOnlyNames = capturedDocumentsReadOnly.flatMap(
1559-
d => d.document?.definitions.map((def: any) => def.name?.value) ?? []
1559+
d => d.document?.definitions.map((def: any) => def.name?.value) ?? [],
15601560
);
15611561

15621562
expect(documentNames).toContain('root');

0 commit comments

Comments
 (0)