UNPKG

1.02 kBTypeScriptView Raw
1/**
2 * Copyright (c) 2021 GraphQL Contributors.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import { useExplorerContext } from '@graphiql/react';
9import { GraphQLType, isListType, isNonNullType } from 'graphql';
10import React from 'react';
11
12type TypeLinkProps = {
13 type: GraphQLType;
14};
15
16export default function TypeLink(props: TypeLinkProps) {
17 const { push } = useExplorerContext({ nonNull: true, caller: TypeLink });
18
19 if (!props.type) {
20 return null;
21 }
22
23 const type = props.type;
24 if (isNonNullType(type)) {
25 return (
26 <>
27 <TypeLink type={type.ofType} />!
28 </>
29 );
30 }
31 if (isListType(type)) {
32 return (
33 <>
34 [<TypeLink type={type.ofType} />]
35 </>
36 );
37 }
38 return (
39 <a
40 className="type-name"
41 onClick={event => {
42 event.preventDefault();
43 push({ name: type.name, def: type });
44 }}
45 href="#"
46 >
47 {type.name}
48 </a>
49 );
50}