在通过 mergeResolvers 导入的外部解析器文件中使用 MongoDB 集合变量时出现 ReferenceError
ReferenceError when using MongoDB Collection variable in external resolver file that was imported via mergeResolvers
这是一个完全简化的例子,可以更好地说明问题!因此,当我使用 解析器查询 getAllUsers
时,MongoDB 集合 Users
在外部解析器文件 user.js
。因此,当我发送该查询时,我得到:
ReferenceError: Users is not defined
这是正确的行为。但是我不想在我的 index.js 中包含所有的解析器,因为这样我有更好的模块化。所以我的所有类型定义和解析器都在这样的外部文件中。
当前文件结构
index.js
/graphql
/typdef
user.graphql
/resolver
user.js
user.graphql
架构正常工作。如前所述,当我使用不可用的 Users
变量执行查询时,只是 user.js
产生了错误。
这里是index.js
和user.js
.
index.js
import express from 'express'
import cors from 'cors'
const app = express()
app.use(cors())
import bodyParser from 'body-parser'
import {graphqlExpress, graphiqlExpress} from 'graphql-server-express'
import {makeExecutableSchema} from 'graphql-tools'
import {fileLoader, mergeTypes, mergeResolvers} from 'merge-graphql-schemas';
import {writeFileSync} from 'fs'
const typeDefs = mergeTypes(fileLoader(`${__dirname}/graphql/typedef/*.graphql`), { all: true })
writeFileSync(`${__dirname}/graphql/typedef.graphql`, typeDefs)
export const start = async () => {
try {
const MONGO_URL = 'mongodb://localhost:27017'
const MongoClient = require('mongodb').MongoClient;
MongoClient.connect(MONGO_URL, function(err, client) {
console.log("Connected successfully to server");
const db = client.db('project');
const Users = db.collection('user')
});
const URL = 'http://localhost'
const homePath = '/graphql'
const PORT = 3001
app.use(
homePath,
bodyParser.json(),
graphqlExpress({schema})
)
app.use(homePath,
graphiqlExpress({
endpointURL: homePath
})
)
app.listen(PORT, () => {
console.log(`Visit ${URL}:${PORT}${homePath}`)
})
} catch (e) {
console.log(e)
}
}
user.js
export default {
Query: {
getAllUsers: async () => {
return (await Users.find({}).toArray()).map(prepare)
}
}
}
将 MongoDB 或 Users
集合传递给解析器文件的最佳方法是什么。或者对于这个问题有更好的解决方案吗?
首先,这不是一个合适的解决方案,因为在外包模式的同时声明全局变量根本不是一个糟糕的设计。但它成功了,也许通过这种方式,有人可以了解如何改进此修复程序。
所以要解决这个问题,我所要做的就是将变量从本地 const 更改为全局。
所以在index.js中const Users = db.collection('user')
被global.Users = db.collection('user')
重写了。
user.js 也一样。这里 return (await Users.find({}).toArray()).map(prepare)
改写为 return (await global.Users.find({}).toArray()).map(prepare)
.
这是一个完全简化的例子,可以更好地说明问题!因此,当我使用 解析器查询 getAllUsers
时,MongoDB 集合 Users
在外部解析器文件 user.js
。因此,当我发送该查询时,我得到:
ReferenceError: Users is not defined
这是正确的行为。但是我不想在我的 index.js 中包含所有的解析器,因为这样我有更好的模块化。所以我的所有类型定义和解析器都在这样的外部文件中。
当前文件结构
index.js
/graphql
/typdef
user.graphql
/resolver
user.js
user.graphql
架构正常工作。如前所述,当我使用不可用的 Users
变量执行查询时,只是 user.js
产生了错误。
这里是index.js
和user.js
.
index.js
import express from 'express'
import cors from 'cors'
const app = express()
app.use(cors())
import bodyParser from 'body-parser'
import {graphqlExpress, graphiqlExpress} from 'graphql-server-express'
import {makeExecutableSchema} from 'graphql-tools'
import {fileLoader, mergeTypes, mergeResolvers} from 'merge-graphql-schemas';
import {writeFileSync} from 'fs'
const typeDefs = mergeTypes(fileLoader(`${__dirname}/graphql/typedef/*.graphql`), { all: true })
writeFileSync(`${__dirname}/graphql/typedef.graphql`, typeDefs)
export const start = async () => {
try {
const MONGO_URL = 'mongodb://localhost:27017'
const MongoClient = require('mongodb').MongoClient;
MongoClient.connect(MONGO_URL, function(err, client) {
console.log("Connected successfully to server");
const db = client.db('project');
const Users = db.collection('user')
});
const URL = 'http://localhost'
const homePath = '/graphql'
const PORT = 3001
app.use(
homePath,
bodyParser.json(),
graphqlExpress({schema})
)
app.use(homePath,
graphiqlExpress({
endpointURL: homePath
})
)
app.listen(PORT, () => {
console.log(`Visit ${URL}:${PORT}${homePath}`)
})
} catch (e) {
console.log(e)
}
}
user.js
export default {
Query: {
getAllUsers: async () => {
return (await Users.find({}).toArray()).map(prepare)
}
}
}
将 MongoDB 或 Users
集合传递给解析器文件的最佳方法是什么。或者对于这个问题有更好的解决方案吗?
首先,这不是一个合适的解决方案,因为在外包模式的同时声明全局变量根本不是一个糟糕的设计。但它成功了,也许通过这种方式,有人可以了解如何改进此修复程序。
所以要解决这个问题,我所要做的就是将变量从本地 const 更改为全局。
所以在index.js中const Users = db.collection('user')
被global.Users = db.collection('user')
重写了。
user.js 也一样。这里 return (await Users.find({}).toArray()).map(prepare)
改写为 return (await global.Users.find({}).toArray()).map(prepare)
.