@asteroidcms/core-utils 0.1.1 → 0.1.3

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
@@ -195,6 +195,77 @@ const { data: topStories } = useCmsContent({
195
195
 
196
196
  ---
197
197
 
198
+ ## `fetchCmsContent` (Next.js / RSC)
199
+
200
+ Server-side counterpart to `useCmsContent`. Use it in Next.js Server Components, route handlers, or any other server context. Accepts a server-side Apollo client plus the same options object as `useCmsContent`, and returns the resolved data directly.
201
+
202
+ Pass a `getClient` function that returns a server-side Apollo client. The shape matches what `registerApolloClient` from `@apollo/client-integration-nextjs` already returns, so you can hand it through directly.
203
+
204
+ ```ts
205
+ // app/lib/cms-server.ts
206
+ import "server-only";
207
+ import { ApolloClient, HttpLink, InMemoryCache } from "@apollo/client";
208
+ import { registerApolloClient } from "@apollo/client-integration-nextjs";
209
+
210
+ export const { getClient, query, PreloadQuery } = registerApolloClient(() => {
211
+ return new ApolloClient({
212
+ cache: new InMemoryCache(),
213
+ link: new HttpLink({
214
+ uri: `${process.env.CMS_API_BASE_URL}/graphql`,
215
+ headers: { "X-API-Key": process.env.CMS_X_API_KEY ?? "" },
216
+ fetchOptions: { next: { revalidate: 60, tags: ["cms:landing_page"] } },
217
+ }),
218
+ });
219
+ });
220
+ ```
221
+
222
+ ```ts
223
+ // app/news/[slug]/page.tsx
224
+ import { fetchCmsContent } from "@asteroidcms/core-utils";
225
+ import { getClient } from "@/app/lib/cms-server";
226
+
227
+ // Single entry
228
+ const article = await fetchCmsContent<Article>(getClient, {
229
+ schema_slug: "news",
230
+ entrySlug: params.slug,
231
+ fullData: true,
232
+ });
233
+
234
+ // List
235
+ const articles = await fetchCmsContent<Article[]>(getClient, {
236
+ schema_slug: "news",
237
+ limit: 10,
238
+ status: "PUBLISHED",
239
+ select: ["title", "slug", "publish_date"],
240
+ });
241
+ ```
242
+
243
+ Outside Next.js you can pass any `() => ApolloClient` - e.g. `() => createApolloClient({ cmsUrl, apiKey })`.
244
+
245
+ Add `import "server-only"` in the file that calls it if you want Next.js to fail the build when it leaks into a client component.
246
+
247
+ ---
248
+
249
+ ## `buildCmsQuery`
250
+
251
+ Lower-level helper that turns a declarative selection into a GraphQL `DocumentNode` plus variables. Used internally by `useCmsContent` and `fetchCmsContent`; exported so you can drive your own Apollo calls (cache reads, prefetching, batching, etc.).
252
+
253
+ ```ts
254
+ import { buildCmsQuery } from "@asteroidcms/core-utils";
255
+
256
+ const { query, variables, isSingle } = buildCmsQuery({
257
+ schema_slug: "news",
258
+ limit: 10,
259
+ status: "PUBLISHED",
260
+ select: ["title", "slug"],
261
+ });
262
+
263
+ const { data } = await apolloClient.query({ query, variables });
264
+ const entries = isSingle ? data.entry : data.entries;
265
+ ```
266
+
267
+ ---
268
+
198
269
  ## `useCmsMutate`
199
270
 
200
271
  `create` / `update` / `delete` against a schema, with the same selection syntax.