Package: graphql-schema-diff
schemadiffschemadiffis 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 clientsIntegrating into CI pipelines to control your api evolutionDocument your api changes and submit them for an approval along with your pull requests.Restricting unwanted changesInstallationThe lib requires python3.6 or greater to work. In order to install it run$python3-mpipinstallgraphql-schema-diffUsageYou can use this package as a lib or as a CLI. You can choose what better suits your needsAs a Libfromschemadiffimportdiff,diff_from_file,print_diffold_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 differenceany(change.breakingorchange.dangerousforchangeinchanges)# Check if there was any breaking or dangerous change# You can also compare from schema fileswithopen('old_schema.gql','w')asf:f.write(old_schema)withopen('new_schema.gql','w')asf:f.write(new_schema)changes=diff_from_file('old_schema.gql','new_schema.gql')print_diff(changes)CLIInside your virtualenv you can invoke the entrypoint to see its usage options$schemadiff-h
Usage:schemadiff[-h]-oOLD_SCHEMA-nNEW_SCHEMA[-j][-aALLOW_LIST][-t][-r][-s]Schemacomparator

optionalarguments:-h,--helpshowthishelpmessageandexit-oOLD_SCHEMA,--old-schemaOLD_SCHEMAPathtooldgraphqlschemafile-nNEW_SCHEMA,--new-schemaNEW_SCHEMAPathtonewgraphqlschemafile-j,--as-jsonOutputadetailedsummaryofchangesinjsonformat-aALLOW_LIST,--allow-listALLOW_LISTPathtotheallowedlistofchanges-t,--tolerantTolerantmode.Erroroutonlyifthere'sabreakingchangebutallowdangerouschanges-r,--restrictionsRestrictedmode.Erroroutonrestrictedchanges.-s,--strictStrictmode.Erroroutondangerousandbreakingchanges.Examples# Compare schemas and output diff to stdoutschemadiff-otests/data/simple_schema.gql-ntests/data/new_schema.gql# Pass a evaluation flag (mixing long arg name and short arg name)schemadiff--old-schematests/data/simple_schema.gql-ntests/data/new_schema.gql--strict# Print output as json with details of each changeschemadiff-otests/data/simple_schema.gql-ntests/data/new_schema.gql--as-json# Save output to a json fileschemadiff-otests/data/simple_schema.gql-ntests/data/new_schema.gql--as-json>changes.json# Compare schemas ignoring allowed changesschemadiff-otests/data/simple_schema.gql-ntests/data/new_schema.gql-aallowlist.json# Compare schemas restricting adding new types without descriptionschemadiff-otests/data/simple_schema.gql-nsimple_schema_new_type_without_description.gql-radd-type-without-descriptionIf you run the cli and see a replacement character (�) or a square box (□) instead of the emojis run$sudoaptinstallfonts-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 😎Restricting changesYou can use this library to validate whether your schema matches a set of rules.Built-in restrictionsThe library has its own built-in restrictions ready-to-use. Just append them to the-rcommand inCLI. You can
add as many as you want.add-type-without-descriptionRestrict adding new GraphQL types without entering a non-empty description.remove-type-descriptionRestrict removing the description from an existing GraphQL type.add-field-without-descriptionRestrict adding fields without description.remove-field-descriptionRestrict removing the description from an existing GraphQL field.add-enum-value-without-descriptionRestrict adding enum value without description.remove-enum-value-descriptionRestrict adding enum value without description.Running the following command, you could restrict type additions without entering a nice description.# Compare schemas restricting adding new types without description
schemadiff -o tests/data/simple_schema.gql -n simple_schema_new_type_without_description.gql -r add-type-without-descriptionAPI ReferenceYou can also read theAPI Referenceif you want to get a better understanding of the inner workings of the libCreditsImplementation was heavily inspired by Marc Girouxruby versionand Kamil Kisielajs implementation.Logo arrows were adapted from the work ofPaul Verhulst @ The Noun ProjectContributors@Checho3388: Added the wholeevaluate_difffunctionality. Thank you!You can contribute reporting bugs, writing issues or pull requests for any new features!