-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (111 loc) · 3.16 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const { ApolloServer, gql } = require('apollo-server');
const uuid = require('uuid/v4');
// This is a (sample) collection of books we'll be able to query
// the GraphQL server for. A more complete example might fetch
// from an existing data source like a REST API or database.
const database = {
books: {
'book_1': {
id: 'book_1',
title: 'Cien años De soledad',
authorId: 'author_1',
},
'book_2': {
id: 'book_2',
title: 'El amor en los tiempos del cólera',
authorId: 'author_1',
}
},
authors: {
'author_1': {
id: 'author_1',
name: 'Gabriel García Márquez',
books: ['book_1', 'book_2']
}
},
};
// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
const typeDefs = gql`
type Book {
id: String!
title: String!
authorId: String!
}
type Author {
id: String!
name: String!
books: [Book]
}
union SearchResult = Book | Author
type Query {
books: [Book]
authors: [Author]
bookById(id: String!): Book
authorById(id: String!): Author
search(text: String): [SearchResult]
}
input BookInput {
title: String!
}
type Mutation {
addAuthor(name: String!, books: [BookInput]): Author!
}
`;
// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
const resolvers = {
Query: {
books: () => Object.values(database.books),
authors: () => Object.values(database.authors),
bookById: (_, { id }) => database.books[id],
authorById: (_, { id }) => database.authors[id],
search: (_, { text }) => ([
...Object.values(database.books).filter((book) => book.title.toLowerCase().includes(text.toLowerCase())),
...Object.values(database.authors).filter((author) => author.name.toLowerCase().includes(text.toLowerCase()))
]),
},
Author: {
books: ({ id }) => Object.values(database.books).filter(book => book.authorId === id),
},
SearchResult: {
__resolveType: searchResult => (searchResult.name ? 'Author' : 'Book'),
},
Mutation: {
addAuthor: (_, { name, books = [] }) => {
const authorId = uuid();
const booksWithId = books.reduce(
(acc, book) => {
const bookId = uuid();
acc[bookId] = {
id: bookId,
title: book.title,
authorId,
};
return acc;
},
{}
);
const author = {
id: authorId,
name,
books: Object.keys(booksWithId),
};
database.authors[author.id] = author;
Object.assign(database.books, booksWithId);
return author;
},
},
};
// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers
// responsible for fetching the data for those types.
const server = new ApolloServer({
typeDefs,
resolvers,
});
// This `listen` method launches a web-server. Existing apps
// can utilize middleware options, which we'll discuss later.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});