79
79
- Suppressing unions.
80
80
- 1-1 and 1-many renamings for fields belonging to object types.
81
81
- Suppressions for fields belonging to object types.
82
+ - Renamings and suppressions for scalar types.
82
83
83
84
Operations that are not yet supported but will be implemented:
84
85
- Suppressions for enums, interfaces, and object types that implement interfaces.
85
- - Renamings and suppressions for scalar types.
86
86
- Renamings and suppressions for fields that belong to either interface types or object types that
87
87
implement interfaces.
88
88
- Renamings and suppressions for enum values.
146
146
builtin_scalar_type_names ,
147
147
check_ast_schema_is_valid ,
148
148
get_copy_of_node_with_new_name ,
149
- get_custom_scalar_names ,
150
149
get_query_type_name ,
151
150
is_valid_nonreserved_name ,
152
151
)
@@ -235,15 +234,12 @@ def rename_schema(
235
234
236
235
schema = build_ast_schema (schema_ast )
237
236
query_type = get_query_type_name (schema )
238
- custom_scalar_names = get_custom_scalar_names (schema )
239
237
240
- _validate_renamings (
241
- schema_ast , type_renamings , field_renamings , query_type , custom_scalar_names
242
- )
238
+ _validate_renamings (schema_ast , type_renamings , field_renamings , query_type )
243
239
244
240
# Rename types, interfaces, enums, unions and suppress types, unions
245
241
schema_ast , reverse_name_map , reverse_field_name_map = _rename_and_suppress_types_and_fields (
246
- schema_ast , type_renamings , field_renamings , query_type , custom_scalar_names
242
+ schema_ast , type_renamings , field_renamings , query_type
247
243
)
248
244
249
245
schema_ast = _rename_and_suppress_query_type_fields (schema_ast , type_renamings , query_type )
@@ -260,14 +256,12 @@ def _validate_renamings(
260
256
type_renamings : Mapping [str , Optional [str ]],
261
257
field_renamings : Mapping [str , Mapping [str , Set [str ]]],
262
258
query_type : str ,
263
- custom_scalar_names : Set [str ],
264
259
) -> None :
265
260
"""Validate the type_renamings argument before attempting to rename the schema.
266
261
267
262
Check for fields with suppressed types or unions whose members were all suppressed. Also,
268
263
confirm type_renamings contains no enums, interfaces, or interface implementation suppressions
269
- because that hasn't been implemented yet. Confirm that no scalars would be suppressed or
270
- renamed.
264
+ because that hasn't been implemented yet.
271
265
272
266
The input AST will not be modified.
273
267
@@ -282,16 +276,14 @@ def _validate_renamings(
282
276
field names belonging to the type to a set of field names for the
283
277
renamed schema
284
278
query_type: name of the query type, e.g. 'RootSchemaQuery'
285
- custom_scalar_names: set of all user defined scalars used in the schema (excluding
286
- builtin scalars)
287
279
288
280
Raises:
289
281
- CascadingSuppressionError if a type/field suppression would require further suppressions
290
282
- NotImplementedError if type_renamings attempts to suppress an enum, an interface, or a
291
283
type implementing an interface
292
284
"""
293
285
_ensure_no_cascading_type_suppressions (schema_ast , type_renamings , field_renamings , query_type )
294
- _ensure_no_unsupported_operations (schema_ast , type_renamings , custom_scalar_names )
286
+ _ensure_no_unsupported_operations (schema_ast , type_renamings )
295
287
296
288
297
289
def _ensure_no_cascading_type_suppressions (
@@ -350,28 +342,23 @@ def _ensure_no_cascading_type_suppressions(
350
342
def _ensure_no_unsupported_operations (
351
343
schema_ast : DocumentNode ,
352
344
type_renamings : Mapping [str , Optional [str ]],
353
- custom_scalar_names : Set [str ],
354
345
) -> None :
355
346
"""Check for unsupported type renaming or suppression operations."""
356
- _ensure_no_unsupported_scalar_operations (type_renamings , custom_scalar_names )
347
+ _ensure_no_unsupported_scalar_operations (type_renamings )
357
348
_ensure_no_unsupported_suppressions (schema_ast , type_renamings )
358
349
359
350
360
351
def _ensure_no_unsupported_scalar_operations (
361
352
type_renamings : Mapping [str , Optional [str ]],
362
- custom_scalar_names : Set [str ],
363
353
) -> None :
364
354
"""Check for unsupported scalar operations."""
365
355
unsupported_scalar_operations = {
366
- scalar_name : type_renamings [scalar_name ]
367
- for scalar_name in custom_scalar_names .union (builtin_scalar_type_names )
368
- if scalar_name in type_renamings
356
+ scalar_name for scalar_name in builtin_scalar_type_names if scalar_name in type_renamings
369
357
}
370
358
if unsupported_scalar_operations :
371
359
raise NotImplementedError (
372
- f"Scalar renaming and suppression is not implemented yet, but type_renamings attempted "
373
- f"to modify the following scalars: { unsupported_scalar_operations } . To fix this, "
374
- f"remove them from type_renamings."
360
+ f"Type_renamings contained renamings for the following built-in scalar types: "
361
+ f"{ unsupported_scalar_operations } . To fix this, remove them from type_renamings."
375
362
)
376
363
377
364
@@ -422,7 +409,6 @@ def _rename_and_suppress_types_and_fields(
422
409
type_renamings : Mapping [str , Optional [str ]],
423
410
field_renamings : Mapping [str , Mapping [str , Set [str ]]],
424
411
query_type : str ,
425
- custom_scalar_names : Set [str ],
426
412
) -> Tuple [DocumentNode , Dict [str , str ], Dict [str , Dict [str , str ]]]:
427
413
"""Rename and suppress types, enums, interfaces, fields using renamings.
428
414
@@ -439,8 +425,6 @@ def _rename_and_suppress_types_and_fields(
439
425
field names belonging to the type to a set of field names for the
440
426
renamed schema
441
427
query_type: name of the query type, e.g. 'RootSchemaQuery'
442
- custom_scalar_names: set of all user defined scalars used in the schema (excluding
443
- builtin scalars)
444
428
445
429
Returns:
446
430
Tuple containing the modified version of the schema AST, the renamed type name to original
@@ -452,9 +436,7 @@ def _rename_and_suppress_types_and_fields(
452
436
- SchemaRenameNameConflictError if the rename causes name conflicts
453
437
- NoOpRenamingError if renamings contains no-op renamings
454
438
"""
455
- visitor = RenameSchemaTypesVisitor (
456
- type_renamings , field_renamings , query_type , custom_scalar_names
457
- )
439
+ visitor = RenameSchemaTypesVisitor (type_renamings , field_renamings , query_type )
458
440
renamed_schema_ast = visit (schema_ast , visitor )
459
441
if visitor .invalid_type_names or visitor .invalid_field_names :
460
442
explanation = (
@@ -614,7 +596,6 @@ class RenameSchemaTypesVisitor(Visitor):
614
596
"ObjectValueNode" ,
615
597
"OperationDefinitionNode" ,
616
598
"OperationTypeDefinitionNode" ,
617
- "ScalarTypeDefinitionNode" ,
618
599
"SchemaDefinitionNode" ,
619
600
"SelectionSetNode" ,
620
601
"StringValueNode" ,
@@ -646,6 +627,7 @@ class RenameSchemaTypesVisitor(Visitor):
646
627
"InterfaceTypeDefinitionNode" ,
647
628
"NamedTypeNode" ,
648
629
"ObjectTypeDefinitionNode" ,
630
+ "ScalarTypeDefinitionNode" ,
649
631
"UnionTypeDefinitionNode" ,
650
632
}
651
633
)
@@ -661,8 +643,8 @@ class RenameSchemaTypesVisitor(Visitor):
661
643
type_renamed_to_builtin_scalar_conflicts : Dict [str , str ]
662
644
663
645
# reverse_name_map maps renamed type name to original type name, containing all non-suppressed
664
- # non-scalar types, including those that were unchanged. Must contain unchanged names to prevent
665
- # type renaming conflicts and raise SchemaRenameNameConflictError when they arise
646
+ # types, including those that were unchanged. Must contain unchanged names to prevent type
647
+ # renaming conflicts and raise SchemaRenameNameConflictError when they arise
666
648
reverse_name_map : Dict [str , str ]
667
649
668
650
# Collects invalid type names in type_renamings. If type_renamings["Foo"] is a string that is
@@ -719,7 +701,6 @@ def __init__(
719
701
type_renamings : Mapping [str , Optional [str ]],
720
702
field_renamings : Mapping [str , Mapping [str , Set [str ]]],
721
703
query_type : str ,
722
- custom_scalar_names : Set [str ],
723
704
) -> None :
724
705
"""Create a visitor for renaming types in a schema AST.
725
706
@@ -731,16 +712,13 @@ def __init__(
731
712
field names belonging to the type to a set of field names for the
732
713
renamed schema
733
714
query_type: name of the query type (e.g. RootSchemaQuery), which will not be renamed
734
- custom_scalar_names: set of all user defined scalars used in the schema (excluding
735
- builtin scalars)
736
715
"""
737
716
self .type_renamings = type_renamings
738
717
self .reverse_name_map = {}
739
718
self .type_name_conflicts = {}
740
719
self .type_renamed_to_builtin_scalar_conflicts = {}
741
720
self .invalid_type_names = {}
742
721
self .query_type = query_type
743
- self .custom_scalar_names = frozenset (custom_scalar_names )
744
722
self .suppressed_type_names = set ()
745
723
self .field_renamings = field_renamings
746
724
self .reverse_field_name_map = {}
@@ -772,11 +750,7 @@ def _rename_or_suppress_or_ignore_name_and_add_to_record(
772
750
"""
773
751
type_name = node .name .value
774
752
775
- if (
776
- type_name == self .query_type
777
- or type_name in self .custom_scalar_names
778
- or type_name in builtin_scalar_type_names
779
- ):
753
+ if type_name == self .query_type or type_name in builtin_scalar_type_names :
780
754
return IDLE
781
755
782
756
desired_type_name = self .type_renamings .get (type_name , type_name ) # Default use original
@@ -788,39 +762,9 @@ def _rename_or_suppress_or_ignore_name_and_add_to_record(
788
762
self .invalid_type_names [type_name ] = desired_type_name
789
763
790
764
# Renaming conflict arises when two types with different names in the original schema have
791
- # the same name in the new schema. There are two ways to produce this conflict:
792
- # 1. when neither type is a custom scalar
793
- # 2. when one type is a custom scalar and the other is not
794
- #
795
- # If neither type is a custom scalar, then desired_type_name will be in
796
- # self.reverse_name_map (because self.reverse_name_map records all non-scalar types in
797
- # the schema). The types named self.reverse_name_map[desired_type_name] and type_name in
798
- # the old schema would both get mapped to desired_type_name in the new schema, even though
799
- # self.reverse_name_map[desired_type_name] != type_name.
800
- #
801
- # If one type in the conflict is a custom scalar, then the conflict arises from
802
- # attempting to rename a non-scalar type (from type_name to desired_type_name) when
803
- # there already exists a custom scalar type named desired_type_name. Custom scalar
804
- # renaming has not been implemented yet so we know for sure that the scalar's name (in
805
- # both the original and new schema) is desired_type_name.
806
- #
807
- # It's also possible to produce a similar conflict where one type is not a custom scalar but
808
- # rather a built-in scalar, e.g. String. That case is handled separately because renaming a
809
- # type to a built-in scalar will never be allowed in any schema, whereas the conflicts here
810
- # arise from conflicting with the existence of other types in the schema.
811
- if (
812
- self .reverse_name_map .get (desired_type_name , type_name ) != type_name
813
- or desired_type_name in self .custom_scalar_names
814
- ):
815
- # If neither type in this conflict is a custom scalar, the two types causing conflict
816
- # were named type_name and self.reverse_name_map[desired_type_name] in the original
817
- # schema.
818
- # If one type in this conflict is a custom scalar, the two types causing conflict were
819
- # named type_name and desired_type_name (the latter being the custom scalar name) in the
820
- # original schema.
821
- conflictingly_renamed_type_name = self .reverse_name_map .get (
822
- desired_type_name , desired_type_name
823
- )
765
+ # the same name in the new schema.
766
+ if self .reverse_name_map .get (desired_type_name , type_name ) != type_name :
767
+ conflictingly_renamed_type_name = self .reverse_name_map [desired_type_name ]
824
768
825
769
# Collect all types in the original schema that would be named desired_type_name in the
826
770
# new schema
0 commit comments