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 ...
随机推荐
- 服务器由于redis未授权访问漏洞被攻击
昨天阿里云拦截到了一次异常登陆,改了密码后就没有管他, 今天阿里云给我发消息说我的服务器可能被黑客利用,存在恶意发包行为....... 不过我不打算只是单纯的重置系统,经过一系列的查找原因后,发现被攻 ...
- bootstrap动态生成层级ul-li 新闻预览 常用方法
<div class="row" id="add-withinfosortId-row" style="display: none"& ...
- 一个简单的JSP程序示例
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- centos中PATH环境变量查看和修改
PAHT环境变量 :定义的是系统搜索命令的路径.<就是自己写的程序不打绝对路径就可以执行,必须放到 $PATH这个文件中>查看命令:echo $PATH 以添加mongodb server ...
- Centos7部署kubernetes Proxy(七)
1.配置kube-proxy使用LVS(三个节点都装上去) [root@linux-node1 ssl]# yum install -y ipvsadm ipset conntrack [root@l ...
- rancher中使用ingress-lbs做负载均衡
rancher 相关资料 http://rancher.com/docs/rancher/v1.6/zh/kubernetes/ingress/ lvs, haproxy, nginx负载均衡器比较 ...
- NioEventLoop(netty 4.1)
里面有个excecutor属性, 在loopgroup实例化loop的时候, 如果execute一个runnable的task的时候,检测loop启动了没有,没启动的话,执行excecutor的exe ...
- Android:DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs
Android studio DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs 一.报错信息 DELETE_FAILED_INTERN ...
- 四条命令快速在Ubuntu16.04上配置DNS服务器
1. apt install dnsmasq -y 2. vim /etc/dnsmasq.d/resolv.conf address=/xxx.yyy.com/21.xx.xx.x 3. servi ...
- 【Python】多线程-3
#练习:线程等待 Event e.set() e.wait() from threading import Thread, Lock import threading import time ...