I need Apollo-GraphQL Expert's help!
Hi! I started to study apollo-graphql. I created one project and it has two modules such as book and user.(I attached project structure image.)[enter image description here][1] Now I use two schemas(book and user), but I want to integrate one. Because in server.js ApolloServer use only one schema and resolver. I attached schema, model and resolver files. If you have experience in it, please help me. Thanks John Davis
/* Book.js */
var mongoose = require('mongoose');
var BookSchema = new mongoose.Schema({
id: String,
isbn: String,
title: String,
author: String,
description: String,
published_year: { type: Number, min: 1945, max: 2020 },
publisher: String,
updated_date: { type: Date, default: Date.now },
});
module.exports = mongoose.model('Book', BookSchema);
/*bookResolvers.js*/
const Book = require('./Book');
const resolvers = {
Query: {
books: () => Book.find({})
},
Mutation: {
addBook: (parent, book) => {
const newBook = new Book({isbn: book.isbn, title: book.title, author: book.author, description: book.description, published_year: book.published_year, publisher: book.publisher});
return newBook.save();
}
}
};
module.exports = resolvers;
/*bookSchema.js*/
const { gql } = require('apollo-server-express');
export const typeDefs = gql`
type Book {
_id: String,
isbn: String,
title: String,
author: String,
description: String,
published_year: Int,
publisher: String,
},
type Query {
books: [Book]
},
type Mutation {
addBook(isbn: String!,
title: String!,
author: String!,
description: String!,
published_year: Int!,
publisher: String!,
): Book
}
`;
/*User.js*/
var mongoose = require('mongoose');
var UserSchema = new mongoose.Schema({
email: String,
password: String
});
module.exports = mongoose.model('User', UserSchema);
/*userResolver.js*/
const User = require('./User');
const resolvers = {
Query: {
login: async (parent, params) => {
const user = await User.findOne({email: params.email}).exec();
console.log(user);
return user;
}
},
Mutation: {
register: (parent, params) => {
const newUser = new User({ email: params.email, password: params.password });
return newUser.save();
}
}
};
module.exports = resolvers;
/*userSchema.js*/
const { gql } = require('apollo-server-express');
const typeDefs = gql`
type User {
_id: String,
email: String,
password: String
},
type Query {
login(email: String, password: String): User
},
type Mutation {
register( email: String, password: String): User
}
`;
module.exports = typeDefs;
/*resolvers.js*/
/*schema.js*/
/*server.js*/
import schema from "./schemas/schema"
import resolvers from "./schemas/resolvers"
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const mongoose = require('./config/database');
const server = new ApolloServer({ schema, resolvers });
const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
<!-- end snippet -->