1
import { Kind, ASTNode } from "graphql"
2

3 1
export const parseLiteral = (
4
  ast: ASTNode
5
): string | boolean | number | object | null => {
6 1
  switch (ast.kind) {
7
    case Kind.STRING:
8 0
      return ast.value
9
    case Kind.BOOLEAN:
10 0
      return ast.value
11
    case Kind.INT:
12 0
      return parseInt(ast.value, 10)
13
    case Kind.FLOAT:
14 0
      return parseFloat(ast.value)
15
    case Kind.OBJECT:
16 0
      return ast.fields.reduce((hash, { name, value }) => {
17 0
        hash[name.value] = parseLiteral(value)
18 0
        return hash
19
      }, {} as Record<any, any>)
20
    case Kind.LIST:
21 0
      return ast.values.map(parseLiteral)
22
    default:
23 0
      return null
24
  }
25
}

Read our documentation on viewing source code .

Loading