Skip to content

Commit

Permalink
add test js project
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed Sep 4, 2024
1 parent 3fd4514 commit 148b0fd
Show file tree
Hide file tree
Showing 8 changed files with 1,744 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/meskill/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1,622 changes: 1,622 additions & 0 deletions projects/meskill/package-lock.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions projects/meskill/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "meskill",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"apollo-server": "^3.13.0",
"axios": "^1.7.7",
"graphql": "^16.9.0"
}
}
5 changes: 5 additions & 0 deletions projects/meskill/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -e

node ./src/index.js
5 changes: 5 additions & 0 deletions projects/meskill/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -e

npm i
14 changes: 14 additions & 0 deletions projects/meskill/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema');
const resolvers = require('./resolvers');

// Initialize Apollo Server
const server = new ApolloServer({
typeDefs,
resolvers
});

// Start the server
server.listen({ port: 8000 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
41 changes: 41 additions & 0 deletions projects/meskill/src/resolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const axios = require('axios');

const BASE_URL = 'http://localhost:3000'; // This is your upstream REST API

const resolvers = {
Query: {
// Resolver for fetching all posts
posts: async () => {
const response = await axios.get(`${BASE_URL}/posts`);
return response.data;
},

// Resolver for fetching a single post by ID
post: async (_, { id }) => {
const response = await axios.get(`${BASE_URL}/posts/${id}`);
return response.data;
},

// Resolver for fetching all users
users: async () => {
const response = await axios.get(`${BASE_URL}/users`);
return response.data;
},

// Resolver for fetching a single user by ID
user: async (_, { id }) => {
const response = await axios.get(`${BASE_URL}/users/${id}`);
return response.data;
}
},

Post: {
// Resolver for fetching the user of a post
user: async (parent) => {
const response = await axios.get(`${BASE_URL}/users/${parent.userId}`);
return response.data;
}
}
};

module.exports = resolvers;
39 changes: 39 additions & 0 deletions projects/meskill/src/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { gql } = require('apollo-server');

const typeDefs = gql`
type Query {
posts: [Post]
post(id: ID!): Post
users: [User]
user(id: ID!): User
}
type Post {
id: ID
title: String
body: String
user: User
}
type User {
id: ID
name: String
username: String
email: String
address: Address
phone: String
website: String
}
type Address {
zipcode: String
geo: Geo
}
type Geo {
lat: Float
lng: Float
}
`;

module.exports = typeDefs;

0 comments on commit 148b0fd

Please sign in to comment.