Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fetch-expo-schema.ts

51 lines2.0 KB

1#!/usr/bin/env node
2/**
3 * Refreshes `schema/expo.graphql` from Expo's API, which answers introspection
4 * without a credential. eas-cli generates its own client from the same
5 * endpoint. Run `vp run generate` afterwards to regenerate the operations.
6 */
7import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
8import * as NodeServices from "@effect/platform-node/NodeServices";
9import * as Effect from "effect/Effect";
10import * as FileSystem from "effect/FileSystem";
11import * as Layer from "effect/Layer";
12import * as Path from "effect/Path";
13import * as Schema from "effect/Schema";
14import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
15import {
16 buildClientSchema,
17 getIntrospectionQuery,
18 lexicographicSortSchema,
19 printSchema,
20 type IntrospectionQuery,
21} from "graphql";
22​
23const EXPO_GRAPHQL_URL = "https://api.expo.dev/graphql";
24​
25const IntrospectionResponse = Schema.Struct({
26 data: Schema.Struct({ __schema: Schema.Unknown }),
27});
28​
29const program = Effect.gen(function* () {
30 const fs = yield* FileSystem.FileSystem;
31 const path = yield* Path.Path;
32 const client = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);
33 const response = yield* HttpClientRequest.post(EXPO_GRAPHQL_URL).pipe(
34 HttpClientRequest.bodyJson({ query: getIntrospectionQuery() }),
35 Effect.flatMap(client.execute),
36 Effect.flatMap(HttpClientResponse.schemaBodyJson(IntrospectionResponse)),
37 );
38 // Sorted so a refresh diffs by what Expo changed, not by its declaration order.
39 const sdl = printSchema(
40 lexicographicSortSchema(buildClientSchema(response.data as unknown as IntrospectionQuery)),
41 );
42 const target = path.join(import.meta.dirname, "..", "schema", "expo.graphql");
43 yield* fs.makeDirectory(path.dirname(target), { recursive: true });
44 yield* fs.writeFileString(target, `${sdl}\n`);
45 yield* Effect.log(`Wrote ${target}`);
46});
47​
48program.pipe(
49 Effect.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)),
50 NodeRuntime.runMain,
51);