如何引入.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开始 ...
随机推荐
- 18.12 SDRAM和NAND FLASH区别
处理器运行时要做大量的数据计算和交换,要求内存读写速度很快. NOR Flash读取快,写入慢,总线结构,能运行代码,价格贵. NAND Flash读取慢,写入快,非总线结构,不能运行代码,价格便宜. ...
- springboot Cacheable(redis),解决key乱码问题
import org.springframework.context.annotation.Bean; import org.springframework.data.redis.core.Redis ...
- Logback动态修改日志级别
https://blog.csdn.net/totally123/article/details/78931287
- MQTT研究之EMQ:【JAVA代码构建X509证书】
这篇帖子,不会过多解释X509证书的基础理论知识,也不会介绍太多SSL/TLS的基本信息,重点介绍如何用java实现SSL协议需要的X509规范的证书. 之前的博文,介绍过用openssl创建证书,并 ...
- 微服务与Spring Cloud资料
Microservices Using Spring Boot and Spring Cloud 微服务注册中心 Eureka 架构深入解读 50+ 顶级开源 Kubernetes 工具列表 Apol ...
- 未来-区块链-Aliyun:阿里云IoT - 所知不止于感知
ylbtech-未来-区块链-Aliyun:阿里云IoT - 所知不止于感知 1.返回顶部 1. 基础产品接入 设备接入 设备管理 数据分析 应用开发 网络管理 边缘计算 设备认证 安全运营 AliO ...
- 你真的理解了for循环吗?反正我是没有
for循环的执行步骤 咱们先来看一个有意思的关于for循环的程序 public class TestFor { public static void main(String[] args) { int ...
- WebBrowser2控件使用
一 简介 这是一个IE实现的com接口, 简单的浏览一个网页可以用这个东西, 局限性是IE现在有好多版本, 不同版本对标准的支持是不一样的, 而且将来不排除windows把IE给干掉了 二 使用 1 ...
- Emacs下scheme编程环境的设置
Scheme编程环境搭建 1.1 安装Chez Scheme git clone https://github.com/cisco/ChezScheme.git cd ChezScheme ./con ...
- centos 6.5 安装redis
1. 下载redis,编译安装 下载地址:https://redis.io/download(建议大家都选择稳定版本) 下载到本地,然后上传到集群 当然也可以通过命令行直接在线下载 $ wget ht ...