node.js - 通过节点 js 脚本运行 GraphQL 变异(Apollo)服务器端
问题描述
我正在运行一个抓取脚本(Puppeteer),并且想将每个抓取的行写入数据库。
我目前收到此错误:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
以下是我的代码的相关部分:
import puppeteer from 'puppeteer'
import $ from 'cheerio'
import { useMutation } from '@apollo/react-hooks'
import moment from 'moment'
import gql from 'graphql-tag'
const ADD_RESULT = gql`
mutation CreateOneResult( $site: String!, $status: String!, $link: String!, $name: String, $publication: String, $decision: String, $clause: String, $commentText: String, $decisionText: String, $noDateReceived: Boolean, $dateReceived: DateTime, $noDateConcluded: Boolean, $dateConcluded: DateTime! ) {
createOneResult(data: {
site: $site,
status: $status,
link: $link,
name: $name,
publication: $publication,
decision: $decision,
clause: $clause,
commentText: $commentText,
decisionText: $decisionText,
noDateReceived: $noDateReceived,
dateReceived: $dateReceived,
noDateConcluded: $noDateConcluded,
dateConcluded: $dateConcluded,
}){
id
}
}
`
/**
* Loop results and follow the link, scraping extra info
*/
export async function doExtraScrapeStepsAndAddToDB(results) {
results = await (async () => {
var updatedResults = []
for (let i = 0; i < results.length; i++) {
let result = results[i]
let extraResultInfo = await module.exports.getExtraPageInfoFromLink(result)
if (extraResultInfo) {
let updatedResultInfo = { ...result, ...extraResultInfo }
let added = await addResultToDB(updatedResultInfo)
console.log(`Added result of ID ${added} to the database`)
updatedResults.push(updatedResultInfo)
}
}
return updatedResults
})()
return results
}
/**
* Add result to DB
*/
export async function addResultToDB(result) {
const [CreateOneResult] = useMutation(ADD_RESULT)
return await CreateOneResult({
variables: {
...result,
...(false == result.noDateConcluded && {
dateReceived: moment(result.dateConcluded, 'DD/MM/YYYY').format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS)
}),
...(false == result.noDateReceived && {
dateReceived: moment(result.dateReceived, 'DD/MM/YYYY').format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS)
}),
}
})
}
doExtraScrapeStepsAndAddToDB
叫做- 它通过以下方式获取每个结果(来自另一个 URL)的更多信息
getExtraPageInfoFromLink
- 最后,完成的结果通过以下方式写入数据库
addResultToDB
我计划在前端使用 GraphQL (Apollo),所以我想我可以在这个抓取脚本中通过在后端使用 GraphQL 突变写入数据库来练习。
useMutation
除了只能在函数组件中使用的函数(?)之外,我不太了解错误的性质。如果是这种情况,这里有什么替代方案?
我在用:
- GraphQL
- 关系
- 棱镜
- 阿波罗
提前致谢!
解决方案
解决了这个问题:
import fetch from 'node-fetch'
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { createHttpLink } from 'apollo-link-http'
const client = new ApolloClient({
link: createHttpLink({
uri: 'http://localhost:3000/api/graphql',
fetch: fetch
}),
cache: new InMemoryCache()
});
return await client.mutate({
mutation: ADD_RESULT,
variables: {
...result,
...(false == result.noDateConcluded && {
dateReceived: moment(result.dateConcluded, 'DD/MM/YYYY').format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS)
}),
...(false == result.noDateReceived && {
dateReceived: moment(result.dateReceived, 'DD/MM/YYYY').format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS)
}),
}
})
推荐阅读
- python - 获取数组中的值
- python - zfit 中拟合和玩具生成的多重限制
- java - java.net.SocketException: Connection reset when sending HTTP request with RestTemplate
- jquery - 如果选中所有子复选框,则选中父复选框
- java - 运行 loadjava 时 AIX 服务器出错:未找到
- apache-spark - spark kryo 注册泛型类
- apache-spark - 比较两个数据集并获取更改了哪些字段
- react-native - 自定义事件以检查反应原生 mapbox gl 地图中是否缺少样式图像
- flutter - 平台特定代码错误:MissingPluginException
- android - 从最小化应用程序列表中删除应用程序时如何保持服务运行?