https://github.com/awslabs/graphql-schema-utilities
A CLI tool to merge schema files, and validate operations against the merged GraphQL Schema. With features like adding your own custom validation rules.
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (9.7%) to scientific vocabulary
Keywords from Contributors
Repository
A CLI tool to merge schema files, and validate operations against the merged GraphQL Schema. With features like adding your own custom validation rules.
Basic Info
- Host: GitHub
- Owner: awslabs
- License: apache-2.0
- Language: TypeScript
- Default Branch: master
- Size: 184 KB
Statistics
- Stars: 93
- Watchers: 9
- Forks: 24
- Open Issues: 14
- Releases: 12
Metadata Files
README.md
graphql-schema-utilities
A CLI tool to merge schema files, and validate operations against a GraphQL Schema.
Installation
sh
npm install -g graphql-schema-utilities
Usage
Using the CLI tool
```sh
graphql-schema-utilities ```
The tool will first merge schema files and then validate the merged schema, throwing errors if the schema isn't valid and exit with exit code 1. If Schema is valid it will check each operation in the file glob by parsing the operation and validating it against the schema. If errors are found, they will be displayed by file name and exit with exit code 1.
Merging schema files
You can merge your schema files across different modules and directories. In this example, you have three different set of files in three different directories:
``` ~/moduleMain/schemas/Root.graphql: type Query;
~/module1/schemas/Book.graphql: extend type Query { bookById(id: ID!): Book }
type Book { id: ID! authorId: ID! }
~/module2/schemas/User.graphql: extend type Query { userById(id: ID!): User }
type User { id: ID! name: String! }
```
Running the CLI utility generates the merged schema file, Merged_schema.graphQL:
```
type Query { userById(id: ID!): User bookById(id: ID!): Book }
type User { id: ID! name: String! }
type Book { id: ID! authorId: ID! }
```
The CLI options:
Options:
-V, --version output the version number.
-o, --output [pattern] The file path where the merged schema will be outputted to.
-s, --schema [pattern] Use a glob path that would define all of your schema files to merge them into a valid schema. (default: "").
-r, --rules [pattern] The file path for your custom rules to validate your operations, and your merged schema. To learn abot how to write your custom rules: check the README.md file (default: "").
-p, --operations [pattern] Use a glob that that contains your graphql operation files to test against the merged schema file. (default: "").
-d, --includeDirectives By default will NOT merge the directives, unless you added this flag.
-h, --help output usage information.
How to merge the schema files:
```sh
graphql-schema-utilities -s "{./FirstDirectory/*/.graphql,./SecondDirectory/users/**/ *.graphql}" ``` That will merge all the schema files in both directories. Note that we are passing the directories as Glob.
How to validate your operations against your merged schema:
```sh
graphql-schema-utilities -s "{./FirstDirectory/*/.graphql,./SecondDirectory/users// .graphql}" -p "./pathtodirectory/operations/.graphql" ``` Note that the "./pathtodirectory/operations/.graphql" *operations path is also using Glob.
How to add your custom validation rules:
These tools will use the validation rules as defined in graphql-js validation rules. But you can create your own validation rule. Here is an example for custom validation rule against your operation which validates that your operation name must be prefixed with Hawaii_ .
```js
file name: custom_rule.ts
import { GraphQLError } from 'graphql';
export function doesNotStartWithHawaii(operationName: string): string {
return "${operationName}" operation does not start with Hawaii_.;
}
/**
* Valid only if it starts with Hawaii.
* A GraphQL document is only valid if all defined operations starts with Hawaii.
*/
export function OperationNameStartsWithHawaii(
context: any,
): any {
const knownOperationNames = Object.create(null);
return {
OperationDefinition(node) {
const operationName = node.name;
if (operationName) {
if (!operationName.value.startsWith('Hawaii_')) {
context.reportError(
new GraphQLError(
doesNotStartWithHawaii(operationName.value)
),
);
} else {
knownOperationNames[operationName.value] = operationName;
}
}
return false;
},
FragmentDefinition: () => false,
}; } ```
Then run the CLI with the rules option:
```sh
graphql-schema-utilities -s "{./FirstDirectory/*/.graphql,./SecondDirectory/users// .graphql}" -p "./pathtodirectory/operations/.graphql" -r "path/to/custom_rule.js" ``` **Note:
1- We are referencing the .js file not the .ts.
2- The path here is NOT Glob, You can use either relative or absolute path.
To learn more about how to write your own custom validation rules against graphql schema or operation files: Validate method in graphql-js.
Merge and Validate programmatically
This tool can be used as a library for a JS app as well. you can call the mergeSchemas async using Promise.
```js const tools = require('graphql-schema-utilities');
const glob = "{./FirstDirectory/*/.graphql,./SecondDirectory/users/**/ *.graphql}" tools.mergeGQLSchemas(glob).then((schema) => { console.log('schema files were merged, and the valid schema is: ', schema) }) .catch((error) => { console.error(error) }) ```
Validate operations using promises:
```js
tools.mergeGQLSchemas('./schema/.graphql').then((schema) => { tools.validateOperations('./queries/.graphql', schema).then((results) => { console.log(results) }) }) ```
Note: you must use quotes around each file glob or the utility will not work properly.
Development
Install dependencies with
sh
npm install
Build
sh
npm run build
Run test in watch mode
sh
npm run test:watch
Contributing
Please help make this tool better. For more information take a look at CONTRIBUTING.md
License
Notes
This package was created based on a fork from graphql-validator that was developed by credit-karma.
Owner
- Name: Amazon Web Services - Labs
- Login: awslabs
- Kind: organization
- Location: Seattle, WA
- Website: http://amazon.com/aws/
- Repositories: 914
- Profile: https://github.com/awslabs
AWS Labs
GitHub Events
Total
- Watch event: 1
- Fork event: 1
Last Year
- Watch event: 1
- Fork event: 1
Committers
Last synced: about 2 years ago
Top Committers
| Name | Commits | |
|---|---|---|
| hsehweil | 2****l | 13 |
| James Siri | 2****i | 7 |
| Mridul Agarwal | m****g@a****m | 4 |
| Ramasamy | s****r@8****m | 3 |
| dependabot[bot] | 4****] | 3 |
| Hani Sehweil | h****l@a****m | 2 |
| Hitesh Kalra | h****a@a****m | 2 |
| Jie Xiao | j****u@g****m | 2 |
| Mark Thill | t****m@a****m | 1 |
| Utsav Murarka | u****r@a****m | 1 |
| mattdhart | m****t@g****m | 1 |
| Mridul Agarwal | 1****9 | 1 |
| shenoyirvine | 6****e | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 11 months ago
All Time
- Total issues: 21
- Total pull requests: 31
- Average time to close issues: about 1 month
- Average time to close pull requests: about 1 month
- Total issue authors: 15
- Total pull request authors: 13
- Average comments per issue: 1.19
- Average comments per pull request: 0.45
- Merged pull requests: 21
- Bot issues: 0
- Bot pull requests: 7
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- hsehweil (5)
- andidev (2)
- kadishmal (2)
- visrahane (1)
- jonathanbiard (1)
- Srinu3366 (1)
- half2me (1)
- shenoyirvine (1)
- thillm-amazon (1)
- psigen (1)
- Dlozitskiy (1)
- fangzhuo-amazon (1)
- xinguaws (1)
- DanielGuelfi (1)
- khitrenovich (1)
Pull Request Authors
- hsehweil (11)
- dependabot[bot] (7)
- sjcodejam (2)
- sivarajramasamy (2)
- shenoyirvine (2)
- askulkarni2 (1)
- mattdhart (1)
- thillm-amazon (1)
- mridulag2899 (1)
- kalrahitesh94 (1)
- pandajiexiao (1)
- juannorris (1)
- khitrenovich (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 2
-
Total downloads:
- npm 40,427 last-month
-
Total dependent packages: 4
(may contain duplicates) -
Total dependent repositories: 7
(may contain duplicates) - Total versions: 15
- Total maintainers: 4
npmjs.org: graphql-schema-utilities
Merge GraphQL Schema files and validate queries against the merged Schema
- Homepage: https://github.com/awslabs/graphql-schema-utilities#readme
- License: Apache-2.0
-
Latest release: 1.1.8
published over 2 years ago
Rankings
Maintainers (3)
npmjs.org: @seriouscoderone/graphql-schema-utilities
My fork of graphql-schema-utilities. Merge GraphQL Schema files and validate queries against the merged Schema
- Homepage: https://github.com/awslabs/graphql-schema-utilities#readme
- License: Apache-2.0
-
Latest release: 1.1.3
published over 4 years ago
Rankings
Maintainers (1)
Dependencies
- 194 dependencies
- @types/code ^4.0.0 development
- @types/glob ^5.0.30 development
- @types/graphql ^14.2.0 development
- @types/lab ^11.1.0 development
- @types/mkdirp ^0.5.0 development
- @types/node ^8.10.49 development
- @types/rimraf 0.0.28 development
- code ^4.0.0 development
- lab ^14.1.0 development
- mkdirp ^0.5.1 development
- rimraf ^2.6.1 development
- tslint ^5.5.0 development
- typescript ^3.1.0 development
- chalk ^2.4.2
- commander ^2.11.0
- glob ^7.1.2
- graphql ^14.3.0
- graphql-tools ^4.0.4
- lodash ^4.17.14