Repository navigation
Conversation
ommeirelles
requested review from
eduardoformiga and
emersonlaurentino
and removed request for
a team
October 6, 2025 16:02
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
matheusps
approved these changes
Oct 14, 2025
1 task
lemagnetic
added a commit
that referenced
this pull request
Nov 4, 2026
…3524) ## What's the purpose of this pull request? Since v4, the `@auth` and `@cacheControl` directives have **no effect on fields declared by a store** in its GraphQL extensions (`src/graphql/{vtex,thirdParty}/typeDefs`). The annotation compiles and shows up in the schema, but the store resolver is never wrapped: - `@auth` doesn't validate the shopper session, so the field answers any caller; - `@cacheControl` doesn't contribute to the response `cache-control` header, so the route falls back to its default rule even when the field is annotated `private`. Native FastStore fields are not affected. In v3 both directives worked on store fields, and the public guides (*Authentication in API extensions*, *Displaying an order summary for the signed-in shopper*) rely on that behavior. The regression came with the v4 schema assembly ([#3111](#3111)): `GraphqlVtexSchema` applies the directive transformers to the native schema and merges the store schema **afterwards**, so store fields are never visited by the transformers. ## How it works? **`packages/api/src/platforms/vtex/index.ts` — `GraphqlVtexSchema`** 1. Build the native executable schema with the directive definitions but **without** applying the transformers. 2. If a store schema is passed, merge it (`mergeSchemas`, unchanged). 3. Apply the transformers (`cacheControl`, then `auth`, same order as before so `@auth` runs first at request time) **once**, on the final schema. This is the v3 semantics (one transformation over native + store fields). Two wrong variants were ruled out, and the tests catch both: - transforming the store schema before the merge: it carries the native type definitions but no native resolvers, so its wrappers would override the real native resolvers; - transforming again after the merge while keeping the pre-merge transformation: native fields would be wrapped twice (`@auth` would call VTEX ID twice per field). **`packages/api/src/directives/{auth,cacheControl}.ts`** The wrappers called `originalResolver?.(...)`, so an annotated field with no explicit resolver always resolved to `undefined`. They now fall back to `defaultFieldResolver`. Native annotated fields all have resolvers (no change); this keeps store fields read from the parent object (e.g. a field of a store-owned type) working once the directives apply. **No public API change**: `GraphqlVtexSchema` keeps its signature and call sites (`@faststore/core`, CLI type generation, api local server and tests are untouched). `withDirectives` stays exported and unchanged. ###⚠️ Behavior change for stores Stores that already annotate their fields will see the annotations take effect after upgrading, as in v3 and as documented: - `@auth` fields start returning `401 UnauthorizedError` to signed-out callers; - `@cacheControl` fields start driving the `cache-control` header (still forced `private` when the auth cookie is present). This should go in the release notes. ### Authentication mechanism change — explicit human approval required Per `AGENTS.md` (Security & Data Handling) and `packages/api/AGENTS.md`, changes to authentication mechanisms require explicit human approval recorded in the PR description. - [ ] Approved by: _reviewer name / date_ ## How to test it? **Automated** — new `packages/api/test/integration/directives.test.ts` builds the final schema the same way `getFinalAPISchema` does in `@faststore/core` (native type defs + store type defs, store resolvers only, then `GraphqlVtexSchema(storeSchema)`) and checks: - store `@auth`: rejected without session (resolver not called), resolves with session; - store field with `@auth` and no resolver: returns the parent value once authorized; - store `@cacheControl`: sets the field policy; aggregates with a native field (min values, most restrictive scope); - `@auth` runs before `@cacheControl` on the same field; - native `validateUser` (with and without a store schema): keeps its resolver, rejects without session, validates the session **exactly once**. ```bash cd packages/api pnpm vitest run test/integration/directives.test.ts pnpm test ``` Results: - on the current `dev` code, the 6 store tests fail and the 4 native tests pass (bug reproduced); - with the fix, all 10 pass and the full api suite is green (362 unit, 88 integration, no snapshot changes); - the tests fail against each of the wrong variants described above. **Manual (store)** — in a store using the CodeSandbox build of this PR, add to `src/graphql/thirdParty/typeDefs`: ```graphql extend type Query { directiveProbeAuth: String @auth directiveProbeCache: String @CacheControl(scope: "private", sMaxAge: 7, staleWhileRevalidate: 11) } ``` with resolvers returning any string, run `faststore dev` and `POST /api/graphql` without cookies: | Request | Before (4.10.x) | Expected with this PR | | --- | --- | --- | | `{ directiveProbeAuth }` | `200` with the resolver value | `401 UnauthorizedError` | | `{ directiveProbeCache }` | `cache-control: no-cache, no-store` | `cache-control: private, s-maxage=7, stale-while-revalidate=11` | | `{ listCreditCards { ... } }` (native control) | `401` | `401` (unchanged) | ### Starters Deploy Preview Validated on brandless.store with the CodeSandbox build of this PR (`50f91f6a`) against 4.10.0: all results matched the expected table above. Full before/after results: #3524 (comment) ## References - Jira: SO-696 - Regression origin: [#3111](#3111) (v4 schema assembly); design introduced on `canary` in [#3063](#3063) - v3 reference: `getFinalAPISchema` in `packages/core/src/server/index.ts` applied `[cacheControlDirective, authDirective]` to the single native + store schema (last unaffected tag `v3.100.5`) --------- Co-authored-by: Cursor <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.