-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
fragment-masking-plugin.ts
169 lines (148 loc) · 6.22 KB
/
fragment-masking-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { PluginFunction } from '@graphql-codegen/plugin-helpers';
const fragmentTypeHelper = `
export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
infer TType,
any
>
? [TType] extends [{ ' $fragmentName'?: infer TKey }]
? TKey extends string
? { ' $fragmentRefs'?: { [key in TKey]: TType } }
: never
: never
: never;`;
const makeFragmentDataHelper = `
export function makeFragmentData<
F extends DocumentTypeDecoration<any, any>,
FT extends ResultOf<F>
>(data: FT, _fragment: F): FragmentType<F> {
return data as FragmentType<F>;
}`;
const defaultUnmaskFunctionName = 'useFragment';
const createUnmaskFunctionTypeDefinitions = (unmaskFunctionName = defaultUnmaskFunctionName) =>
[
`// return non-nullable if \`fragmentType\` is non-nullable
export function ${unmaskFunctionName}<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
): TType;`,
`// return nullable if \`fragmentType\` is nullable
export function ${unmaskFunctionName}<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
): TType | null | undefined;`,
`// return array of non-nullable if \`fragmentType\` is array of non-nullable
export function ${unmaskFunctionName}<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>>
): Array<TType>;`,
`// return array of nullable if \`fragmentType\` is array of nullable
export function ${unmaskFunctionName}<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
): Array<TType> | null | undefined;`,
`// return readonly array of non-nullable if \`fragmentType\` is array of non-nullable
export function ${unmaskFunctionName}<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
): ReadonlyArray<TType>;`,
`// return readonly array of nullable if \`fragmentType\` is array of nullable
export function ${unmaskFunctionName}<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
): ReadonlyArray<TType> | null | undefined;`,
].join('\n');
const createUnmaskFunction = (unmaskFunctionName = defaultUnmaskFunctionName) => `
${createUnmaskFunctionTypeDefinitions(unmaskFunctionName)}
export function ${unmaskFunctionName}<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | Array<FragmentType<DocumentTypeDecoration<TType, any>>> | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
): TType | Array<TType> | ReadonlyArray<TType> | null | undefined {
return fragmentType as any;
}
`;
const isFragmentReadyFunction = (isStringDocumentMode: boolean) => {
if (isStringDocumentMode) {
return `\
export function isFragmentReady<TQuery, TFrag>(
queryNode: TypedDocumentString<TQuery, any>,
fragmentNode: TypedDocumentString<TFrag, any>,
data: FragmentType<TypedDocumentString<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
const fragName = fragmentNode.__meta__?.fragmentName as string | undefined;
if (!deferredFields || !fragName) return true;
const fields = deferredFields[fragName] ?? [];
return fields.length > 0 && fields.every(field => data && field in data);
}
`;
}
return `\
export function isFragmentReady<TQuery, TFrag>(
queryNode: DocumentTypeDecoration<TQuery, any>,
fragmentNode: TypedDocumentNode<TFrag>,
data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
?.deferredFields;
if (!deferredFields) return true;
const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined;
const fragName = fragDef?.name?.value;
const fields = (fragName && deferredFields[fragName]) || [];
return fields.length > 0 && fields.every(field => data && field in data);
}
`;
};
/**
* Plugin for generating fragment masking helper functions.
*/
export const plugin: PluginFunction<{
useTypeImports?: boolean;
augmentedModuleName?: string;
unmaskFunctionName?: string;
emitLegacyCommonJSImports?: boolean;
isStringDocumentMode?: boolean;
}> = (
_,
__,
{ useTypeImports, augmentedModuleName, unmaskFunctionName, emitLegacyCommonJSImports, isStringDocumentMode },
_info
) => {
const documentNodeImport = `${useTypeImports ? 'import type' : 'import'} { ResultOf, DocumentTypeDecoration${
isStringDocumentMode ? '' : ', TypedDocumentNode'
} } from '@graphql-typed-document-node/core';\n`;
const deferFragmentHelperImports = `${useTypeImports ? 'import type' : 'import'} { Incremental${
isStringDocumentMode ? ', TypedDocumentString' : ''
} } from './graphql${emitLegacyCommonJSImports ? '' : '.js'}';\n`;
const fragmentDefinitionNodeImport = isStringDocumentMode
? ''
: `${useTypeImports ? 'import type' : 'import'} { FragmentDefinitionNode } from 'graphql';\n`;
if (augmentedModuleName == null) {
return [
documentNodeImport,
fragmentDefinitionNodeImport,
deferFragmentHelperImports,
`\n`,
fragmentTypeHelper,
`\n`,
createUnmaskFunction(unmaskFunctionName),
`\n`,
makeFragmentDataHelper,
`\n`,
isFragmentReadyFunction(isStringDocumentMode),
].join(``);
}
return [
documentNodeImport,
`declare module "${augmentedModuleName}" {`,
[
...fragmentTypeHelper.split(`\n`),
`\n`,
...createUnmaskFunctionTypeDefinitions(unmaskFunctionName).join('\n').split('\n'),
`\n`,
makeFragmentDataHelper,
]
.map(line => (line === `\n` || line === '' ? line : ` ${line}`))
.join(`\n`),
`}`,
].join(`\n`);
};