Skip to content

Commit 0553ed0

Browse files
author
askbeka
committed
asd
0 parents  commit 0553ed0

9 files changed

+2735
-0
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
end_of_line = lf
10+
# editorconfig-tools is unable to ignore longs strings or urls
11+
max_line_length = null

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "graphql-demo",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"forever": "^0.15.3",
8+
"graphql-yoga": "^1.16.7"
9+
},
10+
"scripts": {
11+
"start": "forever -w --watchDirectory ./src ./src/index.js"
12+
}
13+
}

resolvers.js.bkp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const accounts = [
2+
{ name: 'Beknar' },
3+
{ name: 'Roman' },
4+
];
5+
6+
const transactions = [
7+
{ id: 0, from: 'Roman', to: 'Beknar', amount: 1000 },
8+
];
9+
10+
module.exports = {
11+
Query: {
12+
accounts: () => accounts,
13+
transactions: () => transactions,
14+
},
15+
Mutation: {
16+
addAccount: (parent, { name }) => {
17+
const newAccount = { id: accounts.length, name };
18+
accounts.push(newAccount);
19+
20+
return account;
21+
},
22+
sendMoney: (parent, { from, to, amount }, { pubsub }) => {
23+
const newTransaction = { id: transactions.length, from, to, amount };
24+
transactions.push(newTransaction);
25+
26+
pubsub.publish(to, { newTransaction: newTransaction });
27+
28+
return newTransaction;
29+
},
30+
},
31+
Subscription: {
32+
newTransaction: {
33+
subscribe: (parent, { name }, { pubsub }) => pubsub.asyncIterator(name),
34+
}
35+
},
36+
Account: {
37+
sent: (parent) => transactions.filter(tr => parent.name === tr.from),
38+
received: (parent) => transactions.filter(tr => parent.name === tr.name),
39+
},
40+
Transaction: {
41+
from: (parent) => accounts.find(acc => acc.name === parent.from),
42+
to: (parent) => accounts.find(acc => acc.name === parent.to),
43+
}
44+
}

schema.graphql.bkp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type Query {
2+
accounts: [Account!]!
3+
transactions: [Transaction!]!
4+
}
5+
6+
type Mutation {
7+
addAccount(name: String!): Account
8+
sendMoney(from: String!, to: String!, amount: Float!): Transaction!
9+
}
10+
11+
type Subscription {
12+
newTransaction(name: String!): Transaction,
13+
}
14+
15+
type Account {
16+
name: String!
17+
sent: [Transaction!]!
18+
received: [Transaction]!
19+
}
20+
21+
type Transaction {
22+
id: ID!
23+
from: Account!
24+
to: Account!
25+
amount: Float!
26+
}

src/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { GraphQLServer, PubSub } = require('graphql-yoga')
2+
const resolvers = require('./resolvers')
3+
4+
5+
pubsub = new PubSub();
6+
const server = new GraphQLServer({
7+
typeDefs: './src/schema.graphql',
8+
resolvers,
9+
context: {
10+
pubsub,
11+
},
12+
})
13+
14+
server.start(() => console.log('Server is running on http://localhost:4000'))

src/resolvers.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = {
2+
}

src/schema.graphql

Whitespace-only changes.

0 commit comments

Comments
 (0)