Skip to content

Commit 7a169ef

Browse files
committed
Start GraphQL API
1 parent 1ddb096 commit 7a169ef

File tree

12 files changed

+848
-3
lines changed

12 files changed

+848
-3
lines changed

api/graphql/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { FABRuntime } from "@fab/core";
22
import { makeGraphQLHandler } from "@glenstack/cf-workers-graphql";
33
import { schema } from "./schema/index";
4+
import { voyager } from "./voyager";
45

56
const graphQLHandler = makeGraphQLHandler(schema);
67

78
export default function graphql({ Router }: FABRuntime) {
89
Router.on("/graphql", ({ request }) => graphQLHandler(request));
10+
Router.on(
11+
"/voyager",
12+
async () =>
13+
new Response(voyager, { headers: { "Content-Type": "text/html" } })
14+
);
915
}

api/graphql/schema/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { makeExecutableSchema } from "@graphql-tools/schema";
22
import gql from "graphql-tag";
3+
import { typeDefs as scalarTypeDefs } from "./types/scalars";
4+
import { typeDefs as relayTypeDefs } from "./types/relay";
5+
import { typeDefs as documentTypeDefs } from "./types/document";
6+
import { typeDefs as searchResultTypeDefs } from "./types/searchResult";
37

48
const typeDefs = gql`
59
type Query {
@@ -13,4 +17,13 @@ const resolvers = {
1317
},
1418
};
1519

16-
export const schema = makeExecutableSchema({ typeDefs, resolvers });
20+
export const schema = makeExecutableSchema({
21+
typeDefs: [
22+
typeDefs,
23+
...scalarTypeDefs,
24+
...relayTypeDefs,
25+
documentTypeDefs,
26+
searchResultTypeDefs,
27+
],
28+
resolvers: [resolvers],
29+
});

api/graphql/schema/types/document.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { connectionDefinitions } from "graphql-relay-tools";
2+
import gql from "graphql-tag";
3+
4+
const { connectionType: DocumentConnection } = connectionDefinitions({
5+
name: "Document",
6+
});
7+
8+
export const typeDefs = gql`
9+
type Document implements Node {
10+
id: ID!
11+
url: URL!
12+
}
13+
14+
${DocumentConnection}
15+
`;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { typeDefs as nodeTypeDefs } from "./node";
2+
import { typeDefs as pageInfoTypeDefs } from "./pageInfo";
3+
4+
export const typeDefs = [nodeTypeDefs, pageInfoTypeDefs];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import gql from "graphql-tag";
2+
import { nodeInterface, nodeField } from "graphql-relay-tools";
3+
4+
export const typeDefs = gql`
5+
${nodeInterface}
6+
7+
extend type Query {
8+
${nodeField}
9+
}
10+
`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import gql from "graphql-tag";
2+
import { pageInfoType } from "graphql-relay-tools";
3+
4+
export const typeDefs = gql`
5+
${pageInfoType}
6+
`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { typeDefs as urlTypeDefs } from "./url";
2+
3+
export const typeDefs = [urlTypeDefs];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import gql from "graphql-tag";
2+
3+
export const typeDefs = gql`
4+
scalar URL
5+
`;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { connectionArgs } from "graphql-relay-tools";
2+
import gql from "graphql-tag";
3+
4+
export const typeDefs = gql`
5+
type SearchResult {
6+
documents${connectionArgs()}: DocumentConnection!
7+
}
8+
9+
extend type Query {
10+
search(query: String!): SearchResult!
11+
}
12+
`;

api/graphql/voyager.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export const voyager = `<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="https://cdn.jsdelivr.net/npm/react@16/umd/react.production.min.js"></script>
5+
<script src="https://cdn.jsdelivr.net/npm/react-dom@16/umd/react-dom.production.min.js"></script>
6+
7+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.css" />
8+
<script src="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.min.js"></script>
9+
</head>
10+
<body>
11+
<div id="voyager">Loading...</div>
12+
<script>
13+
function introspectionProvider(introspectionQuery) {
14+
return fetch('/graphql', {
15+
method: 'POST',
16+
headers: { 'Content-Type': 'application/json' },
17+
body: JSON.stringify({ query: introspectionQuery }),
18+
credentials: 'include'
19+
}).then(function (response) {
20+
return response.json()
21+
})
22+
}
23+
24+
GraphQLVoyager.init(document.getElementById('voyager'), {
25+
introspection: introspectionProvider
26+
})
27+
</script>
28+
</body>
29+
</html>`;

0 commit comments

Comments
 (0)