Skip to content

Instantly share code, notes, and snippets.

@alvinslee
Created August 5, 2021 16:47
Show Gist options
  • Save alvinslee/1c08b47886e1cd209124b755f02763b1 to your computer and use it in GitHub Desktop.
Save alvinslee/1c08b47886e1cd209124b755f02763b1 to your computer and use it in GitHub Desktop.
Insomnia+GraphQL - server.js
const users = [
{
id: 1,
name: "Francis",
email: "franny@ha.com",
addresses: [
{
street: "1644 Melrose Place",
city: "Los Angeles",
country: "USA",
},
],
},
{
id: 2,
name: "Janet",
email: "janet@foo.com",
addresses: [
{
street: "1234 Lincoln Avenue",
city: "Dallas",
country: "USA",
},
],
}
];
const resolvers = {
Query: {
getUser: function (_, { id }) {
return users.find(u => u.id === id)
},
},
Mutation: {
addUser: (_, { user }) => {
const id = users.length + 1;
const newUser = {
id,
name: user.name,
email: user.email,
addresses: user.addresses,
};
users.push(newUser)
return newUser;
},
},
};
const fs = require("fs");
const { makeExecutableSchema } = require("@graphql-tools/schema");
// grab the GraphQL type definitions
let typeDefs = fs.readFileSync("schema.graphql", {
encoding: "utf8",
flag: "r",
});
// connect schema with resolvers
const schema = makeExecutableSchema({ typeDefs, resolvers });
const express = require("express");
const { graphqlHTTP } = require("express-graphql");
// make our /graphql endpoint where requests are sent
const app = express();
app.use(
"/graphql",
graphqlHTTP({
schema: schema,
})
);
app.listen(5000, () => console.log("Express is now live at localhost:5000"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment