@@ -9,7 +9,6 @@ import { GraphQLSchema } from 'graphql/type';
9
9
import { buildClientSchema , IntrospectionQuery } from 'graphql/utilities' ;
10
10
import {
11
11
Children ,
12
- type ReactElement ,
13
12
type ReactNode ,
14
13
useEffect ,
15
14
useMemo ,
@@ -52,7 +51,7 @@ export interface VoyagerProps {
52
51
}
53
52
54
53
export default function Voyager ( props : VoyagerProps ) {
55
- const initialDisplayOptions : VoyagerDisplayOptions = useMemo (
54
+ const initialDisplayOptions = useMemo (
56
55
( ) => ( {
57
56
rootType : undefined ,
58
57
skipRelay : true ,
@@ -83,10 +82,19 @@ export default function Voyager(props: VoyagerProps) {
83
82
return null ;
84
83
}
85
84
86
- const introspectionSchema =
87
- introspectionResult . value instanceof GraphQLSchema
88
- ? introspectionResult . value
89
- : buildClientSchema ( introspectionResult . value . data ) ;
85
+ let introspectionSchema ;
86
+ if ( introspectionResult . value instanceof GraphQLSchema ) {
87
+ introspectionSchema = introspectionResult . value ;
88
+ } else {
89
+ if (
90
+ introspectionResult . value . errors != null ||
91
+ introspectionResult . value . data == null
92
+ ) {
93
+ // FIXME: display errors
94
+ return null ;
95
+ }
96
+ introspectionSchema = buildClientSchema ( introspectionResult . value . data ) ;
97
+ }
90
98
91
99
const schema = getSchema (
92
100
introspectionSchema ,
@@ -105,7 +113,13 @@ export default function Voyager(props: VoyagerProps) {
105
113
setSelected ( { typeID : null , edgeID : null } ) ;
106
114
} , [ typeGraph ] ) ;
107
115
108
- const [ selected , setSelected ] = useState ( { typeID : null , edgeID : null } ) ;
116
+ const [ selected , setSelected ] = useState < {
117
+ typeID : string | null ;
118
+ edgeID : string | null ;
119
+ } > ( {
120
+ typeID : null ,
121
+ edgeID : null ,
122
+ } ) ;
109
123
110
124
const {
111
125
allowToChangeSchema = false ,
@@ -115,7 +129,7 @@ export default function Voyager(props: VoyagerProps) {
115
129
hideVoyagerLogo = true ,
116
130
} = props ;
117
131
118
- const viewportRef = useRef ( null ) ;
132
+ const viewportRef = useRef < GraphViewport > ( null ) ;
119
133
useEffect ( ( ) => viewportRef . current ?. resize ( ) , [ hideDocs ] ) ;
120
134
121
135
return (
@@ -143,7 +157,10 @@ export default function Voyager(props: VoyagerProps) {
143
157
function renderPanel ( ) {
144
158
const children = Children . toArray ( props . children ) ;
145
159
const panelHeader = children . find (
146
- ( child : ReactElement ) => child . type === Voyager . PanelHeader ,
160
+ ( child ) =>
161
+ typeof child === 'object' &&
162
+ 'type' in child &&
163
+ child . type === Voyager . PanelHeader ,
147
164
) ;
148
165
149
166
return (
@@ -156,7 +173,7 @@ export default function Voyager(props: VoyagerProps) {
156
173
typeGraph = { typeGraph }
157
174
selectedTypeID = { selected . typeID }
158
175
selectedEdgeID = { selected . edgeID }
159
- onFocusNode = { ( id ) => viewportRef . current . focusNode ( id ) }
176
+ onFocusNode = { ( id ) => viewportRef . current ? .focusNode ( id ) }
160
177
onSelectNode = { handleSelectNode }
161
178
onSelectEdge = { handleSelectEdge }
162
179
/>
@@ -210,7 +227,7 @@ export default function Voyager(props: VoyagerProps) {
210
227
) ;
211
228
}
212
229
213
- function handleSelectNode ( typeID : string ) {
230
+ function handleSelectNode ( typeID : string | null ) {
214
231
setSelected ( ( oldSelected ) => {
215
232
if ( typeID === oldSelected . typeID ) {
216
233
return oldSelected ;
@@ -219,9 +236,9 @@ export default function Voyager(props: VoyagerProps) {
219
236
} ) ;
220
237
}
221
238
222
- function handleSelectEdge ( edgeID : string ) {
239
+ function handleSelectEdge ( edgeID : string | null ) {
223
240
setSelected ( ( oldSelected ) => {
224
- if ( edgeID === oldSelected . edgeID ) {
241
+ if ( edgeID === oldSelected . edgeID || edgeID == null ) {
225
242
// deselect if click again
226
243
return { ...oldSelected , edgeID : null } ;
227
244
} else {
0 commit comments