Skip to content

Commit

Permalink
feat: only export typeEvaluate function
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed Mar 5, 2024
1 parent 6e0b195 commit e3e3875
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/typeEvaluator/functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {FuncCallNode} from '../nodeTypes'
import {walk} from './evaluateQueryType'
import {Scope} from './scope'
import {walk} from './typeEvaluate'
import {NullTypeNode, TypeNode} from './types'

function unionWithoutNull(unionTypeNode: TypeNode): TypeNode {
Expand Down
2 changes: 1 addition & 1 deletion src/typeEvaluator/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {typeEvaluate as evaluateNodeType, evaluateQueryType} from './evaluateQueryType'
export {typeEvaluate} from './typeEvaluate'
// @internal
export {createReferenceTypeNode} from './typeHelpers'
export type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import type {
SelectNode,
ValueNode,
} from '../nodeTypes'
import {parse} from '../parser'
import {handleFuncCallNode} from './functions'
import {optimizeUnions} from './optimizations'
import {Context, Scope} from './scope'
Expand Down Expand Up @@ -54,38 +53,27 @@ const $debug = debug('typeEvaluator:evaluate::debug')
$debug.log = console.log.bind(console) // eslint-disable-line no-console
const $warn = debug('typeEvaluator:evaluate::warn')

/**
* Evaluates the type of a query and schema.
*
* @param ast - The query ast to evaluate.
* @param schema - The schemas to use for type evaluation.
* @returns The type of the query.
* @beta
*/
export function typeEvaluate(ast: ExprNode, schema: Schema): TypeNode {
const parsed = walk({
node: ast,
scope: new Scope([], undefined, new Context(schema)),
})

$trace('evaluateQueryType.parsed %O', parsed)

const optimized = optimizeUnions(parsed)
$trace('evaluateQueryType.optimized %O', optimized)

return optimized
}

/**
* Evaluates the type of a query and schema.
*
* @param query - The query string to evaluate.
* @param schema - The schemas to use for type evaluation.
* @returns The type of the query.
* @throws Error if the query is empty or can't be parsed.
*/
export function evaluateQueryType(query: string, schema: Schema): TypeNode {
if (query === '') {
throw new Error(`query can't be empty`)
}

const ast = parse(query)
$debug('evaluateQueryType.ast %O', ast)
return typeEvaluate(ast, schema)
}

function mapDeref(base: TypeNode, scope: Scope): TypeNode {
if (base.type === 'union') {
return {
Expand Down Expand Up @@ -683,6 +671,7 @@ const OVERRIDE_TYPE_SYMBOL = Symbol('groq-js.type')
* `overrideTypeForNode` overrides the inferred type for a specific node: The
* type evaluator will ignore its built-in logic and instead _always_ return
* this type. This is intended to be used for testing.
* @internal - This is only exported for testing purposes.
*/
export function overrideTypeForNode(node: ExprNode, type: TypeNode): void {
;(node as any)[OVERRIDE_TYPE_SYMBOL] = type
Expand All @@ -694,6 +683,7 @@ export function overrideTypeForNode(node: ExprNode, type: TypeNode): void {
* @param node - The AST node to evaluate.
* @param scope - The current scope.
* @returns The evaluated type of the node.
* @internal
*/
// eslint-disable-next-line complexity
export function walk({node, scope}: {node: ExprNode; scope: Scope}): TypeNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/evaluateQueryType.test.ts TAP coalesce only > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP coalesce only > must match snapshot 1`] = `
Object {
"of": Object {
"attributes": Object {
Expand Down Expand Up @@ -53,7 +53,7 @@ Object {
}
`

exports[`test/evaluateQueryType.test.ts TAP coalesce with projection > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP coalesce with projection > must match snapshot 1`] = `
Object {
"of": Array [
Object {
Expand Down Expand Up @@ -104,7 +104,7 @@ Object {
}
`

exports[`test/evaluateQueryType.test.ts TAP complex 2 > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP complex 2 > must match snapshot 1`] = `
Object {
"of": Object {
"attributes": Object {
Expand Down Expand Up @@ -223,7 +223,7 @@ Object {
}
`

exports[`test/evaluateQueryType.test.ts TAP complex > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP complex > must match snapshot 1`] = `
Object {
"of": Object {
"attributes": Object {
Expand Down Expand Up @@ -555,7 +555,7 @@ Object {
}
`

exports[`test/evaluateQueryType.test.ts TAP filter order doesnt matter > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP filter order doesnt matter > must match snapshot 1`] = `
Object {
"of": Object {
"attributes": Object {
Expand Down Expand Up @@ -638,7 +638,7 @@ Object {
}
`

exports[`test/evaluateQueryType.test.ts TAP flatmap > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP flatmap > must match snapshot 1`] = `
Object {
"of": Object {
"of": Array [
Expand Down Expand Up @@ -713,7 +713,7 @@ Object {
}
`

exports[`test/evaluateQueryType.test.ts TAP misc > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP misc > must match snapshot 1`] = `
Object {
"of": Object {
"attributes": Object {
Expand Down Expand Up @@ -763,7 +763,7 @@ Object {
}
`

exports[`test/evaluateQueryType.test.ts TAP object references > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP object references > must match snapshot 1`] = `
Object {
"of": Object {
"attributes": Object {
Expand Down Expand Up @@ -830,7 +830,7 @@ Object {
}
`

exports[`test/evaluateQueryType.test.ts TAP with conditional splat > must match snapshot 1`] = `
exports[`test/typeEvaluate.test.ts TAP with conditional splat > must match snapshot 1`] = `
Object {
"attributes": Object {
"not match": Object {
Expand Down
Loading

0 comments on commit e3e3875

Please sign in to comment.