如何引入.graphql文件并优雅的使用fragment
你还在为代码中放入长长的模版字符串所苦恼吗,如下图代码片段:

ps:这个是grqphql client在nodejs后端项目的实践,如果你是在前端使用graphql,并使用了webpack,那么这些问题你都不用担心,因为有现成的轮子供你使用,参见相关loader:https://github.com/apollographql/graphql-tag/blob/master/loader.js,
由于项目开发紧张,我们最开始就是采用这图上这种模式,查询语句和业务代码全放在一起,结果是代码阅读和修改极其麻烦,且查询语句没有字段类型提示,graphql提供的fragment也不能使用(这个特性可以复用很多相同的片段)。
随着业务的不断增加,这个问题越来越凸显,我觉得必须想个办法处理一下,思路是:将graphql查询语句抽出来放在以.grqphql结尾的文件中,然后需要的时候再引入进来。webstorm有个插件正好可以实现语法高亮和类型提示,甚至可以再ide里面进行查询,参考:https://plugins.jetbrains.com/plugin/8097-js-graphql。但是问题来了,怎么再业务代码里面引入这个.graphql文件呢? 直接require肯定是不行的,因为这不是js或c++模块,这个确实也有现成的轮子,见:https://github.com/prisma/graphql-import, 但是这个工具在typescript环境下却有不少问题,见相关issue,怎么办呢?业务又催得紧,然后就用了最简单粗暴的方法: fs.readFileSync('./user.graphql','utf8'), 虽然不够优雅但也解决了燃眉之急。
上面这个办法虽然解决了查询语句和业务代码耦合在一起的问题,但是依然不能使用fragment,随着查询语句越来越多,很多片段都是一样的,后来更新的时候不得不同时修改几处代码,我想实现的效果是将fragment也抽离出来放在以.grqphql结尾的文件中,然后再另一个graphql文件中引入,最终拼在一起返回给业务代码
// a.grapqhl
fragment info on User {
name
}
// b.graphql
#import 'a.graphql'
query user{
queryUser {
...info
}
}
// c.js
const b = loadGql('./b.graphql')
返回的b应该是个字符串,像下面这这样子:
fragment info on User{
name
mail
}
query user {
queryUser{
....info
}
}
那么loadGql改怎么实现呢?google一番后,发现有个轮子可以参考下: https://github.com/samsarahq/graphql-loader/blob/master/src/loader.ts 但是这轮子需要配合webpack,不能直接在nodejs环境下直接使用,那就把它改造一下吧,上改造后的代码:
import { validate as graphqlValidate } from "graphql/validation/validate"
import { resolve, join, dirname } from "path"
import { Stats, writeFile,readFileSync, readFile } from "fs"
import pify = require("pify")
import {
DocumentNode,
DefinitionNode,
print as graphqlPrint,
parse as graphqlParse,
Source,
visit,
} from "graphql"
export default function loadGql(filePath: string): string | null {
if (!filePath) return null
try {
const source = readFileSync(filePath, 'utf8')
if(!source) return null
const document = loadSource(source, filePath)
const content = graphqlPrint(document)
return content
} catch (err) {
console.log(err)
return null
}
}
function loadSource(
source: string,
filePath: string,
) {
let document: any = graphqlParse(new Source(source, "GraphQL/file"))
document = extractImports(source, document, filePath)
return document
}
async function stat(
loader: any,
filePath: string,
): Promise<Stats> {
const fsStat: (path: string) => Promise<Stats> = pify(
loader.fs.stat.bind(loader.fs),
)
return fsStat(filePath)
}
function extractImports(source: string, document: DocumentNode, filePath: string): DocumentNode {
const lines = source.split(/(\r\n|\r|\n)/)
const imports: Array<string> = []
lines.forEach(line => {
// Find lines that match syntax with `#import "<file>"`
if (line[0] !== "#") {
return
}
const comment = line.slice(1).split(" ")
if (comment[0] !== "import") {
return
}
const filePathMatch = comment[1] && comment[1].match(/^[\"\'](.+)[\"\']/)
if (!filePathMatch || !filePathMatch.length) {
throw new Error("#import statement must specify a quoted file path")
}
const itemPath = resolve(dirname(filePath), filePathMatch[1])
imports.push(itemPath)
})
const files = imports
const contents = files.map(path => [
readFileSync(path, 'utf8'),
path,
])
const nodes = contents.map(([content, fileContext]) => {
return loadSource(content, fileContext)
}
)
const fragmentDefinitions = nodes.reduce((defs, node) => {
defs.push(...node.definitions)
return defs
}, [] as DefinitionNode[])
const newAst = visit(document, {
enter(node, key, parent, path, ancestors) {
if (node.kind === 'Document') {
const documentNode: DocumentNode = {
definitions: [...fragmentDefinitions, ...node.definitions],
kind: 'Document',
}
return documentNode
}
return node
},
})
return newAst
}
ps:代码为typescript,使用需转换成js
至此,这项工作基本告一段落
如何引入.graphql文件并优雅的使用fragment的更多相关文章
- Vue在单独引入js文件中使用ElementUI的组件
Vue在单独引入js文件中使用ElementUI的组件 问题场景: 我想在vue中的js文件中使用elementUI中的组件,因为我在main.js中引入了element包和它的css,并挂载到了全局 ...
- HTML引入外部文件,解决统一管理导航栏问题。
1.IFrame引入,看看下面的代码 <IFRAME NAME="content_frame" width=100% height=30 marginwidth=0 ...
- html引入css文件
在HTML中,引入CSS的方法主要有行内式.内嵌式.导入式和链接式. 行内式:即在标记的style属性中设定CSS样式,这种方式本质上没有体现出CSS的优势,因此不推荐使用.例: <html&g ...
- Nodejs Express下引入本地文件的方法
Express的结构如下: |---node_modules------用于安装本地模块. |---public------------用于存放用户可以下载到的文件,比如图片.脚本文件.样式表 ...
- jsp文件引入js文件的方式(项目部署于web容器中)
在页面中引入javascript文件的方式是多种多样的,本文介绍两种. 通过<script>标签插入js文件 通过这种方式引入的js,写对js文件和jsp文件的路径很重要.下面给出一个项目 ...
- 引入CSS文件的@import与link的权重分析
我很少在CSS用到@import这个标签,最近看到一句话“link方式的样式的权重 高于@import的权重”,感觉不太对,@import只是一个引入外部文件而已,怎么会有高于link的权重呢?于是我 ...
- 使用EasyUI的插件前需要引入的文件
一.使用EasyUI的插件需要引入一些文件 1.引入相关文件 easyui.css: easyUi的样式文件 icon.css:easyUI的图标样式文件 easyui.min.js:easyUi的类 ...
- asp.net中调用javascript自定义函数的方法(包括引入JavaScript文件)总结
通常javascript代码可以与HTML标签一起直接放在前 端页面中,但如果JS代码多的话一方面不利于维护,另一方面也对搜索引擎不友好,因为页面因此而变得臃肿:所以一般有良好开发习惯的程序员都会把 ...
- jquery,js引入css文件,js引入头尾
jquery,js引入css文件,js引入头尾 今天在项目中,需要把20多个页面加上头和尾部,头和尾是我写的,所以小师傅把这个工作交给我了. 我开始往里面加,先引入common.css,在body开始 ...
随机推荐
- freemaker学习
1,依赖 <!-- Spring Boot Freemarker 依赖 --><dependency> <groupId>org.springframework.b ...
- Jquery 一个页面多个倒计时 实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android studio 中添加依赖model时依赖所需的准备
例如向app中添加依赖core: core要做如下修改: 1.将core中build.gradle中 修改为 . 2.将core中的 applicationId 注释掉.
- 1080P60视频源---verilog
1080P60视频源---verilog `timescale 1ns / 1ps ////////////////////////////////////////////////////////// ...
- MySQL运行内存不足时应采取的措施?
排除故障指南:MySQL运行内存不足时应采取的措施? 天一阁@ 老叶茶馆 1周前 导读 排除故障指南:MySQL运行内存不足时应采取的措施? 翻译团队:知数堂藏经阁项目 - 天一阁 团队成员:天一阁- ...
- linux tee
tee 功能说明:读取标准输入的数据,并将其内容输出成文件. 语 法:tee [-ai][--help][--version][文件...] 补充说明:tee指令会从标准输入设备读取数据,将其内容输出 ...
- JavaScript 原型和对象创建底层原理
1. prototype/__proto__/constructor JS原型链和继承网上已经烂大街了,5毛可以买一堆,这里只提一下: constructor:普通对象和函数对象都有,指向创建它的函数 ...
- cmake中添加-fPIC编译选项方法
合并openjpeg/soxr/vidstab/snappy等多个cmake库时,为了解决下述问题: relocation R_X86_64_32 against `.text' can not be ...
- multipart/form-data和application/x-www-form-urlencoded区别
FORM元素的enctype属性指定了表单数据向服务器提交时所采用的编码类型.例如: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对.这是标准的编码格 ...
- UIView和CALayer区别
(1)首先UIView可以响应用户的触摸事件,Layer不可以. (2)View中frame getter方法,bounds和center,UIView并没有做什么工作:它只是简单的各自调用它底层的C ...