Skip to content

refactor: graphql datalayer - #3063

Merged
matheusps merged 23 commits into
canaryfrom
SFS-2867
Oct 14, 2025
Merged

matheusps merged 23 commits into
canaryfrom
SFS-2867

Conversation

@ommeirelles

Copy link
Copy Markdown
Contributor

No description provided.

@ommeirelles
ommeirelles requested a review from a team as a code owner October 6, 2025 16:02
@ommeirelles
ommeirelles requested review from eduardoformiga and emersonlaurentino and removed request for a team October 6, 2025 16:02
@ommeirelles ommeirelles changed the title Sfs 2867 reafctor: graphql datalayer Oct 6, 2025
@ommeirelles ommeirelles changed the title reafctor: graphql datalayer refactor: graphql datalayer Oct 9, 2025
@codesandbox-ci

codesandbox-ci Bot commented Oct 9, 2025 •

Copy link
Copy Markdown

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
matheusps merged commit b8bedd1 into canary Oct 14, 2025
@matheusps
matheusps deleted the SFS-2867 branch October 14, 2025 10:20
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants