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