Skip to content

Commit 69ad489

Browse files
authored
prefer React.FC type when declaring React components (#3939)
* upd * graphiql-plugin-doc-explorer * graphiql-plugin-doc-explorer * rm * upd * upd * upd * upd * upd * upd * upd * use graphiql react * add todos * upd * add referencePlugin, schemaReference and setSchemaReference * onHasCompletion and useCompletion * upd * upd * upd * upd * prettier * use less diff * try to fix preview * fix unit tests * try another approach * ok, and now let's try .js instead of .cjs * use FC type * use FC type * use FC type * use FC type * use FC type * use FC type * use FC type * use FC type * upd * Delete .changeset/gentle-houses-add.md * fix * tiny fix
1 parent 9f55d93 commit 69ad489

36 files changed

+210
-195
lines changed

.changeset/five-wasps-rule.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@graphiql/plugin-code-exporter': patch
3+
'@graphiql/plugin-doc-explorer': patch
4+
'@graphiql/plugin-explorer': patch
5+
'@graphiql/plugin-history': patch
6+
'@graphiql/react': patch
7+
'graphiql': patch
8+
---
9+
10+
prefer `React.FC` type when declaring React components

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"packageManager": "yarn@1.22.22",
1616
"scripts": {
17+
"types:check": "turbo run types:check",
1718
"dev:graphiql": "turbo run dev --filter=graphiql...",
1819
"build": "yarn build-clean && yarn tsc && yarn build:nontsc",
1920
"build-bundles": "yarn prebuild-bundles && yarn wsrun:noexamples --stages build-bundles",

packages/graphiql-plugin-code-exporter/src/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useOperationsEditorState, type GraphiQLPlugin } from '@graphiql/react';
2-
import React from 'react';
2+
import React, { FC } from 'react';
33
import GraphiQLCodeExporter, {
44
GraphiQLCodeExporterProps,
55
} from 'graphiql-code-exporter';
@@ -9,7 +9,9 @@ import './index.css';
99

1010
type GraphiQLCodeExporterPluginProps = Omit<GraphiQLCodeExporterProps, 'query'>;
1111

12-
function GraphiQLCodeExporterPlugin(props: GraphiQLCodeExporterPluginProps) {
12+
const GraphiQLCodeExporterPlugin: FC<
13+
GraphiQLCodeExporterPluginProps
14+
> = props => {
1315
const [operationsString] = useOperationsEditorState();
1416
return (
1517
<GraphiQLCodeExporter
@@ -18,7 +20,7 @@ function GraphiQLCodeExporterPlugin(props: GraphiQLCodeExporterPluginProps) {
1820
query={operationsString}
1921
/>
2022
);
21-
}
23+
};
2224

2325
export function codeExporterPlugin(
2426
props: GraphiQLCodeExporterPluginProps,

packages/graphiql-plugin-doc-explorer/src/components/__tests__/doc-explorer.spec.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { render } from '@testing-library/react';
22
import { GraphQLInt, GraphQLObjectType, GraphQLSchema } from 'graphql';
3-
import { useContext, useEffect } from 'react';
4-
3+
import { FC, useContext, useEffect } from 'react';
54
import { SchemaContext, SchemaContextType } from '@graphiql/react';
65
import { ExplorerContext, ExplorerContextProvider } from '../../context';
76
import { DocExplorer } from '../doc-explorer';
@@ -31,6 +30,8 @@ const defaultSchemaContext: SchemaContextType = {
3130
isFetching: false,
3231
schema: makeSchema(),
3332
validationErrors: [],
33+
schemaReference: null!,
34+
setSchemaReference: null!,
3435
};
3536

3637
const withErrorSchemaContext: SchemaContextType = {
@@ -39,15 +40,17 @@ const withErrorSchemaContext: SchemaContextType = {
3940
isFetching: false,
4041
schema: new GraphQLSchema({ description: 'GraphQL Schema for testing' }),
4142
validationErrors: [],
43+
schemaReference: null!,
44+
setSchemaReference: null!,
4245
};
4346

44-
function DocExplorerWithContext() {
47+
const DocExplorerWithContext: FC = () => {
4548
return (
4649
<ExplorerContextProvider>
4750
<DocExplorer />
4851
</ExplorerContextProvider>
4952
);
50-
}
53+
};
5154

5255
describe('DocExplorer', () => {
5356
it('renders spinner when the schema is loading', () => {

packages/graphiql-plugin-doc-explorer/src/components/__tests__/field-documentation.spec.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { FC } from 'react';
12
import { fireEvent, render } from '@testing-library/react';
23
import { GraphQLString, GraphQLObjectType, Kind } from 'graphql';
3-
44
import { ExplorerContext, ExplorerFieldDef } from '../../context';
55
import { FieldDocumentation } from '../field-documentation';
66
import { mockExplorerContextValue } from './test-utils';
@@ -53,7 +53,9 @@ const exampleObject = new GraphQLObjectType({
5353
},
5454
});
5555

56-
function FieldDocumentationWithContext(props: { field: ExplorerFieldDef }) {
56+
const FieldDocumentationWithContext: FC<{
57+
field: ExplorerFieldDef;
58+
}> = props => {
5759
return (
5860
<ExplorerContext.Provider
5961
value={mockExplorerContextValue({
@@ -64,7 +66,7 @@ function FieldDocumentationWithContext(props: { field: ExplorerFieldDef }) {
6466
<FieldDocumentation field={props.field} />
6567
</ExplorerContext.Provider>
6668
);
67-
}
69+
};
6870

6971
describe('FieldDocumentation', () => {
7072
it('should render a simple string field', () => {

packages/graphiql-plugin-doc-explorer/src/components/__tests__/type-documentation.spec.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FC } from 'react';
12
import { fireEvent, render } from '@testing-library/react';
23
import {
34
GraphQLBoolean,
@@ -9,13 +10,12 @@ import {
910
GraphQLString,
1011
GraphQLUnionType,
1112
} from 'graphql';
12-
1313
import { SchemaContext } from '@graphiql/react';
1414
import { ExplorerContext } from '../../context';
1515
import { TypeDocumentation } from '../type-documentation';
1616
import { mockExplorerContextValue, unwrapType } from './test-utils';
1717

18-
function TypeDocumentationWithContext(props: { type: GraphQLNamedType }) {
18+
const TypeDocumentationWithContext: FC<{ type: GraphQLNamedType }> = props => {
1919
return (
2020
<SchemaContext.Provider
2121
value={{
@@ -24,6 +24,8 @@ function TypeDocumentationWithContext(props: { type: GraphQLNamedType }) {
2424
isFetching: false,
2525
schema: ExampleSchema,
2626
validationErrors: [],
27+
schemaReference: null!,
28+
setSchemaReference: null!,
2729
}}
2830
>
2931
<ExplorerContext.Provider
@@ -36,7 +38,7 @@ function TypeDocumentationWithContext(props: { type: GraphQLNamedType }) {
3638
</ExplorerContext.Provider>
3739
</SchemaContext.Provider>
3840
);
39-
}
41+
};
4042

4143
describe('TypeDocumentation', () => {
4244
it('renders a top-level query object type', () => {

packages/graphiql-plugin-doc-explorer/src/components/__tests__/type-link.spec.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { fireEvent, render } from '@testing-library/react';
22
import { GraphQLNonNull, GraphQLList, GraphQLString } from 'graphql';
3-
import { ComponentProps } from 'react';
4-
53
import { ExplorerContext } from '../../context';
64
import { TypeLink } from '../type-link';
75
import { mockExplorerContextValue, unwrapType } from './test-utils';
86

97
const nonNullType = new GraphQLNonNull(GraphQLString);
108
const listType = new GraphQLList(GraphQLString);
119

12-
function TypeLinkWithContext(props: ComponentProps<typeof TypeLink>) {
10+
const TypeLinkWithContext: typeof TypeLink = props => {
1311
return (
1412
<ExplorerContext.Provider
1513
value={mockExplorerContextValue({
@@ -30,7 +28,7 @@ function TypeLinkWithContext(props: ComponentProps<typeof TypeLink>) {
3028
</ExplorerContext.Consumer>
3129
</ExplorerContext.Provider>
3230
);
33-
}
31+
};
3432

3533
describe('TypeLink', () => {
3634
it('should render a string', () => {

packages/graphiql-plugin-doc-explorer/src/components/argument.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import { FC } from 'react';
12
import { GraphQLArgument } from 'graphql';
2-
3+
import { MarkdownContent } from '@graphiql/react';
34
import { DefaultValue } from './default-value';
45
import { TypeLink } from './type-link';
5-
66
import './argument.css';
7-
import { MarkdownContent } from '@graphiql/react';
87

98
type ArgumentProps = {
109
/**
@@ -25,7 +24,11 @@ type ArgumentProps = {
2524
inline?: boolean;
2625
};
2726

28-
export function Argument({ arg, showDefaultValue, inline }: ArgumentProps) {
27+
export const Argument: FC<ArgumentProps> = ({
28+
arg,
29+
showDefaultValue,
30+
inline,
31+
}) => {
2932
const definition = (
3033
<span>
3134
<span className="graphiql-doc-explorer-argument-name">{arg.name}</span>
@@ -55,4 +58,4 @@ export function Argument({ arg, showDefaultValue, inline }: ArgumentProps) {
5558
) : null}
5659
</div>
5760
);
58-
}
61+
};

packages/graphiql-plugin-doc-explorer/src/components/default-value.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { FC } from 'react';
12
import { astFromValue, print, ValueNode } from 'graphql';
2-
33
import { ExplorerFieldDef } from '../context';
4-
54
import './default-value.css';
65

76
const printDefault = (ast?: ValueNode | null): string => {
@@ -18,7 +17,7 @@ type DefaultValueProps = {
1817
field: ExplorerFieldDef;
1918
};
2019

21-
export function DefaultValue({ field }: DefaultValueProps) {
20+
export const DefaultValue: FC<DefaultValueProps> = ({ field }) => {
2221
if (!('defaultValue' in field) || field.defaultValue === undefined) {
2322
return null;
2423
}
@@ -34,4 +33,4 @@ export function DefaultValue({ field }: DefaultValueProps) {
3433
</span>
3534
</>
3635
);
37-
}
36+
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
import { FC } from 'react';
12
import { MarkdownContent } from '@graphiql/react';
2-
33
import './deprecation-reason.css';
44

55
type DeprecationReasonProps = {
66
/**
7-
* The deprecation reason as markdown string.
7+
* The deprecation reason as Markdown string.
88
*/
99
children?: string | null;
1010
preview?: boolean;
1111
};
1212

13-
export function DeprecationReason(props: DeprecationReasonProps) {
13+
export const DeprecationReason: FC<DeprecationReasonProps> = props => {
1414
return props.children ? (
1515
<div className="graphiql-doc-explorer-deprecation">
1616
<div className="graphiql-doc-explorer-deprecation-label">Deprecated</div>
@@ -22,4 +22,4 @@ export function DeprecationReason(props: DeprecationReasonProps) {
2222
</MarkdownContent>
2323
</div>
2424
) : null;
25-
}
25+
};
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { FC } from 'react';
12
import { DirectiveNode } from 'graphql';
2-
33
import './directive.css';
44

55
type DirectiveProps = {
@@ -9,10 +9,10 @@ type DirectiveProps = {
99
directive: DirectiveNode;
1010
};
1111

12-
export function Directive({ directive }: DirectiveProps) {
12+
export const Directive: FC<DirectiveProps> = ({ directive }) => {
1313
return (
1414
<span className="graphiql-doc-explorer-directive">
1515
@{directive.name.value}
1616
</span>
1717
);
18-
}
18+
};

packages/graphiql-plugin-doc-explorer/src/components/doc-explorer.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { isType } from 'graphql';
2-
import { ReactNode } from 'react';
3-
2+
import { FC, ReactNode } from 'react';
43
import { ChevronLeftIcon, Spinner, useSchemaContext } from '@graphiql/react';
54
import { useExplorerContext } from '../context';
65
import { FieldDocumentation } from './field-documentation';
76
import { SchemaDocumentation } from './schema-documentation';
87
import { Search } from './search';
98
import { TypeDocumentation } from './type-documentation';
10-
119
import './doc-explorer.css';
1210

13-
export function DocExplorer() {
11+
export const DocExplorer: FC = () => {
1412
const { fetchError, isFetching, schema, validationErrors } = useSchemaContext(
1513
{ nonNull: true, caller: DocExplorer },
1614
);
@@ -84,4 +82,4 @@ export function DocExplorer() {
8482
<div className="graphiql-doc-explorer-content">{content}</div>
8583
</section>
8684
);
87-
}
85+
};

packages/graphiql-plugin-doc-explorer/src/components/field-documentation.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { GraphQLArgument } from 'graphql';
2-
import { useState } from 'react';
3-
2+
import { FC, useState } from 'react';
43
import { Button, MarkdownContent } from '@graphiql/react';
54
import { ExplorerFieldDef } from '../context';
65
import { Argument } from './argument';
@@ -16,27 +15,27 @@ type FieldDocumentationProps = {
1615
field: ExplorerFieldDef;
1716
};
1817

19-
export function FieldDocumentation(props: FieldDocumentationProps) {
18+
export const FieldDocumentation: FC<FieldDocumentationProps> = ({ field }) => {
2019
return (
2120
<>
22-
{props.field.description ? (
21+
{field.description ? (
2322
<MarkdownContent type="description">
24-
{props.field.description}
23+
{field.description}
2524
</MarkdownContent>
2625
) : null}
2726
<DeprecationReason preview={false}>
28-
{props.field.deprecationReason}
27+
{field.deprecationReason}
2928
</DeprecationReason>
3029
<ExplorerSection title="Type">
31-
<TypeLink type={props.field.type} />
30+
<TypeLink type={field.type} />
3231
</ExplorerSection>
33-
<Arguments field={props.field} />
34-
<Directives field={props.field} />
32+
<Arguments field={field} />
33+
<Directives field={field} />
3534
</>
3635
);
37-
}
36+
};
3837

39-
function Arguments({ field }: { field: ExplorerFieldDef }) {
38+
const Arguments: FC<{ field: ExplorerFieldDef }> = ({ field }) => {
4039
const [showDeprecated, setShowDeprecated] = useState(false);
4140
const handleShowDeprecated = () => {
4241
setShowDeprecated(true);
@@ -80,11 +79,11 @@ function Arguments({ field }: { field: ExplorerFieldDef }) {
8079
) : null}
8180
</>
8281
);
83-
}
82+
};
8483

85-
function Directives({ field }: { field: ExplorerFieldDef }) {
86-
const directives = field.astNode?.directives || [];
87-
if (!directives || directives.length === 0) {
84+
const Directives: FC<{ field: ExplorerFieldDef }> = ({ field }) => {
85+
const directives = field.astNode?.directives;
86+
if (!directives?.length) {
8887
return null;
8988
}
9089
return (
@@ -96,4 +95,4 @@ function Directives({ field }: { field: ExplorerFieldDef }) {
9695
))}
9796
</ExplorerSection>
9897
);
99-
}
98+
};

0 commit comments

Comments
 (0)