Skip to content

Commit 06e4f0a

Browse files
use type assertion functions from graphql-js
1 parent 8dc4de5 commit 06e4f0a

File tree

3 files changed

+28
-32
lines changed

3 files changed

+28
-32
lines changed

packages/graphiql-react/src/editor/completion.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import type { Editor, EditorChange } from 'codemirror';
22
import escapeHTML from 'escape-html';
3-
import {
4-
GraphQLList,
5-
GraphQLNonNull,
6-
GraphQLSchema,
7-
GraphQLType,
8-
} from 'graphql';
3+
import { GraphQLSchema, GraphQLType, isListType, isNonNullType } from 'graphql';
94

105
import { ExplorerContextType } from '../explorer';
116
import { markdown } from '../markdown';
@@ -117,10 +112,10 @@ export function onHasCompletion(
117112
}
118113

119114
function renderType(type: GraphQLType): string {
120-
if (type instanceof GraphQLNonNull) {
115+
if (isNonNullType(type)) {
121116
return `${renderType(type.ofType)}!`;
122117
}
123-
if (type instanceof GraphQLList) {
118+
if (isListType(type)) {
124119
return `[${renderType(type.ofType)}]`;
125120
}
126121
return `<a class="typeName">${escapeHTML(type.name)}</a>`;

packages/graphiql/src/components/DocExplorer/TypeDoc.tsx

+21-19
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import React, { ReactNode, useState } from 'react';
9-
import {
10-
GraphQLObjectType,
11-
GraphQLInterfaceType,
12-
GraphQLUnionType,
13-
GraphQLEnumType,
14-
GraphQLEnumValue,
15-
GraphQLNamedType,
16-
isType,
17-
} from 'graphql';
188
import {
199
ExplorerFieldDef,
2010
useExplorerContext,
2111
useSchemaContext,
2212
} from '@graphiql/react';
13+
import {
14+
GraphQLEnumValue,
15+
GraphQLInterfaceType,
16+
GraphQLNamedType,
17+
GraphQLObjectType,
18+
isEnumType,
19+
isInterfaceType,
20+
isNamedType,
21+
isObjectType,
22+
isUnionType,
23+
} from 'graphql';
24+
import React, { ReactNode, useState } from 'react';
2325

2426
import Argument from './Argument';
25-
import MarkdownContent from './MarkdownContent';
26-
import TypeLink from './TypeLink';
2727
import DefaultValue from './DefaultValue';
2828
import FieldLink from './FieldLink';
29+
import MarkdownContent from './MarkdownContent';
30+
import TypeLink from './TypeLink';
2931

3032
export default function TypeDoc() {
3133
const { schema } = useSchemaContext({ nonNull: true });
@@ -35,19 +37,19 @@ export default function TypeDoc() {
3537
const navItem = explorerNavStack[explorerNavStack.length - 1];
3638
const type = navItem.def;
3739

38-
if (!schema || !isType(type)) {
40+
if (!schema || !isNamedType(type)) {
3941
return null;
4042
}
4143

4244
let typesTitle: string | null = null;
4345
let types: readonly (GraphQLObjectType | GraphQLInterfaceType)[] = [];
44-
if (type instanceof GraphQLUnionType) {
46+
if (isUnionType(type)) {
4547
typesTitle = 'possible types';
4648
types = schema.getPossibleTypes(type);
47-
} else if (type instanceof GraphQLInterfaceType) {
49+
} else if (isInterfaceType(type)) {
4850
typesTitle = 'implementations';
4951
types = schema.getPossibleTypes(type);
50-
} else if (type instanceof GraphQLObjectType) {
52+
} else if (isObjectType(type)) {
5153
typesTitle = 'implements';
5254
types = type.getInterfaces();
5355
}
@@ -110,7 +112,7 @@ export default function TypeDoc() {
110112

111113
let valuesDef: ReactNode;
112114
let deprecatedValuesDef: ReactNode;
113-
if (type instanceof GraphQLEnumType) {
115+
if (isEnumType(type)) {
114116
const values = type.getValues();
115117
valuesDef = (
116118
<div className="doc-category">
@@ -156,12 +158,12 @@ export default function TypeDoc() {
156158
('description' in type && type.description) || 'No Description'
157159
}
158160
/>
159-
{type instanceof GraphQLObjectType && typesDef}
161+
{isObjectType(type) && typesDef}
160162
{fieldsDef}
161163
{deprecatedFieldsDef}
162164
{valuesDef}
163165
{deprecatedValuesDef}
164-
{!(type instanceof GraphQLObjectType) && typesDef}
166+
{!isObjectType(type) && typesDef}
165167
</div>
166168
);
167169
}

packages/graphiql/src/components/DocExplorer/TypeLink.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import React from 'react';
9-
import { GraphQLList, GraphQLNonNull, GraphQLType } from 'graphql';
10-
118
import { useExplorerContext } from '@graphiql/react';
9+
import { GraphQLType, isListType, isNonNullType } from 'graphql';
10+
import React from 'react';
1211

1312
type TypeLinkProps = {
1413
type: GraphQLType;
@@ -22,14 +21,14 @@ export default function TypeLink(props: TypeLinkProps) {
2221
}
2322

2423
const type = props.type;
25-
if (type instanceof GraphQLNonNull) {
24+
if (isNonNullType(type)) {
2625
return (
2726
<>
2827
<TypeLink type={type.ofType} />!
2928
</>
3029
);
3130
}
32-
if (type instanceof GraphQLList) {
31+
if (isListType(type)) {
3332
return (
3433
<>
3534
[<TypeLink type={type.ofType} />]

0 commit comments

Comments
 (0)