This commit is contained in:
Riccardo
2021-01-03 20:53:38 +01:00
parent e72890fe28
commit 61acbf02d8
12 changed files with 282 additions and 241 deletions

View File

@@ -7,18 +7,20 @@ import schema from './schema.js';
import './utils/db.js';
import fs from 'fs';
import path from 'path';
import cors from 'cors';
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();
app.use(cors());
app.get('/', (req, res) => {
res.json({
msg: 'Welcome to GraphQL'
msg: 'GraphQL home!'
})
});
@@ -28,32 +30,33 @@ app.use('/graphql', graphqlHTTP({
}));
const server = new ApolloServer({
typeDefs: fs.readFileSync(
path.join(__dirname, 'schema.graphql'),
'utf8'
),
// typeDefs: fs.readFileSync(
// path.join(__dirname, 'schema.graphql'),
// 'utf8'
// ),
schema,
cors: true,
playground: process.env.NODE_ENV === 'development' ? true : false,
context: async ({ req }) => {
// if (!db) {
// try {
// if (!dbClient.isConnected()) await dbClient.connect()
// mongo = dbClient.db('Calendar') // database name
// console.log(db);
// } catch (e) {
// console.log('--->error while connecting with graphql context (db)', e)
// }
// context: async ({ req }) => {
// // if (!db) {
// // try {
// // if (!dbClient.isConnected()) await dbClient.connect()
// // mongo = dbClient.db('Calendar') // database name
// // console.log(db);
// // } catch (e) {
// // console.log('--->error while connecting with graphql context (db)', e)
// // }
return {
...req,
mongoose,
// pubsub,
// userId:
// req && req.headers.authorization
// ? getUserId(req)
// : null
}
},
// return {
// ...req,
// mongoose,
// pubsub,
// userId:
// req && req.headers.authorization
// ? getUserId(req)
// : null
// }
// },
// subscriptions: {
// onConnect: (connectionParams) => {
// if (connectionParams.authToken) {

View File

@@ -1,41 +1,38 @@
import Product from './models/product.js';
// import User from './resolvers/User.js';
// import Appointment from './resolvers/Appointment.js';
// import Follow from './resolvers/Follow.js';
// import Query from './resolvers/Query.js';
// import Mutation from './resolvers/Mutation.js';
// import Subscription from './resolvers/Subscription.js';
export const resolvers = {
Query: {
async allProducts() {
return await Product.find();
const products = await Product.find();
console.log("Tah!", products);
return products;
// return {
// products
// };
},
},
Mutation: {
async createProduct(root, {
input
}) {
return await Product.create(input);
},
async updateProduct(root, {
_id,
input
}) {
return await Product.findOneAndUpdate({
_id
}, input, {
new: true
})
},
async deleteProduct(root, {
_id
}) {
return await Product.findOneAndRemove({
_id
});
},
// async feed(parent, args, context, info) {
// const where = args.filter
// ? {
// OR: [
// { title: { contains: args.filter } },
// { description: { contains: args.filter } }
// ]
// }
// : {};
// console.log(context.mongo);
// const appointments = await context.mongo.appointment.findMany({
// where,
// skip: args.skip,
// take: args.take,
// orderBy: args.orderBy
// });
// const count = await context.mongo.appointment.count({ where });
// return {
// id: 'main-feed',
// appointments,
// count
// };
// }
}
};

View File

@@ -7,7 +7,7 @@ type Query {
orderBy: AppointmentOrderByInput
): Feed!
allProducts: [Product]
# users: [User!]!
users: [User!]!
}
type Feed {
@@ -22,7 +22,17 @@ type Mutation {
description: String!,
start: DateTime!,
end: DateTime!,
): Appointment!
): Appointment!
createProduct(
input: ProductInput
) : Product
updateProduct(
_id: ID!,
input: ProductInput
): Product
deleteProduct(
_id: ID!
) : Product
signup(
email: String!
password: String!
@@ -68,7 +78,7 @@ type Appointment {
type Product {
id: ID!
title: String!
qty: Integer
qty: Int
}
type Follow {
@@ -77,6 +87,12 @@ type Follow {
user: User!
}
input ProductInput {
title: String!
qty: Int
}
input AppointmentOrderByInput {
description: Sort
url: Sort
@@ -88,5 +104,4 @@ enum Sort {
desc
}
scalar Integer
scalar DateTime

View File

@@ -1,45 +1,54 @@
import {
makeExecutableSchema
} from 'graphql-tools';
import {
resolvers
} from './resolvers.js';
import fs from 'fs';
import path from 'path';
const moduleURL = new URL(import.meta.url);
const __dirname = path.dirname(moduleURL.pathname);
const typeDefs = fs.readFileSync(
path.join(__dirname, 'schema.graphql'),
'utf8'
);
const schema = makeExecutableSchema({
typeDefs,
// resolvers
});
export default schema;
// import {
// makeExecutableSchema
// } from 'graphql-tools';
// import {
// resolvers
// } from './resolvers.js';
// const typeDefs = `
// type Product {
// _id: ID!
// title: String!
// qty: Int
// }
// type Query {
// allProducts: [Product]
// }
// `;
// import fs from 'fs';
// import path from 'path';
// const moduleURL = new URL(import.meta.url);
// const __dirname = path.dirname(moduleURL.pathname);
// const typeDefs = fs.readFileSync(
// path.join(__dirname, 'schema.graphql'),
// 'utf8'
// );
// const schema = makeExecutableSchema({
// typeDefs,
// resolvers
// // resolvers
// });
// export default schema;
// export default schema;
import {
makeExecutableSchema
} from 'graphql-tools';
import {
resolvers
} from './resolvers.js';
const typeDefs = `
type Product {
_id: ID!
title: String!
qty: Int
}
type Query {
allProducts: [Product]
}
input ProductInput {
title: String!
qty: Int
}
type Mutation {
createProduct(input: ProductInput) : Product
updateProduct(_id: ID!, input: ProductInput): Product
deleteProduct(_id: ID!) : Product
}
`;
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
export default schema;

View File

@@ -1,20 +0,0 @@
import { SchemaComposer } from 'graphql-compose';
import db from '../utils/db.js';
const schemaComposer = new SchemaComposer();
import { UserQuery, UserMutation } from './user.js';
import { AppointmentQuery, AppointmentMutation } from './appointment.js';
schemaComposer.Query.addFields({
...UserQuery,
...AppointmentQuery,
});
schemaComposer.Mutation.addFields({
...UserMutation,
...AppointmentMutation,
});
export default schemaComposer.buildSchema();