connecting mongodb model to apollo-server 2.0

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



connecting mongodb model to apollo-server 2.0



I am following a tutorial which uses mongo database with graphql, currently the tutor uses apollo-server v1 but I am using apollo-server v2, the problem I'm having is where do i make the connection of my models to graphql,


const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
require("dotenv").config( path: "variables.env" );
const Recipe = require("./models/Recipe");
const User = require("./models/User");

// Bring in GraphQL-Express middleware
const graphiqlExpress, graphqlExpress = require("apollo-server-express");
const makeExecutableSchema = require("graphql-tools");

const typeDefs = require("./schema");
const resolvers = require("./resolvers");

// Create schema
const schema = makeExecutableSchema(
typeDefs,
resolvers
);

// Connects to database
mongoose
.connect(process.env.MONGO_URI)
.then(() => console.log("DB connected"))
.catch(err => console.error(err));

// Initializes application
const app = express();

const corsOptions =
origin: "http://localhost:3000",
credentials: true
;
app.use(cors(corsOptions));

// Create GraphiQL application
app.use("/graphiql", graphiqlExpress( endpointURL: "/graphql" ));

// Connect schemas with GraphQL
app.use(
"/graphql",
bodyParser.json(),
graphqlExpress(
schema,
context:
Recipe,
User

)
);

const PORT = process.env.PORT || 4444;

app.listen(PORT, () =>
console.log(`Server listening on PORT $PORT`);
);



so in apollo-server v1 you pass in your mongo schema (models) in graphqlExpress


Connect schemas with GraphQL
app.use(
"/graphql",
bodyParser.json(),
graphqlExpress(
schema,
context:
Recipe,
User

)
);



but in v2 you no longer use graphqlExpress function,



https://www.apollographql.com/docs/apollo-server/v2/migration-two-dot.html



so how/where do i pass in my database models to?



currently I have this


const express = require ('express')
const mongoose = require ('mongoose')
const ApolloServer = require('apollo-server-express');

require('dotenv').config(path: 'variables.env')

// mongo schemas/models
const Recipe = require('./models/Recipe')
const User = require('./models/User')

//Graphql schema
const typeDefs = require('./schema')
const resolvers = require('./resolvers')

// connects to database
mongoose.connect(process.env.MONGO_URI, useNewUrlParser: true )
.then(()=> console.log('DB connected'))
.catch(error => console.log(error))

const PORT = process.env.PORT || 4444;

const server = new ApolloServer( typeDefs, resolvers );

const app = express();
server.applyMiddleware( app );

app.listen( port: PORT , () =>
console.log(`🚀 Server ready at http://localhost:$PORT$server.graphqlPath`)
)




2 Answers
2



This example works perfect with PG, if you are using Mongo, just replace pg calls with mongoose and you should be good to go.



If you want to be exactly like you were before, you need to add the context function onto your ApolloServer init:


context


const server = new ApolloServer(
typeDefs,
resolvers,
context: ( req ) => (
Recipe,
User
)
);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard