@@ -919,25 +919,51 @@ export type FieldSchema =
919
919
| LegacyBelongsToField
920
920
| LegacyHasManyField ;
921
921
922
- export type ResourceSchema = {
922
+ export type ObjectFieldSchema =
923
+ | GenericField
924
+ | AliasField
925
+ | LocalField
926
+ | ObjectField
927
+ | SchemaObjectField
928
+ | ArrayField
929
+ | SchemaArrayField
930
+ | DerivedField ;
931
+
932
+ /**
933
+ * Represents a schema for a primary resource.
934
+ *
935
+ * Primary resources are objects with a unique identity of their
936
+ * own which may allow them to appear in relationships, or is multiple
937
+ * response document.
938
+ *
939
+ * @typedoc
940
+ */
941
+ export interface ResourceSchema {
923
942
legacy ?: boolean ;
943
+
924
944
/**
925
945
* For primary resources, this should be an IdentityField
926
946
*
927
947
* for schema-objects, this should be either a HashField or null
928
948
*
929
949
* @typedoc
930
950
*/
931
- identity : IdentityField | HashField | null ;
951
+ identity : IdentityField ;
952
+
932
953
/**
933
954
* The name of the schema
934
955
*
935
956
* For cacheable resources, this should be the
936
957
* primary resource type.
937
958
*
938
959
* For object schemas, this should be the name
939
- * of the object schema. object schemas should
940
- * follow the following guidelines for naming
960
+ * of the object schema.
961
+ *
962
+ * The names of object and resource schemas share
963
+ * a single namespace and must not conflict.
964
+ *
965
+ * We recommend a naming convention for object schemas
966
+ * such as below for ensuring uniqueness:
941
967
*
942
968
* - for globally shared objects: The pattern `$field:${KlassName}` e.g. `$field:AddressObject`
943
969
* - for resource-specific objects: The pattern `$${ResourceKlassName}:$field:${KlassName}` e.g. `$User:$field:ReusableAddress`
@@ -946,9 +972,107 @@ export type ResourceSchema = {
946
972
* @typedoc
947
973
*/
948
974
type : string ;
949
- traits ?: string [ ] ;
975
+
976
+ /**
977
+ * The fields that make up the shape of the resource
978
+ */
950
979
fields : FieldSchema [ ] ;
951
- } ;
980
+
981
+ /**
982
+ * A list of traits that this resource implements. The fields for these
983
+ * traits should still be defined in the fields array.
984
+ *
985
+ * Each trait should be a string that matches the `type` of another
986
+ * resource schema. The trait can be abstract and reference a resource
987
+ * type that is never defined as a schema.
988
+ *
989
+ * @typedoc
990
+ */
991
+ traits ?: string [ ] ;
992
+ }
993
+
994
+ /**
995
+ * Represents a schema for an object that is not
996
+ * a primary resource (has no unique identity of its own).
997
+ *
998
+ * ObjectSchemas may not currently contain relationships.
999
+ *
1000
+ * @typedoc
1001
+ */
1002
+ export interface ObjectSchema {
1003
+ /**
1004
+ * Either a HashField from which to calculate an identity or null
1005
+ *
1006
+ * In the case of `null`, the object's identity will be based
1007
+ * on the referential identity of the object in the cache itself
1008
+ * when an identity is needed.
1009
+ *
1010
+ * @typedoc
1011
+ */
1012
+ identity : HashField | null ;
1013
+
1014
+ /**
1015
+ * The name of the schema
1016
+ *
1017
+ * The names of object and resource schemas share
1018
+ * a single namespace and must not conflict.
1019
+ *
1020
+ * We recommend a naming convention for object schemas
1021
+ * such as below for ensuring uniqueness:
1022
+ *
1023
+ * - for globally shared objects: The pattern `$field:${KlassName}` e.g. `$field:AddressObject`
1024
+ * - for resource-specific objects: The pattern `$${ResourceKlassName}:$field:${KlassName}` e.g. `$User:$field:ReusableAddress`
1025
+ * - for inline objects: The pattern `$${ResourceKlassName}.${fieldPath}:$field:anonymous` e.g. `$User.shippingAddress:$field:anonymous`
1026
+ *
1027
+ * @typedoc
1028
+ */
1029
+ type : string ;
1030
+
1031
+ /**
1032
+ * The fields that make up the shape of the object
1033
+ */
1034
+ fields : ObjectFieldSchema [ ] ;
1035
+ }
1036
+
1037
+ /**
1038
+ * A no-op type utility that enables type-checking resource schema
1039
+ * definitions.
1040
+ *
1041
+ * Will return the passed in schema.
1042
+ *
1043
+ * This will not validate relationship inverses or related types,
1044
+ * as doing so would require a full schema graph to be passed in
1045
+ * and no cycles in the graph to be present.
1046
+ *
1047
+ * @param {ResourceSchema } schema
1048
+ * @returns {ResourceSchema } the passed in schema
1049
+ */
1050
+ export function resourceSchema ( schema : ResourceSchema ) : ResourceSchema {
1051
+ return schema ;
1052
+ }
1053
+
1054
+ /**
1055
+ * A no-op type utility that enables type-checking object schema
1056
+ * definitions.
1057
+ *
1058
+ * Will return the passed in schema.
1059
+ *
1060
+ * @param {ObjectSchema } schema
1061
+ * @returns {ObjectSchema } the passed in schema
1062
+ */
1063
+ export function objectSchema ( schema : ObjectSchema ) : ObjectSchema {
1064
+ return schema ;
1065
+ }
1066
+
1067
+ /**
1068
+ * A type utility to narrow a schema to a ResourceSchema
1069
+ *
1070
+ * @param schema
1071
+ * @returns {boolean }
1072
+ */
1073
+ export function isResourceSchema ( schema : ResourceSchema | ObjectSchema ) : schema is ResourceSchema {
1074
+ return schema ?. identity ?. kind === '@id' ;
1075
+ }
952
1076
953
1077
export type LegacyFieldSchema = LegacyAttributeField | LegacyBelongsToField | LegacyHasManyField ;
954
1078
export type LegacyRelationshipSchema = LegacyBelongsToField | LegacyHasManyField ;
0 commit comments