Skip to content
Snippets Groups Projects
Commit 8e445d85 authored by kevinle350's avatar kevinle350
Browse files

Backend Done, need to integrate with frontend

parent ba9edca2
No related branches found
No related tags found
No related merge requests found
# dependencies
/node_modules
\ No newline at end of file
[
{
"id": 1,
"plantName": "Lily",
"maxWaterUsage": "3",
"minMoistureLevel": "LOW",
"hours": "2PM - 5PM"
},
{
"id": 2,
"plantName": "Cactus",
"maxWaterUsage": "2",
"minMoistureLevel": "MED",
"hours": "9PM - 5PM"
},
{
"id": 3,
"plantName": "Tree",
"maxWaterUsage": "4",
"minMoistureLevel": "HIGH",
"hours": "1PM - 3PM"
}
]
\ No newline at end of file
# To run
npm run devStart: To run server
http://localhost:5000/graphql
\ No newline at end of file
const graphql = require('graphql');
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull
} = require('graphql');
const PlantType = new GraphQLObjectType({
name: 'Plant',
description: 'This represents a plant',
fields: () => ({
id: { type: GraphQLNonNull(GraphQLInt) },
plantName: { type: GraphQLNonNull(GraphQLString) },
maxWaterUsage: { type: GraphQLNonNull(GraphQLString) }, // might need to change to int later
minMoistureLevel: { type: GraphQLNonNull(GraphQLString) },
hours: { type: GraphQLNonNull(GraphQLString) },
})
})
module.exports = PlantType;
\ No newline at end of file
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLList,
GraphQLInt,
GraphQLNonNull
} = require('graphql')
const PLANT_DATA = require('../Data/PLANT_DATA.json')
const PlantType = require('./TypeDefs/PlantType')
const RootQueryType = new GraphQLObjectType({
name: 'Query',
description: 'Root Query',
fields: () => ({
plants: {
type: new GraphQLList(PlantType),
description: 'A list of all plants',
resolve: () => PLANT_DATA
},
})
})
const Mutation = new GraphQLObjectType({
name: "Mutation",
fields: {
createPlant: {
type: PlantType,
args: {
plantName: { type: GraphQLNonNull(GraphQLString) },
maxWaterUsage: { type: GraphQLNonNull(GraphQLString) }, // might need to change to int later
minMoistureLevel: { type: GraphQLNonNull(GraphQLString) },
hours: { type: GraphQLNonNull(GraphQLString) },
},
resolve(parent, args) {
PLANT_DATA.push({id: PLANT_DATA.length + 1, plantName: args.plantName, maxWaterUsage: args.maxWaterUsage, minMoistureLevel: args.minMoistureLevel, hours: args.hours})
return args
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQueryType,
mutation: Mutation
})
\ No newline at end of file
This diff is collapsed.
{
"name": "graphql-app",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"devStart": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"express-graphql": "^0.12.0",
"graphql": "^15.8.0"
},
"devDependencies": {
"nodemon": "^2.0.16"
}
}
const express = require('express')
const {graphqlHTTP} = require('express-graphql')
const cors = require("cors");
const PORT = 5000;
const app = express()
app.use(cors());
app.use(express.json());
const schema = require('./Schemas/index.js');
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true
}))
app.listen(PORT, () => console.log('Server Running'))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment