-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathread.ts
More file actions
55 lines (47 loc) · 1.54 KB
/
Copy pathread.ts
File metadata and controls
55 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {
createFlatbreadReadApi,
type FlatbreadRecord,
type FlatbreadRelationCardinality,
type FlatbreadRelationTarget,
} from '../generated/graphql';
import { graphqlFetch } from './graphql';
export type PostAuthorsRelation = FlatbreadRelationTarget<'Post', 'authors'>;
export type PostAuthorsCardinality = FlatbreadRelationCardinality<
'Post',
'authors'
>;
export type PostsAuthorsTagsReadItem = Partial<
Pick<FlatbreadRecord<'Post'>, 'id' | 'tags' | 'title'>
> & {
authors?: PostAuthorsRelation;
};
const postAuthorsCardinality: PostAuthorsCardinality = 'many';
export const flatbreadRead = createFlatbreadReadApi(
async <TData>(source: string, variables?: Record<string, unknown>) =>
graphqlFetch<TData>(source, variables)
);
/**
* Generated TypeScript read API example for the canonical onboarding model.
*
* The default GraphQL selection is generated inside `createFlatbreadReadApi`,
* so this call site does not hand-write a GraphQL document or selection string.
*/
export async function getPostsAuthorsAndTagsViaReadApi(): Promise<
ReadonlyArray<PostsAuthorsTagsReadItem>
> {
const posts = await flatbreadRead.Post.all();
if (postAuthorsCardinality !== 'many') {
throw new Error('Expected Post.authors to be generated as a many relation.');
}
return posts.map((post) => ({
authors: post?.authors,
id: post?.id,
tags: post?.tags,
title: post?.title,
}));
}
export async function getAuthorsViaReadApi(): Promise<
ReadonlyArray<Partial<FlatbreadRecord<'Author'>>>
> {
return flatbreadRead.Author.all();
}