Modularizing your graphQL schemas
Modularizing your graphQL schemas
Working in a kinda big graphql server, schemas can easily get out of hand with all the types, inputs, queries, mutations, subscriptions and whatever else I’m missing. So there should be a way of separating them, modularizing them into smaller and more readable chunks right?
I found a way, and I wanna share it in case it can help you.
The Directory Structure

In this way you have a clear picture of the different type of graphQL files you have and where you should place new ones. I’m still not using Enums so that could be another category.
We’ll talk about the main.graphql
in a minute.
Mashing them together
To combine the different graphql files we will use the graphql-import npm package. This is developed by the guys doing Prisma. It lets you import other graphql files with its import
statement.
Let’s take this example type.
# import JSON from "../scalars/JSON.graphql"
# import JSON from "./Address.graphql"
"Represents a person"
type Person {
"The id by how you identify the person"
id: ID!
"The person's name"
name: String!
"A mock content field for the person, as JSON. For this article."
content: JSON!
"This person address"
address: Address!
}
- First we see that we are imporing the JSON scalar, this is a scalar provided by the graphql-type-json npm package. I added it as a colorful detail.
- Second, the same happens for a type but for Address.
You can image that the rest of the files will be pretty much the same, imports and usages in the schema.
So how do we create the final schema.graphql
that the server will consume??
The “main.graphql”
This file will, recursively, have all your queries, mutations, subscriptions and their used types.
# import Query.* from "./queries/queries.graphql"
# import Mutation.* from "./mutations/mutations.graphql"
# import Subscription from "./subscriptions/subscriptions.graphql"
That’s it. graphql-import
lets you use wildcards for consuming elements in the .graphql
. Check more options in their docs.
The usage is pretty much like this:
import { importSchema } from 'graphql-import'
import { makeExecutableSchema } from 'graphql-tools'
const typeDefs = importSchema('main.graphql');
const resolvers = {};
const schema = makeExecutableSchema({ typeDefs, resolvers });
// etc
Bonus Tracks
Webpack integration
You can use graphql-import-loader to let webpack manage the combining them.
Build the schema in a NPM Script
I need to generate this schema before publishing my npm package, so on the prepublishOnly
I run this:
const fs = require('fs');
const gqlImport = require('graphql-import');
const newSchema = gqlImport.importSchema('path/2/main.graphql');
fs.writeFileSync('path/2/new/generated/schema.graphql', newSchema);
Minified to get it in the script:
{
"scripts": {
"generateGqlSchema": "node -e \"const fs=require('fs'),i=require('graphql-import');fs.writeFileSync('src/main/resources/graphql/generated/schema.graphql',i.importSchema('src/main/resources/graphql/main.graphql'));\""
}
}
I will scale this more in the future so I’ll see how it stands the test of time, but for now it seems to work!
Let me know if you have an idea around it! Cheers!
Modularizing your graphQL schemas的更多相关文章
- 转 How do GraphQL remote schemas work
文章转自 prisma 官方博客,写的很不错 In this article, we want to understand how we can use any existing GraphQL AP ...
- [GraphQL] Write a GraphQL Schema in JavaScript
Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but ...
- GraphQL Gateway Architectures
转自: https://tomasalabes.me/blog/graphql/node/microservices/2018/08/11/graphql-architectures.html Gra ...
- 转 GraphQL Schema Stitching explained: Schema Delegation
转自官方文档 In the last article, we discussed the ins and outs of remote (executable) schemas. These remo ...
- Why GraphQL is Taking Over APIs
A few years ago, I managed a team at DocuSign that was tasked with re-writing the main DocuSign web ...
- [GraphQL] Add an Interface to a GraphQL Schema
As we start building out more complex GraphQL schemas, certain fields start to repeat across differe ...
- The Architectural Principles Behind Vrbo’s GraphQL Implementation
转自:https://medium.com/expedia-group-tech/graphql-component-architecture-principles-homeaway-ede8a58d ...
- 朱晔的互联网架构实践心得S2E5:浅谈四种API设计风格(RPC、REST、GraphQL、服务端驱动)
Web API设计其实是一个挺重要的设计话题,许多公司都会有公司层面的Web API设计规范,几乎所有的项目在详细设计阶段都会进行API设计,项目开发后都会有一份API文档供测试和联调.本文尝试根据自 ...
- GraphQL入门3(Mutation)
创建一个新的支持Mutation的Schema. var GraphQLSchema = require('graphql').GraphQLSchema; var GraphQLObjectType ...
随机推荐
- 3.4 C++名字隐藏
参数:http://www.weixueyuan.net/view/6361.html 总结: 如果派生类中新增一个成员变量,该成员变量与基类中的成员变量同名,则新增的成员变量就会遮蔽从基类中继承过来 ...
- 特殊权限stick_bit
stick_bit 防删除位:文件是否可以被某用户删除,主要取决于该文件所在的目录是否对该用户具有写权限.如果没有写权限,则这个目录下的所有文件都不能删除,同时也不能添加新的文件.如果希望用户能够添加 ...
- powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model.
powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model ...
- 数位DP详解
算法使用范围 在一个区间里面求有多少个满足题目所给的约束条件的数,约束条件必须与数自身的属性有关 下面用kuangbin数位dp的题来介绍 例题 不要62 题意:在一个区间里面求出有多少个不含4和6 ...
- Python如何操作redis
做UI自动化时,遇到一个问题,需要在后台操作完成后,产生结果才能在前端进行操作,但是用自动化在后台操作又很麻烦,就想直接操作数据库,然后再 在前端进行操作:这时遇到一个问题,在后台操作时,会写入到数据 ...
- JavaScript , js 上下文(this 的指代)
上下文 代表 this 变量的值, 以及 它的 指代; 它决定一个函数怎么被调用; 当一个函数作为一个对象的方法被调用的时候, this总是指向 调用这个方法的对象. ----- 1 ,情况一: 字面 ...
- sql 按字段指定值排序
这个需要在排序语句中使用条件判断 例如:表[Table_temp]中列[col1]为字符,属性为varchar(10),排序时需要按照B.A.C的顺序显示,则可按照以下SQL语句: select * ...
- github 多人协作
1.本地生成私钥: ssh-keygen -C "YourEmail@example.com" (这里的email使用github账号)生成公钥和私钥 2.查看私钥,并添加到自己的 ...
- 【Python】socket编程-1
一.什么是socket编程:网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket,编程传输层,socket本质是编程接口(API) 二.网络七层协议:物里层 数据链 ...
- sql,按照时间排序,取前N条
mysql: SELECT * from (SELECT H_TEMPERATURE,TH_TIME FROM wenshidu WHERE TH_TIME <= STR_TO_DATE(' ...