@coasys/ad4m 0.8.0-fix

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/README.md +229 -0
  2. package/jest-setup.ts +1 -0
  3. package/jest.config.js +6 -0
  4. package/jsdoc.json +28 -0
  5. package/lib/index.cjs +8165 -0
  6. package/lib/index.js +8130 -0
  7. package/lib/shims/reflect-metadata.d.ts +0 -0
  8. package/lib/shims/reflect-metadata.js +0 -0
  9. package/lib/shims/type-graphql.d.ts +32 -0
  10. package/lib/shims/type-graphql.js +70 -0
  11. package/lib/src/Ad4mClient.d.ts +26 -0
  12. package/lib/src/Ad4mClient.js +66 -0
  13. package/lib/src/Address.d.ts +1 -0
  14. package/lib/src/Address.js +1 -0
  15. package/lib/src/DID.d.ts +1 -0
  16. package/lib/src/DID.js +1 -0
  17. package/lib/src/Exception.d.ts +6 -0
  18. package/lib/src/Exception.js +7 -0
  19. package/lib/src/Literal.d.ts +7 -0
  20. package/lib/src/Literal.js +76 -0
  21. package/lib/src/PubSub.d.ts +14 -0
  22. package/lib/src/PubSub.js +14 -0
  23. package/lib/src/SmartLiteral.d.ts +12 -0
  24. package/lib/src/SmartLiteral.js +74 -0
  25. package/lib/src/agent/Agent.d.ts +104 -0
  26. package/lib/src/agent/Agent.js +337 -0
  27. package/lib/src/agent/AgentClient.d.ts +57 -0
  28. package/lib/src/agent/AgentClient.js +431 -0
  29. package/lib/src/agent/AgentResolver.d.ts +30 -0
  30. package/lib/src/agent/AgentResolver.js +325 -0
  31. package/lib/src/agent/AgentStatus.d.ts +8 -0
  32. package/lib/src/agent/AgentStatus.js +61 -0
  33. package/lib/src/buildSchema.d.ts +1 -0
  34. package/lib/src/buildSchema.js +36 -0
  35. package/lib/src/expression/Expression.d.ts +28 -0
  36. package/lib/src/expression/Expression.js +143 -0
  37. package/lib/src/expression/ExpressionClient.d.ts +13 -0
  38. package/lib/src/expression/ExpressionClient.js +102 -0
  39. package/lib/src/expression/ExpressionRef.d.ts +9 -0
  40. package/lib/src/expression/ExpressionRef.js +67 -0
  41. package/lib/src/expression/ExpressionResolver.d.ts +10 -0
  42. package/lib/src/expression/ExpressionResolver.js +106 -0
  43. package/lib/src/index.d.ts +30 -0
  44. package/lib/src/index.js +30 -0
  45. package/lib/src/jestSetup.d.ts +1 -0
  46. package/lib/src/jestSetup.js +1 -0
  47. package/lib/src/language/Icon.d.ts +4 -0
  48. package/lib/src/language/Icon.js +24 -0
  49. package/lib/src/language/Language.d.ts +195 -0
  50. package/lib/src/language/Language.js +79 -0
  51. package/lib/src/language/LanguageClient.d.ts +17 -0
  52. package/lib/src/language/LanguageClient.js +137 -0
  53. package/lib/src/language/LanguageContext.d.ts +37 -0
  54. package/lib/src/language/LanguageContext.js +2 -0
  55. package/lib/src/language/LanguageHandle.d.ts +9 -0
  56. package/lib/src/language/LanguageHandle.js +41 -0
  57. package/lib/src/language/LanguageMeta.d.ts +35 -0
  58. package/lib/src/language/LanguageMeta.js +89 -0
  59. package/lib/src/language/LanguageRef.d.ts +6 -0
  60. package/lib/src/language/LanguageRef.js +30 -0
  61. package/lib/src/language/LanguageResolver.d.ts +19 -0
  62. package/lib/src/language/LanguageResolver.js +143 -0
  63. package/lib/src/links/Links.d.ts +39 -0
  64. package/lib/src/links/Links.js +148 -0
  65. package/lib/src/links/Links.test.d.ts +1 -0
  66. package/lib/src/links/Links.test.js +33 -0
  67. package/lib/src/neighbourhood/Neighbourhood.d.ts +10 -0
  68. package/lib/src/neighbourhood/Neighbourhood.js +38 -0
  69. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +25 -0
  70. package/lib/src/neighbourhood/NeighbourhoodClient.js +256 -0
  71. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -0
  72. package/lib/src/neighbourhood/NeighbourhoodProxy.js +54 -0
  73. package/lib/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -0
  74. package/lib/src/neighbourhood/NeighbourhoodResolver.js +180 -0
  75. package/lib/src/perspectives/LinkQuery.d.ts +11 -0
  76. package/lib/src/perspectives/LinkQuery.js +80 -0
  77. package/lib/src/perspectives/Perspective.d.ts +38 -0
  78. package/lib/src/perspectives/Perspective.js +144 -0
  79. package/lib/src/perspectives/Perspective.test.d.ts +1 -0
  80. package/lib/src/perspectives/Perspective.test.js +20 -0
  81. package/lib/src/perspectives/PerspectiveClient.d.ts +54 -0
  82. package/lib/src/perspectives/PerspectiveClient.js +410 -0
  83. package/lib/src/perspectives/PerspectiveDiff.d.ts +9 -0
  84. package/lib/src/perspectives/PerspectiveDiff.js +33 -0
  85. package/lib/src/perspectives/PerspectiveHandle.d.ts +16 -0
  86. package/lib/src/perspectives/PerspectiveHandle.js +59 -0
  87. package/lib/src/perspectives/PerspectiveProxy.d.ts +178 -0
  88. package/lib/src/perspectives/PerspectiveProxy.js +540 -0
  89. package/lib/src/perspectives/PerspectiveResolver.d.ts +37 -0
  90. package/lib/src/perspectives/PerspectiveResolver.js +362 -0
  91. package/lib/src/runtime/RuntimeClient.d.ts +33 -0
  92. package/lib/src/runtime/RuntimeClient.js +263 -0
  93. package/lib/src/runtime/RuntimeResolver.d.ts +47 -0
  94. package/lib/src/runtime/RuntimeResolver.js +328 -0
  95. package/lib/src/schema.gql +382 -0
  96. package/lib/src/subject/SDNADecorators.d.ts +45 -0
  97. package/lib/src/subject/SDNADecorators.js +250 -0
  98. package/lib/src/subject/Subject.d.ts +7 -0
  99. package/lib/src/subject/Subject.js +160 -0
  100. package/lib/src/subject/SubjectEntity.d.ts +31 -0
  101. package/lib/src/subject/SubjectEntity.js +250 -0
  102. package/lib/src/subject/util.d.ts +12 -0
  103. package/lib/src/subject/util.js +73 -0
  104. package/lib/src/typeDefs.d.ts +1 -0
  105. package/lib/src/typeDefs.js +379 -0
  106. package/lib/src/unwrapApolloResult.d.ts +2 -0
  107. package/lib/src/unwrapApolloResult.js +12 -0
  108. package/lib/src/utils.d.ts +2 -0
  109. package/lib/src/utils.js +21 -0
  110. package/package.json +61 -0
  111. package/rollup.config.js +27 -0
  112. package/shims/reflect-metadata.ts +0 -0
  113. package/shims/type-graphql.ts +75 -0
  114. package/src/Ad4mClient.test.ts +1085 -0
  115. package/src/Ad4mClient.ts +63 -0
  116. package/src/Address.ts +1 -0
  117. package/src/DID.ts +1 -0
  118. package/src/Exception.ts +6 -0
  119. package/src/Literal.test.ts +31 -0
  120. package/src/Literal.ts +81 -0
  121. package/src/PubSub.ts +14 -0
  122. package/src/SmartLiteral.ts +75 -0
  123. package/src/agent/Agent.ts +293 -0
  124. package/src/agent/AgentClient.ts +531 -0
  125. package/src/agent/AgentResolver.ts +263 -0
  126. package/src/agent/AgentStatus.ts +44 -0
  127. package/src/buildSchema.ts +41 -0
  128. package/src/expression/Expression.ts +99 -0
  129. package/src/expression/ExpressionClient.ts +99 -0
  130. package/src/expression/ExpressionRef.ts +63 -0
  131. package/src/expression/ExpressionResolver.ts +64 -0
  132. package/src/index.ts +30 -0
  133. package/src/jestSetup.ts +1 -0
  134. package/src/language/Icon.ts +11 -0
  135. package/src/language/Language.ts +266 -0
  136. package/src/language/LanguageClient.ts +160 -0
  137. package/src/language/LanguageContext.ts +37 -0
  138. package/src/language/LanguageHandle.ts +23 -0
  139. package/src/language/LanguageMeta.ts +70 -0
  140. package/src/language/LanguageRef.ts +18 -0
  141. package/src/language/LanguageResolver.ts +98 -0
  142. package/src/links/Links.test.ts +40 -0
  143. package/src/links/Links.ts +107 -0
  144. package/src/neighbourhood/Neighbourhood.ts +22 -0
  145. package/src/neighbourhood/NeighbourhoodClient.ts +276 -0
  146. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -0
  147. package/src/neighbourhood/NeighbourhoodResolver.ts +100 -0
  148. package/src/perspectives/LinkQuery.ts +66 -0
  149. package/src/perspectives/Perspective.test.ts +23 -0
  150. package/src/perspectives/Perspective.ts +118 -0
  151. package/src/perspectives/PerspectiveClient.ts +444 -0
  152. package/src/perspectives/PerspectiveDiff.ts +15 -0
  153. package/src/perspectives/PerspectiveHandle.ts +39 -0
  154. package/src/perspectives/PerspectiveProxy.ts +611 -0
  155. package/src/perspectives/PerspectiveResolver.ts +220 -0
  156. package/src/runtime/RuntimeClient.ts +283 -0
  157. package/src/runtime/RuntimeResolver.ts +193 -0
  158. package/src/subject/SDNADecorators.ts +334 -0
  159. package/src/subject/Subject.ts +153 -0
  160. package/src/subject/SubjectEntity.ts +292 -0
  161. package/src/subject/util.ts +85 -0
  162. package/src/typeDefs.ts +6 -0
  163. package/src/unwrapApolloResult.ts +14 -0
  164. package/src/utils.ts +25 -0
  165. package/tsconfig-browser.json +82 -0
  166. package/tsconfig.json +80 -0
  167. package/yarn-error.log +22814 -0
