-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphql.js
More file actions
65 lines (55 loc) · 1.44 KB
/
graphql.js
File metadata and controls
65 lines (55 loc) · 1.44 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { ApolloServer, gql } = require("apollo-server-lambda");
var { find, filter } = require("lodash");
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Author {
id: Int!
firstName: String
lastName: String
"""
the list of Posts by this author
"""
posts: [Post]
}
type Post {
id: Int!
title: String
author: Author
votes: Int
}
# the schema allows the following query:
type Query {
hello: String
posts: [Post]
author(id: Int!): Author
}
# this schema allows the following mutation:
type Mutation {
upvotePost(postId: Int!): Post
}
`;
// // example data
const authors = [
{ id: 1, firstName: "Tom", lastName: "Coleman" },
{ id: 2, firstName: "Sashko", lastName: "Stubailo" },
{ id: 3, firstName: "Mikhail", lastName: "Novikov" }
];
const posts = [
{ id: 1, authorId: 1, title: "Introduction to GraphQL", votes: 2 },
{ id: 2, authorId: 2, title: "Welcome to Meteor", votes: 3 },
{ id: 3, authorId: 2, title: "Advanced GraphQL", votes: 1 },
{ id: 4, authorId: 3, title: "Launchpad is Cool", votes: 7 }
];
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => "Hello world!",
posts: () => posts,
author: (_, { id }) => find(authors, { id })
}
};
const server = new ApolloServer({ typeDefs, resolvers });
const graphqlHandler = server.createHandler();
module.exports = {
graphqlHandler
};