1.0.5 • Published 3 days ago

schema-filter v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

schema-filter

"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

Examples

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

How it works

  1. extracts all available queries, mutations, subscriptions from given schema
  2. generates filters for each operation type (Query, Mutation, Subscription)
  3. generates reduced-schema by using filters
  4. Further work you may include/exclude single operation by name in check-list as you wish

Commands

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

Getting Started

  1. Install package (one of below command)

    yarn add --dev schema-filter
    # npm i --save schema-filter
  2. 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".
       "node-name-regexes-to-exclude": ["\\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 "node-name-regexes-to-exclude", you should provide this field as well.
       "replacing-custom-scalar-name": "CustomScalarName"
    }

    Mandatory fields

    1 schema-orginal : schema file path to reduce

### Optional fields

[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.

<br>

[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`

<br>

[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`

<br>

[5] `node-name-regexes-to-exclude` : **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 `replacing-custom-scalar-name` as well.

<br>

[6] `replacing-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 `node-name-regexes-to-exclude`, you should provide this field as well.
  1. Initialize/Update filters

    Below command will generate filters file under generated filters/ directory in schema-orginal's directory or where you set in package.json If already filters are generated, this command will update filters by adding newly added operations, not touching already generated operations

    npx schema-filter init
  2. filter(reduce) schema using filters

    execute below command at project root path

    npx schema-filter:filter
  1. 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
  2. if any change is made in check-list, execute below command to filter schema again

    npx schema-filter filter

Contributions

How to Test Locally

  1. clone this repository

    git clone https://github.com/vetching-corporation/schema-filter.git
  2. 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"

How to Build

yarn cb # clean build

Further Requirements?

If you need any extra functionality, please make an issue on github or contact me dev.kyungho@gmail.com

1.0.5

3 days ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

0.0.1

8 months ago