samnoh / graphql-demo

User & Movie Query Server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Graph QL

  • graphql-yoga
  • mongoose
  • ES6+ syntax using babel

TIL

GraphQL

  • Schemas
    • ! means a mandatory variable
# import Test from 'test.graphql'

type Query {
    tests: [Test]!
}

type Mutation {
    addTest(text: String!): Test
}
  • Resolvers
const resolvers = {
    Query: {
        tests: () => {...}
    },
    Mutation: {
        addTest: (_, { id }) => {...}
    }
}
  • GraphQL Server
import { GraphQLServer } from 'graphql-yoga';
import { importSchema } from 'graphql-import';

const typeDefs = importSchema(path.join(__dirname, 'graphql', 'schema.graphql'));
const server = new GraphQLServer({ typeDefs, resolvers });
server.start(() => console.log('GraphQL server running on localhost:4000'));
  • Requests
Query {
    Test {
        text
    }
}
Mutation {
    addTest(text: "Hello") {
        text
    }
}

Babel + nodemon + pm2

  • Install
npm install --save-dev @babel/cli @babel/core @babel/node @babel/preset-env nodemon
npm install -g pm2 && npm install --save pm2
  • .babelrc
{ "presets": ["@babel/preset-env"] }
  • package.json
    • --copy-files -> build with non-JS files including graphql
"scripts": {
    "start": "NODE_ENV=production pm2 start build/app.js -i 0",
    "dev": "nodemon --exec babel-node src/app.js",
    "build": "babel src --out-dir build --copy-files",
}
  • dotenv
import 'dotenv/config'; // import dotenv at the very start

About

User & Movie Query Server


Languages

Language:JavaScript 100.0%