@tinacms/graphql 0.0.0-03bb823-20241101081633 → 0.0.0-059f480-20260324232913

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 ADDED
@@ -0,0 +1,144 @@
1
+ # @tinacms/graphql
2
+
3
+ [Tina](https://tina.io) is a headless content management system with support for Markdown, MDX, JSON, YAML, and more. This package contains the logic required to turn a collection of folders and files into a database that can be queried using [GraphQL](https://tina.io/docs/graphql/queries).
4
+
5
+ ## Features
6
+
7
+ - Query Markdown, MDX, JSON, YAML and more using GraphQL
8
+ - Supports references between documents
9
+ - Pre-generation of schema and query data to expedite website compilation
10
+
11
+ ## Getting Started
12
+
13
+ The easiest way to use this package is as part of a broader TinaCMS site. The starter can be tested locally using:
14
+
15
+ ```
16
+ npx create-tina-app@latest
17
+ ```
18
+
19
+ Alternatively, you can directly install and use this package.
20
+
21
+ ### Install @tinacms/graphql
22
+
23
+ ```
24
+ pnpm install
25
+ pnpm add @tinacms/graphql
26
+ ```
27
+
28
+ ### Build your program
29
+
30
+ The following example demonstrates how to use this package.
31
+
32
+ ```ts
33
+ import { MemoryLevel } from 'memory-level';
34
+ import {
35
+ Database,
36
+ FilesystemBridge,
37
+ buildSchema,
38
+ resolve
39
+ } from '@tinacms/graphql';
40
+ import {
41
+ Schema
42
+ } from '@tinacms/schema-tools';
43
+
44
+ const dir = 'content';
45
+
46
+ // Where to source content from
47
+ const bridge = new FilesystemBridge(dir);
48
+ // Where to store the index data
49
+ const indexStorage = new MemoryLevel<string, Record<string, string>>();
50
+
51
+ // Create the schema/structure of the database
52
+ const rawSchema: Schema = {
53
+ collections: [
54
+ {
55
+ name: 'post',
56
+ path: '', // Don't require content to be placed within a subdirectory
57
+ fields: [
58
+ {
59
+ type: 'string',
60
+ name: 'title',
61
+ isTitle: true,
62
+ required: true
63
+ }
64
+ ]
65
+ }
66
+ ]
67
+ };
68
+ const schema = await buildSchema({
69
+ schema: rawSchema,
70
+ build: {
71
+ publicFolder: '',
72
+ outputFolder: ''
73
+ }
74
+ });
75
+
76
+ // Create the object for editing and querying your repository
77
+ const database = new Database({
78
+ bridge,
79
+ level: indexStorage,
80
+ tinaDirectory: 'tina'
81
+ });
82
+
83
+ // Generate the index data required to support querying
84
+ await database.indexContent(schema)
85
+
86
+ // Query the database and output the result
87
+ // In this case, it will retrieve the title of the post 'in.md'
88
+ const graphQLQuery = `
89
+ query {
90
+ document(collection: "post", relativePath: "in.md") {
91
+ ...on Document {
92
+ _values,
93
+ _sys { title }
94
+ }
95
+ }
96
+ }
97
+ `
98
+ const result = await resolve({
99
+ database,
100
+ query: graphQLQuery,
101
+ variables: {}
102
+ });
103
+
104
+ // Output the result
105
+ console.log(JSON.stringify(result))
106
+ ```
107
+
108
+ For the program to work:
109
+
110
+ 1. Install packages:
111
+ - [`@tinacms/schema-tools`](https://www.npmjs.com/package/@tinacms/schema-tools)
112
+ - [`memory-level`](https://www.npmjs.com/package/memory-level)
113
+ 2. Add a file `content/in.md` of the following form:
114
+
115
+ ```md
116
+ ---
117
+ title: Hello
118
+ ---
119
+ ```
120
+
121
+ The output should be:
122
+
123
+ ```json
124
+ {"data":{"document":{"_values":{"_collection":"post","_template":"post","title":"Hello"},"_sys":{"title":"Hello"}}}}
125
+ ```
126
+
127
+ ## Development
128
+
129
+ The package is part of the [TinaCMS repository](https://github.com/tinacms/tinacms/).
130
+
131
+ ## Documentation
132
+
133
+ Visit [Tina's documentation](https://tina.io/docs/) to learn more.
134
+
135
+ ## Questions?
136
+
137
+ [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?url=https%3A%2F%2Ftinacms.org&text=I%20just%20checked%20out%20@tinacms%20on%20GitHub%20and%20it%20is%20sweet%21&hashtags=TinaCMS%2Cjamstack%2Cheadlesscms)
138
+ [![Forum](https://shields.io/github/discussions/tinacms/tinacms)](https://github.com/tinacms/tinacms/discussions)
139
+
140
+ Visit the [GitHub Discussions](https://github.com/tinacms/tinacms/discussions) or our [Community Discord](https://discord.com/invite/zumN63Ybpf) to ask questions, or look us up on on Twitter at [@tinacms](https://twitter.com/tinacms).
141
+
142
+ ## Contributing
143
+
144
+ Please see our [./CONTRIBUTING.md](https://github.com/tinacms/tinacms/blob/main/CONTRIBUTING.md)
@@ -1,7 +1,7 @@
1
1
  /**
2
2
 
3
3
  */
4
- import { type FieldDefinitionNode, type ScalarTypeDefinitionNode, type InputValueDefinitionNode, type ObjectTypeDefinitionNode, type InterfaceTypeDefinitionNode, type NamedTypeNode, type UnionTypeDefinitionNode, type TypeDefinitionNode, type DirectiveNode, type EnumTypeDefinitionNode, type InputObjectTypeDefinitionNode, type DocumentNode, type FragmentDefinitionNode, type SelectionNode, type FieldNode, type InlineFragmentNode, type OperationDefinitionNode } from 'graphql';
4
+ import { type FieldDefinitionNode, type ScalarTypeDefinitionNode, type InputValueDefinitionNode, type ObjectTypeDefinitionNode, type InterfaceTypeDefinitionNode, type NamedTypeNode, type UnionTypeDefinitionNode, type TypeDefinitionNode, type DirectiveNode, type EnumTypeDefinitionNode, type InputObjectTypeDefinitionNode, type DocumentNode, type FragmentDefinitionNode, type SelectionNode, type FieldNode, type InlineFragmentNode, type OperationDefinitionNode, type DefinitionNode } from 'graphql';
5
5
  export declare const SysFieldDefinition: {
6
6
  kind: "Field";
7
7
  name: {
@@ -153,9 +153,8 @@ export declare const astBuilder: {
153
153
  }) => DocumentNode;
154
154
  };
155
155
  type scalarNames = 'string' | 'boolean' | 'datetime' | 'image' | 'text' | 'number';
156
- export declare const extractInlineTypes: (item: TypeDefinitionNode | TypeDefinitionNode[]) => any;
156
+ export declare const extractInlineTypes: (item: DefinitionNode | DefinitionNode[]) => DefinitionNode[];
157
157
  export declare function walk(maybeNode: TypeDefinitionNode, visited?: WeakSet<WeakKey>): IterableIterator<TypeDefinitionNode>;
158
- export declare function addNamespaceToSchema<T extends object | string>(maybeNode: T, namespace?: string[]): T;
159
158
  export declare const NAMER: {
160
159
  dataFilterTypeNameOn: (namespace: string[]) => string;
161
160
  dataFilterTypeName: (namespace: string[]) => string;
package/dist/build.d.ts CHANGED
@@ -1,16 +1,14 @@
1
1
  /**
2
2
 
3
3
  */
4
+ import { type DocumentNode } from 'graphql';
4
5
  import type { TinaSchema, Config } from '@tinacms/schema-tools';
5
6
  export declare const buildDotTinaFiles: ({ config, flags, buildSDK, }: {
6
7
  config: Config;
7
8
  flags?: string[];
8
9
  buildSDK?: boolean;
9
10
  }) => Promise<{
10
- graphQLSchema: {
11
- kind: "Document";
12
- definitions: any;
13
- };
11
+ graphQLSchema: DocumentNode;
14
12
  tinaSchema: TinaSchema;
15
13
  lookup: Record<string, import("./database").LookupMapType>;
16
14
  fragDoc: string;
@@ -2,7 +2,7 @@
2
2
 
3
3
  */
4
4
  import { LookupMapType } from '../database';
5
- import type { ObjectTypeDefinitionNode, InlineFragmentNode, FieldDefinitionNode } from 'graphql';
5
+ import type { FieldDefinitionNode, InlineFragmentNode, ObjectTypeDefinitionNode } from 'graphql';
6
6
  import type { Collection, Template } from '@tinacms/schema-tools';
7
7
  import { TinaSchema } from '@tinacms/schema-tools';
8
8
  export declare const createBuilder: ({ tinaSchema, }: {
@@ -177,7 +177,7 @@ export declare class Builder {
177
177
  * ```
178
178
  *
179
179
  * @public
180
- * @param collection a Tina Cloud collection
180
+ * @param collection a TinaCloud collection
181
181
  */
182
182
  collectionFragment: (collection: Collection<true>) => Promise<import("graphql").FragmentDefinitionNode>;
183
183
  /**
@@ -1,3 +1,3 @@
1
- import { Template } from '@tinacms/schema-tools/src';
1
+ import { Template } from '@tinacms/schema-tools';
2
2
  export declare const replaceNameOverrides: (template: Template, obj: any) => object;
3
3
  export declare const applyNameOverrides: (template: Template, obj: any) => object;
@@ -3,10 +3,14 @@ import type { Bridge } from './index';
3
3
  * This is the bridge from whatever datasource we need for I/O.
4
4
  * The basic example here is for the filesystem, one is needed
5
5
  * for GitHub has well.
6
+ *
7
+ * @security All public methods validate their `filepath` / `pattern`
8
+ * argument via `assertWithinBase` before performing any I/O. If you add a
9
+ * new method that accepts a path, you MUST validate it the same way.
6
10
  */
7
11
  export declare class FilesystemBridge implements Bridge {
8
12
  rootPath: string;
9
- outputPath?: string;
13
+ outputPath: string;
10
14
  constructor(rootPath: string, outputPath?: string);
11
15
  glob(pattern: string, extension: string): Promise<string[]>;
12
16
  delete(filepath: string): Promise<void>;
@@ -1,8 +1,36 @@
1
+ /**
2
+ * I/O abstraction layer for reading/writing content files.
3
+ *
4
+ * @security **Path traversal (CWE-22):** All `filepath` and `pattern`
5
+ * parameters may originate from user input (e.g. GraphQL mutations or media
6
+ * API requests). Implementations MUST validate that resolved paths stay
7
+ * within their root/output directory before performing any filesystem
8
+ * operation. See `FilesystemBridge.assertWithinBase` and
9
+ * `IsomorphicBridge.assertWithinBase` for reference implementations.
10
+ *
11
+ * The recommended validation pattern is:
12
+ * ```ts
13
+ * const resolved = path.resolve(path.join(baseDir, filepath));
14
+ * if (!resolved.startsWith(path.resolve(baseDir) + path.sep)) {
15
+ * throw new Error('Path traversal detected');
16
+ * }
17
+ * ```
18
+ *
19
+ * If you are adding a new Bridge implementation, add path traversal
20
+ * validation to every method that accepts a filepath from the caller.
21
+ */
1
22
  export interface Bridge {
2
23
  rootPath: string;
24
+ /**
25
+ * @param pattern - Glob pattern prefix (untrusted — validate before use).
26
+ * @param extension - File extension to match.
27
+ */
3
28
  glob(pattern: string, extension: string): Promise<string[]>;
29
+ /** @param filepath - Relative path to delete (untrusted — validate before use). */
4
30
  delete(filepath: string): Promise<void>;
31
+ /** @param filepath - Relative path to read (untrusted — validate before use). */
5
32
  get(filepath: string): Promise<string>;
33
+ /** @param filepath - Relative path to write (untrusted — validate before use). */
6
34
  put(filepath: string, data: string): Promise<void>;
7
35
  /**
8
36
  * Optionally, the bridge can perform
@@ -18,6 +18,11 @@ export type IsomorphicGitBridgeOptions = {
18
18
  };
19
19
  /**
20
20
  * Bridge backed by isomorphic-git
21
+ *
22
+ * @security All public methods (glob, get, put, delete) validate their
23
+ * `filepath` / `pattern` argument via `assertWithinBase` before performing
24
+ * any git operations. If you add a new method that accepts a path, you
25
+ * MUST validate it the same way.
21
26
  */
22
27
  export declare class IsomorphicBridge implements Bridge {
23
28
  rootPath: string;
@@ -2,7 +2,7 @@
2
2
 
3
3
  */
4
4
  import { BatchOp, Level } from './level';
5
- import { Collection } from '@tinacms/schema-tools';
5
+ import type { Collection } from '@tinacms/schema-tools';
6
6
  export declare enum OP {
7
7
  EQ = "eq",
8
8
  GT = "gt",
@@ -49,6 +49,9 @@ export type FilterCondition = {
49
49
  };
50
50
  type StringEscaper = <T extends string | string[]>(input: T) => T;
51
51
  export declare const DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
52
+ export declare const REFS_COLLECTIONS_SORT_KEY = "__refs__";
53
+ export declare const REFS_REFERENCE_FIELD = "__tina_ref__";
54
+ export declare const REFS_PATH_FIELD = "__tina_ref_path__";
52
55
  export declare const DEFAULT_NUMERIC_LPAD = 4;
53
56
  export declare const coerceFilterChainOperands: (filterChain: (BinaryFilter | TernaryFilter)[], escapeString?: StringEscaper) => (BinaryFilter | TernaryFilter)[];
54
57
  export declare const makeFilter: ({ filterChain, }: {
@@ -71,6 +74,7 @@ export declare class FolderTreeBuilder {
71
74
  }
72
75
  export declare const makeFolderOpsForCollection: <T extends object>(folderTree: FolderTree, collection: Collection<true>, indexDefinitions: Record<string, IndexDefinition>, opType: "put" | "del", level: Level, escapeStr?: StringEscaper) => BatchOp[];
73
76
  export declare const makeIndexOpsForDocument: <T extends object>(filepath: string, collection: string | undefined, indexDefinitions: Record<string, IndexDefinition>, data: T, opType: "put" | "del", level: Level, escapeStr?: StringEscaper) => BatchOp[];
77
+ export declare const makeRefOpsForDocument: <T extends object>(filepath: string, collection: string | undefined, references: Record<string, string[]> | undefined | null, data: T, opType: "put" | "del", level: Level) => BatchOp[];
74
78
  export declare const makeStringEscaper: (regex: RegExp, replacement: string) => StringEscaper;
75
79
  export declare const stringEscaper: StringEscaper;
76
80
  export {};
@@ -19,6 +19,7 @@ export interface DatabaseArgs {
19
19
  indexStatusCallback?: IndexStatusCallback;
20
20
  version?: boolean;
21
21
  namespace?: string;
22
+ levelBatchSize?: number;
22
23
  }
23
24
  export interface GitProvider {
24
25
  onPut: (key: string, value: string) => Promise<void>;
@@ -69,9 +70,11 @@ export declare class Database {
69
70
  indexStatusCallback: IndexStatusCallback | undefined;
70
71
  private readonly onPut;
71
72
  private readonly onDelete;
73
+ private readonly levelBatchSize;
72
74
  private tinaSchema;
73
75
  private contentNamespace;
74
76
  private collectionIndexDefinitions;
77
+ private collectionReferences;
75
78
  private _lookup;
76
79
  constructor(config: DatabaseArgs);
77
80
  private collectionForPath;
@@ -125,6 +128,7 @@ export declare class Database {
125
128
  getGraphQLSchemaFromBridge: () => Promise<DocumentNode>;
126
129
  getTinaSchema: (level?: Level) => Promise<Schema>;
127
130
  getSchema: (level?: Level, existingSchema?: Schema) => Promise<TinaSchema>;
131
+ getCollectionReferences: (level?: Level) => Promise<Record<string, Record<string, string[]>>>;
128
132
  getIndexDefinitions: (level?: Level) => Promise<Record<string, Record<string, IndexDefinition>>>;
129
133
  documentExists: (fullpath: unknown) => Promise<boolean>;
130
134
  query: (queryOptions: QueryOptions, hydrator: any) => Promise<{
@@ -1,19 +1,21 @@
1
- /**
2
-
3
- */
1
+ import { Collection, CollectionTemplateable, TinaSchema, normalizePath } from '@tinacms/schema-tools';
4
2
  import * as yup from 'yup';
5
- import { Collection, CollectionTemplateable, normalizePath, TinaSchema } from '@tinacms/schema-tools';
3
+ import { ContentFormat, ContentFrontmatterFormat } from '@tinacms/schema-tools';
6
4
  import { Bridge } from './bridge';
7
5
  export { normalizePath };
8
- export declare const stringifyFile: (content: object, format: FormatType | string, keepTemplateKey: boolean, markdownParseConfig?: {
9
- frontmatterFormat?: "toml" | "yaml" | "json";
6
+ export declare const stringifyFile: (content: object, format: ContentFormat | string, // FIXME
7
+ /** For non-polymorphic documents we don't need the template key */
8
+ keepTemplateKey: boolean, markdownParseConfig?: {
9
+ frontmatterFormat?: ContentFrontmatterFormat;
10
10
  frontmatterDelimiters?: [string, string] | string;
11
+ yamlMaxLineWidth?: number;
11
12
  }) => string;
12
- export declare const parseFile: <T extends object>(content: string, format: FormatType | string, yupSchema: (args: typeof yup) => yup.ObjectSchema<any>, markdownParseConfig?: {
13
- frontmatterFormat?: "toml" | "yaml" | "json";
13
+ export declare const parseFile: <T extends object>(content: string, format: ContentFormat | string, // FIXME
14
+ yupSchema: (args: typeof yup) => yup.ObjectSchema<any>, markdownParseConfig?: {
15
+ frontmatterFormat?: ContentFrontmatterFormat;
16
+ lineWidth?: number;
14
17
  frontmatterDelimiters?: [string, string] | string;
15
18
  }) => T;
16
- export type FormatType = 'json' | 'md' | 'mdx' | 'markdown';
17
19
  export declare const atob: (b64Encoded: string) => string;
18
20
  export declare const btoa: (string: string) => string;
19
21
  export declare const scanAllContent: (tinaSchema: TinaSchema, bridge: Bridge, callback: (collection: Collection<true>, contentPaths: string[]) => Promise<void>) => Promise<string[]>;
package/dist/index.d.ts CHANGED
@@ -16,10 +16,7 @@ export { createSchema } from './schema/createSchema';
16
16
  export { buildDotTinaFiles };
17
17
  export type DummyType = unknown;
18
18
  export declare const buildSchema: (config: Config, flags?: string[]) => Promise<{
19
- graphQLSchema: {
20
- kind: "Document";
21
- definitions: any;
22
- };
19
+ graphQLSchema: import("graphql").DocumentNode;
23
20
  tinaSchema: import("@tinacms/schema-tools").TinaSchema;
24
21
  lookup: Record<string, import("./database").LookupMapType>;
25
22
  fragDoc: string;