Github GraphQL API返回JSON文件内容不完整问题求助
GitHub GraphQL API获取大JSON文件时内容被截断
我开发了一个文档网站,文档存储在GitHub上。我使用GitHub GraphQL API在构建阶段将内容仓库中的文件复制到站点仓库,实现内容本地化,避免持续发起请求。
目前方案部分可行,但存在问题:部分文件返回内容不完整。举个例子:
原文件内容:
{ text: "est simplement du faux texte employé dans la composition et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen de polices de texte. Il n'a pas fait que survivre cinq siècles, mais s'est aussi adapté à la bureautique informatique, sans que son contenu n'en soit modifié" }
API返回的内容(文本被截断但仍显示闭合键):
{ text: "est simplement du faux texte employé dans la composition et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla }
我怀疑是文件大小限制导致的,出现问题的是一个16855行的大型JSON文件。若有遗漏的重要信息,我会补充。
相关代码
import * as fs from 'fs' import * as path from 'path' const dir = path.join('public', 'teste') const openSource = { githubConvertedToken: 'xxxx', } import fetch from 'node-fetch' const body = { query: ` query { repository(owner: "xxxx", name: "xxxx") { object(expression: "HEAD:") { ... on Tree { entries { name object { ... on Blob { text } } } } } } }`, } const baseUrl = 'https://api.github.com/graphql' const headers = { 'Content-Type': 'application/json', Authorization: 'bearer ' + openSource.githubConvertedToken, } const assertDirExists = (dir_path) => { if (!fs.existsSync(dir_path)) { fs.mkdirSync(dir_path) } } fetch(baseUrl, { method: 'POST', headers: headers, body: JSON.stringify(body), }) .then((res) => res.json()) .then((res) => { let content = res assertDirExists(dir) const pathdir = path.join(dir, 'aaa') let count = 0 content.data.repository.object.entries.forEach((element) => { if (element.name.includes('json')) { try { const test = JSON.parse(element.object.text) fs.writeFileSync(`${pathdir}${count}.json`, JSON.stringify(test)) count = count + 1 } catch (err) { console.log('error', err) } } }) }) .catch((error) => console.log('erroooooooooo', error))
内容的提问来源于stack exchange,提问作者Melissa Falcão
相关产品推荐
相关产品推荐

