Skip to content

Commit 519b3fe

Browse files
committed
preview link
1 parent 8316a97 commit 519b3fe

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

src/components/GraphiQLClient.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,48 @@
11
import { createGraphiQLFetcher } from "@graphiql/toolkit";
22
import GraphiQL from "graphiql";
33
import "graphiql/graphiql.css";
4-
import { useState } from "react";
4+
import "~/tailwind.css";
5+
import { useEffect, useState } from "react";
6+
import { use } from "marked";
57

68
const fetcher = createGraphiQLFetcher({
79
url: "https://api.cloudflare.com/client/v4/graphql",
810
});
911

1012
export function GraphiQLClient({ schema }: { schema: any }) {
13+
const [query, setQuery] = useState("");
14+
useEffect(() => {
15+
const search = window.location.search;
16+
const params = new URLSearchParams(search);
17+
18+
setQuery(atob(params.get("query") || ""));
19+
}, []);
20+
1121
const [authToken, setAuthToken] = useState("");
1222
return (
1323
<div className="flex h-screen flex-col">
14-
<div className="flex items-center gap-4 bg-gray-100 p-4">
24+
<div className="flex items-center">
1525
<label
1626
htmlFor="password"
1727
className="text-sm/6 font-medium text-gray-900"
1828
>
1929
Auth Token:
2030
</label>
21-
<div className="mt-2">
31+
<div>
2232
<input
2333
id="password"
2434
name="password"
2535
type="password"
2636
onChange={(e) => setAuthToken(e.target.value)}
27-
className="block w-full rounded-md bg-white px-3 py-1.5 text-base text-gray-900 outline outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline focus:outline-2 focus:-outline-offset-2 focus:outline-orange-500 sm:text-sm/6"
37+
className="w-full rounded-md border-2 border-gray-200 bg-white px-2 py-2 dark:border-gray-700 dark:bg-gray-800"
2838
/>
2939
</div>
3040
</div>
3141
<div className="flex-1">
3242
<GraphiQL
3343
fetcher={fetcher}
3444
schema={schema}
45+
query={query}
3546
headers={JSON.stringify({
3647
authorization: `Bearer ${authToken}`,
3748
})}

src/pages/analytics/graphql-api/api/[name].astro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { compile, type JSONSchema } from "json-schema-to-typescript";
44
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
55
import SchemaViewer from "~/components/models/SchemaViewer.astro";
66
import { Code } from "~/components";
7+
import { LinkButton } from "~/components";
78
import {
89
GraphQLToJSONSchemaBuilder,
910
type Field,
@@ -74,6 +75,9 @@ const gqlQuery = graphQLJSONSchemaToExample(
7475
<h2>{name.split(".").join(" ")}</h2>
7576
<p>{query.description}</p>
7677
<h3>Example</h3>
78+
<LinkButton href={`/analytics/graphql-api/graphiql?query=${btoa(gqlQuery)}`}
79+
>Open in GraphiQL</LinkButton
80+
>
7781
<Code code={gqlQuery} lang="graphql" />
7882
<h3 id="Parameters">Parameters</h3>
7983
<SchemaViewer schema={completeInputType} />

src/pages/analytics/graphql-api/graphiql.astro

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
---
22
import { getCollection } from "astro:content";
33
import { GraphiQLClient } from "~/components/GraphiQLClient";
4-
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
5-
import type { StarlightPageProps } from "@astrojs/starlight/components/StarlightPage.astro";
6-
import Header from "~/components/changelog-next/Header.astro";
74
const [schema] = await getCollection("graphql-api");
8-
9-
const props = {
10-
frontmatter: {
11-
title: "GraphiQL",
12-
tableOfContents: false,
13-
template: "splash",
14-
},
15-
hideTitle: true,
16-
hideBreadcrumbs: true,
17-
} as StarlightPageProps;
185
---
196

207
<GraphiQLClient

src/util/graphQLJSONSchemaToExample.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function pick3(arr: unknown[]) {
44
return arr.slice(0, 3);
55
}
66
export function graphQLJSONSchemaToExample(
7-
group: string,
7+
group: "organizations" | "zones" | "accounts",
88
name: string,
99
description: string,
1010
input: JSONSchema,
@@ -14,7 +14,14 @@ export function graphQLJSONSchemaToExample(
1414
.split(" ")
1515
.map((n: string) => n.charAt(0).toUpperCase() + String(n).slice(1))
1616
.join("")
17-
.replaceAll(".", "");
17+
.replaceAll(".", "")
18+
.replaceAll("-", "");
19+
20+
const groupMapping = {
21+
accounts: "account",
22+
zones: "zone",
23+
organizations: "organization",
24+
};
1825

1926
const outputSections = Object.keys(output.properties || {}).reduce(
2027
(outputQuery, key) => {
@@ -54,9 +61,9 @@ export function graphQLJSONSchemaToExample(
5461
);
5562

5663
const gqlQuery = `
57-
query Get${queryName}($${group}Tag: string, $datetimeStart: string, $datetimeEnd: string) {
64+
query Get${queryName}($${group}Tag: string!, $datetimeStart: Time, $datetimeEnd: Time) {
5865
viewer {
59-
${group}(filter: {${group}Tag: $${group}Tag}) {
66+
${group}(filter: {${groupMapping[group]}Tag: $${group}Tag}) {
6067
${name}(
6168
limit: 100,
6269
filter: {

0 commit comments

Comments
 (0)