Some progress

This commit is contained in:
Riccardo
2021-01-03 16:49:26 +01:00
parent 8dd59ae64b
commit e72890fe28
15 changed files with 158 additions and 382 deletions

View File

@@ -1,17 +1,37 @@
import dotenv from 'dotenv';
import express from 'express';
import dotenv from 'dotenv';
import { graphqlHTTP } from 'express-graphql';
import { ApolloServer, PubSub } from 'apollo-server-express';
import mongoose from 'mongoose';
import schema from './schema.js';
import './utils/db.js';
import schema from './schema/index.js';
import fs from 'fs';
import path from 'path';
const moduleURL = new URL(import.meta.url);
const __dirname = path.dirname(moduleURL.pathname);
const app = express();
const pubsub = new PubSub();
const PORT = 4000;
dotenv.config();
const app = express();
const pubsub = new PubSub();
app.get('/', (req, res) => {
res.json({
msg: 'Welcome to GraphQL'
})
});
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true
}));
const server = new ApolloServer({
schema,
typeDefs: fs.readFileSync(
path.join(__dirname, 'schema.graphql'),
'utf8'
),
cors: true,
playground: process.env.NODE_ENV === 'development' ? true : false,
context: async ({ req }) => {
@@ -27,30 +47,30 @@ const server = new ApolloServer({
return {
...req,
mongoose,
pubsub,
userId:
req && req.headers.authorization
? getUserId(req)
: null
}
},
subscriptions: {
onConnect: (connectionParams) => {
if (connectionParams.authToken) {
return {
mongoose,
userId: getUserId(
null,
connectionParams.authToken
)
};
} else {
return {
mongoose
};
}
// pubsub,
// userId:
// req && req.headers.authorization
// ? getUserId(req)
// : null
}
},
// subscriptions: {
// onConnect: (connectionParams) => {
// if (connectionParams.authToken) {
// return {
// mongoose,
// userId: getUserId(
// null,
// connectionParams.authToken
// )
// };
// } else {
// return {
// mongoose
// };
// }
// }
// },
introspection: true,
tracing: true,
path: '/',
@@ -78,6 +98,9 @@ app.listen({ port: process.env.PORT }, () => {
// const { ApolloServer, PubSub } = require('apollo-server');
// // const { Cors } = require('cors');
// // const { Express } = require('express');