5
\$\begingroup\$

I recently did a quick take-home test for a potential job opportunity. It included building a GraphQL API with nodeJS. I am not an expert in node (mostly use Python at work) but I have used it for some REST APIs /w Express. This was my first time using Nexus GraphQL.

The feedback I got was strong skills in Nexus and postgres, but lacking in the abstraction and organisational side.

I am looking for advice about what could be improved on the abstraction/organisational side of things.

Project code is available at: https://github.com/Sajomancer/node-graphql

Here is the general structure:

πŸ“¦ 
β”œβ”€ Dockerfile
β”œβ”€ README.md
β”œβ”€ backend
β”‚ β”œβ”€ createServer.ts // dependency injection of services
β”‚ β”œβ”€ graphql // GraphQL models with their queries/mutations
β”‚ β”‚ β”œβ”€ Ingredient.ts 
β”‚ β”‚ β”œβ”€ Recipe.ts
β”‚ β”‚ └─ index.ts
β”‚ β”œβ”€ index.ts
β”‚ β”œβ”€ models // internal models
β”‚ β”‚ β”œβ”€ Ingredient.ts
β”‚ β”‚ β”œβ”€ Recipe.ts
β”‚ β”‚ └─ index.ts
β”‚ β”œβ”€ schema.ts
β”‚ └─ services // Services for interfacing with DB
β”‚   β”œβ”€ DatabaseService.ts
β”‚   β”œβ”€ IngredientService.ts
β”‚   └─ RecipeService.ts
β”œβ”€ data
β”‚ β”œβ”€ ingredients.csv
β”‚ β”œβ”€ init.sql
β”‚ └─ price_changes.json
β”œβ”€ docker-compose.yml
β”œβ”€ eslint.config.mjs
β”œβ”€ generated
β”‚ β”œβ”€ nexus-typegen.ts
β”‚ └─ schema.graphql
β”œβ”€ jest.config.ts
β”œβ”€ package-lock.json
β”œβ”€ package.json
β”œβ”€ tests
β”‚ β”œβ”€ api.test.ts
β”‚ β”œβ”€ ingredientService.test.ts
β”‚ └─ recipeService.test.ts
└─ tsconfig.json

backend/graphql/Ingredient.ts

import { objectType, extendType, nonNull, intArg } from "nexus";
export const Ingredient = objectType({
 name: "Ingredient",
 definition(t) {
 t.int("id");
 t.string("name");
 t.string("supplier");
 t.float("currentPrice"); // no aliases?
 },
});
export const IngredientQuery = extendType({
 type: "Query",
 definition(t) {
 t.list.field("ingredients", {
 type: "Ingredient",
 resolve: async (_parent, _args, ctx) => {
 return ctx.ingredientService.getAllIngredients();
 },
 });
 t.field("ingredient", {
 type: "Ingredient",
 args: { id: nonNull(intArg()) },
 resolve: (_parent, args, ctx) =>
 ctx.ingredientService.getIngredientById(args.id),
 });
 },
});

backend/graphql/Recipe.ts

import {
 arg,
 extendType,
 inputObjectType,
 intArg,
 nonNull,
 objectType,
} from "nexus";
import { Ingredient } from "./Ingredient";
export const Recipe = objectType({
 name: "Recipe",
 definition(t) {
 t.int("id");
 t.string("title");
 t.list.field("ingredients", { type: Ingredient });
 t.string("method");
 t.float("totalCost");
 },
});
export const CreateRecipeInput = inputObjectType({
 name: "CreateRecipeInput",
 definition(t) {
 t.string("title");
 t.string("method");
 t.list.field("ingredientIds", { type: "Int" });
 },
});
export const CreateRecipeResult = objectType({
 name: "CreateRecipeResult",
 definition(t) {
 t.int("id");
 t.string("title");
 // could return ingredients/method as well but I don't think it's necessary
 },
});
export const RecipeQuery = extendType({
 type: "Query",
 definition(t) {
 t.list.field("recipes", {
 type: "Recipe",
 resolve: (_parent, _args, ctx) => ctx.recipeService.getAllRecipes(),
 });
 t.field("recipe", {
 type: "Recipe",
 args: { id: nonNull(intArg()) },
 resolve: (_parent, args, ctx) => ctx.recipeService.getRecipeById(args.id),
 });
 },
});
export const RecipeMutation = extendType({
 type: "Mutation",
 definition(t) {
 t.field("createRecipe", {
 type: "CreateRecipeResult",
 args: { data: nonNull(arg({ type: "CreateRecipeInput" })) },
 resolve: (_parent, args, ctx) =>
 ctx.recipeService.createRecipe(args.data),
 });
 },
});

backend/models/Ingredient.ts

export interface Ingredient {
 id: number;
 name: string;
 supplier: string;
 currentPrice: number;
}

backend/models/Recipe.ts

import { Ingredient } from "./Ingredient";
export interface Recipe {
 id: number;
 title: string;
 method: string;
 ingredients: Ingredient[];
 totalCost: number;
}
export interface CreateRecipeInput {
 title: string;
 method: string;
 ingredientIds: number[];
}
export interface CreateRecipeResult {
 id: number;
 title: string;
}

backend/createServer.ts

import { ApolloServer } from "apollo-server";
import { schema } from "./schema";
import { DatabaseService } from "./services/DatabaseService";
import { RecipeService } from "./services/RecipeService";
import { IngredientService } from "./services/IngredientService";
export function createServer(databaseUrl: string | undefined): {
 server: ApolloServer;
 dbService: DatabaseService;
} {
 if (!databaseUrl) {
 throw new Error("DATABASE_URL environment variable is mandatory.");
 }
 const dbService = new DatabaseService(databaseUrl);
 const recipeService = new RecipeService(dbService);
 const ingredientService = new IngredientService(dbService);
 const server = new ApolloServer({
 schema,
 context: () => ({ recipeService, ingredientService }),
 });
 return { server, dbService };
}

backend/index.ts

import { createServer } from "./createServer";
const databaseUrl = process.env.DATABASE_URL;
const { server } = createServer(databaseUrl);
server.listen({ port: 4000 }).then(({ url }) => {
 console.log(`Server running at ${url}`);
});

backend/schema.ts

import { makeSchema } from "nexus";
import path from "path";
import * as types from "./graphql";
export const schema = makeSchema({
 types: types,
 outputs: {
 schema: path.join(__dirname, "../generated/schema.graphql"),
 typegen: path.join(__dirname, "../generated/nexus-typegen.ts"),
 },
});
asked Mar 25 at 14:24
\$\endgroup\$
0

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.