Initial commit
This commit is contained in:
commit
864f37a652
48 changed files with 45596 additions and 0 deletions
15
app/server/graphql/graphql.ts
Normal file
15
app/server/graphql/graphql.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* gql imports */
|
||||
import { QueryResolvers } from "@generated/graphql";
|
||||
|
||||
/* Query */
|
||||
import { BooksManyResolver } from "./resolvers/booksManyResolver";
|
||||
import { BookOneResolver } from "./resolvers/bookOneResolver";
|
||||
|
||||
export const Query: QueryResolvers = {
|
||||
booksMany: BooksManyResolver,
|
||||
bookOne: BookOneResolver,
|
||||
};
|
||||
|
||||
export const Mutation = {};
|
||||
|
||||
export const Subscription = {};
|
||||
13
app/server/graphql/resolvers/bookOneResolver.ts
Normal file
13
app/server/graphql/resolvers/bookOneResolver.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import { BookInput, Book } from "@generated/graphql";
|
||||
import { books } from "./booksManyResolver";
|
||||
|
||||
export async function BookOneResolver(
|
||||
_: any,
|
||||
{ ...input }: BookInput,
|
||||
): Promise<Book> {
|
||||
console.log("Called 'BookOneResolver' resolver...");
|
||||
|
||||
const book = books.filter((item) => item.id === input.id)[0];
|
||||
|
||||
return book;
|
||||
}
|
||||
20
app/server/graphql/resolvers/booksManyResolver.ts
Normal file
20
app/server/graphql/resolvers/booksManyResolver.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { Book } from "@generated/graphql";
|
||||
|
||||
export const books = [
|
||||
{
|
||||
id: "1",
|
||||
title: "some-title-1",
|
||||
author: "someone",
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
title: "some-title-1",
|
||||
author: "someone",
|
||||
},
|
||||
];
|
||||
|
||||
export async function BooksManyResolver(_: any): Promise<Book[]> {
|
||||
console.log("Called 'BooksManyResolver' resolver...");
|
||||
|
||||
return books;
|
||||
}
|
||||
14
app/server/graphql/schema.graphql
Normal file
14
app/server/graphql/schema.graphql
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
type Query {
|
||||
booksMany: [Book]!
|
||||
bookOne(input: BookInput!): [Book]!
|
||||
}
|
||||
|
||||
type Book {
|
||||
id: ID!
|
||||
title: String!
|
||||
author: String!
|
||||
}
|
||||
|
||||
input BookInput {
|
||||
id: ID!
|
||||
}
|
||||
54
app/server/main.ts
Normal file
54
app/server/main.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import "dotenv/config";
|
||||
import express from "express";
|
||||
import cors from "cors";
|
||||
import ViteExpress from "vite-express";
|
||||
import { ApolloServer } from "@apollo/server";
|
||||
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
|
||||
import { expressMiddleware } from "@apollo/server/express4";
|
||||
import http from "http";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
import { Query, Mutation, Subscription } from "./graphql/graphql";
|
||||
|
||||
const __dirname = import.meta.dirname;
|
||||
const HOST = process.env.VITE_HOST || "localhost";
|
||||
const PORT = parseInt(process.env.VITE_PORT || "3000");
|
||||
const MOTD = `\n\n\n\x1b[35m-server is listening-\x1b[0m\n\nhttp://${HOST}:${PORT}\nhttp://localhost:${PORT}\n\n\n`;
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
||||
const typeDefs = fs.readFileSync(
|
||||
path.join(__dirname, "graphql", "schema.graphql"),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const resolvers = {
|
||||
Query,
|
||||
// Mutation,
|
||||
// Subscription,
|
||||
};
|
||||
|
||||
const apollo = new ApolloServer({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
plugins: [ApolloServerPluginDrainHttpServer({ httpServer: server })],
|
||||
});
|
||||
|
||||
await apollo.start();
|
||||
|
||||
app.use(
|
||||
"/graphql",
|
||||
cors<cors.CorsRequest>(),
|
||||
express.json(),
|
||||
expressMiddleware(apollo),
|
||||
);
|
||||
|
||||
await new Promise<void>((resolve) =>
|
||||
server.listen({ hostname: HOST, port: PORT }, resolve),
|
||||
);
|
||||
|
||||
console.log(MOTD);
|
||||
|
||||
ViteExpress.bind(app, server);
|
||||
Loading…
Add table
Add a link
Reference in a new issue