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

Setup graphql-yoga #9

Merged
merged 1 commit into from Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions package.json
Expand Up @@ -24,12 +24,10 @@
},
"dependencies": {
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"graphql-tools": "^4.0.5",
"graphql-yoga": "^1.18.3",
"lodash": "^4.17.15",
"morgan": "^1.9.1",
"prisma-client-lib": "^1.34.7"
}
}
9 changes: 9 additions & 0 deletions src/graphql/index.js
@@ -0,0 +1,9 @@
import { makeExecutableSchema } from 'graphql-tools';
import userGraphql from './user.graphql';

const schema = makeExecutableSchema({
typeDefs: [userGraphql.typeDefs],
resolvers: [userGraphql.resolvers]
});

export default schema;
46 changes: 46 additions & 0 deletions src/graphql/user.graphql.js
@@ -0,0 +1,46 @@
import userService from '@/services/user.service';

const typeDefs = `
type Query {
users: [User!]!
user(id: ID!): User
}
type Mutation {
createUser(email: String, name: String!): User
updateUser(id: ID!, email: String, name: String!): User
deleteUser(id: ID!): User
}
type User {
id: ID!
email: String
name: String!
}
`;

const resolvers = {
Query: {
users() {
return userService.getUsers();
},
user(_, args) {
return userService.getUser(args.id);
}
},
Mutation: {
createUser(_, args) {
return userService.createUser(args);
},
updateUser(_, args) {
const { id, name, email } = args;
return userService.updateUser(id, { name, email });
},
deleteUser(_, args) {
return userService.deleteUser(args.id);
}
}
};

export default {
typeDefs,
resolvers
};
38 changes: 14 additions & 24 deletions src/index.js
@@ -1,31 +1,21 @@
import dotenv from 'dotenv';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import logger from 'morgan';
import { GraphQLServer } from 'graphql-yoga';
import schema from './graphql';

// Config env.
dotenv.config();

// Port.
const port = process.env.PORT || 9000;
// Initialize graphql server.
const server = new GraphQLServer({ schema });

// Initialize app.
const app = express();

// Log request to the console.
app.use(logger('dev'));

// Body parser.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Enable cors.
app.use(cors());

// Routes.
app.get('/', (req, res) => res.send('<p>👋 Xin chào</p>'));
// Graphql options.
const options = {
port: process.env.PORT || 9000
};

// Start server.
app.listen(port, () => {
console.log(`Server started on http://localhost:${port}`);
});
server.start(options, () =>
console.log(
`Server is running on http://localhost:${options.port}`
)
);