aboutsummaryrefslogtreecommitdiff
path: root/source/library/state/schema.svelte.ts
diff options
context:
space:
mode:
Diffstat (limited to 'source/library/state/schema.svelte.ts')
-rw-r--r--source/library/state/schema.svelte.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/source/library/state/schema.svelte.ts b/source/library/state/schema.svelte.ts
new file mode 100644
index 0000000..c5f148f
--- /dev/null
+++ b/source/library/state/schema.svelte.ts
@@ -0,0 +1,41 @@
+
+
+
+/*** IMPORT ------------------------------------------- ***/
+
+import {
+ buildClientSchema,
+ getIntrospectionQuery,
+ printSchema,
+ type GraphQLSchema,
+ type IntrospectionQuery
+} from "graphql";
+
+/*** UTILITY ------------------------------------------ ***/
+
+import type { Fetcher } from "../fetcher/types.ts";
+
+/*** EXPORT ------------------------------------------- ***/
+
+export class SchemaStore {
+ error = $state<string | null>(null);
+ loading = $state(false);
+ schema = $state<GraphQLSchema | null>(null);
+ sdl = $state<string>("");
+
+ async introspect(fetcher: Fetcher) {
+ this.loading = true;
+ this.error = null;
+
+ try {
+ const result = await fetcher({ query: getIntrospectionQuery() });
+ const data = (result as { data: IntrospectionQuery }).data;
+ this.schema = buildClientSchema(data);
+ this.sdl = printSchema(this.schema);
+ } catch(err) {
+ this.error = String(err);
+ } finally {
+ this.loading = false;
+ }
+ }
+}