-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.ts
82 lines (75 loc) · 1.82 KB
/
index.ts
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
import * as express from 'express';
import * as bodyParser from 'body-parser';
import {graphqlExpress, graphiqlExpress} from 'apollo-server-express';
import {makeRemoteExecutableSchema, mergeSchemas, introspectSchema} from 'graphql-tools';
import {createApolloFetch,} from 'apollo-fetch';
async function run() {
const createRemoteSchema = async (uri: string) => {
const fetcher = createApolloFetch({uri});
return makeRemoteExecutableSchema({
schema: await introspectSchema(fetcher),
fetcher
});
}
const universeSchema = await createRemoteSchema('https://www.universe.com/graphql/beta')
const weatherSchema = await createRemoteSchema('https://5rrx10z19.lp.gql.zone/graphql');
const linkSchemaDefs = `
extend type Location {
weather: Weather
}
extend type Event {
location: Location
}
`
const schema = mergeSchemas({
schemas: [universeSchema, weatherSchema, linkSchemaDefs],
resolvers: mergeInfo => ({
Event: {
location: {
fragment: `fragment EventFragment on Event {cityName}`,
resolve(parent: any, args: any, context: any, info: any) {
const place: string = parent.cityName
return mergeInfo.delegate(
'query',
'location',
{place},
context,
info
)
}
}
}
})
})
const app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({schema}));
app.use(
'/graphiql',
graphiqlExpress({
endpointURL: '/graphql',
query: `query {
event(id: "5983706debf3140039d1e8b4") {
title
description
url
location {
city
country
weather {
summary
temperature
}
}
}
}
`,
})
);
app.listen(3000);
console.log('Server running. Open http://localhost:3000/graphiql to run queries.');
}
try {
run();
} catch (e) {
console.log(e, e.message, e.stack);
}