@@ -13,29 +13,13 @@ const psqlPool = require('../database/psql/dbConnection');
1313// import the mongo Models (they are on the export of dbConnection)
1414const mongoConnectionAndModels = require ( '../database/mongo/dbConnection' ) ;
1515
16- // flow test (if needed)
17- // app.use(express.json()); // parse req.body to json
18- // app.use((req, res, next) => {
19- // console.log(`METHOD: ${req.method}, \nURL: ${req.url}, \nBODY: ${JSON.stringify(req.body)}\n`);
20- // return next();
21- // });
22-
23- // massively helpful resource: https://marmelab.com/blog/2017/09/06/dive-into-graphql-part-iii-building-a-graphql-server-with-nodejs.html
24- // makeExe.Sch. is used to combine the typeDef and resolvers. this allows us to
25- // define type resolvers which are essential to relational data in my opinion...
26- // const schema = makeExecutableSchema({ typeDefs, resolvers });
16+ // async function to start the server (to await the mongo connection)
2717const startServer = async ( ) => {
2818 // this is asyncronous, so use await to avoid sending an unresolved promise to context in app.use
2919
20+ // await the mongo connection to be passed into context only when it is connected
3021 const mongo = await mongoConnectionAndModels ( ) ;
3122
32- // console.log(mongo); // contains { CartModel: Model { Cart } }
33-
34- // console.log('--before default / use');
35- // THIS WAS THROWING ERRORS BECAUSE A FETCH TO ANY ENDPOINT HAS TO GO THROUGH HERE
36- // app.use('/', (req, res) => res.send('hello'));
37-
38-
3923 console . log ( '--before graphql endpoint code' ) ;
4024 // setup the single graphql endpoint
4125 app . use ( '/graphql' ,
@@ -54,77 +38,15 @@ const startServer = async () => {
5438 app . listen ( PORT , ( ) => console . log ( `Listening on PORT ${ PORT } ` ) ) ;
5539} ;
5640
41+ // example that is used to slow down the startup of the server for demonstration purposes only
5742// async function that delays how long until the server runs, for testing
58- // async function breakShit () {
43+ // async function slowDownStuff () {
5944// return new Promise((resolve) => {
6045// setTimeout(() => { resolve(); }, 5000);
6146// });
6247// }
63- // breakShit ().then(() => startServer());
48+ // slowDownStuff ().then(() => startServer());
6449
6550
6651// run the async function defined above to connect to mongo and run the server
6752startServer ( ) ;
68-
69- /*
70- * EXAMPLE QUERY FROM THE FRONT END (FETCH)
71- fetch('/graphql', {
72- method: 'POST',
73- headers: {'Content-Type': 'application/json'},
74- body: JSON.stringify({ query: '{ addresses { address address2 city state zipCode } }' })
75- })
76- .then(res => res.json())
77- .then(data => console.log(data))
78- .catch(err => console.log('ERROR!!!', err));
79- */
80-
81- /*
82- * EXAMPLE QUERY FROM THE FRONT END FOR A MUTATION
83- fetch('/graphql', {
84- method: 'POST',
85- headers: { 'Content-Type': 'application/json' },
86- body: JSON.stringify({
87- query: `mutation {
88- addCustomer (
89- firstName: "testing2"
90- lastName: "testingLast"
91- phoneNumber: "347-306-5701klhjkg"
92- email: "alex@al ,hjkhex.com"
93- ) {
94- #asking for what data we want back
95- firstName
96- lastName
97- cart { products }
98- }
99- }`,
100- }),
101- })
102- .then((res) => res.json())
103- .then((data) => console.log(data))
104- .catch((err) => console.log('ERROR!!!', err));
105- */
106-
107- function graphQuill ( ) { }
108- graphQuill ( `
109- {
110- customer (id: 10) {
111- firstName
112- lastName
113- email
114- phoneNumber
115- address {
116- address
117- address2
118- city
119- state
120- zipCode
121- }
122- }
123- }
124- ` ) ;
125-
126- graphQuill ( `{
127- warehouse (warehouseId: 5) {
128- name
129- }
130- }` ) ;
0 commit comments