Skip to main content

Compare GraphQL Schemas

Project description

Build Status codecov Codacy Badge License: GPL v3 Python 3.6+

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, format_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(format_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(format_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 [-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
  -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

$ schemadiff -o tests/data/simple_schema.gql -n tests/data/new_schema.gql

$ schemadiff --old-schema tests/data/simple_schema.gql -n tests/data/new_schema.gql --tolerant

$ schemadiff -o tests/data/simple_schema.gql --new-schema tests/data/new_schema.gql --strict

Sample output

✔️ Field `c` was added to object type `Query` Field `a` was removed from object type `Query`
⚠️ Default value for argument `x` on field `Field.calculus` changed from `0` to `1`

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
$ fc-cache -f -v

That should install noto emoji fonts and refresh the font cache so you can see the proper emojis 😎

Credits

Implementation was heavily inspired by Marc Giroux ruby version and Kamil Kisiela js implementation.

Contributions

Bug reports, Feature requests or pull request are welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphql-schema-diff-0.0.5.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

graphql_schema_diff-0.0.5-py3-none-any.whl (45.1 kB view details)

Uploaded Python 3

File details

Details for the file graphql-schema-diff-0.0.5.tar.gz.

File metadata

  • Download URL: graphql-schema-diff-0.0.5.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.10

File hashes

Hashes for graphql-schema-diff-0.0.5.tar.gz
Algorithm Hash digest
SHA256 17f4ed5d7c93345ddb07498031cf47f66c55930851c13ac38618990d6ee2d835
MD5 8a27dcbc976d6b8236de2f4caadf5a1f
BLAKE2b-256 cd15ea85cc0885eb85797eb0541b4c04031191a23193265e2e283f9a37e363c3

See more details on using hashes here.

File details

Details for the file graphql_schema_diff-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: graphql_schema_diff-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 45.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.10

File hashes

Hashes for graphql_schema_diff-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9126ed38f7f723dd75fefb8fe88f2d40b1a1a1fe68e3a9a646df4a1ead529605
MD5 84092c0b07941b43a238400063c7c2f6
BLAKE2b-256 abc13af071b5cbd2d3b95747c93132c237661ec0bde3ce7030eebacdd0747ec7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page