forked from aws/amazon-neptune-for-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
43 lines (37 loc) · 1.48 KB
/
index.mjs
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
import {ApolloServer} from '@apollo/server';
import {startStandaloneServer} from '@apollo/server/standalone';
import {buildSubgraphSchema} from '@apollo/subgraph';
import {readFileSync} from 'fs';
import {gql} from 'graphql-tag';
import {resolveEvent} from './neptune.mjs'
import dotenv from 'dotenv';
dotenv.config();
const typeDefs = gql(readFileSync('./output.schema.graphql', 'utf-8'));
const queryDefinition = typeDefs.definitions.find(
definition => definition.kind === 'ObjectTypeDefinition' && definition.name.value === 'Query'
);
const queryNames = queryDefinition ? queryDefinition.fields.map(field => field.name.value) : [];
const resolvers = {
// only Query is supported for now, no Mutations
Query: queryNames.reduce((accumulator, queryName) => {
accumulator[queryName] = (parent, args, context, info) => {
const event = {
field: info.fieldName,
arguments: args,
selectionSet: info.fieldNodes[0].selectionSet,
};
return resolveEvent(event).then((result) => {
return result;
});
};
return accumulator;
}, {}),
};
const server = process.env.SUBGRAPH === 'true' ? new ApolloServer({
schema: buildSubgraphSchema([{
typeDefs,
resolvers
}])
}) : new ApolloServer({typeDefs, resolvers});
const {url} = await startStandaloneServer(server);
console.log(`🚀 Server ready at ${url}`);