Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type evaluator: Add support for delta mode queries #276

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/typeEvaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ export function handleFuncCallNode(node: FuncCallNode, scope: Scope): TypeNode {
return {type: 'array', of: {type: 'string'}}
})
}
case 'delta.changedAny':
case 'delta.changedOnly': {
const selector = walk({node: node.args[0], scope})
if (selector.type === 'null') {
return {type: 'boolean', value: false}
}

if (selector.type === 'unknown') {
return {type: 'unknown'}
}

return {type: 'boolean'}
}
default: {
return {type: 'unknown'}
}
Expand Down
17 changes: 16 additions & 1 deletion src/typeEvaluator/typeEvaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
AndNode,
ArrayCoerceNode,
ArrayNode,
ContextNode,
DerefNode,
EverythingNode,
ExprNode,
Expand Down Expand Up @@ -1072,6 +1073,18 @@ function handleOrNode(node: OrNode, scope: Scope): TypeNode {
)
}

function handleContextNode(node: ContextNode, scope: Scope): TypeNode {
switch (node.key) {
case 'before':
case 'after': {
return scope.value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit off. before() and after() shouldn't refer to scope. It just uses the context: https://spec.groq.dev/draft/#global_before(). We can maybe assume that it's an element of schema, although technically we should probably rather introduce a new option to typeEvaluate which lets you specify the type of this value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not retuning the scope, it's returning the current value that's in the scope. Which is the current schema that hasn't been filtered out

}
default: {
return {type: 'unknown'}
}
}
}

const OVERRIDE_TYPE_SYMBOL = Symbol('groq-js.type')

/**
Expand Down Expand Up @@ -1193,10 +1206,12 @@ export function walk({node, scope}: {node: ExprNode; scope: Scope}): TypeNode {
case 'Pos': {
return handlePosNode(node, scope)
}
case 'Context': {
return handleContextNode(node, scope)
}
// everything else
case 'Asc':
case 'Desc':
case 'Context':
case 'Tuple':
case 'Selector':
case 'InRange': {
Expand Down
4 changes: 4 additions & 0 deletions src/typeEvaluator/typeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ export function isFuncCall(node: ExprNode, name: string): boolean {

return node.type === 'FuncCall' && `${node.namespace}::${node.name}` === name
}

export function arrayOf<T extends TypeNode = TypeNode>(of: T): ArrayTypeNode<T> {
return {type: 'array', of}
}
42 changes: 41 additions & 1 deletion test/typeEvaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import t from 'tap'

import {parse} from '../src/parser'
import {typeEvaluate} from '../src/typeEvaluator/typeEvaluate'
import {createReferenceTypeNode, nullUnion, unionOf} from '../src/typeEvaluator/typeHelpers'
import {
arrayOf,
createReferenceTypeNode,
nullUnion,
unionOf,
} from '../src/typeEvaluator/typeHelpers'
import type {
ArrayTypeNode,
Document,
Expand Down Expand Up @@ -3328,6 +3333,41 @@ t.test('function: sanity::partOfRelease', (t) => {
t.end()
})

t.test('delta mode', (t) => {
const variants = [
{
query: `*[_type == "author" && before()._type == "other"]`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really how before() should be used though. It's technically a correct example, but it doesn't use the scope, but rather refers to the before/after values in the context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we operate on the types and not values. The only case where we can know the value if it's a string literal. I get that comparing _type like this is not realistic per se, but I wanted to test wether it would exclude all types, practically the same as _type == "author" && _type == "other" which would always return an empty set.

expect: arrayOf(unionOf()),
},
{
query: `*[_type == "author" && after()._type == "other"]`,
expect: arrayOf(unionOf()),
},
{
query: `*[_type == "author" && after().name == "other"]`,
expect: arrayOf({type: 'object', attributes: authorDocument.attributes}),
},
{
query: `*[_type == "author" && before().name == "other"]`,
expect: arrayOf({type: 'object', attributes: authorDocument.attributes}),
},
{
query: `*[_type == "author" && delta::changedOnly(name)]`,
expect: arrayOf({type: 'object', attributes: authorDocument.attributes}),
},
{
query: `*[_type == "author" && delta::changedOnly(missing)]`,
expect: arrayOf(unionOf()),
},
] as const
for (const {query, expect} of variants) {
const ast = parse(query, {mode: 'delta'})
const res = typeEvaluate(ast, schemas)
t.strictSame(res, expect)
}
t.end()
})

function findSchemaType(name: string): TypeNode {
const type = schemas.find((s) => s.name === name)
if (!type) {
Expand Down