@@ -0,0 +1,118 @@
1
+ import { Field, ObjectType, InputType } from "type-graphql";
2
+ import { ExpressionGeneric } from "../expression/Expression";
3
+ import { Link, LinkExpression, LinkExpressionInput, LinkInput } from "../links/Links";
4
+ import { LinkQuery } from "./LinkQuery";
5
+
6
+ /** A Perspective represents subjective meaning, encoded through
7
+ * associations between expressions, a.k.a. Links, that is a graph
8
+ * over the objective Expressions of any subset of Languages.
9
+ *
10
+ * This type represents the clean onotological concept of a Perspective.
11
+ * An instance of this class can be regarded as an immutable snapshot of
12
+ * a mutable perspective.
13
+ *
14
+ * The types PerspectiveProxy and PerspectiveHandle are used when dealing
15
+ * with an instantiated mutable perspective as is done through most of
16
+ * the GraphQL mutations.
17
+ */
18
+ @ObjectType()
19
+ export class Perspective {
20
+ /** The content of the perspective, a list/graph of links */
21
+ @Field(type => [LinkExpression])
22
+ links: LinkExpression[]
23
+
24
+ constructor(links?: LinkExpression[]) {
25
+ if(links) {
26
+ this.links = links
27
+ } else {
28
+ this.links = []
29
+ }
30
+ }
31
+
32
+ /** Convenience function for filtering links just like with PerspectiveProxy */
33
+ get(query: LinkQuery): LinkExpression[] {
34
+ if(!query || !query.source && !query.predicate && !query.target) {
35
+ return this.links
36
+ }
37
+
38
+ if(query.source) {
39
+ let result = JSON.parse(JSON.stringify(this.links))
40
+ // @ts-ignore
41
+ if(query.target) result = result.filter(l => l.data.target === query.target)
42
+ // @ts-ignore
43
+ if(query.predicate) result = result.filter(l => l.data.predicate === query.predicate)
44
+ //@ts-ignore
45
+ if (query.fromDate) result = result.filter(l => new Date(l.timestamp) >= query.fromDate!)
46
+ //@ts-ignore
47
+ if (query.untilDate) result = result.filter(l => new Date(l.timestamp) <= query.untilDate!)
48
+ // console.debug("result", result)
49
+ if (query.limit) result = result.slice(0, query.limit);
50
+ return result
51
+ }
52
+
53
+ // console.debug("getLinks 3")
54
+
55
+ if(query.target) {
56
+ //@ts-ignore
57
+ let result = JSON.parse(JSON.stringify(this.links))
58
+ // @ts-ignore
59
+ if(query.predicate) result = result.filter(l => l.data.predicate === query.predicate)
60
+ //@ts-ignore
61
+ if (query.fromDate) result = result.filter(l => new Date(l.timestamp) >= query.fromDate!)
62
+ //@ts-ignore
63
+ if (query.untilDate) result = result.filter(l => new Date(l.timestamp) <= query.untilDate!)
64
+ if (query.limit) result = result.slice(0, query.limit);
65
+ return result
66
+ }
67
+
68
+ // console.debug("getLinks 4")
69
+
70
+ //@ts-ignore
71
+ let result = JSON.parse(JSON.stringify(this.links))
72
+ result = result.filter(link => link.data.predicate === query.predicate)
73
+ if (query.limit) result = result.slice(0, query.limit)
74
+ return result
75
+ }
76
+
77
+ /** Convenience function to get the target of the first link that matches the given query
78
+ * This makes sense when the query is expected to return only one link
79
+ * and the target of that link is what you are looking for.
80
+ */
81
+ getSingleTarget(query: LinkQuery): string|void {
82
+ delete query.target
83
+ const foundLinks = this.get(query)
84
+ if(foundLinks.length)
85
+ return foundLinks[0].data.target
86
+ else
87
+ return null
88
+ }
89
+
90
+ }
91
+
92
+ @InputType()
93
+ export class PerspectiveInput {
94
+ @Field(type => [LinkExpressionInput])
95
+ links: LinkExpressionInput[]
96
+ }
97
+
98
+ @InputType()
99
+ export class PerspectiveUnsignedInput {
100
+ @Field(type => [LinkInput])
101
+ links: LinkInput[]
102
+
103
+ constructor(links?: LinkInput[]) {
104
+ if(links)
105
+ this.links = links
106
+ else
107
+ this.links = []
108
+ }
109
+
110
+ static fromLink(link: Link): PerspectiveUnsignedInput {
111
+ let obj = new PerspectiveUnsignedInput()
112
+ obj.links.push(link)
113
+ return obj
114
+ }
115
+ }
116
+
117
+ @ObjectType()
118
+ export class PerspectiveExpression extends ExpressionGeneric(Perspective) {};
@@ -0,0 +1,444 @@
1
+ import { ApolloClient, gql } from "@apollo/client/core";
2
+ import { ExpressionRendered } from "../expression/Expression";
3
+ import { ExpressionClient } from "../expression/ExpressionClient";
4
+ import { Link, LinkExpressionInput, LinkExpression, LinkInput, LinkMutations, LinkExpressionMutations } from "../links/Links";
5
+ import { NeighbourhoodClient } from "../neighbourhood/NeighbourhoodClient";
6
+ import { NeighbourhoodProxy } from "../neighbourhood/NeighbourhoodProxy";
7
+ import unwrapApolloResult from "../unwrapApolloResult";
8
+ import { LinkQuery } from "./LinkQuery";
9
+ import { Perspective } from "./Perspective";
10
+ import { PerspectiveHandle, PerspectiveState } from "./PerspectiveHandle";
11
+ import { LinkStatus, PerspectiveProxy } from './PerspectiveProxy';
12
+
13
+ const LINK_EXPRESSION_FIELDS = `
14
+ author
15
+ timestamp
16
+ status
17
+ data { source, predicate, target }
18
+ proof { valid, invalid, signature, key }
19
+ `
20
+
21
+ const PERSPECTIVE_HANDLE_FIELDS = `
22
+ uuid
23
+ name
24
+ sharedUrl
25
+ state
26
+ neighbourhood {
27
+ data {
28
+ linkLanguage
29
+ meta {
30
+ links
31
+ {
32
+ author
33
+ timestamp
34
+ data { source, predicate, target }
35
+ proof { valid, invalid, signature, key }
36
+ }
37
+ }
38
+ }
39
+ author
40
+ }
41
+ `
42
+
43
+ export type PerspectiveHandleCallback = (perspective: PerspectiveHandle) => null
44
+ export type UuidCallback = (uuid: string) => null
45
+ export type LinkCallback = (link: LinkExpression) => null
46
+ export type SyncStateChangeCallback = (state: PerspectiveState) => null
47
+
48
+ export class PerspectiveClient {
49
+ #apolloClient: ApolloClient<any>
50
+ #perspectiveAddedCallbacks: PerspectiveHandleCallback[]
51
+ #perspectiveUpdatedCallbacks: PerspectiveHandleCallback[]
52
+ #perspectiveRemovedCallbacks: UuidCallback[]
53
+ #perspectiveSyncStateChangeCallbacks: SyncStateChangeCallback[]
54
+ #expressionClient?: ExpressionClient
55
+ #neighbourhoodClient?: NeighbourhoodClient
56
+
57
+ constructor(client: ApolloClient<any>, subscribe: boolean = true) {
58
+ this.#apolloClient = client
59
+ this.#perspectiveAddedCallbacks = []
60
+ this.#perspectiveUpdatedCallbacks = []
61
+ this.#perspectiveRemovedCallbacks = []
62
+ this.#perspectiveSyncStateChangeCallbacks = []
63
+
64
+ if(subscribe) {
65
+ this.subscribePerspectiveAdded()
66
+ this.subscribePerspectiveUpdated()
67
+ this.subscribePerspectiveRemoved()
68
+ }
69
+ }
70
+
71
+ setExpressionClient(client: ExpressionClient) {
72
+ this.#expressionClient = client
73
+ }
74
+
75
+ setNeighbourhoodClient(client: NeighbourhoodClient) {
76
+ this.#neighbourhoodClient = client
77
+ }
78
+
79
+ async all(): Promise<PerspectiveProxy[]> {
80
+ const { perspectives } = unwrapApolloResult(await this.#apolloClient.query({
81
+ query: gql`query perspectives {
82
+ perspectives {
83
+ ${PERSPECTIVE_HANDLE_FIELDS}
84
+ }
85
+ }`
86
+ }))
87
+ return perspectives.map(handle => new PerspectiveProxy(handle, this))
88
+ }
89
+
90
+ async byUUID(uuid: string): Promise<PerspectiveProxy|null> {
91
+ const { perspective } = unwrapApolloResult(await this.#apolloClient.query({
92
+ query: gql`query perspective($uuid: String!) {
93
+ perspective(uuid: $uuid) {
94
+ ${PERSPECTIVE_HANDLE_FIELDS}
95
+ }
96
+ }`,
97
+ variables: { uuid }
98
+ }))
99
+ if(!perspective) return null
100
+ return new PerspectiveProxy(perspective, this)
101
+ }
102
+
103
+ async snapshotByUUID(uuid: string): Promise<Perspective|null> {
104
+ const { perspectiveSnapshot } = unwrapApolloResult(await this.#apolloClient.query({
105
+ query: gql`query perspectiveSnapshot($uuid: String!) {
106
+ perspectiveSnapshot(uuid: $uuid) {
107
+ links { ${LINK_EXPRESSION_FIELDS} }
108
+ }
109
+ }`,
110
+ variables: { uuid }
111
+ }))
112
+ return perspectiveSnapshot
113
+ }
114
+ async publishSnapshotByUUID(uuid: string): Promise<string|null> {
115
+ const { perspectivePublishSnapshot } = unwrapApolloResult(await this.#apolloClient.mutate({
116
+ mutation: gql`mutation perspectivePublishSnapshot($uuid: String!) {
117
+ perspectivePublishSnapshot(uuid: $uuid)
118
+ }`,
119
+ variables: { uuid }
120
+ }))
121
+ return perspectivePublishSnapshot
122
+ }
123
+
124
+ async queryLinks(uuid: string, query: LinkQuery): Promise<LinkExpression[]> {
125
+ const { perspectiveQueryLinks } = unwrapApolloResult(await this.#apolloClient.query({
126
+ query: gql`query perspectiveQueryLinks($uuid: String!, $query: LinkQuery!) {
127
+ perspectiveQueryLinks(query: $query, uuid: $uuid) {
128
+ ${LINK_EXPRESSION_FIELDS}
129
+ }
130
+ }`,
131
+ variables: { uuid, query }
132
+ }))
133
+ return perspectiveQueryLinks
134
+ }
135
+
136
+ async queryProlog(uuid: string, query: string): Promise<any> {
137
+ const { perspectiveQueryProlog } = unwrapApolloResult(await this.#apolloClient.query({
138
+ query: gql`query perspectiveQueryProlog($uuid: String!, $query: String!) {
139
+ perspectiveQueryProlog(uuid: $uuid, query: $query)
140
+ }`,
141
+ variables: { uuid, query }
142
+ }))
143
+
144
+ return JSON.parse(perspectiveQueryProlog)
145
+ }
146
+
147
+ async add(name: string): Promise<PerspectiveProxy> {
148
+ const { perspectiveAdd } = unwrapApolloResult(await this.#apolloClient.mutate({
149
+ mutation: gql`mutation perspectiveAdd($name: String!) {
150
+ perspectiveAdd(name: $name) {
151
+ ${PERSPECTIVE_HANDLE_FIELDS}
152
+ }
153
+ }`,
154
+ variables: { name }
155
+ }))
156
+ return new PerspectiveProxy(perspectiveAdd, this)
157
+ }
158
+
159
+ async update(uuid: string, name: string): Promise<PerspectiveProxy> {
160
+ const { perspectiveUpdate } = unwrapApolloResult(await this.#apolloClient.mutate({
161
+ mutation: gql`mutation perspectiveUpdate($uuid: String!, $name: String!) {
162
+ perspectiveUpdate(uuid: $uuid, name: $name) {
163
+ ${PERSPECTIVE_HANDLE_FIELDS}
164
+ }
165
+ }`,
166
+ variables: { uuid, name }
167
+ }))
168
+ return new PerspectiveProxy(perspectiveUpdate, this)
169
+ }
170
+
171
+ async remove(uuid: string): Promise<{perspectiveRemove: boolean}> {
172
+ return unwrapApolloResult(await this.#apolloClient.mutate({
173
+ mutation: gql`mutation perspectiveRemove($uuid: String!) {
174
+ perspectiveRemove(uuid: $uuid)
175
+ }`,
176
+ variables: { uuid }
177
+ }))
178
+ }
179
+
180
+ async addLink(uuid: string, link: Link, status?: LinkStatus): Promise<LinkExpression> {
181
+ const { perspectiveAddLink } = unwrapApolloResult(await this.#apolloClient.mutate({
182
+ mutation: gql`mutation perspectiveAddLink($uuid: String!, $link: LinkInput!, $status: String){
183
+ perspectiveAddLink(link: $link, uuid: $uuid, status: $status) {
184
+ ${LINK_EXPRESSION_FIELDS}
185
+ }
186
+ }`,
187
+ variables: { uuid, link, status }
188
+ }))
189
+ return perspectiveAddLink
190
+ }
191
+
192
+ async addLinks(uuid: string, links: Link[], status?: LinkStatus): Promise<LinkExpression[]> {
193
+ const { perspectiveAddLinks } = unwrapApolloResult(await this.#apolloClient.mutate({
194
+ mutation: gql`mutation perspectiveAddLinks($uuid: String!, $links: [LinkInput!]!, $status: String){
195
+ perspectiveAddLinks(links: $links, uuid: $uuid, status: $status) {
196
+ ${LINK_EXPRESSION_FIELDS}
197
+ }
198
+ }`,
199
+ variables: { uuid, links, status }
200
+ }))
201
+ return perspectiveAddLinks
202
+ }
203
+
204
+ async removeLinks(uuid: string, links: LinkExpressionInput[]): Promise<LinkExpression[]> {
205
+ const { perspectiveRemoveLinks } = unwrapApolloResult(await this.#apolloClient.mutate({
206
+ mutation: gql`mutation perspectiveRemoveLinks($uuid: String!, $links: [LinkExpressionInput!]!){
207
+ perspectiveRemoveLinks(links: $links, uuid: $uuid) {
208
+ ${LINK_EXPRESSION_FIELDS}
209
+ }
210
+ }`,
211
+ variables: { uuid, links }
212
+ }))
213
+ return perspectiveRemoveLinks
214
+ }
215
+
216
+ async linkMutations(uuid: string, mutations: LinkMutations, status?: LinkStatus): Promise<LinkExpressionMutations> {
217
+ const { perspectiveLinkMutations } = unwrapApolloResult(await this.#apolloClient.mutate({
218
+ mutation: gql`mutation perspectiveLinkMutations($uuid: String!, $mutations: LinkMutations!, $status: String){
219
+ perspectiveLinkMutations(mutations: $mutations, uuid: $uuid, status: $status) {
220
+ additions {
221
+ ${LINK_EXPRESSION_FIELDS}
222
+ }
223
+ removals {
224
+ ${LINK_EXPRESSION_FIELDS}
225
+ }
226
+ }
227
+ }`,
228
+ variables: { uuid, mutations, status }
229
+ }))
230
+ return perspectiveLinkMutations
231
+ }
232
+
233
+ async addLinkExpression(uuid: string, link: LinkExpressionInput, status?: LinkStatus): Promise<LinkExpression> {
234
+ const { perspectiveAddLinkExpression } = unwrapApolloResult(await this.#apolloClient.mutate({
235
+ mutation: gql`mutation perspectiveAddLinkExpression($uuid: String!, $link: LinkExpressionInput!, $status: String){
236
+ perspectiveAddLinkExpression(link: $link, uuid: $uuid, status: $status) {
237
+ ${LINK_EXPRESSION_FIELDS}
238
+ }
239
+ }`,
240
+ variables: { uuid, link }
241
+ }))
242
+ return perspectiveAddLinkExpression
243
+ }
244
+
245
+ async updateLink(uuid: string, oldLink: LinkExpressionInput, newLink: LinkInput): Promise<LinkExpression> {
246
+ delete oldLink.__typename
247
+ delete oldLink.data.__typename
248
+ delete oldLink.proof.__typename
249
+ const { perspectiveUpdateLink } = unwrapApolloResult(await this.#apolloClient.mutate({
250
+ mutation: gql`mutation perspectiveUpdateLink(
251
+ $uuid: String!,
252
+ $newLink: LinkInput!
253
+ $oldLink: LinkExpressionInput!
254
+ ){
255
+ perspectiveUpdateLink(
256
+ newLink: $newLink,
257
+ oldLink: $oldLink,
258
+ uuid: $uuid
259
+ ) {
260
+ ${LINK_EXPRESSION_FIELDS}
261
+ }
262
+ }`,
263
+ variables: { uuid, oldLink, newLink }
264
+ }))
265
+
266
+ if (!perspectiveUpdateLink.status) {
267
+ delete perspectiveUpdateLink.status
268
+ }
269
+
270
+ return perspectiveUpdateLink
271
+ }
272
+
273
+ async removeLink(uuid: string, link: LinkExpressionInput): Promise<{perspectiveRemoveLink: boolean}> {
274
+ delete link.__typename
275
+ delete link.data.__typename
276
+ delete link.proof.__typename
277
+ delete link.status
278
+ return unwrapApolloResult(await this.#apolloClient.mutate({
279
+ mutation: gql`mutation perspectiveRemoveLink($link: LinkExpressionInput!, $uuid: String!) {
280
+ perspectiveRemoveLink(link: $link, uuid: $uuid)
281
+ }`,
282
+ variables: { uuid, link }
283
+ }))
284
+ }
285
+
286
+ // ExpressionClient functions, needed for Subjects:
287
+ async getExpression(expressionURI: string): Promise<ExpressionRendered> {
288
+ return await this.#expressionClient.get(expressionURI)
289
+ }
290
+
291
+ async createExpression(content: any, languageAddress: string): Promise<string> {
292
+ return await this.#expressionClient.create(content, languageAddress)
293
+ }
294
+
295
+ // Subscriptions:
296
+ addPerspectiveAddedListener(cb: PerspectiveHandleCallback) {
297
+ this.#perspectiveAddedCallbacks.push(cb)
298
+ }
299
+
300
+ subscribePerspectiveAdded() {
301
+ this.#apolloClient.subscribe({
302
+ query: gql` subscription {
303
+ perspectiveAdded { ${PERSPECTIVE_HANDLE_FIELDS} }
304
+ }
305
+ `}).subscribe({
306
+ next: result => {
307
+ this.#perspectiveAddedCallbacks.forEach(cb => {
308
+ cb(result.data.perspectiveAdded)
309
+ })
310
+ },
311
+ error: (e) => console.error(e)
312
+ })
313
+ }
314
+
315
+ addPerspectiveUpdatedListener(cb: PerspectiveHandleCallback) {
316
+ this.#perspectiveUpdatedCallbacks.push(cb)
317
+ }
318
+
319
+ subscribePerspectiveUpdated() {
320
+ this.#apolloClient.subscribe({
321
+ query: gql` subscription {
322
+ perspectiveUpdated { ${PERSPECTIVE_HANDLE_FIELDS} }
323
+ }
324
+ `}).subscribe({
325
+ next: result => {
326
+ this.#perspectiveUpdatedCallbacks.forEach(cb => {
327
+ cb(result.data.perspectiveUpdated)
328
+ })
329
+ },
330
+ error: (e) => console.error(e)
331
+ })
332
+ }
333
+
334
+ addPerspectiveSyncedListener(cb: SyncStateChangeCallback) {
335
+ this.#perspectiveSyncStateChangeCallbacks.push(cb)
336
+ }
337
+
338
+ async addPerspectiveSyncStateChangeListener(uuid: String, cb: SyncStateChangeCallback[]): Promise<void> {
339
+ this.#apolloClient.subscribe({
340
+ query: gql` subscription {
341
+ perspectiveSyncStateChange(uuid: "${uuid}")
342
+ }
343
+ `}).subscribe({
344
+ next: result => {
345
+ cb.forEach(c => {
346
+ c(result.data.perspectiveSyncStateChange)
347
+ })
348
+ },
349
+ error: (e) => console.error(e)
350
+ })
351
+
352
+ await new Promise<void>(resolve => setTimeout(resolve, 500))
353
+ }
354
+
355
+ addPerspectiveRemovedListener(cb: UuidCallback) {
356
+ this.#perspectiveRemovedCallbacks.push(cb)
357
+ }
358
+
359
+ subscribePerspectiveRemoved() {
360
+ this.#apolloClient.subscribe({
361
+ query: gql` subscription {
362
+ perspectiveRemoved
363
+ }
364
+ `}).subscribe({
365
+ next: result => {
366
+ this.#perspectiveRemovedCallbacks.forEach(cb => {
367
+ cb(result.data.perspectiveRemoved)
368
+ })
369
+ },
370
+ error: (e) => console.error(e)
371
+ })
372
+ }
373
+
374
+ async addPerspectiveLinkAddedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
375
+ this.#apolloClient.subscribe({
376
+ query: gql` subscription {
377
+ perspectiveLinkAdded(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
378
+ }
379
+ `}).subscribe({
380
+ next: result => {
381
+ cb.forEach(c => {
382
+ c(result.data.perspectiveLinkAdded)
383
+ })
384
+ },
385
+ error: (e) => console.error(e)
386
+ })
387
+
388
+ await new Promise<void>(resolve => setTimeout(resolve, 500))
389
+ }
390
+
391
+ async addPerspectiveLinkRemovedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
392
+ this.#apolloClient.subscribe({
393
+ query: gql` subscription {
394
+ perspectiveLinkRemoved(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
395
+ }
396
+ `}).subscribe({
397
+ next: result => {
398
+ cb.forEach(c => {
399
+ if (!result.data.perspectiveLinkRemoved.status) {
400
+ delete result.data.perspectiveLinkRemoved.status
401
+ }
402
+ c(result.data.perspectiveLinkRemoved)
403
+ })
404
+ },
405
+ error: (e) => console.error(e)
406
+ })
407
+
408
+ await new Promise<void>(resolve => setTimeout(resolve, 500))
409
+ }
410
+
411
+ async addPerspectiveLinkUpdatedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
412
+ this.#apolloClient.subscribe({
413
+ query: gql` subscription {
414
+ perspectiveLinkUpdated(uuid: "${uuid}") {
415
+ oldLink {
416
+ ${LINK_EXPRESSION_FIELDS}
417
+ }
418
+ newLink {
419
+ ${LINK_EXPRESSION_FIELDS}
420
+ }
421
+ }
422
+ }
423
+ `}).subscribe({
424
+ next: result => {
425
+ cb.forEach(c => {
426
+ if (!result.data.perspectiveLinkUpdated.newLink.status) {
427
+ delete result.data.perspectiveLinkUpdated.newLink.status
428
+ }
429
+ if (!result.data.perspectiveLinkUpdated.oldLink.status) {
430
+ delete result.data.perspectiveLinkUpdated.oldLink.status
431
+ }
432
+ c(result.data.perspectiveLinkUpdated)
433
+ })
434
+ },
435
+ error: (e) => console.error(e)
436
+ })
437
+
438
+ await new Promise<void>(resolve => setTimeout(resolve, 500))
439
+ }
440
+
441
+ getNeighbourhoodProxy(uuid: string): NeighbourhoodProxy {
442
+ return new NeighbourhoodProxy(this.#neighbourhoodClient, uuid)
443
+ }
444
+ }
@@ -0,0 +1,15 @@
1
+ import { Field, ObjectType } from "type-graphql";
2
+ import { ExpressionGeneric } from "../expression/Expression";
3
+ import { LinkExpression } from "../links/Links";
4
+
5
+ @ObjectType()
6
+ export class PerspectiveDiff {
7
+ @Field(type => [LinkExpression])
8
+ additions: LinkExpression[]
9
+
10
+ @Field(type => [LinkExpression])
11
+ removals: LinkExpression[]
12
+ }
13
+
14
+ @ObjectType()
15
+ export class PerspectiveDiffExpression extends ExpressionGeneric(PerspectiveDiff) {};
@@ -0,0 +1,39 @@
1
+ import { Field, ObjectType } from "type-graphql";
2
+ import { NeighbourhoodExpression } from "../neighbourhood/Neighbourhood";
3
+
4
+ export enum PerspectiveState {
5
+ Private = "Private",
6
+ NeighbourhoodJoinInitiated = "NeighbourhoodJoinInitiated",
7
+ LinkLanguageFailedToInstall = "LinkLanguageFailedToInstall",
8
+ LinkLanguageInstalledButNotSynced = "LinkLanguageInstalledButNotSynced",
9
+ Synced = "Synced",
10
+ }
11
+
12
+ // This type is used in the GraphQL interface to reference a mutable
13
+ // prespective that is implemented locally by the Ad4m runtime.
14
+ // The UUID is used in mutations to identify the perspective that gets mutated.
15
+ @ObjectType()
16
+ export class PerspectiveHandle {
17
+ @Field()
18
+ uuid: string
19
+ @Field()
20
+ name: string
21
+ @Field()
22
+ state: PerspectiveState
23
+
24
+ @Field(type => String, {nullable: true})
25
+ sharedUrl?: string
26
+
27
+ @Field(type => NeighbourhoodExpression, {nullable: true})
28
+ neighbourhood?: NeighbourhoodExpression
29
+
30
+ constructor(uuid?: string, name?: string, state?: PerspectiveState) {
31
+ this.uuid = uuid
32
+ this.name = name
33
+ if (state) {
34
+ this.state = state
35
+ } else {
36
+ this.state = PerspectiveState.Private
37
+ }
38
+ }
39
+ }