Repository: DefinitelyTyped/dt-mergebot
Pull request #309 (2021-01-04 18:26:52)

asJson() -> toJSON()
Cosmetic suggestion, please dismiss if it's not an improvement.

Replace `asJson()` with `toJSON()`, which `JSON.stringify()` calls automatically.


Patch:
diff --git a/src/commands/create-fixture.ts b/src/commands/create-fixture.ts
index f03a3ba..2c537e0 100644
--- a/src/commands/create-fixture.ts
+++ b/src/commands/create-fixture.ts
@@ -51,7 +51,7 @@ export default async function main(directory: string, overwriteInfo: boolean) {
 
   const mutationsFixturePath = join(fixturePath, "mutations.json");
   const mutations = await executePrActions(actions, response.data, /*dry*/ true);
-  writeJsonSync(mutationsFixturePath, mutations.map(m => m.asJson()));
+  writeJsonSync(mutationsFixturePath, mutations);
 
   console.log(`Recorded`);
 
diff --git a/src/execute-pr-actions.ts b/src/execute-pr-actions.ts
index 03de302..b6ea3f8 100644
--- a/src/execute-pr-actions.ts
+++ b/src/execute-pr-actions.ts
@@ -1,3 +1,4 @@
+import { MutationOptions } from "@apollo/client/core";
 import * as schema from "@octokit/graphql-schema/schema";
 import { PR as PRQueryResult, PR_repository_pullRequest } from "./queries/schema/PR";
 import { Actions, LabelNames, LabelName } from "./compute-pr-actions";
@@ -22,7 +23,8 @@ export async function executePrActions(actions: Actions, info: PRQueryResult, dr
     ]);
     if (!dry) {
         // Perform mutations one at a time
-        for (const mutation of mutations) await client.mutate<void, typeof mutations[number]["variables"]>(mutation);
+        for (const mutation of mutations)
+            await client.mutate(mutation as MutationOptions<void, typeof mutations[number]["variables"]>);
     }
     return mutations;
 }
diff --git a/src/graphql-client.ts b/src/graphql-client.ts
index 6df123e..f85c6f4 100644
--- a/src/graphql-client.ts
+++ b/src/graphql-client.ts
@@ -1,5 +1,5 @@
 import fetch from "node-fetch";
-import { ApolloClient, gql, HttpLink, InMemoryCache, TypedDocumentNode } from "@apollo/client/core";
+import { ApolloClient, gql, HttpLink, InMemoryCache, MutationOptions, TypedDocumentNode } from "@apollo/client/core";
 import { print } from "graphql";
 import * as schema from "@octokit/graphql-schema/schema";
 
@@ -15,20 +15,16 @@ const link = new HttpLink({ uri, headers, fetch });
 
 export const client = new ApolloClient({ cache, link, defaultOptions: { query: { errorPolicy: "all" } } });
 
-type Mutation<T> = {
-    mutation: TypedDocumentNode<void, { input: T }>,
-    variables: { input: T },
-    asJson: () => unknown
-};
-
-export function createMutation<T>(name: keyof schema.Mutation, input: T): Mutation<T> {
-    return {
-        mutation: gql`mutation($input: ${name[0].toUpperCase() + name.slice(1)}Input!) {
-                        ${name}(input: $input) { __typename } }
-                     ` as TypedDocumentNode<void, { input: T }>,
-        variables: { input },
-        asJson: function() { return { ...this, mutation: print(this.mutation) } }
+export function createMutation<T>(name: keyof schema.Mutation, input: T): MutationOptions<void, { input: T }> {
+    const mutation = {
+        toJSON: () => print(mutation),
+        ...(gql`mutation($input: ${name[0].toUpperCase() + name.slice(1)}Input!) {
+                    ${name}(input: $input) {
+                        __typename
+                    }
+                }` as TypedDocumentNode<void, { input: T }>),
     };
+    return { mutation, variables: { input } };
 }
 
 function getAuthToken() {
diff --git a/src/run.ts b/src/run.ts
index 250ce0a..68d6cec 100755
--- a/src/run.ts
+++ b/src/run.ts
@@ -87,7 +87,7 @@ const start = async function () {
         if (args["show-actions"]) show("Actions", actions);
         // Act on the actions
         const mutations = await executePrActions(actions, info.data, args.dry);
-        if (args["show-mutations"] ?? args.dry) show("Mutations", mutations.map(m => m.asJson()));
+        if (args["show-mutations"] ?? args.dry) show("Mutations", mutations);
     }
     if (args.dry || !args.cleanup) return;
     //


Test patch:
diff --git a/src/_tests/fixturedActions.test.ts b/src/_tests/fixturedActions.test.ts
index a3f4fec..d530a3d 100644
--- a/src/_tests/fixturedActions.test.ts
+++ b/src/_tests/fixturedActions.test.ts
@@ -49,7 +49,7 @@ async function testFixture(dir: string) {
   expect(JSONString(derived)).toMatchFile(derivedPath);
 
   const mutations = await executePrActions(action, response.data, /*dry*/ true);
-  expect(JSONString(mutations.map(m => m.asJson()))).toMatchFile(mutationsPath);
+  expect(JSONString(mutations)).toMatchFile(mutationsPath);
 }
 
 describe("Test fixtures", () => {