UNPKG

789 BJavaScriptView Raw
1import { isNonNullType, Kind, isListType } from 'graphql';
2import { inspect } from './inspect.js';
3export function astFromType(type) {
4 if (isNonNullType(type)) {
5 const innerType = astFromType(type.ofType);
6 if (innerType.kind === Kind.NON_NULL_TYPE) {
7 throw new Error(`Invalid type node ${inspect(type)}. Inner type of non-null type cannot be a non-null type.`);
8 }
9 return {
10 kind: Kind.NON_NULL_TYPE,
11 type: innerType,
12 };
13 }
14 else if (isListType(type)) {
15 return {
16 kind: Kind.LIST_TYPE,
17 type: astFromType(type.ofType),
18 };
19 }
20 return {
21 kind: Kind.NAMED_TYPE,
22 name: {
23 kind: Kind.NAME,
24 value: type.name,
25 },
26 };
27}