Skip to content

Commit cae2122

Browse files
committed
(wip) spike on adding top-level types to Paper
1 parent 633c239 commit cae2122

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

packages/paper/src/paper.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ import {
3636
// > https://exploringjs.com/deep-js/ch_proxies.html
3737
setAutoFreeze(false);
3838

39-
export class Paper<UserOperations extends OperationMap = OperationMap> {
40-
protected history: DocumentStore[] = [];
41-
protected current: DocumentStore;
39+
export class Paper<
40+
SchemaTypes extends Record<string, any> = Record<string, any>,
41+
UserOperations extends OperationMap = OperationMap
42+
> {
43+
protected history: DocumentStore<SchemaTypes>[] = [];
44+
protected current: DocumentStore<SchemaTypes>;
4245
protected sourceGraphQLSchema: GraphQLSchema;
4346
protected mutateQueue: Queue = new Queue();
4447

@@ -57,7 +60,7 @@ export class Paper<UserOperations extends OperationMap = OperationMap> {
5760

5861
constructor(graphqlSchema: Parameters<typeof createSchema>[0], options?: { operations?: UserOperations }) {
5962
const schema = createSchema(graphqlSchema);
60-
this.current = createDocumentStore(schema);
63+
this.current = createDocumentStore(schema) as DocumentStore<SchemaTypes>;
6164
this.sourceGraphQLSchema = schema;
6265

6366
this.operations = {
@@ -66,8 +69,8 @@ export class Paper<UserOperations extends OperationMap = OperationMap> {
6669
};
6770
}
6871

69-
get data(): DocumentStore {
70-
return proxyWrap(this.sourceGraphQLSchema, this.current);
72+
get data(): DocumentStore<SchemaTypes> {
73+
return proxyWrap(this.sourceGraphQLSchema, this.current) as DocumentStore<SchemaTypes>;
7174
}
7275

7376
find(documentOrKey: KeyOrDocument): Document | undefined {
@@ -103,7 +106,7 @@ export class Paper<UserOperations extends OperationMap = OperationMap> {
103106

104107
const next = await produce(current, async (draft) => {
105108
const { transactionResult, eventQueue } = await transaction<typeof operations>(
106-
draft,
109+
draft as DocumentStore<SchemaTypes>,
107110
schema,
108111
operations,
109112
hooks,

packages/paper/src/types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ export type DocumentKey = string;
77
export type GraphQLTypeName = string;
88
export type KeyOrDocument = DocumentKey | Document;
99

10-
export type Document = {
11-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
12-
[k: string]: any;
10+
export type Document<T extends Record<string, any> = Record<string, any>> = T & {
1311
[DOCUMENT_KEY_SYMBOL]: DocumentKey;
1412
[DOCUMENT_CONNECTIONS_SYMBOL]: ConnectionsMap;
1513
[DOCUMENT_GRAPHQL_TYPENAME]: GraphQLTypeName;
1614
__typename: string;
1715
};
1816

17+
type Special = Document<{ name: string }>;
18+
const a: Special = { name: 'hello' };
19+
console.log(a);
20+
1921
export type DocumentPartial = Partial<Document>;
2022

2123
// connections
@@ -25,7 +27,9 @@ type Connections = Array<DocumentKey>;
2527

2628
// store
2729

28-
export type DocumentStore<Typename extends string = string> = Record<Typename, Document[]>;
30+
export type DocumentStore<
31+
SchemaTypes extends Record<string, Document<SchemaTypes[keyof SchemaTypes]>> = Record<string, any>
32+
> = Record<keyof SchemaTypes, Document<SchemaTypes[keyof SchemaTypes]>[]>;
2933

3034
// operations
3135

packages/paper/test/unit/paper.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ const schemaString = `
2121
`;
2222
const graphqlSchema = buildSchema(schemaString);
2323

24+
type Person = {
25+
name: string;
26+
};
27+
28+
type SchemaTypes = {
29+
Person: Person;
30+
};
31+
2432
describe('mutation operations', () => {
25-
let paper: Paper;
33+
let paper: Paper<SchemaTypes>;
2634

2735
beforeEach(() => {
28-
paper = new Paper(graphqlSchema);
36+
paper = new Paper<SchemaTypes>(graphqlSchema);
2937
});
3038

3139
it('can create a document', async () => {

0 commit comments

Comments
 (0)