Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

groq-js

sanity-io561.8kMIT1.14.2TypeScript support: included

npm stat npm version [![gzip size][

sanity, json, query, groq

readme

GROQ-JS

npm stat npm version gzip size size

GROQ-JS is a JavaScript implementation of GROQ which follows the official specification.

import {parse, evaluate} from 'groq-js'

let input = '*[_type == "user"]{name}'

// Returns an ESTree-inspired syntax tree
let tree = parse(input)

let dataset = [
  {_type: 'user', name: 'Michael'},
  {_type: 'company', name: 'Bluth Company'},
]

// Evaluate a tree against a dataset
let value = await evaluate(tree, {dataset})

// Gather everything into one JavaScript object
let result = await value.get()

console.log(result)

Table of contents:

Installation

npm i groq-js
yarn add groq-js
pnpm install groq-js

Documentation

See API.md for the public API.

Learn GROQ

Free egghead GROQ introduction course by John Lindquist

Versioning

GROQ

The GROQ spec version is independent of the groq-js library version. When you import groq-js you need to be explicit on which GROQ version you want to use. The GROQ version is tied to the groq-spec. This allows us to update the library and its API independent of the GROQ version.

GROQ-JS

GROQ-JS follows SemVer. See the changelog for recent changes. This is an "experimental" release and anything may change at any time, but we're trying to keep changes as minimal as possible:

  • The public API of the parser/evaluator will most likely stay the same in future versions.
  • The syntax tree is not considered a public API and may change at any time.
  • This package always implements the latest version of GROQ according to the specification.

Releasing a new version of GROQ-JS

Run the "CI & Release" workflow. Make sure to select the main branch and check "Release new version".

Version will be automatically bumped based on conventional commits since the last release.

Semantic release will only release on configured branches, so it is safe to run release on any branch.

Note: commits with chore: will be ignored. If you want updated dependencies to trigger a new version, use fix(deps): instead.

License

MIT © Sanity.io

Tests

Tests are written in Jest:

# Install dependencies
npm i

# Run tests
npm test

You can also generate tests from the official GROQ test suite:

# Fetch and generate test file:
./test/generate.sh

# Run tests as usual:
npm test

You can generate tests from a specific version:

GROQTEST_SUITE_VERSION=v1.0.0 ./test/generate.sh

or from a file (as generated by the test suite):

GROQTEST_SUITE=suite.ndjson ./test/generate.sh

The test arguments are passed to tap, so you can use arguments, e.g. to run a specific set of tests:

npm test -g "array::join"

changelog

📓 Changelog

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.14.2 (2024-12-09)

Bug Fixes

  • ensure coalesce handles optional/unknown/null properly (610e21e)

1.14.1 (2024-11-28)

Bug Fixes

  • pipe functions in projections (40e6c25)

1.14.0 (2024-11-04)

Features

  • add array intersects function (ced9e90)
  • can extract attribute key from Group (46ba490)
  • typeEvaluator: implement proper support for defined in filters (d319f1c)
  • upgrade groq-test-suite version to v0.1.46 (a71e3ec)

Bug Fixes

  • functions: fix typo mapConcrete to mapNode (7f57a91)

1.13.0 (2024-08-26)

Features

  • add new GROQ function sanity::documentsOf (c086187)
  • typeEvaluator: do not let unknown always resolve to unknown (41679a3)
  • typeEvaluator: remove resolve condition, use walk to determine conditions (38ca510)

1.12.0 (2024-07-26)

Features

  • typeEvaluator: add support for global::dateTime (8ed40ea)
  • typeEvaluator: add support for global::length (a244bdc)

1.11.1 (2024-07-25)

Bug Fixes

  • typeEvaluator: fix bug when splatting over optional attributes (92d2449)

1.11.0 (2024-07-23)

Features

  • add support for sanity::versionOf() (fb2f5d0)
  • add type evaluator for sanity::versionOf() (db54be4)
  • typeEvaluator: validate if arg to sanity::versionOf is string (0f4d047)

1.10.0 (2024-07-02)

Features

  • typeEvaluate: preserve value when casting string upper/lower case (fc189a0)
  • typeEvaluator: add support for array functions (86b1053)
  • typeEvaluator: add support for dateTime::now() (9ad8200)
  • typeEvaluator: add support for global::now() (d8cf2d9)
  • typeEvaluator: add support for round() (641ecc6)
  • typeEvaluator: add support for upper() and lower() (0934a3c)
  • typeEvaluator: expand all unions inside a object (1a5106d)

Bug Fixes

  • typeEvaluator: bail early if splatting over a non-union/object (117ec01)
  • typeEvaluator: handle access{attribute,element) when resolving conditions (313369c)
  • typeEvaluator: handle not nodes when resolving conditions (c940472)
  • typeEvaluator: handle rest on object splat operations (2ce28eb)
  • typeEvaluator: make sure we are not inside a hidden scope when resolving parent nodes (f109098)
  • typeEvaluator: map over concrete values and return them as alternatives (f3adc53)
  • typeEvaluator: move object splat into separate methods to ensure conrete mapping (8f1a8d9)
  • typeEvaluator: properly handle object splats with non-concrete types (97aee3e)
  • typeEvaluator: return undefined by default when resolving condition (bfb3d5e)

1.9.0 (2024-05-14)

Features

  • typeEvaluator: add support for global::references (af958db)
  • typeEvaluator: add support for string::split (dd823dd)
  • typeEvaluator: add support for string::startsWith (6b51180)

Bug Fixes

  • typeEvaluator: update hash group to capture start/end (531fe81)

1.8.0 (2024-04-24)

Features

  • typeEvaluation: add support for global::string (133d8af)
  • typeEvaluator: add support for count (49e05c7)

Bug Fixes

  • loosen strictness for equality with unions (04b4b36)
  • typeEvaluation: map over unions to support filtering null-unions (6ad2398)
  • types: use the strictest tsconfig preset (#222) (8a83812)

1.7.0 (2024-04-02)

Features

  • typeEvaluation: let optional resolved condition be evaulated (91ef6b5)

1.6.1 (2024-04-02)

Bug Fixes

  • remove typeEvaluator typesVersions export (#207) (80bb8cb)

1.6.0 (2024-04-02)

Features

  • type-evaluation: add support for Pos and Neg nodes (bef4ad5)

Bug Fixes

  • Exports should be exports (5cea533)
  • deps: bump @sanity/pkg-utils to v5.1.5 (d2e8f6c)
  • remove legacy typings field, as types is already used (7c05d5e)
  • test ecospark push override (1e4b895)

1.5.0 (2024-03-19)

Features

  • access optinal attributes should return null (8e3f7e4)
  • add support for slice in type evaluator (7877990)
  • add tsdoc to describe the type nodes (65cbc41)
  • add type evaluator (637603b)
  • evaluateQueryType: add more tests (#180) (5d2528e)
  • export type evaluation with version 1 (62c6b7f)
  • only export typeEvaluate function (e3e3875)
  • replace esbuild with tsx. fixes linenumber (8a60c87)
  • typeEvaluator: rename main function to typeEvaluate (9c2f345)

Bug Fixes

  • dont access attributes inside arrays (0e267bc)
  • dont stringify objects, let debug/util handle it (bb7d646)
  • forward type on map unexpected (8131a71)
  • handle flatmap over unions correctly (70dc0c3)
  • order unions (5a940e0)
  • query node type eval tests (522bd9b)
  • recursively lookup attributes in object rest (fecc1a1)
  • refactor and reuse scope handling from evaluator (7bed827)
  • remove un-evaluated Slice-case (349645b)
  • typeEvaluate: resolve inline when mapping type (7a3742c)
  • update snapshots (0860eaf)
  • various type evaluation bugs (3da42d7)

1.4.3 (2024-02-21)

Bug Fixes

  • deps: update dependency @sanity/pkg-utils to ^4.2.8 (#174) (5d353eb)

1.4.2 (2024-02-21)

Bug Fixes

1.4.1 (2024-01-25)

Bug Fixes

  • deps: update dependency @sanity/pkg-utils to v4 (#163) (065f28f)

1.4.0 (2024-01-18)

Features

1.3.0 (2023-09-08)

Features

Bug Fixes

1.2.0 (2023-08-23)

Features

  • add option to provide a custom dereference function (#98) (7e5c789)

1.1.12 (2023-08-10)

Bug Fixes

  • handle parenthesis directly inside filters (abe94a9)

1.1.11 (2023-08-07)

Bug Fixes

  • add node.module condition for bundlers (67365ac)

1.1.10 (2023-08-04)

Bug Fixes

1.1.9 (2023-04-13)

Bug Fixes

  • package.json: add repository field (3638394)

1.1.8 (2023-03-06)

Bug Fixes

  • deps: update devdependencies (non-major) (#100) (39dc24b)

1.1.7 (2023-02-14)

Bug Fixes

  • use pkg-utils to generate JS and typings (195352f)

1.1.6 (2023-01-18)

Bug Fixes

  • namespaced functions should consume any whitespace before function arguments (481e88d)

1.1.5 (2023-01-12)

Bug Fixes

  • add API.md to npm publish (e92bb78)

1.1.4 (2023-01-12)

Bug Fixes

1.1.3 (2023-01-12)

Bug Fixes

1.1.2 (2023-01-12)

Bug Fixes

  • add sideEffects: false for better tree-shaking (77b3366)
  • setup release automation (f676dff)
  • support swcMinify in NextJS 13 (9beabc1)

v1.0.0-rc - 2022-07-28

New features:

  • Implement array::unique()
  • Implement aggregation functions on the math namespace:
    • min()
    • max()
    • sum()
    • avg()
  • Implement functions on the string namespace:
    • startsWith()
    • split()
  • Implement functions on the array namespace:
    • join()
    • compact()
  • Introduce support for using specific GROQ versions
  • Introduce validation for selector syntax

Tooling changes:

  • Support local test via $GROQTEST_SUITE

v0.4.0-beta.2 - 2022-03-21

Bug fixes

  • Correctly accept function calls with trailing commas: select(123,).
  • Correctly parse object expressions which start with literal strings (e.g. {"mail" == … => …}).
  • Correctly accept comments with no text (// on a single line).

v0.4.0-beta.1

Bug fixes:

  • Fix error ReferenceError: marks is not defined which happened while parsing strings in strict mode.

v0.4.0-beta.0 - 2021-12-14

New features:

  • Support specifying now() and identity() during evaluation.
  • Support for validating Delta GROQ.

Backwards compatible API changes:

  • Introduce params and mode option on parse.
  • Introduce before and after option on evaluate.
  • The main file is now an UMD file usable in the browser, exported under groqJS.

Private changes:

  • Rewrite parser, optimizing for performance.

Tooling changes:

  • Use Vite and esbuild instead of TSDX.
  • Switch completely over from Jest to Tap for running test.

v0.3.0 - 2021-06-25

API changes:

  • Introduce params parameter on parse which is required to properly parse an expression (@judofyr)

GROQ compatibility fixes:

  • Preserve nulls in objects (@judofyr, #36)
  • Add support for arrays in length (@judofyr)
  • Use stable sorting in order (@judofyr)
  • Implement string function (@judofyr, @israelroldan)
  • Add support for arrays in references (@judofyr)
  • Implement dateTime (@judofyr)
  • Implement proper array traversal (@judofyr)
  • Implement score function (@judofyr)
  • Implement namespaced functions (@judofyr)
  • Implement lower and upper (@judofyr)
  • Implement pt::text (@judofyr)
  • Improve match behavior (@judofyr)

Private changes:

  • Simplify and restructure AST (@judofyr)
  • Split up SyntaxNode into a special ExprNode represent what's returned from parse (@judofyr)

Tooling changes:

  • Use tap for running tests since Jest is too slow (@judofyr)

v0.2.0 - 2020-10-20

  • Require native generators (@rexxars)
  • Support concatenation of arrays (@rexxars)
  • Correctly fail on invalid asc/desc (@judofyr)
  • Projection should not be mapped inside traversals (@judofyr)
  • getType: Detect Pair correctly (@judofyr)
  • Add support for object merging (@judofyr)
  • Improve match semantics (@judofyr)
  • Fix ESM build (@rexxars)

v0.1.8 - 2020-09-27

  • Upgrade dependencies (@rexxars)

v0.1.7 - 2020-06-15

  • Fix build so that it includes rawParser (@judofyr)
  • Work around bug in babel-plugin-transform-async-to-promises (@judofyr)

v0.1.6 - 2020-06-15

  • Migrate to TypeScript (#18, @rexxars, @judofyr)
  • Update semantics to work with latest GROQ spec

v0.1.5 - 2020-03-17

  • Add LICENSE.md
  • Add license-field in package.json
  • Fix crash during dereferencing when the dataset is non-array
  • Implement now() function (#16, @rexxars)

v0.1.4 - 2019-10-15

  • Add custom syntax error object (#10, @asbjornh)

v0.1.3 - 2019-09-17

  • Implement path() function (#7)

v0.1.2 - 2019-09-12

  • Add support for parameters (#6)

v0.1.1 - 2019-09-10

  • Add support for escape sequences in string literals

v0.1.0 - 2019-09-09

  • Big rewrite with much improved support

v0.0.1 - 2019-07-10

  • Initial hacky version