UserType will be undefined if I require it on the top of the transaction_type.js
If I require it in fields arrow function, everything will work great.
See comments in transaction_type.js
Can anyone help me with this javascript issue?
transaction_type.js
const graphql = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLID, GraphQLList } = graphql;
const UserType = require('./user_type.js');
// UserType is undefined
const User = require('../models/User');
const Transaction = require('../models/Transaction');
const TransactionType = new GraphQLObjectType({
name: 'TransactionType',
fields: () => ({
id: { type: GraphQLID },
title: { type: GraphQLString },
type: { type: GraphQLString },
price: { type: GraphQLString },
date: { type: GraphQLString },
user: {
// If I comment this line
type: UserType,
// And uncomment this
// type: require('./user_type.js'),
// It will work good
resolve(parentValue) {
return User.findOne({ _id: parentValue._userId })
.catch(err => console.log(err));
}
}
})
});
module.exports = TransactionType;
user_type.js
const graphql = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLID, GraphQLList, GraphQLInt } = graphql;
const TransactionType = require('./transaction_type');
const User = require('../models/User');
const Transaction = require('../models/Transaction');
const UserType = new GraphQLObjectType({
name: 'UserType',
fields: () => ({
id: { type: GraphQLID },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
age: { type: GraphQLInt },
transactions: {
type: new GraphQLList(TransactionType),
resolve(parentValue) {
return Transaction.find({ _userId: parentValue._id })
.catch(err => console.log(err));
}
}
})
});
via Александр Чорнокондратенко
No comments:
Post a Comment