aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-macro-relay/bun-macro-relay.tsx
blob: 6c12c1f45f949594a4dc93bb5a1a3a230219012f (plain) (blame)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { parse, print } from "graphql";

//
// 1. Parse the GraphQL tag.
// 2. From the parsed GraphQL query, get the AST definition.
// 3. From the AST definition, inject an import to that file inside the artifact directory
// 4. (TODO) MD5 the printed source text
// 5. (TODO) At runtime, if md5 !== import.md5, then warn the user that the query has changed
//    but the file hasn't been updated so it must be reloaded.
// 6. Replace the TemplateLiteral with the default identifier from the injected import
let artifactDirectory: string = `__generated__`;

if (process.env.RELAY_ARTIFACT_DIRECTORY) {
  artifactDirectory = process.env.RELAY_ARTIFACT_DIRECTORY;
}

if (process.env.BUN_MACRO_RELAY_ARTIFACT_DIRECTORY) {
  artifactDirectory = process.env.BUN_MACRO_RELAY_ARTIFACT_DIRECTORY;
}

// TODO: platform-independent path cleaning
if (!artifactDirectory.startsWith("/")) {
  while (artifactDirectory.endsWith("/")) {
    artifactDirectory = artifactDirectory.substring(
      0,
      artifactDirectory.length - 1
    );
  }

  artifactDirectory = Bun.cwd + artifactDirectory;
}

export function graphql(node) {
  let query;

  if (node instanceof <call />) {
    query = node.arguments[0].toString();
  } else if (node instanceof <template />) {
    query = node.toString();
  }

  if (typeof query !== "string" || query.length === 0) {
    throw new Error("BunMacroRelay: Unexpected empty graphql string.");
  }

  const ast = parse(query);

  if (ast.definitions.length === 0) {
    throw new Error("BunMacroRelay: Unexpected empty graphql tag.");
  }

  const definition = ast.definitions[0];

  if (
    definition.kind !== "FragmentDefinition" &&
    definition.kind !== "OperationDefinition"
  ) {
    throw new Error(
      `BunMacroRelay: Expected a fragment, mutation, query, or subscription, got "${definition.kind}"`
    );
  }

  const graphqlDefinition = definition;

  const definitionName = graphqlDefinition.name && graphqlDefinition.name.value;
  if (!definitionName) {
    throw new Error("GraphQL operations and fragments must contain names");
  }

  const identifiername = `${definitionName}_$gql`;

  const importStmt = (
    <import
      default={identifiername}
      path={`${artifactDirectory}/${definitionName}.graphql`}
    />
  );

  return (
    <>
      <inject>{importStmt}</inject>
      <id to={importStmt.namespace[identifiername]} pure />
    </>
  );
}