@sanity/client 6.1.4 → 6.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -65,6 +65,9 @@ export async function updateDocumentTitle(_id, title) {
65
65
  - [UMD](#umd)
66
66
  - [Specifying API version](#specifying-api-version)
67
67
  - [Performing queries](#performing-queries)
68
+ - [Using perspectives](#using-perspectives)
69
+ - [`published`](#published)
70
+ - [`previewDrafts`](#previewdrafts)
68
71
  - [Fetching Content Source Maps](#fetching-content-source-maps)
69
72
  - [Listening to queries](#listening-to-queries)
70
73
  - [Fetch a single document](#fetch-a-single-document)
@@ -405,6 +408,162 @@ client.fetch(query, params).then((bikes) => {
405
408
 
406
409
  Perform a query using the given parameters (if any).
407
410
 
411
+ ### Using perspectives
412
+
413
+ The `perspective` option can be used to specify special filtering behavior for queries. The default value is `raw`, which means no special filtering is applied, while [`published`](#published) and [`previewDrafts`](#previewdrafts) can be used to optimize for specific use cases.
414
+
415
+ #### `published`
416
+
417
+ Useful for when you want to be sure that draft documents are not returned in production. Pairs well with private datasets.
418
+
419
+ With a dataset that looks like this:
420
+
421
+ ```json
422
+ [
423
+ {
424
+ "_type": "author",
425
+ "_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
426
+ "name": "George Martin"
427
+ },
428
+ {
429
+ "_type": "author",
430
+ "_id": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
431
+ "name": "George R.R. Martin"
432
+ },
433
+ {
434
+ "_type": "author",
435
+ "_id": "drafts.f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
436
+ "name": "Stephen King"
437
+ }
438
+ ]
439
+ ```
440
+
441
+ And a query like this:
442
+
443
+ ```ts
444
+ import {createClient} from '@sanity/client'
445
+
446
+ const client = createClient({
447
+ ...config,
448
+ useCdn: true, // set to `false` to bypass the edge cache
449
+ perspective: 'published',
450
+ })
451
+
452
+ const authors = await client.fetch('*[_type == "author"]')
453
+ ```
454
+
455
+ Then `authors` will only contain documents that don't have a `drafts.` prefix in their `_id`, in this case just "George Martin":
456
+
457
+ ```json
458
+ [
459
+ {
460
+ "_type": "author",
461
+ "_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
462
+ "name": "George Martin"
463
+ }
464
+ ]
465
+ ```
466
+
467
+ #### `previewDrafts`
468
+
469
+ Designed to help answer the question "What is our app going to look like after all the draft documents are published?".
470
+
471
+ Given a dataset like this:
472
+
473
+ ```json
474
+ [
475
+ {
476
+ "_type": "author",
477
+ "_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
478
+ "name": "George Martin"
479
+ },
480
+ {
481
+ "_type": "author",
482
+ "_id": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
483
+ "name": "George R.R. Martin"
484
+ },
485
+ {
486
+ "_type": "author",
487
+ "_id": "drafts.f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
488
+ "name": "Stephen King"
489
+ },
490
+ {
491
+ "_type": "author",
492
+ "_id": "6b3792d2-a9e8-4c79-9982-c7e89f2d1e75",
493
+ "name": "Terry Pratchett"
494
+ }
495
+ ]
496
+ ```
497
+
498
+ And a query like this:
499
+
500
+ ```ts
501
+ import {createClient} from '@sanity/client'
502
+
503
+ const client = createClient({
504
+ ...config,
505
+ useCdn: false, // the `previewDrafts` perspective requires this to be `false`
506
+ perspective: 'previewDrafts',
507
+ })
508
+
509
+ const authors = await client.fetch('*[_type == "author"]')
510
+ ```
511
+
512
+ Then `authors` will look like this. Note that the result dedupes documents with a preference for the draft version:
513
+
514
+ ```json
515
+ [
516
+ {
517
+ "_type": "author",
518
+ "_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
519
+ "_originalId": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
520
+ "name": "George R.R. Martin"
521
+ },
522
+ {
523
+ "_type": "author",
524
+ "_id": "f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
525
+ "_originalId": "drafts.f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
526
+ "name": "Stephen King"
527
+ },
528
+ {
529
+ "_type": "author",
530
+ "_id": "6b3792d2-a9e8-4c79-9982-c7e89f2d1e75",
531
+ "_originalId": "6b3792d2-a9e8-4c79-9982-c7e89f2d1e75",
532
+ "name": "Terry Pratchett"
533
+ }
534
+ ]
535
+ ```
536
+
537
+ Since the query simulates what the result will be after publishing the drafts, the `_id` doesn't contain the `drafts.` prefix. If you want to check if a document is a draft or not you can use the `_originalId` field, which is only available when using the `previewDrafts` perspective.
538
+
539
+ ```ts
540
+ const authors = await client.fetch(`*[_type == "author"]{..., "status": select(
541
+ _originalId in path("drafts.**") => "draft",
542
+ "published"
543
+ )}`)
544
+ ```
545
+
546
+ Which changes the result to be:
547
+
548
+ ```json
549
+ [
550
+ {
551
+ "_type": "author",
552
+ "_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
553
+ "_originalId": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
554
+ "name": "George R.R. Martin",
555
+ "status": "draft"
556
+ },
557
+ {
558
+ "_type": "author",
559
+ "_id": "f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
560
+ "_originalId": "f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
561
+ "name": "Stephen King",
562
+ "status": "published"
563
+ }
564
+ ]
565
+ ```
566
+
408
567
  ### Fetching Content Source Maps
409
568
 
410
569
  Content Source Maps annotate fragments in your query results with metadata about its origin: the field, document, and dataset it originated from.
@@ -878,7 +878,7 @@ function _requestObservable(client, httpRequest, options) {
878
878
  ...options.query
879
879
  };
880
880
  }
881
- if (["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/query/") === 0) {
881
+ if (["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/query/") === 0) {
882
882
  if (config.resultSourceMap) {
883
883
  options.query = {
884
884
  resultSourceMap: true,