-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql_server.js
54 lines (47 loc) · 1.33 KB
/
graphql_server.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
const chalk = require('chalk');
// eslint-disable-next-line no-console
const log = console.log;
import { ApolloServer } from 'apollo-server';
import { findMock } from './src/Mock';
import gql from 'graphql-tag';
const schema = gql`
type DataLng {
id: ID!
name: String
}
type DataConnectionLng {
items: [DataLng]
nextToken: String
}
type Query {
listData(language: String, nextToken: String): DataConnectionLng
}
`;
const serverFindMock = (parent, args, context, info) => {
const result = findMock(info.fieldName, info.variableValues);
const msg = `Ask mock for ${info.fieldName} ${JSON.stringify(info.variableValues)} -->`;
if (result !== undefined) log(chalk.yellowBright(`${msg} found`));
else log(chalk.red(`${msg} NOT found`));
// To be checked why not to return undefined
return result !== undefined ? result : 'failed_mocked';
};
// A map of functions which return data for the schema.
const resolvers = {
Query: {},
};
const server = new ApolloServer({
typeDefs: schema,
mocks: {
DataConnectionLng: serverFindMock,
},
mockEntireSchema: false,
resolvers,
context: async () => {
return {
req_context: true,
};
},
});
server.listen({ port: process.env.REACT_APP_GRAPHQL_SERVER_PORT }).then(({ url }) => {
log(chalk.green(`🚀 Graphql server ready at ${url}`));
});