Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

express-graphql is outdated use graphql-http instead #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 79 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,85 @@
const express = require('express');
const bodyParser = require('body-parser');
const express = require("express");
const bodyParser = require("body-parser");
// const graphqlHttp = require("express-graphql");
const { createHandler } = require("graphql-http/lib/use/express");
const { buildSchema } = require("graphql");
const { ruruHTML } = require("ruru/server");

const app = express();

const events = [];

app.use(bodyParser.json());

app.get('/', (req, res, next) => {
res.send('Hello World!');
})
app.use(
"/graphql",
//middleware
createHandler({
// graphqlHttp({
schema: buildSchema(`
#define the types

type Events{
_id: ID!
title: String!
description: String!
price: Float!
date: String!
}

input EventInput{
title: String!
description: String!
price: Float!
date: String!
}

type RootQuery{
#name can be anything
#define real endpoints we support for incoming queries
events:[Events!]!
#no null
}

type RootMutation{
createEvent(eventInput: EventInput): Events
}
# root schema node
schema{
query: RootQuery #fetch data
mutation: RootMutation #change data
}
`),
rootValue: {
events: () => {
// resolver is just a function
// return ["cooking", "sailing", "coding"];
return events;
},
createEvent: (args) => {
// const eventName = args.name;
// return eventName;
const event = {
_id: Math.random().toString(),
title: args.eventInput.title,
description: args.eventInput.description,
price: +args.eventInput.price,
date: args.eventInput.date,
};
console.log(args);
events.push(event);
return event;
},
},
// this object has all resolver function - need to match our schema end points by name
// graphiql: true, // use ruru package
})
);

app.get("/", (_req, res) => {
res.type("html");
res.end(ruruHTML({ endpoint: "/graphql" }));
// res.send("hello world");
});

app.listen(3000);
app.listen(3000);
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"name": "graphql-react-event-booking",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4"
"body-parser": "^1.20.3",
"express": "^4.21.2",
"express-graphql": "^0.12.0",
"graphql": "^15.3.0",
"graphql-http": "^1.22.3",
"ruru": "^2.0.0-beta.17"
},
"devDependencies": {
"nodemon": "^1.18.7"
"nodemon": "^3.1.9"
}
}