Skip to content

feat(views): implement dev mode #1086

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

Merged
merged 2 commits into from
Jun 16, 2025
Merged

Conversation

danielgroves
Copy link

@danielgroves danielgroves commented May 21, 2025

Add dev mode to views client:

Outbound requests need to look something like this:

POST /v2025-01-01/views/my-view-id/emulate?returnQuery=false HTTP/1.1
Host: apicdn.sanity.url
Content-Type: application/json
Accept: application/json

{
  "query": "*[_type == \"article\" && category == $cat]",
  "params": {
    "cat": "technology"
  },
  "connections": [
    {
      "query": "*[_type == \"document\"]",
      "resourceType": "dataset",
      "resourceId": "project123.dataset456"
    }
  ]
}

We make the request by calling the views client like this:

import { createViewClient, ViewResourceType } from '@sanity/client/views'

const client = createViewClient({
  apiVersion: '2025-01-01',
  viewOverrides: [
    {
      id: 'my-view-id',
      connections: [
        {
          query: '*[_type == "document"]',
          resourceType: ViewResourceType.Dataset,
          resourceId: 'project123.dataset456'
        }
      ]
    }
  ]
})

// This automatically uses the emulate endpoint because dataset connections are detected
const result = await client.fetch(
  'my-view-id', 
  '*[_type == "article" && category == $cat]',
  { cat: 'technology' }
)

Copy link

vercel bot commented May 21, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
tsdocs-client ⬜️ Ignored (Inspect) Jun 16, 2025 10:42am

Copy link
Contributor

github-actions bot commented May 21, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 88.3% 3671 / 4157
🔵 Statements 88.3% 3671 / 4157
🔵 Functions 88.01% 301 / 342
🔵 Branches 89.68% 1078 / 1202
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/SanityClient.ts 65.65% 97.95% 57.44% 65.65% 108-109, 140-151, 212-224, 233-237, 249-253, 311-328, 386-402, 460-471, 558-585, 692-703, 734-747, 831-856, 880-892, 950-961, 998-999, 1007-1010, 1019-1023, 1041-1042, 1051-1052, 2060-2063
src/config.ts 88.57% 88.57% 100% 88.57% 27-28, 53, 69-71, 96-97, 99-100, 104, 106-109, 138
src/types.ts 100% 100% 100% 100%
src/data/dataMethods.ts 98.18% 95.37% 100% 98.18% 113, 481, 690-692, 730-731, 738-739, 756
src/datasets/DatasetsClient.ts 85.33% 100% 69.23% 85.33% 24-25, 34-35, 43-44, 50-54
src/projects/ProjectsClient.ts 77.27% 100% 75% 77.27% 27-31, 39-43
src/releases/ReleasesClient.ts 100% 100% 100% 100%
src/users/UsersClient.ts 80% 100% 83.33% 80% 22-29
src/views/index.ts 100% 92.85% 100% 100%
Generated in workflow #3479 for commit 096e554 by the Vitest Coverage Report Action

@danielgroves danielgroves force-pushed the poc/views-support-fetch branch from 6dcb406 to db80b4a Compare May 21, 2025 14:23
@danielgroves danielgroves force-pushed the poc/views-support-dev branch 2 times, most recently from 3208f8f to 76d0229 Compare May 21, 2025 15:46
Base automatically changed from poc/views-support-fetch to poc/views-support May 27, 2025 12:51
@danielgroves danielgroves force-pushed the poc/views-support-dev branch 3 times, most recently from 880ef78 to 9355865 Compare June 6, 2025 15:35
@danielgroves danielgroves changed the title feat(views): define override interface feat(views): implement dev mode Jun 6, 2025
@danielgroves danielgroves force-pushed the poc/views-support-dev branch 4 times, most recently from f9ba967 to 06e9206 Compare June 11, 2025 16:23
when overrrides are set switch to the emulate endpoint
@danielgroves danielgroves force-pushed the poc/views-support-dev branch from 06e9206 to fac01df Compare June 11, 2025 16:28
@danielgroves danielgroves requested review from mwain and a team June 11, 2025 16:33
@danielgroves danielgroves marked this pull request as ready for review June 11, 2025 17:06
@@ -1228,11 +1229,45 @@ export interface UnfilteredResponseWithoutQuery extends ResponseQueryOptions {
returnQuery: false
}

/** @public */
export enum ViewResourceType {
Copy link
Member

Choose a reason for hiding this comment

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

this seems like a new concept to expose, is there any other way?

Copy link
Author

Choose a reason for hiding this comment

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

Just tyring to keep the conventions in the right direction for consistency everywhere.

The existing client makes assumptions about the resoure type, which we could do, but in my opinion with a new client we shouldn't be afraid to move in the target direction.

sanityClient({
  projectId: xxxx,
  dataset: xxxx
})

We could follow that here too, but to me that just feels like we're intentionally adding something to fix later.

@danielgroves danielgroves merged commit 2e9866a into poc/views-support Jun 16, 2025
20 checks passed
@danielgroves danielgroves deleted the poc/views-support-dev branch June 16, 2025 15:11
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants