twenty-sdk 0.6.0 → 0.6.2

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 (70) hide show
  1. package/README.md +6 -2
  2. package/dist/{HtmlTagToRemoteComponent-DzQ2I9Lz.js → HtmlTagToRemoteComponent-CgtwxDeH.js} +11 -11
  3. package/dist/{HtmlTagToRemoteComponent-DBOkvIRB.mjs → HtmlTagToRemoteComponent-D_EW5OLu.mjs} +1743 -1640
  4. package/dist/cli/utilities/api/api-service.d.ts +4 -0
  5. package/dist/cli/utilities/build/common/esbuild-watcher.d.ts +5 -2
  6. package/dist/cli/utilities/build/common/typecheck-plugin.d.ts +1 -1
  7. package/dist/cli/utilities/build/manifest/manifest-extract-config.d.ts +2 -0
  8. package/dist/cli/utilities/client/client-service.d.ts +5 -1
  9. package/dist/cli/utilities/client/twenty-client-template.d.ts +42 -0
  10. package/dist/cli/utilities/dev/orchestrator/dev-mode-orchestrator.d.ts +2 -0
  11. package/dist/cli/utilities/dev/orchestrator/steps/start-watchers-orchestrator-step.d.ts +2 -0
  12. package/dist/cli.cjs +430 -75
  13. package/dist/cli.mjs +3175 -2725
  14. package/dist/front-component-renderer/index.cjs +11 -11
  15. package/dist/front-component-renderer/index.d.ts +3 -3
  16. package/dist/front-component-renderer/index.mjs +2186 -1906
  17. package/dist/front-component-renderer/remote/generated/remote-components.d.ts +44 -0
  18. package/dist/front-component-renderer/remote/generated/remote-elements.d.ts +93 -0
  19. package/dist/front-component-renderer/types/FrontComponentHostCommunicationApi.d.ts +2 -1
  20. package/dist/{get-function-input-schema-BZ7_XyUh-GxSybvDe.js → get-function-input-schema-BZ7_XyUh-CCSi0u_q.js} +1 -1
  21. package/dist/{get-function-input-schema-BZ7_XyUh-By3UDh4s.mjs → get-function-input-schema-BZ7_XyUh-DAPandzB.mjs} +1 -1
  22. package/dist/index.cjs +2 -2
  23. package/dist/index.mjs +122 -89
  24. package/dist/sdk/application/application-config.d.ts +1 -1
  25. package/dist/sdk/front-component-api/constants/AllowedHtmlElements.d.ts +1 -0
  26. package/dist/sdk/front-component-api/constants/AllowedHtmlElements.js +104 -1
  27. package/dist/sdk/front-component-api/constants/AllowedHtmlElements.js.map +1 -1
  28. package/dist/sdk/front-component-api/constants/EventToReact.js +22 -3
  29. package/dist/sdk/front-component-api/constants/EventToReact.js.map +1 -1
  30. package/dist/sdk/front-component-api/constants/SerializedEventData.d.ts +7 -0
  31. package/dist/sdk/front-component-api/globals/frontComponentHostCommunicationApi.d.ts +2 -0
  32. package/dist/sdk/front-component-api/globals/frontComponentHostCommunicationApi.js.map +1 -1
  33. package/dist/sdk/index.d.ts +3 -0
  34. package/dist/sdk/index.js +70 -66
  35. package/dist/sdk/index.js.map +1 -1
  36. package/dist/sdk/logic-functions/define-post-install-logic-function.d.ts +6 -0
  37. package/dist/sdk/logic-functions/define-post-install-logic-function.js +12 -0
  38. package/dist/sdk/logic-functions/define-post-install-logic-function.js.map +1 -0
  39. package/dist/sdk/logic-functions/define-pre-install-logic-function.d.ts +6 -0
  40. package/dist/sdk/logic-functions/define-pre-install-logic-function.js +12 -0
  41. package/dist/sdk/logic-functions/define-pre-install-logic-function.js.map +1 -0
  42. package/dist/sdk/logic-functions/install-logic-function-payload-type.d.ts +4 -0
  43. package/generated/core/index.ts +406 -0
  44. package/generated/core/runtime/batcher.ts +265 -0
  45. package/generated/core/runtime/createClient.ts +68 -0
  46. package/generated/core/runtime/error.ts +29 -0
  47. package/generated/core/runtime/fetcher.ts +98 -0
  48. package/generated/core/runtime/generateGraphqlOperation.ts +225 -0
  49. package/generated/core/runtime/index.ts +13 -0
  50. package/generated/core/runtime/linkTypeMap.ts +139 -0
  51. package/generated/core/runtime/typeSelection.ts +98 -0
  52. package/generated/core/runtime/types.ts +69 -0
  53. package/generated/core/schema.graphql +36745 -0
  54. package/generated/core/schema.ts +39341 -0
  55. package/generated/core/types.ts +45860 -0
  56. package/generated/index.ts +2 -0
  57. package/generated/metadata/index.ts +469 -0
  58. package/generated/metadata/runtime/batcher.ts +265 -0
  59. package/generated/metadata/runtime/createClient.ts +68 -0
  60. package/generated/metadata/runtime/error.ts +29 -0
  61. package/generated/metadata/runtime/fetcher.ts +98 -0
  62. package/generated/metadata/runtime/generateGraphqlOperation.ts +225 -0
  63. package/generated/metadata/runtime/index.ts +13 -0
  64. package/generated/metadata/runtime/linkTypeMap.ts +139 -0
  65. package/generated/metadata/runtime/typeSelection.ts +98 -0
  66. package/generated/metadata/runtime/types.ts +69 -0
  67. package/generated/metadata/schema.graphql +3971 -0
  68. package/generated/metadata/schema.ts +8357 -0
  69. package/generated/metadata/types.ts +10044 -0
  70. package/package.json +17 -1
