Skip to content

Commit 9ddc740

Browse files
committed
First commit
0 parents  commit 9ddc740

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+40856
-0
lines changed

backend/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

backend/package-lock.json

Lines changed: 1421 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "graphql-server-example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "ts-node-dev src/index.ts",
6+
"scripts": {
7+
"dev": "ts-node-dev --transpile-only --ignore-watch node_modules src/index.ts"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"apollo-server": "^2.21.0",
14+
"graphql": "^15.5.0",
15+
"mongoose": "^5.11.17"
16+
},
17+
"devDependencies": {
18+
"ts-node": "^9.1.1",
19+
"ts-node-dev": "^1.1.1",
20+
"typescript": "^4.1.5"
21+
}
22+
}

backend/schema.graphql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
type User {
2+
id: ID!
3+
name: String!
4+
email: String!,
5+
}
6+
7+
type Message {
8+
id: ID!
9+
user: User!
10+
content: String!
11+
}
12+
13+
type Query {
14+
users: [User!]!
15+
user(id: ID!): User
16+
chat: [Message!]!
17+
}
18+
19+
type Subscription {
20+
messageSent: Message
21+
}
22+
23+
type Mutation {
24+
createUser(name: String!, email: String!): User
25+
deleteUser(id: ID!): User
26+
sendMessage(userID: ID!, content: String!): Message
27+
}

backend/src/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ApolloServer, gql } from 'apollo-server';
2+
import mongoose from 'mongoose';
3+
import path from 'path';
4+
import fs from 'fs';
5+
6+
import resolvers from './resolvers';
7+
8+
const pathToSchema = path.resolve(__dirname, '..', 'schema.graphql');
9+
const typeDefs = gql`${fs.readFileSync(pathToSchema)}`;
10+
11+
mongoose.connect('mongodb://localhost:27017/graphqlnode', {
12+
useNewUrlParser: true,
13+
useUnifiedTopology: true,
14+
});
15+
16+
const server = new ApolloServer({
17+
typeDefs,
18+
resolvers,
19+
tracing: true,
20+
subscriptions: {
21+
path: '/subscriptions'
22+
}
23+
});
24+
25+
server.listen().then(({ url }) => {
26+
console.log(`Server is ready at ${url}`);
27+
})

backend/src/resolvers.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import User from './schemas/Users';
2+
import Message from './schemas/Message';
3+
import { IResolvers, PubSub } from 'apollo-server';
4+
5+
const pubsub = new PubSub();
6+
const CHAT_CHANNEL = 'CHAT_CHANNEL';
7+
8+
const resolvers: IResolvers = {
9+
Query: {
10+
users: () => User.find(),
11+
user: (_, { id }) => User.findById(id),
12+
chat: () => {
13+
return Message.find().sort({ "timestamp": -1 }).populate('user');
14+
}
15+
},
16+
17+
Mutation: {
18+
createUser: (_, { name, email }) => User.create({ name, email }),
19+
deleteUser: (_, { id }) => User.remove(id),
20+
sendMessage: async (_, { userID, content }) => {
21+
// Busca o usuário por id
22+
const user = await User.findById(userID);
23+
24+
// Salva a mensagem no banco de dados
25+
const messageSent = await Message.create({ user, content });
26+
console.log(messageSent);
27+
28+
// Pública a mensagem
29+
pubsub.publish(CHAT_CHANNEL, { messageSent });
30+
31+
return messageSent;
32+
}
33+
},
34+
35+
Subscription: {
36+
messageSent: {
37+
subscribe: () => pubsub.asyncIterator([CHAT_CHANNEL])
38+
}
39+
}
40+
41+
};
42+
43+
export default resolvers;

backend/src/schemas/Message.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import mongoose from 'mongoose';
2+
3+
const MessageSchema = new mongoose.Schema({
4+
user: {
5+
type: mongoose.Schema.Types.ObjectId,
6+
ref: 'User'
7+
},
8+
content: String,
9+
}, { timestamps: true });
10+
11+
export default mongoose.model('Message', MessageSchema);

backend/src/schemas/Users.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {model, Schema} from 'mongoose';
2+
3+
const UserSchema = new Schema({
4+
name: String,
5+
email: String,
6+
});
7+
8+
export default model('User', UserSchema);

backend/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
5+
"strict": true, /* Enable all strict type-checking options. */
6+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
7+
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
8+
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
9+
"skipLibCheck": true, /* Skip type checking of declaration files. */
10+
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
11+
12+
"outDir": "./dist"
13+
}
14+
}

0 commit comments

Comments
 (0)