Repository navigation
Expand file tree
/
Copy pathfetch-expo-schema.ts
More file actions
54 lines (50 loc) · 2.04 KB
/
Copy pathfetch-expo-schema.ts
File metadata and controls
54 lines (50 loc) · 2.04 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
#!/usr/bin/env node
/**
* Refreshes `schema/expo.graphql` from Expo's API, which answers introspection
* without a credential. eas-cli generates its own client from the same
* endpoint. Run `vp run generate` afterwards to regenerate the operations.
*/
import { pipe } from "effect/Function";
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
import {
buildClientSchema,
getIntrospectionQuery,
lexicographicSortSchema,
printSchema,
type IntrospectionQuery,
} from "graphql";
const EXPO_GRAPHQL_URL = "https://api.expo.dev/graphql";
const IntrospectionResponse = Schema.Struct({
data: Schema.Struct({ __schema: Schema.Unknown }),
});
const program = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const client = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);
const response = yield* HttpClientRequest.post(EXPO_GRAPHQL_URL).pipe(
HttpClientRequest.bodyJson({ query: getIntrospectionQuery() }),
Effect.flatMap(client.execute),
Effect.flatMap(HttpClientResponse.schemaBodyJson(IntrospectionResponse)),
);
// Sorted so a refresh diffs by what Expo changed, not by its declaration order.
const sdl = pipe(
buildClientSchema(response.data as unknown as IntrospectionQuery),
lexicographicSortSchema,
printSchema,
);
const target = path.join(import.meta.dirname, "..", "schema", "expo.graphql");
yield* fs.makeDirectory(path.dirname(target), { recursive: true });
yield* fs.writeFileString(target, `${sdl}\n`);
yield* Effect.log(`Wrote ${target}`);
});
program.pipe(
Effect.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)),
NodeRuntime.runMain,
);