forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (42 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const { ApolloServer } = require('apollo-server');
const gql = require('graphql-tag');
const {getData, postData} = require('./helpers');
const typeDefs = gql`
type User {
id: String!
name: String!
balance: Int!
}
type Query {
getUser(id: String!): User
users(name: String): [User]
}
type Mutation {
addUser(name: String!, balance: Int!): User
}
`;
// replace with actual REST endpoint
const restAPIEndpoint = 'https://rest-user-api.herokuapp.com';
const resolvers = {
Query: {
getUser: async (_, { id }) => {
return await getData(restAPIEndpoint + '/users/' + id);
},
users: async (_, { name }) => {
var nameParams = '';
if (name) {
nameParams = '?name=' + name;
}
return await getData(restAPIEndpoint + '/users' + nameParams );
}
},
Mutation: {
addUser: async (_, { name, balance } ) => {
return await postData(restAPIEndpoint + '/users', { name, balance } );
}
}
};
const schema = new ApolloServer({ typeDefs, resolvers });
schema.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
console.log(`schema ready at ${url}`);
});