@@ -0,0 +1,265 @@
1
+ // @ts-nocheck
2
+ import type { GraphqlOperation } from './generateGraphqlOperation'
3
+ import { GenqlError } from './error'
4
+
5
+ type Variables = Record<string, any>
6
+
7
+ type QueryError = Error & {
8
+ message: string
9
+
10
+ locations?: Array<{
11
+ line: number
12
+ column: number
13
+ }>
14
+ path?: any
15
+ rid: string
16
+ details?: Record<string, any>
17
+ }
18
+ type Result = {
19
+ data: Record<string, any>
20
+ errors: Array<QueryError>
21
+ }
22
+ type Fetcher = (
23
+ batchedQuery: GraphqlOperation | Array<GraphqlOperation>,
24
+ ) => Promise<Array<Result>>
25
+ type Options = {
26
+ batchInterval?: number
27
+ shouldBatch?: boolean
28
+ maxBatchSize?: number
29
+ }
30
+ type Queue = Array<{
31
+ request: GraphqlOperation
32
+ resolve: (...args: Array<any>) => any
33
+ reject: (...args: Array<any>) => any
34
+ }>
35
+
36
+ /**
37
+ * takes a list of requests (queue) and batches them into a single server request.
38
+ * It will then resolve each individual requests promise with the appropriate data.
39
+ * @private
40
+ * @param {QueryBatcher} client - the client to use
41
+ * @param {Queue} queue - the list of requests to batch
42
+ */
43
+ function dispatchQueueBatch(client: QueryBatcher, queue: Queue): void {
44
+ let batchedQuery: any = queue.map((item) => item.request)
45
+
46
+ if (batchedQuery.length === 1) {
47
+ batchedQuery = batchedQuery[0]
48
+ }
49
+
50
+ client.fetcher(batchedQuery).then((responses: any) => {
51
+ if (queue.length === 1 && !Array.isArray(responses)) {
52
+ if (responses.errors && responses.errors.length) {
53
+ queue[0].reject(
54
+ new GenqlError(responses.errors, responses.data),
55
+ )
56
+ return
57
+ }
58
+
59
+ queue[0].resolve(responses)
60
+ return
61
+ } else if (responses.length !== queue.length) {
62
+ throw new Error('response length did not match query length')
63
+ }
64
+
65
+ for (let i = 0; i < queue.length; i++) {
66
+ if (responses[i].errors && responses[i].errors.length) {
67
+ queue[i].reject(
68
+ new GenqlError(responses[i].errors, responses[i].data),
69
+ )
70
+ } else {
71
+ queue[i].resolve(responses[i])
72
+ }
73
+ }
74
+ })
75
+ }
76
+
77
+ /**
78
+ * creates a list of requests to batch according to max batch size.
79
+ * @private
80
+ * @param {QueryBatcher} client - the client to create list of requests from from
81
+ * @param {Options} options - the options for the batch
82
+ */
83
+ function dispatchQueue(client: QueryBatcher, options: Options): void {
84
+ const queue = client._queue
85
+ const maxBatchSize = options.maxBatchSize || 0
86
+ client._queue = []
87
+
88
+ if (maxBatchSize > 0 && maxBatchSize < queue.length) {
89
+ for (let i = 0; i < queue.length / maxBatchSize; i++) {
90
+ dispatchQueueBatch(
91
+ client,
92
+ queue.slice(i * maxBatchSize, (i + 1) * maxBatchSize),
93
+ )
94
+ }
95
+ } else {
96
+ dispatchQueueBatch(client, queue)
97
+ }
98
+ }
99
+ /**
100
+ * Create a batcher client.
101
+ * @param {Fetcher} fetcher - A function that can handle the network requests to graphql endpoint
102
+ * @param {Options} options - the options to be used by client
103
+ * @param {boolean} options.shouldBatch - should the client batch requests. (default true)
104
+ * @param {integer} options.batchInterval - duration (in MS) of each batch window. (default 6)
105
+ * @param {integer} options.maxBatchSize - max number of requests in a batch. (default 0)
106
+ * @param {boolean} options.defaultHeaders - default headers to include with every request
107
+ *
108
+ * @example
109
+ * const fetcher = batchedQuery => fetch('path/to/graphql', {
110
+ * method: 'post',
111
+ * headers: {
112
+ * Accept: 'application/json',
113
+ * 'Content-Type': 'application/json',
114
+ * },
115
+ * body: JSON.stringify(batchedQuery),
116
+ * credentials: 'include',
117
+ * })
118
+ * .then(response => response.json())
119
+ *
120
+ * const client = new QueryBatcher(fetcher, { maxBatchSize: 10 })
121
+ */
122
+
123
+ export class QueryBatcher {
124
+ fetcher: Fetcher
125
+ _options: Options
126
+ _queue: Queue
127
+
128
+ constructor(
129
+ fetcher: Fetcher,
130
+ {
131
+ batchInterval = 6,
132
+ shouldBatch = true,
133
+ maxBatchSize = 0,
134
+ }: Options = {},
135
+ ) {
136
+ this.fetcher = fetcher
137
+ this._options = {
138
+ batchInterval,
139
+ shouldBatch,
140
+ maxBatchSize,
141
+ }
142
+ this._queue = []
143
+ }
144
+
145
+ /**
146
+ * Fetch will send a graphql request and return the parsed json.
147
+ * @param {string} query - the graphql query.
148
+ * @param {Variables} variables - any variables you wish to inject as key/value pairs.
149
+ * @param {[string]} operationName - the graphql operationName.
150
+ * @param {Options} overrides - the client options overrides.
151
+ *
152
+ * @return {promise} resolves to parsed json of server response
153
+ *
154
+ * @example
155
+ * client.fetch(`
156
+ * query getHuman($id: ID!) {
157
+ * human(id: $id) {
158
+ * name
159
+ * height
160
+ * }
161
+ * }
162
+ * `, { id: "1001" }, 'getHuman')
163
+ * .then(human => {
164
+ * // do something with human
165
+ * console.log(human);
166
+ * });
167
+ */
168
+ fetch(
169
+ query: string,
170
+ variables?: Variables,
171
+ operationName?: string,
172
+ overrides: Options = {},
173
+ ): Promise<Result> {
174
+ const request: GraphqlOperation = {
175
+ query,
176
+ }
177
+ const options = Object.assign({}, this._options, overrides)
178
+
179
+ if (variables) {
180
+ request.variables = variables
181
+ }
182
+
183
+ if (operationName) {
184
+ request.operationName = operationName
185
+ }
186
+
187
+ const promise = new Promise<Result>((resolve, reject) => {
188
+ this._queue.push({
189
+ request,
190
+ resolve,
191
+ reject,
192
+ })
193
+
194
+ if (this._queue.length === 1) {
195
+ if (options.shouldBatch) {
196
+ setTimeout(
197
+ () => dispatchQueue(this, options),
198
+ options.batchInterval,
199
+ )
200
+ } else {
201
+ dispatchQueue(this, options)
202
+ }
203
+ }
204
+ })
205
+ return promise
206
+ }
207
+
208
+ /**
209
+ * Fetch will send a graphql request and return the parsed json.
210
+ * @param {string} query - the graphql query.
211
+ * @param {Variables} variables - any variables you wish to inject as key/value pairs.
212
+ * @param {[string]} operationName - the graphql operationName.
213
+ * @param {Options} overrides - the client options overrides.
214
+ *
215
+ * @return {Promise<Array<Result>>} resolves to parsed json of server response
216
+ *
217
+ * @example
218
+ * client.forceFetch(`
219
+ * query getHuman($id: ID!) {
220
+ * human(id: $id) {
221
+ * name
222
+ * height
223
+ * }
224
+ * }
225
+ * `, { id: "1001" }, 'getHuman')
226
+ * .then(human => {
227
+ * // do something with human
228
+ * console.log(human);
229
+ * });
230
+ */
231
+ forceFetch(
232
+ query: string,
233
+ variables?: Variables,
234
+ operationName?: string,
235
+ overrides: Options = {},
236
+ ): Promise<Result> {
237
+ const request: GraphqlOperation = {
238
+ query,
239
+ }
240
+ const options = Object.assign({}, this._options, overrides, {
241
+ shouldBatch: false,
242
+ })
243
+
244
+ if (variables) {
245
+ request.variables = variables
246
+ }
247
+
248
+ if (operationName) {
249
+ request.operationName = operationName
250
+ }
251
+
252
+ const promise = new Promise<Result>((resolve, reject) => {
253
+ const client = new QueryBatcher(this.fetcher, this._options)
254
+ client._queue = [
255
+ {
256
+ request,
257
+ resolve,
258
+ reject,
259
+ },
260
+ ]
261
+ dispatchQueue(client, options)
262
+ })
263
+ return promise
264
+ }
265
+ }
@@ -0,0 +1,68 @@
1
+ // @ts-nocheck
2
+
3
+ import { type BatchOptions, createFetcher } from './fetcher'
4
+ import type { ExecutionResult, LinkedType } from './types'
5
+ import {
6
+ generateGraphqlOperation,
7
+ type GraphqlOperation,
8
+ } from './generateGraphqlOperation'
9
+
10
+ export type Headers =
11
+ | HeadersInit
12
+ | (() => HeadersInit)
13
+ | (() => Promise<HeadersInit>)
14
+
15
+ export type BaseFetcher = (
16
+ operation: GraphqlOperation | GraphqlOperation[],
17
+ ) => Promise<ExecutionResult | ExecutionResult[]>
18
+
19
+ export type ClientOptions = Omit<RequestInit, 'body' | 'headers'> & {
20
+ url?: string
21
+ batch?: BatchOptions | boolean
22
+ fetcher?: BaseFetcher
23
+ fetch?: Function
24
+ headers?: Headers
25
+ }
26
+
27
+ export const createClient = ({
28
+ queryRoot,
29
+ mutationRoot,
30
+ subscriptionRoot,
31
+ ...options
32
+ }: ClientOptions & {
33
+ queryRoot?: LinkedType
34
+ mutationRoot?: LinkedType
35
+ subscriptionRoot?: LinkedType
36
+ }) => {
37
+ const fetcher = createFetcher(options)
38
+ const client: {
39
+ query?: Function
40
+ mutation?: Function
41
+ } = {}
42
+
43
+ if (queryRoot) {
44
+ client.query = (request: any) => {
45
+ if (!queryRoot) throw new Error('queryRoot argument is missing')
46
+
47
+ const resultPromise = fetcher(
48
+ generateGraphqlOperation('query', queryRoot, request),
49
+ )
50
+
51
+ return resultPromise
52
+ }
53
+ }
54
+ if (mutationRoot) {
55
+ client.mutation = (request: any) => {
56
+ if (!mutationRoot)
57
+ throw new Error('mutationRoot argument is missing')
58
+
59
+ const resultPromise = fetcher(
60
+ generateGraphqlOperation('mutation', mutationRoot, request),
61
+ )
62
+
63
+ return resultPromise
64
+ }
65
+ }
66
+
67
+ return client as any
68
+ }
@@ -0,0 +1,29 @@
1
+ // @ts-nocheck
2
+ export class GenqlError extends Error {
3
+ errors: Array<GraphqlError> = []
4
+ /**
5
+ * Partial data returned by the server
6
+ */
7
+ data?: any
8
+ constructor(errors: any[], data: any) {
9
+ let message = Array.isArray(errors)
10
+ ? errors.map((x) => x?.message || '').join('\n')
11
+ : ''
12
+ if (!message) {
13
+ message = 'GraphQL error'
14
+ }
15
+ super(message)
16
+ this.errors = errors
17
+ this.data = data
18
+ }
19
+ }
20
+
21
+ interface GraphqlError {
22
+ message: string
23
+ locations?: Array<{
24
+ line: number
25
+ column: number
26
+ }>
27
+ path?: string[]
28
+ extensions?: Record<string, any>
29
+ }
@@ -0,0 +1,98 @@
1
+ // @ts-nocheck
2
+ import { QueryBatcher } from './batcher'
3
+
4
+ import type { ClientOptions } from './createClient'
5
+ import type { GraphqlOperation } from './generateGraphqlOperation'
6
+ import { GenqlError } from './error'
7
+
8
+ export interface Fetcher {
9
+ (gql: GraphqlOperation): Promise<any>
10
+ }
11
+
12
+ export type BatchOptions = {
13
+ batchInterval?: number // ms
14
+ maxBatchSize?: number
15
+ }
16
+
17
+ const DEFAULT_BATCH_OPTIONS = {
18
+ maxBatchSize: 10,
19
+ batchInterval: 40,
20
+ }
21
+
22
+ export const createFetcher = ({
23
+ url,
24
+ headers = {},
25
+ fetcher,
26
+ fetch: _fetch,
27
+ batch = false,
28
+ ...rest
29
+ }: ClientOptions): Fetcher => {
30
+ if (!url && !fetcher) {
31
+ throw new Error('url or fetcher is required')
32
+ }
33
+ if (!fetcher) {
34
+ fetcher = async (body) => {
35
+ let headersObject =
36
+ typeof headers == 'function' ? await headers() : headers
37
+ headersObject = headersObject || {}
38
+ if (typeof fetch === 'undefined' && !_fetch) {
39
+ throw new Error(
40
+ 'Global `fetch` function is not available, pass a fetch polyfill to Genql `createClient`',
41
+ )
42
+ }
43
+ let fetchImpl = _fetch || fetch
44
+ const res = await fetchImpl(url!, {
45
+ headers: {
46
+ 'Content-Type': 'application/json',
47
+ ...headersObject,
48
+ },
49
+ method: 'POST',
50
+ body: JSON.stringify(body),
51
+ ...rest,
52
+ })
53
+ if (!res.ok) {
54
+ throw new Error(`${res.statusText}: ${await res.text()}`)
55
+ }
56
+ const json = await res.json()
57
+ return json
58
+ }
59
+ }
60
+
61
+ if (!batch) {
62
+ return async (body) => {
63
+ const json = await fetcher!(body)
64
+ if (Array.isArray(json)) {
65
+ return json.map((json) => {
66
+ if (json?.errors?.length) {
67
+ throw new GenqlError(json.errors || [], json.data)
68
+ }
69
+ return json.data
70
+ })
71
+ } else {
72
+ if (json?.errors?.length) {
73
+ throw new GenqlError(json.errors || [], json.data)
74
+ }
75
+ return json.data
76
+ }
77
+ }
78
+ }
79
+
80
+ const batcher = new QueryBatcher(
81
+ async (batchedQuery) => {
82
+ // console.log(batchedQuery) // [{ query: 'query{user{age}}', variables: {} }, ...]
83
+ const json = await fetcher!(batchedQuery)
84
+ return json as any
85
+ },
86
+ batch === true ? DEFAULT_BATCH_OPTIONS : batch,
87
+ )
88
+
89
+ return async ({ query, variables }) => {
90
+ const json = await batcher.fetch(query, variables)
91
+ if (json?.data) {
92
+ return json.data
93
+ }
94
+ throw new Error(
95
+ 'Genql batch fetcher returned unexpected result ' + JSON.stringify(json),
96
+ )
97
+ }
98
+ }
@@ -0,0 +1,225 @@
1
+ // @ts-nocheck
2
+ import type { LinkedField, LinkedType } from './types'
3
+
4
+ export interface Args {
5
+ [arg: string]: any | undefined
6
+ }
7
+
8
+ export interface Fields {
9
+ [field: string]: Request
10
+ }
11
+
12
+ export type Request = boolean | number | Fields
13
+
14
+ export interface Variables {
15
+ [name: string]: {
16
+ value: any
17
+ typing: [LinkedType, string]
18
+ }
19
+ }
20
+
21
+ export interface Context {
22
+ root: LinkedType
23
+ varCounter: number
24
+ variables: Variables
25
+ fragmentCounter: number
26
+ fragments: string[]
27
+ }
28
+
29
+ export interface GraphqlOperation {
30
+ query: string
31
+ variables?: { [name: string]: any }
32
+ operationName?: string
33
+ }
34
+
35
+ const parseRequest = (
36
+ request: Request | undefined,
37
+ ctx: Context,
38
+ path: string[],
39
+ ): string => {
40
+ if (typeof request === 'object' && '__args' in request) {
41
+ const args: any = request.__args
42
+ let fields: Request | undefined = { ...request }
43
+ delete fields.__args
44
+ const argNames = Object.keys(args)
45
+
46
+ if (argNames.length === 0) {
47
+ return parseRequest(fields, ctx, path)
48
+ }
49
+
50
+ const field = getFieldFromPath(ctx.root, path)
51
+
52
+ const argStrings = argNames.map((argName) => {
53
+ ctx.varCounter++
54
+ const varName = `v${ctx.varCounter}`
55
+
56
+ const typing = field.args && field.args[argName] // typeMap used here, .args
57
+
58
+ if (!typing) {
59
+ throw new Error(
60
+ `no typing defined for argument \`${argName}\` in path \`${path.join(
61
+ '.',
62
+ )}\``,
63
+ )
64
+ }
65
+
66
+ ctx.variables[varName] = {
67
+ value: args[argName],
68
+ typing,
69
+ }
70
+
71
+ return `${argName}:$${varName}`
72
+ })
73
+ return `(${argStrings})${parseRequest(fields, ctx, path)}`
74
+ } else if (typeof request === 'object' && Object.keys(request).length > 0) {
75
+ const fields = request
76
+ const fieldNames = Object.keys(fields).filter((k) => Boolean(fields[k]))
77
+
78
+ if (fieldNames.length === 0) {
79
+ throw new Error(
80
+ `field selection should not be empty: ${path.join('.')}`,
81
+ )
82
+ }
83
+
84
+ const type =
85
+ path.length > 0 ? getFieldFromPath(ctx.root, path).type : ctx.root
86
+ const scalarFields = type.scalar
87
+
88
+ let scalarFieldsFragment: string | undefined
89
+
90
+ if (fieldNames.includes('__scalar')) {
91
+ const falsyFieldNames = new Set(
92
+ Object.keys(fields).filter((k) => !Boolean(fields[k])),
93
+ )
94
+ if (scalarFields?.length) {
95
+ ctx.fragmentCounter++
96
+ scalarFieldsFragment = `f${ctx.fragmentCounter}`
97
+
98
+ ctx.fragments.push(
99
+ `fragment ${scalarFieldsFragment} on ${
100
+ type.name
101
+ }{${scalarFields
102
+ .filter((f) => !falsyFieldNames.has(f))
103
+ .join(',')}}`,
104
+ )
105
+ }
106
+ }
107
+
108
+ const fieldsSelection = fieldNames
109
+ .filter((f) => !['__scalar', '__name'].includes(f))
110
+ .map((f) => {
111
+ const parsed = parseRequest(fields[f], ctx, [...path, f])
112
+
113
+ if (f.startsWith('on_')) {
114
+ ctx.fragmentCounter++
115
+ const implementationFragment = `f${ctx.fragmentCounter}`
116
+
117
+ const typeMatch = f.match(/^on_(.+)/)
118
+
119
+ if (!typeMatch || !typeMatch[1])
120
+ throw new Error('match failed')
121
+
122
+ ctx.fragments.push(
123
+ `fragment ${implementationFragment} on ${typeMatch[1]}${parsed}`,
124
+ )
125
+
126
+ return `...${implementationFragment}`
127
+ } else {
128
+ return `${f}${parsed}`
129
+ }
130
+ })
131
+ .concat(scalarFieldsFragment ? [`...${scalarFieldsFragment}`] : [])
132
+ .join(',')
133
+
134
+ return `{${fieldsSelection}}`
135
+ } else {
136
+ return ''
137
+ }
138
+ }
139
+
140
+ export const generateGraphqlOperation = (
141
+ operation: 'query' | 'mutation' | 'subscription',
142
+ root: LinkedType,
143
+ fields?: Fields,
144
+ ): GraphqlOperation => {
145
+ const ctx: Context = {
146
+ root: root,
147
+ varCounter: 0,
148
+ variables: {},
149
+ fragmentCounter: 0,
150
+ fragments: [],
151
+ }
152
+ const result = parseRequest(fields, ctx, [])
153
+
154
+ const varNames = Object.keys(ctx.variables)
155
+
156
+ const varsString =
157
+ varNames.length > 0
158
+ ? `(${varNames.map((v) => {
159
+ const variableType = ctx.variables[v].typing[1]
160
+ return `$${v}:${variableType}`
161
+ })})`
162
+ : ''
163
+
164
+ const operationName = fields?.__name || ''
165
+
166
+ return {
167
+ query: [
168
+ `${operation} ${operationName}${varsString}${result}`,
169
+ ...ctx.fragments,
170
+ ].join(','),
171
+ variables: Object.keys(ctx.variables).reduce<{ [name: string]: any }>(
172
+ (r, v) => {
173
+ r[v] = ctx.variables[v].value
174
+ return r
175
+ },
176
+ {},
177
+ ),
178
+ ...(operationName ? { operationName: operationName.toString() } : {}),
179
+ }
180
+ }
181
+
182
+ export const getFieldFromPath = (
183
+ root: LinkedType | undefined,
184
+ path: string[],
185
+ ) => {
186
+ let current: LinkedField | undefined
187
+
188
+ if (!root) throw new Error('root type is not provided')
189
+
190
+ if (path.length === 0) throw new Error(`path is empty`)
191
+
192
+ path.forEach((f) => {
193
+ const type = current ? current.type : root
194
+
195
+ if (!type.fields)
196
+ throw new Error(`type \`${type.name}\` does not have fields`)
197
+
198
+ const possibleTypes = Object.keys(type.fields)
199
+ .filter((i) => i.startsWith('on_'))
200
+ .reduce(
201
+ (types, fieldName) => {
202
+ const field = type.fields && type.fields[fieldName]
203
+ if (field) types.push(field.type)
204
+ return types
205
+ },
206
+ [type],
207
+ )
208
+
209
+ let field: LinkedField | null = null
210
+
211
+ possibleTypes.forEach((type) => {
212
+ const found = type.fields && type.fields[f]
213
+ if (found) field = found
214
+ })
215
+
216
+ if (!field)
217
+ throw new Error(
218
+ `type \`${type.name}\` does not have a field \`${f}\``,
219
+ )
220
+
221
+ current = field
222
+ })
223
+
224
+ return current as LinkedField
225
+ }
@@ -0,0 +1,13 @@
1
+ // @ts-nocheck
2
+ export { createClient } from './createClient'
3
+ export type { ClientOptions } from './createClient'
4
+ export type { FieldsSelection } from './typeSelection'
5
+ export { generateGraphqlOperation } from './generateGraphqlOperation'
6
+ export type { GraphqlOperation } from './generateGraphqlOperation'
7
+ export { linkTypeMap } from './linkTypeMap'
8
+ // export { Observable } from 'zen-observable-ts'
9
+ export { createFetcher } from './fetcher'
10
+ export { GenqlError } from './error'
11
+ export const everything = {
12
+ __scalar: true,
13
+ }