Skip to content

Commit a89a0dc

Browse files
authored
refactor(typegen): turn off fragment masking and fix typegen config (#560)
refactor(typegen): turn off fragment masking - because it complicates things too much and gives little gain for devs fix(lib): lint fix(codegen): document syntax - ? is preventing the codegen from matching anything
2 parents 83b22e2 + 9982f17 commit a89a0dc

File tree

6 files changed

+392
-214
lines changed

6 files changed

+392
-214
lines changed

codegen.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import { CodegenConfig } from '@graphql-codegen/cli';
33
const config: CodegenConfig = {
44
// schema: 'http://localhost:5000/graphql',
55
schema: 'https://dev-api.cofacts.tw/graphql',
6-
documents: ['pages/**/*.tsx?', 'components/**/*.tsx?', 'lib/**/*.tsx'],
6+
documents: [
7+
'pages/**/*.{ts,tsx}',
8+
'components/**/*.{ts,tsx}',
9+
'lib/**/*.{ts,tsx}',
10+
],
711
generates: {
812
'./typegen/': {
913
preset: 'client',
1014
plugins: [],
1115
presetConfig: {
12-
// Rename useFragment to avoid conflicting with ESLint React hook rule
13-
// https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#embrace-fragment-masking-principles
14-
fragmentMasking: { unmaskFunctionName: 'getFragmentData' },
16+
fragmentMasking: false,
1517
},
1618
},
1719
},

lib/__tests__/text.tsx

+25-41
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import React from 'react';
2-
import { makeFragmentData } from '../../typegen';
3-
import {
4-
linkify,
5-
nl2br,
6-
truncate,
7-
highlightSections,
8-
HighlightFields,
9-
} from '../text';
2+
import { linkify, nl2br, truncate, highlightSections } from '../text';
103

114
describe('text', () => {
125
describe('linkify', () => {
@@ -185,28 +178,22 @@ describe('highlightSections', () => {
185178
it('return empty arrays for empty highlights', () => {
186179
expect(
187180
highlightSections(
188-
makeFragmentData(
189-
{
190-
text: null,
191-
reference: null,
192-
hyperlinks: null,
193-
},
194-
HighlightFields
195-
),
181+
{
182+
text: null,
183+
reference: null,
184+
hyperlinks: null,
185+
},
196186
classes
197187
)
198188
).toMatchInlineSnapshot(`Array []`);
199189

200190
expect(
201191
highlightSections(
202-
makeFragmentData(
203-
{
204-
text: null,
205-
reference: null,
206-
hyperlinks: [],
207-
},
208-
HighlightFields
209-
),
192+
{
193+
text: null,
194+
reference: null,
195+
hyperlinks: [],
196+
},
210197
classes
211198
)
212199
).toMatchInlineSnapshot(`Array []`);
@@ -215,23 +202,20 @@ describe('highlightSections', () => {
215202
it('wraps <mark> to all highlightable fields', () => {
216203
expect(
217204
highlightSections(
218-
makeFragmentData(
219-
{
220-
text:
221-
'<HIGHLIGHT>Lorem ipsum</HIGHLIGHT> dolor sit <HIGHLIGHT>amet</HIGHLIGHT>, consectetur <HIGHLIGHT>adipiscing elit</HIGHLIGHT>',
222-
reference:
223-
'【誤導】<HIGHLIGHT>大陸東北男子穿短袖短褲晨跑</HIGHLIGHT>?<HIGHLIGHT>凍成人型冰棍影片</HIGHLIGHT>?2018酒醉凍傷致死事件\n\nMyGoPen查證參考:\nhttps://www.mygopen.com/2021/01/freeze-Morning-jogging.html',
224-
hyperlinks: [
225-
{
226-
title:
227-
'<HIGHLIGHT>MyGoPen</HIGHLIGHT>|這是假消息: 【假LINE】大同寶寶貼圖詐騙新手法!連到外部網站再騙你加',
228-
summary:
229-
'今年為一九<HIGHLIGHT>六四</HIGHLIGHT>年以來首度無颱風侵台',
230-
},
231-
],
232-
},
233-
HighlightFields
234-
),
205+
{
206+
text:
207+
'<HIGHLIGHT>Lorem ipsum</HIGHLIGHT> dolor sit <HIGHLIGHT>amet</HIGHLIGHT>, consectetur <HIGHLIGHT>adipiscing elit</HIGHLIGHT>',
208+
reference:
209+
'【誤導】<HIGHLIGHT>大陸東北男子穿短袖短褲晨跑</HIGHLIGHT>?<HIGHLIGHT>凍成人型冰棍影片</HIGHLIGHT>?2018酒醉凍傷致死事件\n\nMyGoPen查證參考:\nhttps://www.mygopen.com/2021/01/freeze-Morning-jogging.html',
210+
hyperlinks: [
211+
{
212+
title:
213+
'<HIGHLIGHT>MyGoPen</HIGHLIGHT>|這是假消息: 【假LINE】大同寶寶貼圖詐騙新手法!連到外部網站再騙你加',
214+
summary:
215+
'今年為一九<HIGHLIGHT>六四</HIGHLIGHT>年以來首度無颱風侵台',
216+
},
217+
],
218+
},
235219
classes
236220
)
237221
).toMatchInlineSnapshot(`

lib/text.tsx

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import React, { CSSProperties } from 'react';
22
import { gql } from 'graphql-tag';
3-
import { print } from 'graphql';
43
import { withStyles, WithStyles, createStyles } from '@material-ui/core/styles';
5-
import { FragmentType, getFragmentData } from '../typegen/fragment-masking';
6-
import { graphql } from '../typegen';
4+
import { HighlightFieldsFragment } from 'typegen/graphql';
75

86
const BREAK = { $$BREAK: true } as const;
97

@@ -260,7 +258,7 @@ function getMarkElems(
260258
.filter(jsxElem => !!jsxElem);
261259
}
262260

263-
const TypedHighlightFields = graphql(/* GraphQL */ `
261+
export const HighlightFields = gql`
264262
fragment HighlightFields on Highlights {
265263
text
266264
reference
@@ -269,31 +267,20 @@ const TypedHighlightFields = graphql(/* GraphQL */ `
269267
summary
270268
}
271269
}
272-
`);
273-
274-
/**
275-
* @deprecated - Remove export after all dependent queries use codegen graphql() instead of graphql-tag gql.
276-
* @see https://stackoverflow.com/a/72802504/1582110
277-
*/
278-
export const HighlightFields = gql`
279-
${print(TypedHighlightFields)}
280270
`;
281271

282272
/**
283273
* Processes Highlights object from rumors-api
284274
*/
285275
export function highlightSections(
286-
highlightFields: FragmentType<typeof TypedHighlightFields>,
276+
highlightFields: HighlightFieldsFragment,
287277
classes: {
288278
highlight?: string;
289279
reference?: string;
290280
hyperlinks?: string;
291281
}
292282
) {
293-
const { text, reference, hyperlinks } = getFragmentData(
294-
TypedHighlightFields,
295-
highlightFields
296-
);
283+
const { text, reference, hyperlinks } = highlightFields;
297284

298285
const jsxElems = [];
299286

typegen/fragment-masking.ts

-55
This file was deleted.

0 commit comments

Comments
 (0)