"Tool to reduce graphql schema size"
Given schema, this package extracts all available queries, mutations, subscriptions and then make list of those to determine whether to use each graphql operation, by using that check-list, reduced-schema is generated. Actually-not used operations are not included in final reduced-schema
Feel free to check example/ You may figure out what's happening there.
Given below schema & our generated Query.json
, Mutation.json
, Subscription.json
filter file
(which is also generated/updated by schema-filter init
command)
type Post {
id: ID!
title: String!
content: String!
}
type Query {
getPost(id: ID!): Post
getAllPosts: [Post!]!
}
type Mutation {
createPost(title: String!, content: String!): Post!
updatePost(id: ID!, title: String, content: String): Post!
deletePost(id: ID!): ID!
updatePostWithInput(id: ID!, data: PostUpdateDataInput): Post
}
type Subscription {
postCreated: Post!
postUpdated(id: ID!): Post!
postDeleted(id: ID!): ID!
}
input PostUpdateDataInput {
id: ID
title: String
content: String
}
This package finally generates below reduced-schema If some types are not used(reachable) in any operations, those types are excluded in reduced-schema (even though not visible in below example for now, feel sory for that, later I will update this example)
Only actually-used operations are included in reduced-schema
type Post {
id: ID!
title: String!
content: String!
}
type Query {
getPost(id: ID!): Post
}
type Mutation {
createPost(title: String!, content: String!): Post!
deletePost(id: ID!): ID!
updatePostWithInput(id: ID!, data: CustomScalarName): Post
}
type Subscription {
postDeleted(id: ID!): ID!
}
scalar CustomScalarName
- extracts all available queries, mutations, subscriptions from given schema
- generates
filters
for each operation type (Query
,Mutation
,Subscription
) - generates reduced-schema by using filters
- [Further work] you may include/exclude single operation by name in check-list as you wish
For every command, you should add prefix npx schema-filter
to execute
To show available commands, execute one of below command after installation
npx schema-filter
npx schema-filter --help
-
Install package (one of below command)
yarn add --dev schema-filter # npm i --save schema-filter
-
Add/Adjust below configuration in your package.json
only schmea-original is "MANDATORY", others are optional
"schema-filter": { // "MANDATORY" // give your schema "file" path "schema-original":"lib/src/gql/schema.graphql", // give "directory" path to store filters as you wish "filters":"lib/src/schema-filters/", // give "file" path to store reduced-schema as you wish "schema-reduced":"lib/src/gql/schema-reduced.graphql", // after initialization of filters, there would be new operations // for newly added operations, you can set default behavior (whether to incldue or not) // for all operation type, default value is `true` "batch-setting": { "Query": true, "Mutation": false, "Subscription": true, }, // If you need to exclude some input types by name, you can set Regular Expressions. // With the given example, you may exclude input types such as "PostUpdateDataInput". "input-type-name-regexes-to-remove": ["\\b\\w+(Update)\\w+(Input)\\b"], // The excluded input type nodes will be replaced with the custom scalar type, // and its name could be set here. // If you provide "input-type-name-regexes-to-remove", you should provide this field as well. "custom-scalar-name": "CustomScalarName" }
[1]
schema-orginal
: schema file path to reduce[2]
filters
: directory path to store generated filter files- If not given, generated filter files' path are determined by
schema-orginal
's path - Directory will be same with
schema-orginal
's directory - Directory name will be
filters
and filters will be generated under it.
[3]
schema-reduced
: file path to store reduced schema- If not given, generated-reduced schema file information is determined by
schema-orginal
's path - Directory will be same with
schema-orginal
's directory - Filename will be
schema-reduced.graphql
[4]
batch-setting
: default filter value for newly added operations by operation type- After initialization of filters, there would be new operations. for newly added operations, you can set default behavior (whether to incldue or not)
- For all operation type, default value is
true
[5]
input-type-name-regexes-to-remove
: Regular-Expression-like string values array to exclude some input types by name- If you need to exclude some input types by name, you can set Regular Expressions.
- If you provide this field, you should provide
custom-scalar-name
as well.
[6]
custom-scalar-name
: Custom scalar type name to replace excluded input types- The excluded input type nodes will be replaced with the custom scalar type,
- and its name could be set here.
- If you provide
input-type-name-regexes-to-remove
, you should provide this field as well.
- If not given, generated filter files' path are determined by
-
Initialize/Update filters
Below command will generate filters file under generated
filters/
directory inschema-orginal
's directory or where you set inpackage.json
If already filters are generated, this command will update filters by adding newly added operations, not touching already generated operationsnpx schema-filter init
-
filter(reduce) schema using filters
execute below command at project root path
npx schema-filter:filter
-
[Further Work] toggle on/off single operation in filters
How to include operation
npx schema-filter include {operation-name} npx schema-filter include {operation-name} -a # call with -a option to filter schema again using new check-list
How to exclude operation
npx schema-filter exclude {operation-name} npx schema-filter exclude {operation-name} -a # call with -a option to filter schema again using new check-list
-
if any change is made in check-list, execute below command to filter schema again
npx schema-filter filter
-
clone this repository
git clone https://github.com/vetching-corporation/schema-filter.git
-
check below commands in package.json
"test:init": "yarn build && node build/index.js init", "test:include": "yarn build && node build/index.js include", "test:exclude": "yarn build && node build/index.js exclude", "test:filter": "yarn build && node build/index.js filter"
yarn cb # clean build
If you need any extra functionality, please make an issue on github or contact me [dev.kyungho@gmail.com]