0

(Currently not using Apollo)

So I have server.js:

let graphqlHTTP = require('express-graphql');
... // (other graphQL setup)
let users = []; // or any DB solution here

const {
    UserType,
    UserFactory
} = require('./schema/typeDef.js');
const { QueryType } = require('./schema/query.js');
... // similar imports
// other server declarations

And for example, I have typeDef.js:

const UserType = new GraphQLObjectType({
    name: 'User',
    fields: {
        id: { type: new GraphQLNonNull(GraphQLInt) },
        username: { type: new GraphQLNonNull(GraphQLString }
    }
});

const UserFactory = function(username, id) {
    return {username: username, id: id};
};

module.exports = { UserType, UserFactory };

The problem is, I want to be able to add the UserType to the DB (in this code example, the users array) in server.js; but of course, typeDef.js does not have access to the declared DB in server.js.

I initially separated the files because I didn't want server.js to be too bloated with schema code.

How should I go about this? Thanks!

Conanap
  • 155
  • 1
  • 11

1 Answers1

0

If you've come to the sacred land with no answer, I've got one for you. Follow the guide in this link.

Essentially, create a db with mongoose, export it, and use index.js in different folders to stitch them together. Resolvers will be in a different folder than types, each with their own index.js.

You can now access db methods in resolvers by having it require the db module. Note that you do not need babel, and I've found that cors causes problems (so you may want to remove that if it's causing you problem too).

Conanap
  • 155
  • 1
  • 11