Initial commit

This commit is contained in:
Bohdan Denysenko 2024-12-29 10:56:43 +00:00 • committed by GitHub
commit 864f37a652
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 45596 additions and 0 deletions

View 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 = {};

View 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;
}

View 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;
}

View 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
View 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);