Mirror: The spec-compliant minimum of client-side GraphQL.

feat: Establish type compatibility with graphql (#3)

* feat: Establish type compatibility with graphql

* Merge null and boolean match

* Remove Kind usage in values.ts

* Turn GraphQLError type compatible

* Fix ASTNode typings

* Fix type compatibility of parser tools

* Add type checks for visitor

* Apply Prettier formatting

* Add run option to vitest

authored by kitten.sh and committed by GitHub c98aa6fb 56083bb3

+5
.changeset/shy-oranges-itch.md
··· 1 + --- 2 + 'benchmark': patch 3 + --- 4 + 5 + Establish type compatibility with `graphql` package.
+1 -1
package.json
··· 29 29 "client-side graphql" 30 30 ], 31 31 "scripts": { 32 - "test": "vitest", 32 + "test": "vitest run", 33 33 "check": "tsc", 34 34 "lint": "eslint --ext=js,ts .", 35 35 "build": "rollup -c scripts/rollup.config.mjs",
+83 -27
src/ast.ts
··· 1 - import type { Kind } from './kind'; 1 + import type { Kind, OperationTypeNode } from './kind'; 2 + import type { Location } from './types'; 2 3 3 - export interface Location { 4 - readonly start: number; 5 - readonly end: number; 6 - readonly source: Source; 7 - } 4 + import type { 5 + TypeSystemDefinitionNode, 6 + TypeSystemExtensionNode, 7 + SchemaDefinitionNode, 8 + OperationTypeDefinitionNode, 9 + ScalarTypeDefinitionNode, 10 + ObjectTypeDefinitionNode, 11 + FieldDefinitionNode, 12 + InputValueDefinitionNode, 13 + InterfaceTypeDefinitionNode, 14 + UnionTypeDefinitionNode, 15 + EnumTypeDefinitionNode, 16 + EnumValueDefinitionNode, 17 + InputObjectTypeDefinitionNode, 18 + DirectiveDefinitionNode, 19 + SchemaExtensionNode, 20 + ScalarTypeExtensionNode, 21 + ObjectTypeExtensionNode, 22 + InterfaceTypeExtensionNode, 23 + UnionTypeExtensionNode, 24 + EnumTypeExtensionNode, 25 + InputObjectTypeExtensionNode, 26 + } from './schemaAst'; 8 27 9 - export interface Source { 10 - body: string; 11 - name: string; 12 - locationOffset: { 13 - line: number; 14 - column: number; 15 - }; 16 - } 17 - 18 - export declare type ASTNode = 28 + export type ASTNode = 19 29 | NameNode 20 30 | DocumentNode 21 31 | OperationDefinitionNode ··· 39 49 | DirectiveNode 40 50 | NamedTypeNode 41 51 | ListTypeNode 42 - | NonNullTypeNode; 52 + | NonNullTypeNode 53 + | SchemaDefinitionNode 54 + | OperationTypeDefinitionNode 55 + | ScalarTypeDefinitionNode 56 + | ObjectTypeDefinitionNode 57 + | FieldDefinitionNode 58 + | InputValueDefinitionNode 59 + | InterfaceTypeDefinitionNode 60 + | UnionTypeDefinitionNode 61 + | EnumTypeDefinitionNode 62 + | EnumValueDefinitionNode 63 + | InputObjectTypeDefinitionNode 64 + | DirectiveDefinitionNode 65 + | SchemaExtensionNode 66 + | ScalarTypeExtensionNode 67 + | ObjectTypeExtensionNode 68 + | InterfaceTypeExtensionNode 69 + | UnionTypeExtensionNode 70 + | EnumTypeExtensionNode 71 + | InputObjectTypeExtensionNode; 43 72 44 73 export interface NameNode { 45 74 readonly kind: Kind.NAME; 46 75 readonly value: string; 76 + readonly loc?: Location; 47 77 } 48 78 49 79 export interface DocumentNode { 50 80 readonly kind: Kind.DOCUMENT; 51 - readonly definitions: ReadonlyArray<ExecutableDefinitionNode>; 81 + readonly definitions: ReadonlyArray<DefinitionNode>; 52 82 readonly loc?: Location; 53 83 } 54 84 55 - export type DefinitionNode = OperationDefinitionNode | FragmentDefinitionNode; 85 + export type DefinitionNode = 86 + | ExecutableDefinitionNode 87 + | TypeSystemDefinitionNode 88 + | TypeSystemExtensionNode; 89 + 56 90 export type ExecutableDefinitionNode = OperationDefinitionNode | FragmentDefinitionNode; 57 91 58 92 export interface OperationDefinitionNode { ··· 62 96 readonly variableDefinitions?: ReadonlyArray<VariableDefinitionNode>; 63 97 readonly directives?: ReadonlyArray<DirectiveNode>; 64 98 readonly selectionSet: SelectionSetNode; 65 - } 66 - 67 - export const enum OperationTypeNode { 68 - QUERY = 'query', 69 - MUTATION = 'mutation', 70 - SUBSCRIPTION = 'subscription', 99 + readonly loc?: Location; 71 100 } 72 101 73 102 export interface VariableDefinitionNode { ··· 76 105 readonly type: TypeNode; 77 106 readonly defaultValue?: ConstValueNode; 78 107 readonly directives?: ReadonlyArray<ConstDirectiveNode>; 108 + readonly loc?: Location; 79 109 } 80 110 81 111 export interface VariableNode { 82 112 readonly kind: Kind.VARIABLE; 83 113 readonly name: NameNode; 114 + readonly loc?: Location; 84 115 } 85 116 86 117 export interface SelectionSetNode { 87 - kind: Kind.SELECTION_SET; 88 - selections: ReadonlyArray<SelectionNode>; 118 + readonly kind: Kind.SELECTION_SET; 119 + readonly selections: ReadonlyArray<SelectionNode>; 120 + readonly loc?: Location; 89 121 } 90 122 91 123 export declare type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode; ··· 97 129 readonly arguments?: ReadonlyArray<ArgumentNode>; 98 130 readonly directives?: ReadonlyArray<DirectiveNode>; 99 131 readonly selectionSet?: SelectionSetNode; 132 + readonly loc?: Location; 100 133 } 101 134 102 135 export interface ArgumentNode { 103 136 readonly kind: Kind.ARGUMENT; 104 137 readonly name: NameNode; 105 138 readonly value: ValueNode; 139 + readonly loc?: Location; 106 140 } 107 141 108 142 export interface ConstArgumentNode { 109 143 readonly kind: Kind.ARGUMENT; 110 144 readonly name: NameNode; 111 145 readonly value: ConstValueNode; 146 + readonly loc?: Location; 112 147 } 113 148 114 149 export interface FragmentSpreadNode { 115 150 readonly kind: Kind.FRAGMENT_SPREAD; 116 151 readonly name: NameNode; 117 152 readonly directives?: ReadonlyArray<DirectiveNode>; 153 + readonly loc?: Location; 118 154 } 119 155 120 156 export interface InlineFragmentNode { ··· 122 158 readonly typeCondition?: NamedTypeNode; 123 159 readonly directives?: ReadonlyArray<DirectiveNode>; 124 160 readonly selectionSet: SelectionSetNode; 161 + readonly loc?: Location; 125 162 } 126 163 127 164 export interface FragmentDefinitionNode { ··· 130 167 readonly typeCondition: NamedTypeNode; 131 168 readonly directives?: ReadonlyArray<DirectiveNode>; 132 169 readonly selectionSet: SelectionSetNode; 170 + readonly loc?: Location; 133 171 } 134 172 135 173 export type ValueNode = ··· 156 194 export interface IntValueNode { 157 195 readonly kind: Kind.INT; 158 196 readonly value: string; 197 + readonly loc?: Location; 159 198 } 160 199 161 200 export interface FloatValueNode { 162 201 readonly kind: Kind.FLOAT; 163 202 readonly value: string; 203 + readonly loc?: Location; 164 204 } 205 + 165 206 export interface StringValueNode { 166 207 readonly kind: Kind.STRING; 167 208 readonly value: string; 168 209 readonly block?: boolean; 210 + readonly loc?: Location; 169 211 } 170 212 171 213 export interface BooleanValueNode { 172 214 readonly kind: Kind.BOOLEAN; 173 215 readonly value: boolean; 216 + readonly loc?: Location; 174 217 } 175 218 176 219 export interface NullValueNode { 177 220 readonly kind: Kind.NULL; 221 + readonly loc?: Location; 178 222 } 179 223 180 224 export interface EnumValueNode { 181 225 readonly kind: Kind.ENUM; 182 226 readonly value: string; 227 + readonly loc?: Location; 183 228 } 184 229 185 230 export interface ListValueNode { 186 231 readonly kind: Kind.LIST; 187 232 readonly values: ReadonlyArray<ValueNode>; 233 + readonly loc?: Location; 188 234 } 189 235 190 236 export interface ConstListValueNode { 191 237 readonly kind: Kind.LIST; 192 238 readonly values: ReadonlyArray<ConstValueNode>; 239 + readonly loc?: Location; 193 240 } 194 241 195 242 export interface ObjectValueNode { 196 243 readonly kind: Kind.OBJECT; 197 244 readonly fields: ReadonlyArray<ObjectFieldNode>; 245 + readonly loc?: Location; 198 246 } 199 247 200 248 export interface ConstObjectValueNode { 201 249 readonly kind: Kind.OBJECT; 202 250 readonly fields: ReadonlyArray<ConstObjectFieldNode>; 251 + readonly loc?: Location; 203 252 } 204 253 205 254 export interface ObjectFieldNode { 206 255 readonly kind: Kind.OBJECT_FIELD; 207 256 readonly name: NameNode; 208 257 readonly value: ValueNode; 258 + readonly loc?: Location; 209 259 } 210 260 211 261 export interface ConstObjectFieldNode { 212 262 readonly kind: Kind.OBJECT_FIELD; 213 263 readonly name: NameNode; 214 264 readonly value: ConstValueNode; 265 + readonly loc?: Location; 215 266 } 216 267 217 268 export interface DirectiveNode { 218 269 readonly kind: Kind.DIRECTIVE; 219 270 readonly name: NameNode; 220 271 readonly arguments?: ReadonlyArray<ArgumentNode>; 272 + readonly loc?: Location; 221 273 } 222 274 223 275 export interface ConstDirectiveNode { 224 276 readonly kind: Kind.DIRECTIVE; 225 277 readonly name: NameNode; 226 278 readonly arguments?: ReadonlyArray<ConstArgumentNode>; 279 + readonly loc?: Location; 227 280 } 228 281 229 282 export declare type TypeNode = NamedTypeNode | ListTypeNode | NonNullTypeNode; ··· 231 284 export interface NamedTypeNode { 232 285 readonly kind: Kind.NAMED_TYPE; 233 286 readonly name: NameNode; 287 + readonly loc?: Location; 234 288 } 235 289 236 290 export interface ListTypeNode { 237 291 readonly kind: Kind.LIST_TYPE; 238 292 readonly type: TypeNode; 293 + readonly loc?: Location; 239 294 } 240 295 241 296 export interface NonNullTypeNode { 242 297 readonly kind: Kind.NON_NULL_TYPE; 243 298 readonly type: NamedTypeNode | ListTypeNode; 299 + readonly loc?: Location; 244 300 }
+9 -5
src/error.ts
··· 1 - import { Maybe, Extensions } from './types'; 2 - import { ASTNode, Source } from './ast'; 1 + import { Maybe, Extensions, Source } from './types'; 2 + import { ASTNode } from './ast'; 3 3 4 4 export class GraphQLError extends Error { 5 - readonly locations: undefined; 5 + readonly locations: ReadonlyArray<any> | undefined; 6 6 readonly path: ReadonlyArray<string | number> | undefined; 7 - readonly nodes: ReadonlyArray<ASTNode> | undefined; 7 + readonly nodes: ReadonlyArray<any> | undefined; 8 8 readonly source: Source | undefined; 9 9 readonly positions: ReadonlyArray<number> | undefined; 10 10 readonly originalError: Error | undefined; ··· 41 41 this.extensions = extensions || {}; 42 42 } 43 43 44 - toJSON() { 44 + toJSON(): any { 45 45 return { ...this }; 46 46 } 47 47 48 48 toString() { 49 49 return this.message; 50 + } 51 + 52 + get [Symbol.toStringTag]() { 53 + return 'GraphQLError'; 50 54 } 51 55 }
+3
src/index.ts
··· 1 + export type { Source, Location } from './types'; 2 + 1 3 export * from './ast'; 4 + export * from './schemaAst'; 2 5 export * from './kind'; 3 6 export * from './error'; 4 7 export * from './parser';
+62
src/kind.d.ts
··· 1 + export declare enum Kind { 2 + /** Name */ 3 + NAME = 'Name', 4 + /** Document */ 5 + DOCUMENT = 'Document', 6 + OPERATION_DEFINITION = 'OperationDefinition', 7 + VARIABLE_DEFINITION = 'VariableDefinition', 8 + SELECTION_SET = 'SelectionSet', 9 + FIELD = 'Field', 10 + ARGUMENT = 'Argument', 11 + /** Fragments */ 12 + FRAGMENT_SPREAD = 'FragmentSpread', 13 + INLINE_FRAGMENT = 'InlineFragment', 14 + FRAGMENT_DEFINITION = 'FragmentDefinition', 15 + /** Values */ 16 + VARIABLE = 'Variable', 17 + INT = 'IntValue', 18 + FLOAT = 'FloatValue', 19 + STRING = 'StringValue', 20 + BOOLEAN = 'BooleanValue', 21 + NULL = 'NullValue', 22 + ENUM = 'EnumValue', 23 + LIST = 'ListValue', 24 + OBJECT = 'ObjectValue', 25 + OBJECT_FIELD = 'ObjectField', 26 + /** Directives */ 27 + DIRECTIVE = 'Directive', 28 + /** Types */ 29 + NAMED_TYPE = 'NamedType', 30 + LIST_TYPE = 'ListType', 31 + NON_NULL_TYPE = 'NonNullType', 32 + /** Type System Definitions */ 33 + SCHEMA_DEFINITION = 'SchemaDefinition', 34 + OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition', 35 + /** Type Definitions */ 36 + SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition', 37 + OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition', 38 + FIELD_DEFINITION = 'FieldDefinition', 39 + INPUT_VALUE_DEFINITION = 'InputValueDefinition', 40 + INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition', 41 + UNION_TYPE_DEFINITION = 'UnionTypeDefinition', 42 + ENUM_TYPE_DEFINITION = 'EnumTypeDefinition', 43 + ENUM_VALUE_DEFINITION = 'EnumValueDefinition', 44 + INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition', 45 + /** Directive Definitions */ 46 + DIRECTIVE_DEFINITION = 'DirectiveDefinition', 47 + /** Type System Extensions */ 48 + SCHEMA_EXTENSION = 'SchemaExtension', 49 + /** Type Extensions */ 50 + SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension', 51 + OBJECT_TYPE_EXTENSION = 'ObjectTypeExtension', 52 + INTERFACE_TYPE_EXTENSION = 'InterfaceTypeExtension', 53 + UNION_TYPE_EXTENSION = 'UnionTypeExtension', 54 + ENUM_TYPE_EXTENSION = 'EnumTypeExtension', 55 + INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension', 56 + } 57 + 58 + export declare enum OperationTypeNode { 59 + QUERY = 'query', 60 + MUTATION = 'mutation', 61 + SUBSCRIPTION = 'subscription', 62 + }
+47
src/kind.js
··· 1 + export const Kind = { 2 + NAME: 'Name', 3 + DOCUMENT: 'Document', 4 + OPERATION_DEFINITION: 'OperationDefinition', 5 + VARIABLE_DEFINITION: 'VariableDefinition', 6 + SELECTION_SET: 'SelectionSet', 7 + FIELD: 'Field', 8 + ARGUMENT: 'Argument', 9 + FRAGMENT_SPREAD: 'FragmentSpread', 10 + INLINE_FRAGMENT: 'InlineFragment', 11 + FRAGMENT_DEFINITION: 'FragmentDefinition', 12 + VARIABLE: 'Variable', 13 + OBJECT: 'ObjectValue', 14 + OBJECT_FIELD: 'ObjectField', 15 + DIRECTIVE: 'Directive', 16 + NAMED_TYPE: 'NamedType', 17 + LIST_TYPE: 'ListType', 18 + NON_NULL_TYPE: 'NonNullType', 19 + 20 + /* 21 + SCHEMA_DEFINITION: 'SchemaDefinition', 22 + OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition', 23 + SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition', 24 + OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition', 25 + FIELD_DEFINITION: 'FieldDefinition', 26 + INPUT_VALUE_DEFINITION: 'InputValueDefinition', 27 + INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition', 28 + UNION_TYPE_DEFINITION: 'UnionTypeDefinition', 29 + ENUM_TYPE_DEFINITION: 'EnumTypeDefinition', 30 + ENUM_VALUE_DEFINITION: 'EnumValueDefinition', 31 + INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition', 32 + DIRECTIVE_DEFINITION: 'DirectiveDefinition', 33 + SCHEMA_EXTENSION: 'SchemaExtension', 34 + SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension', 35 + OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension', 36 + INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension', 37 + UNION_TYPE_EXTENSION: 'UnionTypeExtension', 38 + ENUM_TYPE_EXTENSION: 'EnumTypeExtension', 39 + INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension', 40 + */ 41 + }; 42 + 43 + export const OperationTypeNode = { 44 + QUERY: 'query', 45 + MUTATION: 'mutation', 46 + SUBSCRIPTION: 'subscription', 47 + };
-32
src/kind.ts
··· 1 - export enum Kind { 2 - /** Name */ 3 - NAME = 'Name', 4 - /** Document */ 5 - DOCUMENT = 'Document', 6 - OPERATION_DEFINITION = 'OperationDefinition', 7 - VARIABLE_DEFINITION = 'VariableDefinition', 8 - SELECTION_SET = 'SelectionSet', 9 - FIELD = 'Field', 10 - ARGUMENT = 'Argument', 11 - /** Fragments */ 12 - FRAGMENT_SPREAD = 'FragmentSpread', 13 - INLINE_FRAGMENT = 'InlineFragment', 14 - FRAGMENT_DEFINITION = 'FragmentDefinition', 15 - /** Values */ 16 - VARIABLE = 'Variable', 17 - INT = 'IntValue', 18 - FLOAT = 'FloatValue', 19 - STRING = 'StringValue', 20 - BOOLEAN = 'BooleanValue', 21 - NULL = 'NullValue', 22 - ENUM = 'EnumValue', 23 - LIST = 'ListValue', 24 - OBJECT = 'ObjectValue', 25 - OBJECT_FIELD = 'ObjectField', 26 - /** Directives */ 27 - DIRECTIVE = 'Directive', 28 - /** Types */ 29 - NAMED_TYPE = 'NamedType', 30 - LIST_TYPE = 'ListType', 31 - NON_NULL_TYPE = 'NonNullType', 32 - }
+91 -72
src/parser.ts
··· 4 4 * in graphql.js it will only parse the query language, but not the schema 5 5 * language. 6 6 */ 7 - import { Kind } from './kind'; 7 + import { Kind, OperationTypeNode } from './kind'; 8 8 import { GraphQLError } from './error'; 9 + import { Source } from './types'; 9 10 import type * as ast from './ast'; 10 11 11 12 let input: string; 12 13 let idx: number; 13 14 14 - function error(kind: Kind) { 15 + function error(kind: string) { 15 16 return new GraphQLError(`Syntax Error: Unexpected token at ${idx} in ${kind}`); 16 17 } 17 18 ··· 58 59 let match: string | undefined; 59 60 if ((match = advance(nameRe))) { 60 61 return { 61 - kind: Kind.NAME, 62 + kind: 'Name' as Kind.NAME, 62 63 value: match, 63 64 }; 64 65 } 65 66 } 66 67 67 - const nullRe = /null/y; 68 - const boolRe = /true|false/y; 68 + const constRe = /null|true|false/y; 69 69 const variableRe = /\$[_\w][_\d\w]*/y; 70 70 const intRe = /[-]?\d+/y; 71 71 const floatRe = /(?:[-]?\d+)?(?:\.\d+)(?:[eE][+-]?\d+)?/y; ··· 79 79 function value(constant: boolean): ast.ValueNode | undefined { 80 80 let out: ast.ValueNode | undefined; 81 81 let match: string | undefined; 82 - if (advance(nullRe)) { 83 - out = { kind: Kind.NULL }; 84 - } else if ((match = advance(boolRe))) { 85 - out = { 86 - kind: Kind.BOOLEAN, 87 - value: match === 'true', 88 - }; 82 + if ((match = advance(constRe))) { 83 + out = 84 + match === 'null' 85 + ? { 86 + kind: 'NullValue' as Kind.NULL, 87 + } 88 + : { 89 + kind: 'BooleanValue' as Kind.BOOLEAN, 90 + value: match === 'true', 91 + }; 89 92 } else if (!constant && (match = advance(variableRe))) { 90 93 out = { 91 - kind: Kind.VARIABLE, 94 + kind: 'Variable' as Kind.VARIABLE, 92 95 name: { 93 - kind: Kind.NAME, 96 + kind: 'Name' as Kind.NAME, 94 97 value: match.slice(1), 95 98 }, 96 99 }; 97 100 } else if ((match = advance(floatRe))) { 98 101 out = { 99 - kind: Kind.FLOAT, 102 + kind: 'FloatValue' as Kind.FLOAT, 100 103 value: match, 101 104 }; 102 105 } else if ((match = advance(intRe))) { 103 106 out = { 104 - kind: Kind.INT, 107 + kind: 'IntValue' as Kind.INT, 105 108 value: match, 106 109 }; 107 110 } else if ((match = advance(nameRe))) { 108 111 out = { 109 - kind: Kind.ENUM, 112 + kind: 'EnumValue' as Kind.ENUM, 110 113 value: match, 111 114 }; 112 115 } else if ((match = advance(blockStringRe))) { 113 116 out = { 114 - kind: Kind.STRING, 117 + kind: 'StringValue' as Kind.STRING, 115 118 value: blockString(match.slice(3, -3)), 116 119 block: true, 117 120 }; 118 121 } else if ((match = advance(stringRe))) { 119 122 out = { 120 - kind: Kind.STRING, 123 + kind: 'StringValue' as Kind.STRING, 121 124 value: complexStringRe.test(match) ? (JSON.parse(match) as string) : match.slice(1, -1), 122 125 block: false, 123 126 }; ··· 136 139 ignored(); 137 140 const values: ast.ValueNode[] = []; 138 141 while ((match = value(constant))) values.push(match); 139 - if (input.charCodeAt(idx++) !== 93 /*']'*/) throw error(Kind.LIST); 142 + if (input.charCodeAt(idx++) !== 93 /*']'*/) throw error('ListValue'); 140 143 ignored(); 141 144 return { 142 - kind: Kind.LIST, 145 + kind: 'ListValue' as Kind.LIST, 143 146 values, 144 147 }; 145 148 } ··· 153 156 let _name: ast.NameNode | undefined; 154 157 while ((_name = name())) { 155 158 ignored(); 156 - if (input.charCodeAt(idx++) !== 58 /*':'*/) throw error(Kind.OBJECT_FIELD); 159 + if (input.charCodeAt(idx++) !== 58 /*':'*/) throw error('ObjectField' as Kind.OBJECT_FIELD); 157 160 ignored(); 158 161 const _value = value(constant); 159 - if (!_value) throw error(Kind.OBJECT_FIELD); 162 + if (!_value) throw error('ObjectField'); 160 163 fields.push({ 161 - kind: Kind.OBJECT_FIELD, 164 + kind: 'ObjectField' as Kind.OBJECT_FIELD, 162 165 name: _name, 163 166 value: _value, 164 167 }); 165 168 } 166 - if (input.charCodeAt(idx++) !== 125 /*'}'*/) throw error(Kind.OBJECT); 169 + if (input.charCodeAt(idx++) !== 125 /*'}'*/) throw error('ObjectValue'); 167 170 ignored(); 168 171 return { 169 - kind: Kind.OBJECT, 172 + kind: 'ObjectValue' as Kind.OBJECT, 170 173 fields, 171 174 }; 172 175 } ··· 181 184 let _name: ast.NameNode | undefined; 182 185 while ((_name = name())) { 183 186 ignored(); 184 - if (input.charCodeAt(idx++) !== 58 /*':'*/) throw error(Kind.ARGUMENT); 187 + if (input.charCodeAt(idx++) !== 58 /*':'*/) throw error('Argument'); 185 188 ignored(); 186 189 const _value = value(constant); 187 - if (!_value) throw error(Kind.ARGUMENT); 190 + if (!_value) throw error('Argument'); 188 191 args.push({ 189 - kind: Kind.ARGUMENT, 192 + kind: 'Argument' as Kind.ARGUMENT, 190 193 name: _name, 191 194 value: _value, 192 195 }); 193 196 } 194 - if (!args.length || input.charCodeAt(idx++) !== 41 /*')'*/) throw error(Kind.ARGUMENT); 197 + if (!args.length || input.charCodeAt(idx++) !== 41 /*')'*/) throw error('Argument'); 195 198 ignored(); 196 199 } 197 200 return args; ··· 206 209 while (input.charCodeAt(idx) === 64 /*'@'*/) { 207 210 idx++; 208 211 const _name = name(); 209 - if (!_name) throw error(Kind.DIRECTIVE); 212 + if (!_name) throw error('Directive'); 210 213 ignored(); 211 214 directives.push({ 212 - kind: Kind.DIRECTIVE, 215 + kind: 'Directive' as Kind.DIRECTIVE, 213 216 name: _name, 214 217 arguments: arguments_(constant), 215 218 }); ··· 228 231 ignored(); 229 232 _alias = _name; 230 233 _name = name(); 231 - if (!_name) throw error(Kind.FIELD); 234 + if (!_name) throw error('Field'); 232 235 ignored(); 233 236 } 234 237 return { 235 - kind: Kind.FIELD, 238 + kind: 'Field' as Kind.FIELD, 236 239 alias: _alias, 237 240 name: _name, 238 241 arguments: arguments_(false), ··· 249 252 idx++; 250 253 ignored(); 251 254 const _type = type(); 252 - if (!_type || input.charCodeAt(idx++) !== 93 /*']'*/) throw error(Kind.LIST_TYPE); 255 + if (!_type || input.charCodeAt(idx++) !== 93 /*']'*/) throw error('ListType'); 253 256 match = { 254 - kind: Kind.LIST_TYPE, 257 + kind: 'ListType' as Kind.LIST_TYPE, 255 258 type: _type, 256 259 }; 257 260 } else if ((match = name())) { 258 261 match = { 259 - kind: Kind.NAMED_TYPE, 262 + kind: 'NamedType' as Kind.NAMED_TYPE, 260 263 name: match, 261 264 }; 262 265 } else { 263 - throw error(Kind.NAMED_TYPE); 266 + throw error('NamedType'); 264 267 } 265 268 266 269 ignored(); ··· 268 271 idx++; 269 272 ignored(); 270 273 return { 271 - kind: Kind.NON_NULL_TYPE, 274 + kind: 'NonNullType' as Kind.NON_NULL_TYPE, 272 275 type: match, 273 276 }; 274 277 } else { ··· 281 284 if (advance(typeConditionRe)) { 282 285 ignored(); 283 286 const _name = name(); 284 - if (!_name) throw error(Kind.NAMED_TYPE); 287 + if (!_name) throw error('NamedType'); 285 288 ignored(); 286 289 return { 287 - kind: Kind.NAMED_TYPE, 290 + kind: 'NamedType' as Kind.NAMED_TYPE, 288 291 name: _name, 289 292 }; 290 293 } ··· 300 303 let _name: ast.NameNode | undefined; 301 304 if ((_name = name()) && _name.value !== 'on') { 302 305 return { 303 - kind: Kind.FRAGMENT_SPREAD, 306 + kind: 'FragmentSpread' as Kind.FRAGMENT_SPREAD, 304 307 name: _name, 305 308 directives: directives(false), 306 309 }; ··· 309 312 const _typeCondition = typeCondition(); 310 313 const _directives = directives(false); 311 314 const _selectionSet = selectionSet(); 312 - if (!_selectionSet) throw error(Kind.INLINE_FRAGMENT); 315 + if (!_selectionSet) throw error('InlineFragment'); 313 316 return { 314 - kind: Kind.INLINE_FRAGMENT, 317 + kind: 'InlineFragment' as Kind.INLINE_FRAGMENT, 315 318 typeCondition: _typeCondition, 316 319 directives: _directives, 317 320 selectionSet: _selectionSet, ··· 328 331 ignored(); 329 332 const selections: ast.SelectionNode[] = []; 330 333 while ((match = fragmentSpread() || field())) selections.push(match); 331 - if (!selections.length || input.charCodeAt(idx++) !== 125 /*'}'*/) 332 - throw error(Kind.SELECTION_SET); 334 + if (!selections.length || input.charCodeAt(idx++) !== 125 /*'}'*/) throw error('SelectionSet'); 333 335 ignored(); 334 336 return { 335 - kind: Kind.SELECTION_SET, 337 + kind: 'SelectionSet' as Kind.SELECTION_SET, 336 338 selections, 337 339 }; 338 340 } ··· 347 349 ignored(); 348 350 while ((match = advance(variableRe))) { 349 351 ignored(); 350 - if (input.charCodeAt(idx++) !== 58 /*':'*/) throw error(Kind.VARIABLE_DEFINITION); 352 + if (input.charCodeAt(idx++) !== 58 /*':'*/) throw error('VariableDefinition'); 351 353 const _type = type(); 352 - if (!_type) throw error(Kind.VARIABLE_DEFINITION); 354 + if (!_type) throw error('VariableDefinition'); 353 355 let _defaultValue: ast.ValueNode | undefined; 354 356 if (input.charCodeAt(idx) === 61 /*'='*/) { 355 357 idx++; 356 358 ignored(); 357 359 _defaultValue = value(true); 358 - if (!_defaultValue) throw error(Kind.VARIABLE_DEFINITION); 360 + if (!_defaultValue) throw error('VariableDefinition'); 359 361 } 360 362 ignored(); 361 363 vars.push({ 362 - kind: Kind.VARIABLE_DEFINITION, 364 + kind: 'VariableDefinition' as Kind.VARIABLE_DEFINITION, 363 365 variable: { 364 - kind: Kind.VARIABLE, 366 + kind: 'Variable' as Kind.VARIABLE, 365 367 name: { 366 - kind: Kind.NAME, 368 + kind: 'Name' as Kind.NAME, 367 369 value: match.slice(1), 368 370 }, 369 371 }, ··· 372 374 directives: directives(true), 373 375 }); 374 376 } 375 - if (input.charCodeAt(idx++) !== 41 /*')'*/) throw error(Kind.VARIABLE_DEFINITION); 377 + if (input.charCodeAt(idx++) !== 41 /*')'*/) throw error('VariableDefinition'); 376 378 ignored(); 377 379 } 378 380 return vars; ··· 383 385 if (advance(fragmentDefinitionRe)) { 384 386 ignored(); 385 387 const _name = name(); 386 - if (!_name) throw error(Kind.FRAGMENT_DEFINITION); 388 + if (!_name) throw error('FragmentDefinition'); 387 389 ignored(); 388 390 const _typeCondition = typeCondition(); 389 - if (!_typeCondition) throw error(Kind.FRAGMENT_DEFINITION); 391 + if (!_typeCondition) throw error('FragmentDefinition'); 390 392 const _directives = directives(false); 391 393 const _selectionSet = selectionSet(); 392 - if (!_selectionSet) throw error(Kind.FRAGMENT_DEFINITION); 394 + if (!_selectionSet) throw error('FragmentDefinition'); 393 395 return { 394 - kind: Kind.FRAGMENT_DEFINITION, 396 + kind: 'FragmentDefinition' as Kind.FRAGMENT_DEFINITION, 395 397 name: _name, 396 398 typeCondition: _typeCondition, 397 399 directives: _directives, ··· 415 417 const _selectionSet = selectionSet(); 416 418 if (_selectionSet) { 417 419 return { 418 - kind: Kind.OPERATION_DEFINITION, 419 - operation: (_operation || 'query') as ast.OperationTypeNode, 420 + kind: 'OperationDefinition' as Kind.OPERATION_DEFINITION, 421 + operation: (_operation || 'query') as OperationTypeNode, 420 422 name: _name, 421 423 variableDefinitions: _variableDefinitions, 422 424 directives: _directives, ··· 426 428 } 427 429 428 430 function document(): ast.DocumentNode { 429 - let match: ast.DefinitionNode | void; 431 + let match: ast.ExecutableDefinitionNode | void; 430 432 ignored(); 431 - const definitions: ast.DefinitionNode[] = []; 433 + const definitions: ast.ExecutableDefinitionNode[] = []; 432 434 while ((match = fragmentDefinition() || operationDefinition())) definitions.push(match); 433 - if (idx !== input.length) throw error(Kind.DOCUMENT); 435 + if (idx !== input.length) throw error('Document'); 434 436 return { 435 - kind: Kind.DOCUMENT, 437 + kind: 'Document' as Kind.DOCUMENT, 436 438 definitions, 437 439 }; 438 440 } 439 441 440 - export function parse(string: string): ast.DocumentNode { 441 - input = string; 442 + type ParseOptions = { 443 + [option: string]: any; 444 + }; 445 + 446 + export function parse( 447 + string: string | Source, 448 + _options?: ParseOptions | undefined 449 + ): ast.DocumentNode { 450 + input = typeof string.body === 'string' ? string.body : string; 442 451 idx = 0; 443 452 return document(); 444 453 } 445 454 446 - export function parseValue(string: string): ast.ValueNode | undefined { 447 - input = string; 455 + export function parseValue( 456 + string: string | Source, 457 + _options?: ParseOptions | undefined 458 + ): ast.ValueNode { 459 + input = typeof string.body === 'string' ? string.body : string; 448 460 idx = 0; 449 461 ignored(); 450 - return value(false); 462 + const _value = value(false); 463 + if (!_value) throw error('ValueNode'); 464 + return _value; 451 465 } 452 466 453 - export function parseType(string: string): ast.TypeNode | undefined { 454 - input = string; 467 + export function parseType( 468 + string: string | Source, 469 + _options?: ParseOptions | undefined 470 + ): ast.TypeNode { 471 + input = typeof string.body === 'string' ? string.body : string; 455 472 idx = 0; 456 - return type(); 473 + const _type = type(); 474 + if (!_type) throw error('TypeNode'); 475 + return _type; 457 476 }
+27 -25
src/printer.ts
··· 1 - import { Kind } from './kind'; 2 1 import { ASTNode } from './ast'; 3 2 4 3 export function printString(string: string) { ··· 17 16 export function print(node: ASTNode): string { 18 17 let out: string; 19 18 switch (node.kind) { 20 - case Kind.OPERATION_DEFINITION: 19 + case 'OperationDefinition': 21 20 if ( 22 21 node.operation === 'query' && 23 22 !node.name && ··· 35 34 if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' '); 36 35 return out + ' ' + print(node.selectionSet); 37 36 38 - case Kind.VARIABLE_DEFINITION: 37 + case 'VariableDefinition': 39 38 out = print(node.variable) + ': ' + print(node.type); 40 39 if (node.defaultValue) out += ' = ' + print(node.defaultValue); 41 40 if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' '); 42 41 return out; 43 42 44 - case Kind.FIELD: 43 + case 'Field': 45 44 out = (node.alias ? print(node.alias) + ': ' : '') + node.name.value; 46 45 if (hasItems(node.arguments)) { 47 46 const args = node.arguments.map(print); ··· 54 53 if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' '); 55 54 return node.selectionSet ? out + ' ' + print(node.selectionSet) : out; 56 55 57 - case Kind.STRING: 56 + case 'StringValue': 58 57 return node.block ? printBlockString(node.value) : printString(node.value); 59 58 60 - case Kind.BOOLEAN: 59 + case 'BooleanValue': 61 60 return '' + node.value; 62 61 63 - case Kind.NULL: 62 + case 'NullValue': 64 63 return 'null'; 65 64 66 - case Kind.INT: 67 - case Kind.FLOAT: 68 - case Kind.ENUM: 69 - case Kind.NAME: 65 + case 'IntValue': 66 + case 'FloatValue': 67 + case 'EnumValue': 68 + case 'Name': 70 69 return node.value; 71 70 72 - case Kind.LIST: 71 + case 'ListValue': 73 72 return '[' + node.values.map(print).join(', ') + ']'; 74 73 75 - case Kind.OBJECT: 74 + case 'ObjectValue': 76 75 return '{' + node.fields.map(print).join(', ') + '}'; 77 76 78 - case Kind.OBJECT_FIELD: 77 + case 'ObjectField': 79 78 return node.name.value + ': ' + print(node.value); 80 79 81 - case Kind.VARIABLE: 80 + case 'Variable': 82 81 return '$' + node.name.value; 83 82 84 - case Kind.DOCUMENT: 83 + case 'Document': 85 84 return hasItems(node.definitions) ? node.definitions.map(print).join('\n\n') : ''; 86 85 87 - case Kind.SELECTION_SET: 86 + case 'SelectionSet': 88 87 return '{\n ' + node.selections.map(print).join('\n').replace(/\n/g, '\n ') + '\n}'; 89 88 90 - case Kind.ARGUMENT: 89 + case 'Argument': 91 90 return node.name.value + ': ' + print(node.value); 92 91 93 - case Kind.FRAGMENT_SPREAD: 92 + case 'FragmentSpread': 94 93 out = '...' + node.name.value; 95 94 if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' '); 96 95 return out; 97 96 98 - case Kind.INLINE_FRAGMENT: 97 + case 'InlineFragment': 99 98 out = '...'; 100 99 if (node.typeCondition) out += ' on ' + node.typeCondition.name.value; 101 100 if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' '); 102 101 return out + ' ' + print(node.selectionSet); 103 102 104 - case Kind.FRAGMENT_DEFINITION: 103 + case 'FragmentDefinition': 105 104 out = 'fragment ' + node.name.value; 106 105 out += ' on ' + node.typeCondition.name.value; 107 106 if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' '); 108 107 return out + ' ' + print(node.selectionSet); 109 108 110 - case Kind.DIRECTIVE: 109 + case 'Directive': 111 110 out = '@' + node.name.value; 112 111 if (hasItems(node.arguments)) out += '(' + node.arguments.map(print).join(', ') + ')'; 113 112 return out; 114 113 115 - case Kind.NAMED_TYPE: 114 + case 'NamedType': 116 115 return node.name.value; 117 116 118 - case Kind.LIST_TYPE: 117 + case 'ListType': 119 118 return '[' + print(node.type) + ']'; 120 119 121 - case Kind.NON_NULL_TYPE: 120 + case 'NonNullType': 122 121 return print(node.type) + '!'; 122 + 123 + default: 124 + return ''; 123 125 } 124 126 }
+198
src/schemaAst.ts
··· 1 + import type { Location } from './types'; 2 + import type { Kind, OperationTypeNode } from './kind'; 3 + 4 + import type { 5 + StringValueNode, 6 + ConstDirectiveNode, 7 + ConstValueNode, 8 + NamedTypeNode, 9 + TypeNode, 10 + NameNode, 11 + } from './ast'; 12 + 13 + /** Type System Definition */ 14 + export declare type TypeSystemDefinitionNode = 15 + | SchemaDefinitionNode 16 + | TypeDefinitionNode 17 + | DirectiveDefinitionNode; 18 + 19 + export interface SchemaDefinitionNode { 20 + readonly kind: Kind.SCHEMA_DEFINITION; 21 + readonly loc?: Location; 22 + readonly description?: StringValueNode; 23 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 24 + readonly operationTypes: ReadonlyArray<OperationTypeDefinitionNode>; 25 + } 26 + 27 + export interface OperationTypeDefinitionNode { 28 + readonly kind: Kind.OPERATION_TYPE_DEFINITION; 29 + readonly loc?: Location; 30 + readonly operation: OperationTypeNode; 31 + readonly type: NamedTypeNode; 32 + } 33 + 34 + /** Type Definition */ 35 + export declare type TypeDefinitionNode = 36 + | ScalarTypeDefinitionNode 37 + | ObjectTypeDefinitionNode 38 + | InterfaceTypeDefinitionNode 39 + | UnionTypeDefinitionNode 40 + | EnumTypeDefinitionNode 41 + | InputObjectTypeDefinitionNode; 42 + 43 + export interface ScalarTypeDefinitionNode { 44 + readonly kind: Kind.SCALAR_TYPE_DEFINITION; 45 + readonly loc?: Location; 46 + readonly description?: StringValueNode; 47 + readonly name: NameNode; 48 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 49 + } 50 + 51 + export interface ObjectTypeDefinitionNode { 52 + readonly kind: Kind.OBJECT_TYPE_DEFINITION; 53 + readonly loc?: Location; 54 + readonly description?: StringValueNode; 55 + readonly name: NameNode; 56 + readonly interfaces?: ReadonlyArray<NamedTypeNode>; 57 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 58 + readonly fields?: ReadonlyArray<FieldDefinitionNode>; 59 + } 60 + 61 + export interface FieldDefinitionNode { 62 + readonly kind: Kind.FIELD_DEFINITION; 63 + readonly loc?: Location; 64 + readonly description?: StringValueNode; 65 + readonly name: NameNode; 66 + readonly arguments?: ReadonlyArray<InputValueDefinitionNode>; 67 + readonly type: TypeNode; 68 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 69 + } 70 + 71 + export interface InputValueDefinitionNode { 72 + readonly kind: Kind.INPUT_VALUE_DEFINITION; 73 + readonly loc?: Location; 74 + readonly description?: StringValueNode; 75 + readonly name: NameNode; 76 + readonly type: TypeNode; 77 + readonly defaultValue?: ConstValueNode; 78 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 79 + } 80 + 81 + export interface InterfaceTypeDefinitionNode { 82 + readonly kind: Kind.INTERFACE_TYPE_DEFINITION; 83 + readonly loc?: Location; 84 + readonly description?: StringValueNode; 85 + readonly name: NameNode; 86 + readonly interfaces?: ReadonlyArray<NamedTypeNode>; 87 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 88 + readonly fields?: ReadonlyArray<FieldDefinitionNode>; 89 + } 90 + 91 + export interface UnionTypeDefinitionNode { 92 + readonly kind: Kind.UNION_TYPE_DEFINITION; 93 + readonly loc?: Location; 94 + readonly description?: StringValueNode; 95 + readonly name: NameNode; 96 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 97 + readonly types?: ReadonlyArray<NamedTypeNode>; 98 + } 99 + 100 + export interface EnumTypeDefinitionNode { 101 + readonly kind: Kind.ENUM_TYPE_DEFINITION; 102 + readonly loc?: Location; 103 + readonly description?: StringValueNode; 104 + readonly name: NameNode; 105 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 106 + readonly values?: ReadonlyArray<EnumValueDefinitionNode>; 107 + } 108 + 109 + export interface EnumValueDefinitionNode { 110 + readonly kind: Kind.ENUM_VALUE_DEFINITION; 111 + readonly loc?: Location; 112 + readonly description?: StringValueNode; 113 + readonly name: NameNode; 114 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 115 + } 116 + 117 + export interface InputObjectTypeDefinitionNode { 118 + readonly kind: Kind.INPUT_OBJECT_TYPE_DEFINITION; 119 + readonly loc?: Location; 120 + readonly description?: StringValueNode; 121 + readonly name: NameNode; 122 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 123 + readonly fields?: ReadonlyArray<InputValueDefinitionNode>; 124 + } 125 + /** Directive Definitions */ 126 + export interface DirectiveDefinitionNode { 127 + readonly kind: Kind.DIRECTIVE_DEFINITION; 128 + readonly loc?: Location; 129 + readonly description?: StringValueNode; 130 + readonly name: NameNode; 131 + readonly arguments?: ReadonlyArray<InputValueDefinitionNode>; 132 + readonly repeatable: boolean; 133 + readonly locations: ReadonlyArray<NameNode>; 134 + } 135 + /** Type System Extensions */ 136 + export declare type TypeSystemExtensionNode = SchemaExtensionNode | TypeExtensionNode; 137 + export interface SchemaExtensionNode { 138 + readonly kind: Kind.SCHEMA_EXTENSION; 139 + readonly loc?: Location; 140 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 141 + readonly operationTypes?: ReadonlyArray<OperationTypeDefinitionNode>; 142 + } 143 + /** Type Extensions */ 144 + export declare type TypeExtensionNode = 145 + | ScalarTypeExtensionNode 146 + | ObjectTypeExtensionNode 147 + | InterfaceTypeExtensionNode 148 + | UnionTypeExtensionNode 149 + | EnumTypeExtensionNode 150 + | InputObjectTypeExtensionNode; 151 + export interface ScalarTypeExtensionNode { 152 + readonly kind: Kind.SCALAR_TYPE_EXTENSION; 153 + readonly loc?: Location; 154 + readonly name: NameNode; 155 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 156 + } 157 + 158 + export interface ObjectTypeExtensionNode { 159 + readonly kind: Kind.OBJECT_TYPE_EXTENSION; 160 + readonly loc?: Location; 161 + readonly name: NameNode; 162 + readonly interfaces?: ReadonlyArray<NamedTypeNode>; 163 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 164 + readonly fields?: ReadonlyArray<FieldDefinitionNode>; 165 + } 166 + 167 + export interface InterfaceTypeExtensionNode { 168 + readonly kind: Kind.INTERFACE_TYPE_EXTENSION; 169 + readonly loc?: Location; 170 + readonly name: NameNode; 171 + readonly interfaces?: ReadonlyArray<NamedTypeNode>; 172 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 173 + readonly fields?: ReadonlyArray<FieldDefinitionNode>; 174 + } 175 + 176 + export interface UnionTypeExtensionNode { 177 + readonly kind: Kind.UNION_TYPE_EXTENSION; 178 + readonly loc?: Location; 179 + readonly name: NameNode; 180 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 181 + readonly types?: ReadonlyArray<NamedTypeNode>; 182 + } 183 + 184 + export interface EnumTypeExtensionNode { 185 + readonly kind: Kind.ENUM_TYPE_EXTENSION; 186 + readonly loc?: Location; 187 + readonly name: NameNode; 188 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 189 + readonly values?: ReadonlyArray<EnumValueDefinitionNode>; 190 + } 191 + 192 + export interface InputObjectTypeExtensionNode { 193 + readonly kind: Kind.INPUT_OBJECT_TYPE_EXTENSION; 194 + readonly loc?: Location; 195 + readonly name: NameNode; 196 + readonly directives?: ReadonlyArray<ConstDirectiveNode>; 197 + readonly fields?: ReadonlyArray<InputValueDefinitionNode>; 198 + }
+19
src/types.ts
··· 3 3 export interface Extensions { 4 4 [extension: string]: unknown; 5 5 } 6 + 7 + export type Source = 8 + | any 9 + | { 10 + body: string; 11 + name: string; 12 + locationOffset: { 13 + line: number; 14 + column: number; 15 + }; 16 + }; 17 + 18 + export type Location = 19 + | any 20 + | { 21 + start: number; 22 + end: number; 23 + source: Source; 24 + };
+16 -17
src/values.ts
··· 1 1 import { TypeNode, ValueNode } from './ast'; 2 - import { Kind } from './kind'; 3 2 import { Maybe } from './types'; 4 3 5 4 export function valueFromASTUntyped( ··· 7 6 variables?: Maybe<Record<string, any>> 8 7 ): unknown { 9 8 switch (node.kind) { 10 - case Kind.NULL: 9 + case 'NullValue': 11 10 return null; 12 - case Kind.INT: 11 + case 'IntValue': 13 12 return parseInt(node.value, 10); 14 - case Kind.FLOAT: 13 + case 'FloatValue': 15 14 return parseFloat(node.value); 16 - case Kind.STRING: 17 - case Kind.ENUM: 18 - case Kind.BOOLEAN: 15 + case 'StringValue': 16 + case 'EnumValue': 17 + case 'BooleanValue': 19 18 return node.value; 20 - case Kind.LIST: { 19 + case 'ListValue': { 21 20 const values: unknown[] = []; 22 21 for (const value of node.values) values.push(valueFromASTUntyped(value, variables)); 23 22 return values; 24 23 } 25 - case Kind.OBJECT: { 24 + case 'ObjectValue': { 26 25 const obj = Object.create(null); 27 26 for (const field of node.fields) 28 27 obj[field.name.value] = valueFromASTUntyped(field.value, variables); 29 28 return obj; 30 29 } 31 - case Kind.VARIABLE: 30 + case 'Variable': 32 31 return variables && variables[node.name.value]; 33 32 } 34 33 } ··· 38 37 type: TypeNode, 39 38 variables?: Maybe<Record<string, any>> 40 39 ): unknown { 41 - if (node.kind === Kind.VARIABLE) { 40 + if (node.kind === 'Variable') { 42 41 const variableName = node.name.value; 43 42 return variables ? valueFromTypeNode(variables[variableName], type, variables) : undefined; 44 - } else if (type.kind === Kind.NON_NULL_TYPE) { 45 - return node.kind !== Kind.NULL ? valueFromTypeNode(node, type, variables) : undefined; 46 - } else if (node.kind === Kind.NULL) { 43 + } else if (type.kind === 'NonNullType') { 44 + return node.kind !== 'NullValue' ? valueFromTypeNode(node, type, variables) : undefined; 45 + } else if (node.kind === 'NullValue') { 47 46 return null; 48 - } else if (type.kind === Kind.LIST_TYPE) { 49 - if (node.kind === Kind.LIST) { 47 + } else if (type.kind === 'ListType') { 48 + if (node.kind === 'ListValue') { 50 49 const values: unknown[] = []; 51 50 for (const value of node.values) { 52 51 const coerced = valueFromTypeNode(value, type.type, variables); ··· 58 57 } 59 58 return values; 60 59 } 61 - } else if (type.kind === Kind.NAMED_TYPE) { 60 + } else if (type.kind === 'NamedType') { 62 61 switch (type.name.value) { 63 62 case 'Int': 64 63 case 'Float':
+3 -3
tsconfig.json
··· 5 5 "noEmit": true, 6 6 "esModuleInterop": true, 7 7 "noUnusedLocals": true, 8 - "rootDir": "./src", 8 + "rootDir": ".", 9 9 "baseUrl": ".", 10 - "outDir": "dist/cjs", 10 + "allowJs": true, 11 11 "lib": ["dom", "esnext"], 12 12 "jsx": "react", 13 13 "declaration": false, ··· 20 20 "skipLibCheck": true, 21 21 "isolatedModules": true 22 22 }, 23 - "include": ["src"] 23 + "include": ["src", "typings"] 24 24 }
+18
typings/document.ts
··· 1 + import type * as graphql from 'graphql'; 2 + import type * as graphqlWeb from '../src/index'; 3 + 4 + export function documentInput(input: graphqlWeb.DocumentNode): graphql.DocumentNode { 5 + return input; 6 + } 7 + 8 + export function documentOutput(input: graphql.DocumentNode): graphqlWeb.DocumentNode { 9 + return input; 10 + } 11 + 12 + export function nodeInput(input: graphqlWeb.ASTNode): graphql.ASTNode { 13 + return input; 14 + } 15 + 16 + export function nodeOutput(input: graphql.ASTNode): graphqlWeb.ASTNode { 17 + return input; 18 + }
+10
typings/error.ts
··· 1 + import type * as graphql from 'graphql'; 2 + import type * as graphqlWeb from '../src/index'; 3 + 4 + export function errorInput(input: graphqlWeb.GraphQLError): graphql.GraphQLError { 5 + return input; 6 + } 7 + 8 + export function errorOutput(input: graphql.GraphQLError): graphqlWeb.GraphQLError { 9 + return input; 10 + }
+18
typings/parser.ts
··· 1 + import type * as graphql from 'graphql'; 2 + import type * as graphqlWeb from '../src/index'; 3 + 4 + export function parseInput(input: typeof graphqlWeb.parse): typeof graphql.parse { 5 + return input; 6 + } 7 + 8 + export function parseOutput(input: typeof graphql.parse): typeof graphqlWeb.parse { 9 + return input; 10 + } 11 + 12 + export function parseValueInput(input: typeof graphqlWeb.parseValue): typeof graphql.parseValue { 13 + return input; 14 + } 15 + 16 + export function parseValueOutput(input: typeof graphql.parseValue): typeof graphqlWeb.parseValue { 17 + return input; 18 + }
+10
typings/visitor.ts
··· 1 + import type * as graphql from 'graphql'; 2 + import type * as graphqlWeb from '../src/index'; 3 + 4 + export function visitInput(input: typeof graphqlWeb.visit): typeof graphql.visit { 5 + return input; 6 + } 7 + 8 + export function visitOutput(input: typeof graphql.visit): typeof graphqlWeb.visit { 9 + return input; 10 + }