Compare GraphQL Schemas
Project description
schemadiff
schemadiff
is a lib that shows you the difference between two GraphQL Schemas.
It takes two schemas from a string or a file and gives you a list of changes between both versions.
This might be useful for
- Detecting breaking changes before they reach the api clients
- Integrating into CI pipelines to control your api evolution
- Document your api changes and submit them for approval along with your pull requests.
Installation
The lib requires python3.6 or greater to work. In order to install it run
$ python3 -m pip install graphql-schema-diff
Usage
You can use this package as a lib or as a cli. You can choose what better suits your needs
Lib
>>> from schemadiff import diff, diff_from_file, print_diff
>>> old_schema = """
schema {
query: Query
}
type Query {
a: Int!,
sum(start: Float=0): Int
}
"""
>>> new_schema = """
schema {
query: Query
}
type Query {
b: String,
sum(start: Float=1): Int
}
"""
>>> changes = diff(old_schema, new_schema)
>>> print_diff(changes) # Pretty print difference
>>> any(change.breaking or change.dangerous for change in changes) # Check if there was any breaking or dangerous change
# You can also compare from schema files
>>> with open('old_schema.gql', 'w') as f:
... f.write(old_schema)
>>> with open('new_schema.gql', 'w') as f:
... f.write(new_schema)
>>> changes = diff_from_file('old_schema.gql', 'new_schema.gql')
>>> print_diff(changes)
CLI
Inside your virtualenv you can invoke the entrypoint to see its usage options
$ schemadiff -h
Usage: schemadiff [-h] -o OLD_SCHEMA -n NEW_SCHEMA [-j] [-a ALLOW_LIST] [-t] [-s]
Schema comparator
optional arguments:
-h, --help show this help message and exit
-o OLD_SCHEMA, --old-schema OLD_SCHEMA
Path to old graphql schema file
-n NEW_SCHEMA, --new-schema NEW_SCHEMA
Path to new graphql schema file
-j, --as-json Output a detailed summary of changes in json format
-a ALLOW_LIST, --allow-list ALLOW_LIST
Path to the allowed list of changes
-t, --tolerant Tolerant mode. Error out only if there's a breaking
change but allow dangerous changes
-s, --strict Strict mode. Error out on dangerous and breaking
changes.
Examples
# Compare schemas and output diff to stdout
schemadiff -o tests/data/simple_schema.gql -n tests/data/new_schema.gql`
# Pass a evaluation flag (mixing long arg name and short arg name)
schemadiff --old-schema tests/data/simple_schema.gql -n tests/data/new_schema.gql --strict`
# Print output as json with details of each change
schemadiff -o tests/data/simple_schema.gql -n tests/data/new_schema.gql --as-json
# Save output to a json file
schemadiff -o tests/data/simple_schema.gql -n tests/data/new_schema.gql --as-json > changes.json
# Compare schemas ignoring allowed changes
schemadiff -o tests/data/simple_schema.gql -n tests/data/new_schema.gql -a allowlist.json
If you run the cli and see a replacement character (�) or a square box (□) instead of the emojis run
$ sudo apt install fonts-noto-color-emoji $ vim ~/.config/fontconfig/fonts.conf # and paste https://gist.github.com/Ambro17/80bce76d07a6eb74323db2ca9b887263 $ fc-cache -f -vThat should install noto emoji fonts and set is as the fallback font to render emojis 😎
Credits
Implementation was heavily inspired by Marc Giroux ruby version and Kamil Kisiela js implementation.
Logo arrows were adapted from the work of Paul Verhulst @ The Noun Project
Contributions
Bug reports, Feature requests or pull request are welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file graphql-schema-diff-1.0.1.tar.gz
.
File metadata
- Download URL: graphql-schema-diff-1.0.1.tar.gz
- Upload date:
- Size: 28.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c22d3e718822d1a775a6545ca4517f0f91896d6c2469059e17f1672dc054afe5 |
|
MD5 | d841d5d73a91f7a04b852669e2f7ef09 |
|
BLAKE2b-256 | 307e396682ab142555cacde6f28b2cb9c0e13931318c93ed350f7b19a9782fbb |
File details
Details for the file graphql_schema_diff-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: graphql_schema_diff-1.0.1-py3-none-any.whl
- Upload date:
- Size: 47.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4133c80c458a8cf1efeebf8b0433ca9efd9b0ca8f1e973a1bde03987c0e40bf |
|
MD5 | 6f8ccc08c19cad0bc2c3e9885ed1f6c4 |
|
BLAKE2b-256 | 87b6c77951aada5d34d3dd3b94f048fd9c682e74923c1da7a978d3c9c3b7197b |