|
2 | 2 | from collections import OrderedDict
|
3 | 3 | from datetime import date, datetime
|
4 | 4 | from decimal import Decimal
|
| 5 | +from hashlib import sha256 |
5 | 6 | from itertools import chain
|
6 | 7 |
|
7 | 8 | import arrow
|
|
16 | 17 | GraphQLNonNull,
|
17 | 18 | GraphQLObjectType,
|
18 | 19 | GraphQLScalarType,
|
| 20 | + GraphQLSchema, |
19 | 21 | GraphQLString,
|
| 22 | + lexicographic_sort_schema, |
| 23 | + print_schema, |
20 | 24 | )
|
21 | 25 | from graphql.type.directives import specified_directives
|
22 | 26 | import six
|
@@ -447,3 +451,30 @@ def check_for_nondefault_directive_names(directives):
|
447 | 451 | raise AssertionError(
|
448 | 452 | u"Unsupported directives found: {}".format(nondefault_directives_found)
|
449 | 453 | )
|
| 454 | + |
| 455 | + |
| 456 | +def compute_schema_fingerprint(schema: GraphQLSchema) -> str: |
| 457 | + """Compute a fingerprint compactly representing the data in the given schema. |
| 458 | +
|
| 459 | + The fingerprint is not sensitive to things like type or field order. This function is guaranteed |
| 460 | + to be robust enough that if two GraphQLSchema have the same fingerprint, then they also |
| 461 | + represent the same schema. |
| 462 | +
|
| 463 | + Because of internal implementation changes, different versions of this library *may* produce |
| 464 | + different fingerprints for the same schema. Since cross-version fingerprint stability |
| 465 | + is an *explicit non-goal* here, changing a schema's fingerprint will not be considered |
| 466 | + a breaking change. |
| 467 | +
|
| 468 | + The fingerprint is computed on a best-effort basis and has some known issues at the moment. |
| 469 | + Please see the discussion in the pull request below for more details. |
| 470 | + https://github.com/kensho-technologies/graphql-compiler/pull/737 |
| 471 | +
|
| 472 | + Args: |
| 473 | + schema: the schema for which to compute a fingerprint. |
| 474 | +
|
| 475 | + Returns: |
| 476 | + a hexadecimal string fingerprint compactly representing the data in the schema. |
| 477 | + """ |
| 478 | + lexicographically_sorted_schema = lexicographic_sort_schema(schema) |
| 479 | + text = print_schema(lexicographically_sorted_schema) |
| 480 | + return sha256(text.encode("utf-8")).hexdigest() |
0 commit comments