@fourtwelvelabs/fetch-contentful 0.4.0 → 1.0.0
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/CHANGELOG.md +177 -0
- package/README.md +192 -57
- package/dist/cli/index.mjs +82 -77
- package/dist/cli/index.mjs.map +1 -1
- package/dist/index.cjs +309 -139
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +254 -26
- package/dist/index.d.ts +254 -26
- package/dist/index.mjs +305 -140
- package/dist/index.mjs.map +1 -1
- package/docs/tada.md +57 -32
- package/package.json +1 -1
package/docs/tada.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Typed queries with gql.tada
|
|
2
2
|
|
|
3
3
|
[gql.tada](https://gql-tada.0no.co) infers TypeScript types from your GraphQL
|
|
4
|
-
documents
|
|
4
|
+
documents _as you write them_ — no codegen step in your dev loop, no
|
|
5
5
|
hand-written result interfaces that drift from the query. This package ships
|
|
6
6
|
a companion that points it at a Contentful space: one command writes the
|
|
7
7
|
schema, the editor plugin config and the `graphql` helper, and
|
|
@@ -25,10 +25,10 @@ peer dependencies — they only matter at author time, so they belong in
|
|
|
25
25
|
yarn add -D gql.tada @0no-co/graphqlsp
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
**2. Run `tada-init`.**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
**2. Run `tada-init`.** It reads your credentials from `.env.local`, then
|
|
29
|
+
`.env`, then the shell — the same variable names the library uses at runtime
|
|
30
|
+
(see the README's Configuration section). In a typical Next.js project that
|
|
31
|
+
means no arguments at all:
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
34
|
npx @fourtwelvelabs/fetch-contentful tada-init
|
|
@@ -46,7 +46,7 @@ It downloads the schema as SDL, adds `@0no-co/graphqlsp` to
|
|
|
46
46
|
Add `--dry-run` first if you'd rather see the changes before they happen.
|
|
47
47
|
|
|
48
48
|
That last file is the one you import from as you write queries. It's yours —
|
|
49
|
-
commit it, edit it — and it exists because it has to be bound to
|
|
49
|
+
commit it, edit it — and it exists because it has to be bound to _your_
|
|
50
50
|
space's schema, so it could never ship inside the package:
|
|
51
51
|
|
|
52
52
|
```ts
|
|
@@ -70,7 +70,7 @@ that query's result and variable types by matching it against `introspection`.
|
|
|
70
70
|
See [Manual setup](#manual-setup) for writing this file yourself.
|
|
71
71
|
|
|
72
72
|
**3. Restart the TypeScript server** so the editor picks up the plugin. In
|
|
73
|
-
VS Code: <kbd>⌘⇧P</kbd> →
|
|
73
|
+
VS Code: <kbd>⌘⇧P</kbd> → _TypeScript: Restart TS Server_.
|
|
74
74
|
|
|
75
75
|
**4. Write a query**, importing `graphql` from the file step 2 generated.
|
|
76
76
|
Autocomplete, field validation and types all come from your space's real
|
|
@@ -88,7 +88,9 @@ const PageQuery = graphql(`
|
|
|
88
88
|
title
|
|
89
89
|
publishedAt
|
|
90
90
|
sectionsCollection {
|
|
91
|
-
items {
|
|
91
|
+
items {
|
|
92
|
+
heading
|
|
93
|
+
}
|
|
92
94
|
}
|
|
93
95
|
}
|
|
94
96
|
}
|
|
@@ -106,7 +108,7 @@ No type arguments, no result interface, no cast.
|
|
|
106
108
|
|
|
107
109
|
**The result type follows the response you actually receive.** gql.tada types
|
|
108
110
|
the raw wire shape; `fetchContentful` then applies its own shaping and
|
|
109
|
-
unwrapping to
|
|
111
|
+
unwrapping to _that_ type, so `pageCollection.items` arrives as `pages` — an
|
|
110
112
|
array — in both the value and the type. `shapeResponseData: false` and
|
|
111
113
|
`unwrapRootField: false` are reflected too:
|
|
112
114
|
|
|
@@ -114,7 +116,7 @@ array — in both the value and the type. `shapeResponseData: false` and
|
|
|
114
116
|
const raw = await fetchContentful(PageQuery, {
|
|
115
117
|
variables: { slug: 'home' },
|
|
116
118
|
shapeResponseData: false,
|
|
117
|
-
unwrapRootField: false
|
|
119
|
+
unwrapRootField: false
|
|
118
120
|
});
|
|
119
121
|
// ^? { pageCollection: { items: [...] } | null }
|
|
120
122
|
```
|
|
@@ -135,14 +137,14 @@ await fetchContentful(PageQuery, { variables: { slgu: 'home' } });
|
|
|
135
137
|
**Contentful's custom scalars are typed**, via the `ContentfulScalars` map
|
|
136
138
|
that `tada-init` wires up. Without it every one of them would be `unknown`:
|
|
137
139
|
|
|
138
|
-
| Scalar
|
|
139
|
-
|
|
|
140
|
-
| `DateTime`
|
|
141
|
-
| `Dimension` | `number` (1–4000, image transforms)
|
|
142
|
-
| `HexColor`
|
|
143
|
-
| `JSON`
|
|
144
|
-
| `Quality`
|
|
145
|
-
| `Circle`
|
|
140
|
+
| Scalar | TypeScript |
|
|
141
|
+
| ----------- | ------------------------------------------------------------ |
|
|
142
|
+
| `DateTime` | `string` (ISO 8601 at UTC) |
|
|
143
|
+
| `Dimension` | `number` (1–4000, image transforms) |
|
|
144
|
+
| `HexColor` | `string` (`"rgb:ffffff"`) |
|
|
145
|
+
| `JSON` | `JsonValue` (recursive JSON) |
|
|
146
|
+
| `Quality` | `number` (1–100) |
|
|
147
|
+
| `Circle` | `{ lat, lon, radius }` — a `_within_circle` search area |
|
|
146
148
|
| `Rectangle` | `{ topLeftLat, topLeftLon, bottomRightLat, bottomRightLon }` |
|
|
147
149
|
|
|
148
150
|
`Circle` and `Rectangle` are geographic search areas used by location
|
|
@@ -223,7 +225,7 @@ Contentful can tell you when the content model changes; the schema file can
|
|
|
223
225
|
then update itself through a pull request rather than by someone remembering
|
|
224
226
|
to run a command.
|
|
225
227
|
|
|
226
|
-
**1. Add a Contentful webhook.** In
|
|
228
|
+
**1. Add a Contentful webhook.** In _Settings → Webhooks_, create one that
|
|
227
229
|
fires on **ContentType → publish** and **unpublish**, pointing at GitHub's
|
|
228
230
|
`repository_dispatch` endpoint:
|
|
229
231
|
|
|
@@ -301,19 +303,20 @@ fetch-contentful tada-refresh [options] Re-download the schema only
|
|
|
301
303
|
|
|
302
304
|
Both commands accept the same options. Flags win over environment variables.
|
|
303
305
|
|
|
304
|
-
| Option
|
|
305
|
-
|
|
|
306
|
-
| `--space <id>`
|
|
307
|
-
| `--environment <id>`
|
|
308
|
-
| `--token <token>`
|
|
309
|
-
| `--schema-path <path>`
|
|
310
|
-
| `--tada-output <path>`
|
|
311
|
-
| `--graphql-file <path>` | `./src/graphql.ts`
|
|
312
|
-
| `--tsconfig <path>`
|
|
313
|
-
| `--dry-run`
|
|
314
|
-
| `--force`
|
|
315
|
-
| `--cwd <path>`
|
|
316
|
-
|
|
|
306
|
+
| Option | Default | Notes |
|
|
307
|
+
| ----------------------- | --------------------------------------- | --------------------------------------- |
|
|
308
|
+
| `--space <id>` | `CONTENTFUL_SPACE_ID` | Required |
|
|
309
|
+
| `--environment <id>` | `CONTENTFUL_ENVIRONMENT`, then `master` | |
|
|
310
|
+
| `--token <token>` | `CONTENTFUL_ACCESS_TOKEN` | Content Delivery API token |
|
|
311
|
+
| `--schema-path <path>` | `./contentful-schema.graphql` | Where the SDL is written |
|
|
312
|
+
| `--tada-output <path>` | `./src/contentful-env.d.ts` | Where gql.tada writes its types |
|
|
313
|
+
| `--graphql-file <path>` | `./src/graphql.ts` | `tada-init` only |
|
|
314
|
+
| `--tsconfig <path>` | `./tsconfig.json` | `tada-init` only |
|
|
315
|
+
| `--dry-run` | | Print the changes; write nothing |
|
|
316
|
+
| `--force` | | Overwrite an existing graphql file |
|
|
317
|
+
| `--cwd <path>` | | Run against another directory |
|
|
318
|
+
| `--env-file <path>` | `.env.local`, then `.env` | Read credentials from this file instead |
|
|
319
|
+
| `-h`, `--help` | | |
|
|
317
320
|
|
|
318
321
|
The `NEXT_PUBLIC_`-prefixed variable names are read as fallbacks, exactly as
|
|
319
322
|
the library reads them at runtime.
|
|
@@ -324,4 +327,26 @@ and formatting, and never overwrites `graphql.ts` unless you pass `--force`.
|
|
|
324
327
|
If it cannot patch a tsconfig safely, it changes nothing and prints the block
|
|
325
328
|
for you to paste in.
|
|
326
329
|
|
|
330
|
+
### Where configuration comes from
|
|
331
|
+
|
|
332
|
+
The commands resolve each setting from the first of these that has a value:
|
|
333
|
+
|
|
334
|
+
1. a flag — `--space`, `--token`, `--environment`
|
|
335
|
+
2. the shell environment
|
|
336
|
+
3. `.env.local`
|
|
337
|
+
4. `.env`
|
|
338
|
+
|
|
339
|
+
The library itself never reads files from disk — that would break bundlers
|
|
340
|
+
and edge runtimes, and your framework already owns that job. The CLI is a
|
|
341
|
+
different case: it runs from a shell before anything has populated
|
|
342
|
+
`process.env`, and a Next.js project keeps its credentials in `.env.local`,
|
|
343
|
+
which Next only reads when Next itself boots. So the CLI reads that file
|
|
344
|
+
directly, without overwriting anything you have genuinely exported.
|
|
345
|
+
|
|
346
|
+
If your credentials live somewhere else, point at the file:
|
|
347
|
+
|
|
348
|
+
```bash
|
|
349
|
+
npx @fourtwelvelabs/fetch-contentful tada-refresh --env-file .env.contentful
|
|
350
|
+
```
|
|
351
|
+
|
|
327
352
|
The access token is never printed — not in output, not in error messages.
|
package/package.json
CHANGED