-
Notifications
You must be signed in to change notification settings - Fork 24
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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"]`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't really how There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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) { | ||
|
There was a problem hiding this comment.
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()
andafter()
shouldn't refer toscope
. It just uses the context: https://spec.groq.dev/draft/#global_before(). We can maybe assume that it's an element ofschema
, although technically we should probably rather introduce a new option totypeEvaluate
which lets you specify the type of this value.There was a problem hiding this comment.
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