Skip to content

Commit 4d65086

Browse files
committed
Task_4_Done
1 parent 019167f commit 4d65086

File tree

7 files changed

+79
-23
lines changed

7 files changed

+79
-23
lines changed

.eslintrc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ecmaVersion": 2018
1717
},
1818
"rules": {
19-
"global-require": 0
19+
"global-require": 0,
20+
"import/no-extraneous-dependencies": 0
2021
}
21-
}
22+
}

scripts/createTestData.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node
22
/* eslint-disable no-console */
33

4+
require('dotenv/config');
5+
46
const db = require('../src/db');
57

68
Promise.all(db.createTables()).then(() => {

scripts/dropTables.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node
22
/* eslint-disable no-console */
33

4+
require('dotenv/config');
5+
46
const db = require('../src/db');
57

68
Promise.all(db.dropTables()).then(() => {

src/db.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const createTables = () => {
2525
name VARCHAR(30) NOT NULL,
2626
username VARCHAR(30) NOT NULL,
2727
email VARCHAR(30) NOT NULL UNIQUE,
28-
password_digest VARCHAR(100) NOT NULL
28+
password_digest VARCHAR(100) NOT NULL,
29+
location VARCHAR(100)
2930
)`,
3031
`CREATE TABLE IF NOT EXISTS posts(
3132
id UUID PRIMARY KEY,
@@ -60,12 +61,12 @@ const dropTables = () => {
6061
const createTestData = () => {
6162
// password for test user: password
6263
const testDataQueries = [
63-
`INSERT INTO users(id, name, username, email, password_digest)
64-
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b58c', 'Tyler', 'tyler1337', '[email protected]', '$2b$10$yv9DxXTpvBmBYKu8rXoSIONn3BZB5/jQRDPMKt/YUAq8eTYoXGwKu')
64+
`INSERT INTO users(id, name, username, email, password_digest, location)
65+
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b58c', 'Tyler', 'tyler1337', '[email protected]', '$2b$10$yv9DxXTpvBmBYKu8rXoSIONn3BZB5/jQRDPMKt/YUAq8eTYoXGwKu', 'Budapest')
6566
RETURNING *
6667
`,
67-
`INSERT INTO users(id, name, username, email, password_digest)
68-
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b68c', 'Marla', 'marlaSinger', '[email protected]', '$2b$10$yv9DxXTpvBmBYKu8rXoSIONn3BZB5/jQRDPMKt/YUAq8eTYoXGwKu')
68+
`INSERT INTO users(id, name, username, email, password_digest, location)
69+
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b68c', 'Marla', 'marlaSinger', '[email protected]', '$2b$10$yv9DxXTpvBmBYKu8rXoSIONn3BZB5/jQRDPMKt/YUAq8eTYoXGwKu', 'London')
6970
RETURNING *
7071
`,
7172
`INSERT INTO posts(id, title, description, content, author, timestamp)

src/fetcher.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
const querystring = require('querystring');
2+
13
const bcrypt = require('bcrypt');
24
const { get } = require('lodash');
35
const uuid = require('uuidv4').default;
6+
const axios = require('axios');
47

8+
const config = require('./config');
59
const db = require('./db');
610

711

@@ -92,12 +96,12 @@ const hashPassword = (password) => new Promise((resolve, reject) => (
9296

9397
const createUser = async (args) => {
9498
const {
95-
password, name, username, email,
99+
password, name, username, email, location,
96100
} = get(args, 'input', {});
97101
const hashedPassword = await hashPassword(password);
98102
const { rows } = await db.query({
99-
text: 'INSERT INTO users(id, name, username, email, password_digest) VALUES($1, $2, $3, $4, $5) RETURNING *',
100-
values: [uuid(), name, username, email, hashedPassword],
103+
text: 'INSERT INTO users(id, name, username, email, password_digest, location) VALUES($1, $2, $3, $4, $5, $6) RETURNING *',
104+
values: [uuid(), name, username, email, hashedPassword, location],
101105
});
102106
const user = rows[0];
103107
delete user.password_digest;
@@ -125,13 +129,39 @@ const createComment = async (args) => {
125129
};
126130

127131

128-
// TODO fire a request to the open weather API to fetch the today's weather
129-
// https://api.openweathermap.org/data/2.5/weather
130-
// "config.openWeatherMapAPIKey" should contain your API key from the environment
131-
// Make sure you only return items that you have declared in your schema!
132-
function getWeather() {
133-
// eslint-disable-next-line no-console
134-
console.log('JS Conf Budapest 2019!');
132+
async function getWeather({ location }) {
133+
const APPID = config.openWeatherMapAPIKey;
134+
const query = querystring.stringify({
135+
q: location,
136+
units: 'metric',
137+
APPID,
138+
});
139+
140+
const url = new URL('https://api.openweathermap.org/data/2.5/weather');
141+
url.search = query;
142+
143+
const { data } = await axios
144+
.get(url.toString());
145+
146+
const {
147+
coord: {
148+
lat,
149+
lon,
150+
},
151+
main: {
152+
humidity,
153+
temp,
154+
pressure,
155+
},
156+
} = data;
157+
158+
return {
159+
lat,
160+
lon,
161+
humidity,
162+
pressure,
163+
temp,
164+
};
135165
}
136166

137167
module.exports = {

src/schema.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ const {
1010
const {
1111
getHello,
1212
resolveQuery,
13+
getWeather,
1314
signin,
1415
} = require('./fetcher');
16+
const {
17+
Weather,
18+
} = require('./schema/weather');
1519
const {
1620
UserConnection, UserFieldFilter, UserFieldOrder, UserMutations, User,
1721
} = require('./schema/user');
@@ -37,8 +41,14 @@ const queryType = new GraphQLObjectType({
3741
},
3842
resolve: (_, args) => signin(args),
3943
},
40-
// TODO create a top level weather resolver that accepts a location parameter
4144
weather: {
45+
type: Weather,
46+
args: {
47+
location: {
48+
type: new GraphQLNonNull(GraphQLString),
49+
},
50+
},
51+
resolve: (_, args) => getWeather(args),
4252
},
4353
users: {
4454
type: UserConnection,

src/schema/user.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const {
1010
} = require('graphql');
1111
const { get } = require('lodash');
1212

13-
const { resolveQuery, createUser } = require('../fetcher');
13+
const { Weather } = require('./weather');
14+
const { resolveQuery, createUser, getWeather } = require('../fetcher');
1415
const {
1516
Node, PageInfo, FilterOperation, OrderDirection,
1617
} = require('./common');
@@ -49,10 +50,15 @@ const User = new GraphQLObjectType({
4950
interfaces: [Node],
5051
isTypeOf: (value) => value instanceof Object,
5152
fields: {
52-
id: { type: GraphQLID },
53-
name: { type: GraphQLString },
54-
username: { type: GraphQLString },
55-
email: { type: GraphQLString },
53+
id: { type: new GraphQLNonNull(GraphQLID) },
54+
name: { type: new GraphQLNonNull(GraphQLString) },
55+
username: { type: new GraphQLNonNull(GraphQLString) },
56+
email: { type: new GraphQLNonNull(GraphQLString) },
57+
location: { type: GraphQLString },
58+
weather: {
59+
type: Weather,
60+
resolve: ({ location }) => (location ? getWeather({ location }) : {}),
61+
},
5662
posts: {
5763
type: PostConnection,
5864
args: {
@@ -116,6 +122,10 @@ const CreateUserInput = new GraphQLInputObjectType({
116122
username: { type: new GraphQLNonNull(GraphQLString) },
117123
email: { type: new GraphQLNonNull(GraphQLString) },
118124
password: { type: new GraphQLNonNull(GraphQLString) },
125+
location: {
126+
type: GraphQLString,
127+
defaultValue: 'Budapest',
128+
},
119129
},
120130
});
121131

0 commit comments

Comments
 (0)