Skip to content

Commit 7ca1e66

Browse files
committed
Make schemaConfig extensible
1 parent 9824be4 commit 7ca1e66

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

bin/generate-schema-config

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ introspection.then((introspection) => {
3737
});
3838
});
3939

40-
const json = Object.fromEntries(
41-
[...interfaceToTypes.entries()].map(([key, value]) => [key, [...value]]),
42-
);
40+
const json = {
41+
interfaceToTypes: Object.fromEntries(
42+
[...interfaceToTypes.entries()].map(([key, value]) => [key, [...value]]),
43+
),
44+
};
4345

4446
console.log(JSON.stringify(json, null, 2));
4547

example/gql-config.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2-
"Node": [
3-
"Film",
4-
"Species",
5-
"Planet",
6-
"Person",
7-
"Starship",
8-
"Vehicle"
9-
]
2+
"interfaceToTypes": {
3+
"Node": [
4+
"Film",
5+
"Species",
6+
"Planet",
7+
"Person",
8+
"Starship",
9+
"Vehicle"
10+
]
11+
}
1012
}

src/cache/cache.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export const getCacheKeyFromJson = (json: unknown): Option<symbol> => {
2828
.otherwise(() => Option.None());
2929
};
3030

31+
export type SchemaConfig = {
32+
interfaceToTypes: Record<string, string[]>;
33+
};
34+
3135
export const getCacheKeyFromOperationNode = (
3236
operationNode: OperationDefinitionNode,
3337
): Option<symbol> => {
@@ -48,9 +52,12 @@ export class ClientCache {
4852

4953
schemaConfig: Record<string, Set<string>>;
5054

51-
constructor(schemaConfig: Record<string, string[]>) {
55+
constructor(schemaConfig: SchemaConfig) {
5256
this.schemaConfig = Object.fromEntries(
53-
Object.entries(schemaConfig).map(([key, value]) => [key, new Set(value)]),
57+
Object.entries(schemaConfig.interfaceToTypes).map(([key, value]) => [
58+
key,
59+
new Set(value),
60+
]),
5461
);
5562
}
5663

src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DocumentNode, GraphQLError } from "@0no-co/graphql.web";
22
import { Future, Option, Result } from "@swan-io/boxed";
33
import { Request, badStatusToError, emptyToError } from "@swan-io/request";
44
import { P, match } from "ts-pattern";
5-
import { ClientCache } from "./cache/cache";
5+
import { ClientCache, SchemaConfig } from "./cache/cache";
66
import { optimizeQuery, readOperationFromCache } from "./cache/read";
77
import { writeOperationToCache } from "./cache/write";
88
import {
@@ -35,7 +35,7 @@ export type ClientConfig = {
3535
url: string;
3636
headers?: Record<string, string>;
3737
makeRequest?: MakeRequest;
38-
schemaConfig: Record<string, string[]>;
38+
schemaConfig: SchemaConfig;
3939
};
4040

4141
const defaultMakeRequest: MakeRequest = ({

src/react/ClientContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { createContext } from "react";
22
import { Client } from "../client";
33

44
export const ClientContext = createContext(
5-
new Client({ url: "/graphql", schemaConfig: {} }),
5+
new Client({ url: "/graphql", schemaConfig: { interfaceToTypes: {} } }),
66
);

test/cache.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
} from "./data";
2525

2626
test("Write & read in cache", () => {
27-
const cache = new ClientCache({});
27+
const cache = new ClientCache({ interfaceToTypes: {} });
2828

2929
const preparedAppQuery = inlineFragments(addTypenames(appQuery));
3030

@@ -185,7 +185,7 @@ test("Write & read in cache", () => {
185185
.getWithDefault("no delta"),
186186
).toMatchSnapshot();
187187

188-
const cache2 = new ClientCache({});
188+
const cache2 = new ClientCache({ interfaceToTypes: {} });
189189

190190
writeOperationToCache(
191191
cache2,
@@ -206,7 +206,7 @@ test("Write & read in cache", () => {
206206
}),
207207
).toMatchObject(Option.Some(Result.Ok(onboardingInfoResponse)));
208208

209-
const cache3 = new ClientCache({});
209+
const cache3 = new ClientCache({ interfaceToTypes: {} });
210210

211211
writeOperationToCache(
212212
cache3,
@@ -358,7 +358,9 @@ test("Write & read in cache", () => {
358358
const preparedBrandingQuery = inlineFragments(addTypenames(brandingQuery));
359359

360360
const cache4 = new ClientCache({
361-
ProjectSettings: ["LiveProjectSettings", "SandboxProjectSettings"],
361+
interfaceToTypes: {
362+
ProjectSettings: ["LiveProjectSettings", "SandboxProjectSettings"],
363+
},
362364
});
363365

364366
writeOperationToCache(cache4, preparedBrandingQuery, brandingResponse, {

0 commit comments

Comments
 (0)