Skip to content

Commit

Permalink
Merge pull request #8 from skanehira/support-comment-endpoint
Browse files Browse the repository at this point in the history
feat: set endpoint with comment.
  • Loading branch information
skanehira committed Feb 11, 2023
2 parents 41e5b0e + d9b9baf commit 470bda7
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ GraphQL client for Vim/Neovim.

- `:GraphQLSetEndpoint`
Set graphql endpoint.
Also you can add the comment to set endpoint.

If you want see more info, please refer help.

Expand Down
50 changes: 39 additions & 11 deletions denops/graphql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,46 @@ export async function edit(denops: Denops): Promise<void> {
await openRespBuffer(denops, `${fname}.output.json`);
}

export function getEndpoint(
query: string[],
bufName: string,
): string {
// if first line is comment and its has 'endpoint: ' prefix, then use endpoint
const comment = query.at(0);
if (comment && comment.includes("# endpoint: ")) {
const matched = comment.match(/http.*:\/\/.*/);
if (matched) {
return matched[0];
}
}

const endpoint = endpoints[bufName];
if (endpoint) {
return endpoint;
}

throw new Error(
"not found endpoint, please set endpoint by :GraphQLSetEndpoint",
);
}

export async function execute(denops: Denops): Promise<void> {
if (await denops.eval("&ft") !== "graphql") {
throw new Error(`file type is not 'graphql'`);
}
const queryBufName = await denops.call("bufname") as string;
const endpoint = endpoints[queryBufName];
if (!endpoint) {
throw new Error(
"not found endpoint, please set endpoint by :GraphQLSetEndpoint",
);
}
const query = await denops.call(
"getbufline",
queryBufName,
1,
"$",
) as string[];
const endpoint = getEndpoint(query, queryBufName);
console.log(endpoint);

const respBufName = `${queryBufName}.output.json`;
await openRespBuffer(denops, respBufName);

const query =
(await denops.call("getbufline", queryBufName, 1, "$") as string[])
.join("\n");

const variableBufName = `${queryBufName}.variables.json`;
let variables = "";
if (await denops.call("bufexists", variableBufName)) {
Expand Down Expand Up @@ -90,7 +111,7 @@ export async function execute(denops: Denops): Promise<void> {
method: "POST",
headers: headers,
body: JSON.stringify({
query: query,
query: query.join("\n"),
variables: variables ? JSON.parse(variables) : null,
}),
});
Expand All @@ -117,6 +138,13 @@ export async function setEndpoint(denops: Denops, arg: unknown): Promise<void> {
endpoints[bufname] = arg as string;
}

// for test
export function clearEndpoint() {
for (const k of Object.keys(endpoints)) {
delete endpoints[k];
}
}

export async function editHttpHeader(
denops: Denops,
): Promise<void> {
Expand Down
70 changes: 68 additions & 2 deletions denops/graphql/graphql_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals, test } from "./deps_test.ts";
import { assertEquals, assertRejects, test } from "./deps_test.ts";
import { Denops } from "./deps.ts";
import { edit, execute, setEndpoint } from "./graphql.ts";
import { clearEndpoint, edit, execute, setEndpoint } from "./graphql.ts";

const testEndpoint =
"https://swapi-graphql.netlify.app/.netlify/functions/index";
Expand All @@ -9,6 +9,7 @@ test({
mode: "all",
name: "execute without variables",
fn: async (denops: Denops) => {
clearEndpoint();
const bufname = "test.graphql";
await denops.cmd(`new ${bufname} | set ft=graphql`);
const q = `
Expand Down Expand Up @@ -89,3 +90,68 @@ query Query($limit: Int!) {
assertEquals(JSON.parse(got), JSON.parse(want));
},
});

test({
mode: "all",
name: "execute using endpoint that in comment",
fn: async (denops: Denops) => {
clearEndpoint();
const bufname = "test.graphql";
await denops.cmd(`new ${bufname} | set ft=graphql`);
const q =
`# endpoint: https://swapi-graphql.netlify.app/.netlify/functions/index
query Query {
allFilms(first: 1) {
films {
title
}
}
} `;

await denops.call("setline", 1, q);
await execute(denops);
const got = (await denops.call(
"getbufline",
`${bufname}.output.json`,
1,
"$",
) as string[])
.join(
"\n",
);

const want = await Deno.readTextFile(
"denops/graphql/testdata/output.json",
);

assertEquals(JSON.parse(got), JSON.parse(want));
},
});

test({
mode: "all",
name: "not found endpoint",
fn: async (denops: Denops) => {
clearEndpoint();
const bufname = "test.graphql";
await denops.cmd(`new ${bufname} | set ft=graphql`);
const q = `# endpoint:
query Query {
allFilms(first: 1) {
films {
title
}
}
} `;

await denops.call("setline", 1, q);

await assertRejects(
async () => {
await execute(denops);
},
Error,
"not found endpoint, please set endpoint by :GraphQLSetEndpoint",
);
},
});
17 changes: 16 additions & 1 deletion doc/graphql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,27 @@ COMMANDS *graphql-commands*
:GraphQLSetEndpoint {endpoint} *:GraphQLSetEndpoint*
Set graphql endpoint.
You have to set endpoint before execute query.
You should set endpoint before execute query.

The endpoint will be relate the current buffer name.
So if you change the file name or open the other file,
you have to set endpoint again.

Also you can add the endpoint with comment.
In that case, please add `# endpoint: <your endpoint>` at first line.
>
# endpoint: https://api.github.com/graphql
query {
viewer {
publicKeys(first: 5) {
nodes {
key
}
}
}
}
>
------------------------------------------------------------------------------
BUFFER *graphql-buffers*

Expand Down

0 comments on commit 470bda7

Please sign in to comment.