-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaccounts.js
76 lines (69 loc) · 1.42 KB
/
accounts.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
const { ApolloServer } = require('apollo-server')
const { buildFederatedSchema } = require('@apollo/federation')
const { post } = require('httpie')
const { parse } = require('graphql')
const typeDefs = /* GraphQL */ `
extend type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
name: String
username: String
}
`
async function main() {
// Push schema to registry
await post(`http://localhost:3000/schema/push`, {
body: {
typeDefs: typeDefs,
graphName: 'my_graph',
serviceName: 'accounts',
routingUrl: 'http://localhost:4001/graphql',
},
})
startServer()
}
function startServer() {
const resolvers = {
Query: {
me() {
return users[0]
},
},
User: {
__resolveReference(object) {
return users.find((user) => user.id === object.id)
},
},
}
const server = new ApolloServer({
schema: buildFederatedSchema([
{
typeDefs: parse(typeDefs),
resolvers,
},
]),
})
server.listen({ port: 4001 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})
const users = [
{
id: '1',
name: 'Ada Lovelace',
birthDate: '1815-12-10',
username: '@ada',
},
{
id: '2',
name: 'Alan Turing',
birthDate: '1912-06-23',
username: '@complete',
},
]
}
main().catch((err) => {
console.error(err)
process.exit(1)
})