@aws-amplify/api-graphql 3.4.3 → 3.4.4-graphql-api-functional-options.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 (57) hide show
  1. package/lib/facade-iterable-paging.d.ts +19 -0
  2. package/lib/facade-iterable-paging.js +115 -0
  3. package/lib/facade-iterable-paging.js.map +1 -0
  4. package/lib/facade-light-flattening.d.ts +19 -0
  5. package/lib/facade-light-flattening.js +69 -0
  6. package/lib/facade-light-flattening.js.map +1 -0
  7. package/lib/facade-no-flattening.d.ts +19 -0
  8. package/lib/facade-no-flattening.js +37 -0
  9. package/lib/facade-no-flattening.js.map +1 -0
  10. package/lib/types/facade-iterable-paging.d.ts +106 -0
  11. package/lib/types/facade-iterable-paging.js +3 -0
  12. package/lib/types/facade-iterable-paging.js.map +1 -0
  13. package/lib/types/facade-light-flattening.d.ts +84 -0
  14. package/lib/types/facade-light-flattening.js +3 -0
  15. package/lib/types/facade-light-flattening.js.map +1 -0
  16. package/lib/types/facade-no-flattening.d.ts +48 -0
  17. package/lib/types/facade-no-flattening.js +3 -0
  18. package/lib/types/facade-no-flattening.js.map +1 -0
  19. package/lib/types/v6.d.ts +48 -0
  20. package/lib/types/v6.js +3 -0
  21. package/lib/types/v6.js.map +1 -0
  22. package/lib/v6.d.ts +22 -0
  23. package/lib/v6.js +45 -0
  24. package/lib/v6.js.map +1 -0
  25. package/lib-esm/facade-iterable-paging.d.ts +19 -0
  26. package/lib-esm/facade-iterable-paging.js +109 -0
  27. package/lib-esm/facade-iterable-paging.js.map +1 -0
  28. package/lib-esm/facade-light-flattening.d.ts +19 -0
  29. package/lib-esm/facade-light-flattening.js +63 -0
  30. package/lib-esm/facade-light-flattening.js.map +1 -0
  31. package/lib-esm/facade-no-flattening.d.ts +19 -0
  32. package/lib-esm/facade-no-flattening.js +31 -0
  33. package/lib-esm/facade-no-flattening.js.map +1 -0
  34. package/lib-esm/types/facade-iterable-paging.d.ts +106 -0
  35. package/lib-esm/types/facade-iterable-paging.js +1 -0
  36. package/lib-esm/types/facade-iterable-paging.js.map +1 -0
  37. package/lib-esm/types/facade-light-flattening.d.ts +84 -0
  38. package/lib-esm/types/facade-light-flattening.js +1 -0
  39. package/lib-esm/types/facade-light-flattening.js.map +1 -0
  40. package/lib-esm/types/facade-no-flattening.d.ts +48 -0
  41. package/lib-esm/types/facade-no-flattening.js +1 -0
  42. package/lib-esm/types/facade-no-flattening.js.map +1 -0
  43. package/lib-esm/types/v6.d.ts +48 -0
  44. package/lib-esm/types/v6.js +1 -0
  45. package/lib-esm/types/v6.js.map +1 -0
  46. package/lib-esm/v6.d.ts +22 -0
  47. package/lib-esm/v6.js +42 -0
  48. package/lib-esm/v6.js.map +1 -0
  49. package/package.json +10 -7
  50. package/src/facade-iterable-paging.ts +150 -0
  51. package/src/facade-light-flattening.ts +139 -0
  52. package/src/facade-no-flattening.ts +100 -0
  53. package/src/types/facade-iterable-paging.ts +200 -0
  54. package/src/types/facade-light-flattening.ts +171 -0
  55. package/src/types/facade-no-flattening.ts +110 -0
  56. package/src/types/v6.ts +110 -0
  57. package/src/v6.ts +103 -0
@@ -0,0 +1,48 @@
1
+ import { GraphQLResult } from './index';
2
+ declare type UnionKeys<T> = T extends T ? keyof T : never;
3
+ declare type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, undefined>> : never;
4
+ export declare type StrictUnion<T> = StrictUnionHelper<T, T>;
5
+ export declare type SimpleAuthMode = {
6
+ authMode?: 'AMAZON_COGNITO_USERPOOLS' | 'API_KEY' | 'AWS_IAM';
7
+ };
8
+ export declare type LambdaAuthMode = {
9
+ authMode: 'AWS_LAMBDA';
10
+ authToken: string;
11
+ };
12
+ export declare type AuthMode = StrictUnion<SimpleAuthMode | LambdaAuthMode>;
13
+ export declare type GeneratedQuery<InputType, OutputType> = string & {
14
+ __generatedQueryInput: InputType;
15
+ __generatedQueryOutput: OutputType;
16
+ };
17
+ export declare type GraphqlQueryOverrides<IN extends {}, OUT extends {}> = {
18
+ variables: IN;
19
+ result: OUT;
20
+ };
21
+ export declare type GraphqlQueryParams<TYPED_GQL_STRING extends string, FALLBACK_TYPES> = AuthMode & {
22
+ variables: TYPED_GQL_STRING extends GeneratedQuery<infer IN, infer OUT> ? IN : FALLBACK_TYPES extends GraphqlQueryOverrides<infer IN, infer OUT> ? IN : any;
23
+ };
24
+ export declare type GraphqlQueryResult<T extends string, S> = T extends GeneratedQuery<infer IN, infer OUT> ? GraphQLResult<OUT> : S extends GraphqlQueryOverrides<infer IN, infer OUT> ? GraphQLResult<OUT> : any;
25
+ /** GraphQL mutate */
26
+ export declare type GeneratedMutation<InputType, OutputType> = string & {
27
+ __generatedMutationInput: InputType;
28
+ __generatedMutationOutput: OutputType;
29
+ };
30
+ export declare type GraphqlMutationParams<TYPED_GQL_STRING extends string, OVERRIDE_TYPE> = AuthMode & {
31
+ variables: TYPED_GQL_STRING extends GeneratedMutation<infer IN, infer OUT> ? IN : OVERRIDE_TYPE extends GraphqlQueryOverrides<infer OVERRIDE_IN, infer OUT> ? OVERRIDE_IN : any;
32
+ };
33
+ export declare type GraphqlMutationResult<TYPED_GQL_STRING extends string, FALLBACK_TYPES> = TYPED_GQL_STRING extends GeneratedMutation<infer IN, infer OUT> ? GraphQLResult<OUT> : FALLBACK_TYPES extends GraphqlQueryOverrides<infer IN, infer OUT> ? GraphQLResult<OUT> : any;
34
+ /** GraphQL subscribe */
35
+ export declare type GeneratedSubscription<InputType, OutputType> = string & {
36
+ __generatedSubscriptionInput: InputType;
37
+ __generatedSubscriptionOutput: OutputType;
38
+ };
39
+ export declare type GraphqlSubscriptionParams<TYPED_GQL_STRING extends string, FALLBACK_TYPES> = AuthMode & {
40
+ variables: TYPED_GQL_STRING extends GeneratedSubscription<infer IN, infer OUT> ? IN : FALLBACK_TYPES extends GraphqlQueryOverrides<infer IN, infer OUT> ? IN : any;
41
+ };
42
+ export declare type GraphqlSubscriptionResult<TYPED_GQL_STRING extends string, FALLBACK_TYPES> = {
43
+ provider: any;
44
+ value: {
45
+ data: TYPED_GQL_STRING extends GeneratedSubscription<infer IN, infer OUT> ? OUT : FALLBACK_TYPES extends GraphqlQueryOverrides<infer IN, infer OUT> ? OUT : any;
46
+ };
47
+ };
48
+ export {};
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=v6.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"v6.js","sourceRoot":"","sources":["../../src/types/v6.ts"],"names":[],"mappings":""}
@@ -0,0 +1,22 @@
1
+ import { Observable } from 'zen-observable-ts';
2
+ import { GraphqlQueryParams, GraphqlQueryResult, GraphqlMutationParams, GraphqlMutationResult, GraphqlSubscriptionParams, GraphqlSubscriptionResult } from './types/v6';
3
+ import { GraphQLOptions, GraphQLResult } from './types';
4
+ /**
5
+ * The proposed interface additions to the TypeBeast
6
+ */
7
+ declare class V6Client {
8
+ graphql<T = any>(options: GraphQLOptions, additionalHeaders?: {
9
+ [key: string]: string;
10
+ }): Observable<GraphqlSubscriptionResult<'', T>> | Promise<GraphQLResult<T>>;
11
+ query<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(document: TYPED_GQL_STRING, queryParams?: GraphqlQueryParams<TYPED_GQL_STRING, FALLBACK_TYPES>, additionalHeaders?: {
12
+ [key: string]: string;
13
+ }): Promise<GraphqlQueryResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
14
+ mutate<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(document: TYPED_GQL_STRING, queryParams?: GraphqlMutationParams<TYPED_GQL_STRING, FALLBACK_TYPES>, additionalHeaders?: {
15
+ [key: string]: string;
16
+ }): Promise<GraphqlMutationResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
17
+ subscribe<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(document: TYPED_GQL_STRING, queryParams?: GraphqlSubscriptionParams<TYPED_GQL_STRING, FALLBACK_TYPES>, additionalHeaders?: {
18
+ [key: string]: string;
19
+ }): Observable<GraphqlSubscriptionResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
20
+ }
21
+ export declare function generateClient(): V6Client;
22
+ export {};
package/lib-esm/v6.js ADDED
@@ -0,0 +1,42 @@
1
+ import { __assign, __read } from "tslib";
2
+ import { GraphQLAPI } from './GraphQLAPI';
3
+ import { parse, print } from 'graphql';
4
+ /**
5
+ * The proposed interface additions to the TypeBeast
6
+ */
7
+ var V6Client = /** @class */ (function () {
8
+ function V6Client() {
9
+ }
10
+ V6Client.prototype.graphql = function (options, additionalHeaders) {
11
+ var parsedQuery = typeof options.query === 'string'
12
+ ? parse(options.query)
13
+ : parse(print(options.query));
14
+ var _a = __read(parsedQuery.definitions.filter(function (def) { return def.kind === 'OperationDefinition'; }), 1), _b = _a[0], operationDef = _b === void 0 ? {} : _b;
15
+ var operationType = operationDef.operation;
16
+ var childOptions = __assign(__assign({}, options), { query: print(parsedQuery) }); // todo
17
+ switch (operationType) {
18
+ case 'query':
19
+ return this.query(childOptions.query, childOptions, additionalHeaders);
20
+ case 'mutation':
21
+ return this.mutate(childOptions.query, childOptions, additionalHeaders);
22
+ case 'subscription':
23
+ return this.subscribe(childOptions.query, childOptions, additionalHeaders);
24
+ default:
25
+ throw new Error("invalid operation type: " + operationType);
26
+ }
27
+ };
28
+ V6Client.prototype.query = function (document, queryParams, additionalHeaders) {
29
+ return GraphQLAPI.graphql(__assign(__assign({}, (queryParams || {})), { query: document }), additionalHeaders);
30
+ };
31
+ V6Client.prototype.mutate = function (document, queryParams, additionalHeaders) {
32
+ return GraphQLAPI.graphql(__assign(__assign({}, (queryParams || {})), { query: document }), additionalHeaders);
33
+ };
34
+ V6Client.prototype.subscribe = function (document, queryParams, additionalHeaders) {
35
+ return GraphQLAPI.graphql(__assign(__assign({}, (queryParams || {})), { query: document }), additionalHeaders);
36
+ };
37
+ return V6Client;
38
+ }());
39
+ export function generateClient() {
40
+ return new V6Client();
41
+ }
42
+ //# sourceMappingURL=v6.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"v6.js","sourceRoot":"","sources":["../src/v6.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,KAAK,EAAE,KAAK,EAA2B,MAAM,SAAS,CAAC;AAahE;;GAEG;AACH;IAAA;IAgFA,CAAC;IA/EA,0BAAO,GAAP,UACC,OAAuB,EACvB,iBAA6C;QAE7C,IAAM,WAAW,GAChB,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ;YAChC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YACtB,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QAE1B,IAAA,6GAEL,EAFM,UAAiB,EAAjB,sCAEN,CAAC;QACM,IAAA,sCAAwB,CACS;QAEzC,IAAM,YAAY,GAAG,sBACjB,OAAO,KACV,KAAK,EAAE,KAAK,CAAC,WAAW,CAAC,GAClB,CAAC,CAAC,OAAO;QAEjB,QAAQ,aAAa,EAAE;YACtB,KAAK,OAAO;gBACX,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,iBAAiB,CAAC,CAAC;YACxE,KAAK,UAAU;gBACd,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,iBAAiB,CAAC,CAAC;YACzE,KAAK,cAAc;gBAClB,OAAO,IAAI,CAAC,SAAS,CACpB,YAAY,CAAC,KAAK,EAClB,YAAY,EACZ,iBAAiB,CACjB,CAAC;YACH;gBACC,MAAM,IAAI,KAAK,CAAC,6BAA2B,aAAe,CAAC,CAAC;SAC7D;IACF,CAAC;IAED,wBAAK,GAAL,UACC,QAA0B,EAC1B,WAAkE,EAClE,iBAA6C;QAE7C,OAAO,UAAU,CAAC,OAAO,uBAEnB,CAAC,WAAW,IAAI,EAAE,CAAS,KAC/B,KAAK,EAAE,QAAQ,KAEhB,iBAAiB,CACgD,CAAC;IACpE,CAAC;IAED,yBAAM,GAAN,UACC,QAA0B,EAC1B,WAAqE,EACrE,iBAA6C;QAE7C,OAAO,UAAU,CAAC,OAAO,uBAEnB,CAAC,WAAW,IAAI,EAAE,CAAS,KAC/B,KAAK,EAAE,QAAQ,KAEhB,iBAAiB,CACmD,CAAC;IACvE,CAAC;IAED,4BAAS,GAAT,UACC,QAA0B,EAC1B,WAAyE,EACzE,iBAA6C;QAE7C,OAAO,UAAU,CAAC,OAAO,uBAEnB,CAAC,WAAW,IAAI,EAAE,CAAS,KAC/B,KAAK,EAAE,QAAQ,KAEhB,iBAAiB,CAGjB,CAAC;IACH,CAAC;IACF,eAAC;AAAD,CAAC,AAhFD,IAgFC;AAED,MAAM,UAAU,cAAc;IAC7B,OAAO,IAAI,QAAQ,EAAE,CAAC;AACvB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "3.4.3",
3
+ "version": "3.4.4-graphql-api-functional-options.2+627f308ed",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -50,11 +50,11 @@
50
50
  "internals"
51
51
  ],
52
52
  "dependencies": {
53
- "@aws-amplify/api-rest": "3.3.2",
54
- "@aws-amplify/auth": "5.5.3",
55
- "@aws-amplify/cache": "5.1.3",
56
- "@aws-amplify/core": "5.5.2",
57
- "@aws-amplify/pubsub": "5.3.3",
53
+ "@aws-amplify/api-rest": "3.3.3-graphql-api-functional-options.2+627f308ed",
54
+ "@aws-amplify/auth": "5.5.4-graphql-api-functional-options.2+627f308ed",
55
+ "@aws-amplify/cache": "5.1.4-graphql-api-functional-options.2+627f308ed",
56
+ "@aws-amplify/core": "5.5.3-graphql-api-functional-options.2+627f308ed",
57
+ "@aws-amplify/pubsub": "5.3.4-graphql-api-functional-options.2+627f308ed",
58
58
  "graphql": "15.8.0",
59
59
  "tslib": "^1.8.0",
60
60
  "uuid": "^3.2.1",
@@ -88,6 +88,9 @@
88
88
  "^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
89
89
  },
90
90
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
91
+ "testPathIgnorePatterns": [
92
+ "__tests__/helpers/"
93
+ ],
91
94
  "moduleFileExtensions": [
92
95
  "ts",
93
96
  "tsx",
@@ -112,5 +115,5 @@
112
115
  "lib-esm"
113
116
  ]
114
117
  },
115
- "gitHead": "b1061a190d11d1313e3c669dbeea79564cb6ea89"
118
+ "gitHead": "627f308ed709b6c0ab8ec750d7235f3e007a669a"
116
119
  }
@@ -0,0 +1,150 @@
1
+ import { GraphQLAPI } from './GraphQLAPI';
2
+ import { Observable } from 'zen-observable-ts';
3
+ import { parse, print, OperationDefinitionNode } from 'graphql';
4
+
5
+ /**
6
+ * `GraphQLOptions` currently includes `{ query: string | DocumentNode, ... }`, but I'm thinking
7
+ * we swap that for `query: string` or `query: string | T extends Whatever`.
8
+ */
9
+ import { GraphQLOptions } from './types';
10
+
11
+ import {
12
+ GraphQLResult,
13
+ GraphqlQueryParams,
14
+ GraphqlQueryResult,
15
+ GraphqlMutationParams,
16
+ GraphqlMutationResult,
17
+ GraphqlSubscriptionParams,
18
+ GraphqlSubscriptionResult,
19
+ } from './types/facade-iterable-paging';
20
+
21
+ export function graphql<T = any>(
22
+ options: GraphQLOptions,
23
+ additionalHeaders?: { [key: string]: string }
24
+ ): Observable<GraphqlSubscriptionResult<'', T>> | Promise<GraphQLResult<T>> {
25
+ const parsedQuery =
26
+ typeof options.query === 'string'
27
+ ? parse(options.query)
28
+ : parse(print(options.query));
29
+
30
+ const [operationDef = {}] = parsedQuery.definitions.filter(
31
+ def => def.kind === 'OperationDefinition'
32
+ );
33
+ const { operation: operationType } = operationDef as OperationDefinitionNode;
34
+
35
+ const childOptions = {
36
+ ...options,
37
+ query: print(parsedQuery),
38
+ } as any;
39
+
40
+ switch (operationType) {
41
+ case 'query':
42
+ return query(childOptions.query, childOptions, additionalHeaders);
43
+ case 'mutation':
44
+ return mutate(childOptions.query, childOptions, additionalHeaders);
45
+ case 'subscription':
46
+ return subscribe(childOptions.query, childOptions, additionalHeaders);
47
+ default:
48
+ throw new Error(`invalid operation type: ${operationType}`);
49
+ }
50
+ }
51
+
52
+ export function query<
53
+ FALLBACK_TYPES = unknown,
54
+ TYPED_GQL_STRING extends string = string
55
+ >(
56
+ document: TYPED_GQL_STRING,
57
+ queryParams?: GraphqlQueryParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
58
+ additionalHeaders?: { [key: string]: string }
59
+ ): Promise<GraphqlQueryResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
60
+ return (
61
+ GraphQLAPI.graphql(
62
+ {
63
+ ...((queryParams || {}) as any),
64
+ query: document,
65
+ },
66
+ additionalHeaders
67
+ ) as any
68
+ ).then(raw => {
69
+ const responseKey = raw.data ? Object.keys(raw.data)[0] : undefined;
70
+ if (responseKey) {
71
+ if (Array.isArray(raw.data[responseKey]?.items)) {
72
+ return {
73
+ data: (async function* _() {
74
+ for (const item of raw.data[responseKey]?.items) {
75
+ yield item;
76
+ }
77
+ })(),
78
+ errors: raw.errors,
79
+ };
80
+ } else {
81
+ return {
82
+ data: raw.data[responseKey],
83
+ errors: raw.errors,
84
+ };
85
+ }
86
+ } else {
87
+ return raw;
88
+ }
89
+ }) as Promise<GraphqlQueryResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
90
+ }
91
+
92
+ export function mutate<
93
+ FALLBACK_TYPES = unknown,
94
+ TYPED_GQL_STRING extends string = string
95
+ >(
96
+ document: TYPED_GQL_STRING,
97
+ queryParams?: GraphqlMutationParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
98
+ additionalHeaders?: { [key: string]: string }
99
+ ): Promise<GraphqlMutationResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
100
+ return (
101
+ GraphQLAPI.graphql(
102
+ {
103
+ ...((queryParams || {}) as any),
104
+ query: document,
105
+ },
106
+ additionalHeaders
107
+ ) as any
108
+ ).then(raw => {
109
+ const responseKey = raw.data ? Object.keys(raw.data)[0] : undefined;
110
+ if (responseKey) {
111
+ return {
112
+ data: raw.data[responseKey],
113
+ errors: raw.errors,
114
+ };
115
+ } else {
116
+ return raw;
117
+ }
118
+ }) as Promise<GraphqlMutationResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
119
+ }
120
+
121
+ export function subscribe<
122
+ FALLBACK_TYPES = unknown,
123
+ TYPED_GQL_STRING extends string = string
124
+ >(
125
+ document: TYPED_GQL_STRING,
126
+ queryParams?: GraphqlSubscriptionParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
127
+ additionalHeaders?: { [key: string]: string }
128
+ ): Observable<GraphqlSubscriptionResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
129
+ return (
130
+ GraphQLAPI.graphql(
131
+ {
132
+ ...((queryParams || {}) as any),
133
+ query: document,
134
+ },
135
+ additionalHeaders
136
+ ) as Observable<any>
137
+ ).map(raw => {
138
+ const messageKey = raw.value.data
139
+ ? Object.keys(raw.value.data)[0]
140
+ : undefined;
141
+ return {
142
+ value: {
143
+ data: messageKey ? raw.value.data[messageKey] : undefined,
144
+ provider: raw.provider,
145
+ },
146
+ };
147
+ }) as unknown as Observable<
148
+ GraphqlSubscriptionResult<TYPED_GQL_STRING, FALLBACK_TYPES>
149
+ >;
150
+ }
@@ -0,0 +1,139 @@
1
+ import { GraphQLAPI } from './GraphQLAPI';
2
+ import { Observable } from 'zen-observable-ts';
3
+ import { parse, print, OperationDefinitionNode } from 'graphql';
4
+
5
+ /**
6
+ * `GraphQLOptions` currently includes `{ query: string | DocumentNode, ... }`, but I'm thinking
7
+ * we swap that for `query: string` or `query: string | T extends Whatever`.
8
+ */
9
+ import { GraphQLOptions } from './types';
10
+
11
+ import {
12
+ GraphQLResult,
13
+ GraphqlQueryParams,
14
+ GraphqlQueryResult,
15
+ GraphqlMutationParams,
16
+ GraphqlMutationResult,
17
+ GraphqlSubscriptionParams,
18
+ GraphqlSubscriptionResult,
19
+ } from './types/facade-light-flattening';
20
+
21
+ export function graphql<T = any>(
22
+ options: GraphQLOptions,
23
+ additionalHeaders?: { [key: string]: string }
24
+ ): Observable<GraphqlSubscriptionResult<'', T>> | Promise<GraphQLResult<T>> {
25
+ const parsedQuery =
26
+ typeof options.query === 'string'
27
+ ? parse(options.query)
28
+ : parse(print(options.query));
29
+
30
+ const [operationDef = {}] = parsedQuery.definitions.filter(
31
+ def => def.kind === 'OperationDefinition'
32
+ );
33
+ const { operation: operationType } = operationDef as OperationDefinitionNode;
34
+
35
+ const childOptions = {
36
+ ...options,
37
+ query: print(parsedQuery),
38
+ } as any;
39
+
40
+ switch (operationType) {
41
+ case 'query':
42
+ return query(childOptions.query, childOptions, additionalHeaders);
43
+ case 'mutation':
44
+ return mutate(childOptions.query, childOptions, additionalHeaders);
45
+ case 'subscription':
46
+ return subscribe(childOptions.query, childOptions, additionalHeaders);
47
+ default:
48
+ throw new Error(`invalid operation type: ${operationType}`);
49
+ }
50
+ }
51
+
52
+ export function query<
53
+ FALLBACK_TYPES = unknown,
54
+ TYPED_GQL_STRING extends string = string
55
+ >(
56
+ document: TYPED_GQL_STRING,
57
+ queryParams?: GraphqlQueryParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
58
+ additionalHeaders?: { [key: string]: string }
59
+ ): Promise<GraphqlQueryResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
60
+ return (
61
+ GraphQLAPI.graphql(
62
+ {
63
+ ...((queryParams || {}) as any),
64
+ query: document,
65
+ },
66
+ additionalHeaders
67
+ ) as any
68
+ ).then(raw => {
69
+ const responseKey = raw.data ? Object.keys(raw.data)[0] : undefined;
70
+ if (responseKey) {
71
+ return {
72
+ data: raw.data[responseKey],
73
+ errors: raw.errors,
74
+ };
75
+ } else {
76
+ return raw;
77
+ }
78
+ }) as Promise<GraphqlQueryResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
79
+ }
80
+
81
+ export function mutate<
82
+ FALLBACK_TYPES = unknown,
83
+ TYPED_GQL_STRING extends string = string
84
+ >(
85
+ document: TYPED_GQL_STRING,
86
+ queryParams?: GraphqlMutationParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
87
+ additionalHeaders?: { [key: string]: string }
88
+ ): Promise<GraphqlMutationResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
89
+ return (
90
+ GraphQLAPI.graphql(
91
+ {
92
+ ...((queryParams || {}) as any),
93
+ query: document,
94
+ },
95
+ additionalHeaders
96
+ ) as any
97
+ ).then(raw => {
98
+ const responseKey = raw.data ? Object.keys(raw.data)[0] : undefined;
99
+ if (responseKey) {
100
+ return {
101
+ data: raw.data[responseKey],
102
+ errors: raw.errors,
103
+ };
104
+ } else {
105
+ return raw;
106
+ }
107
+ }) as Promise<GraphqlMutationResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
108
+ }
109
+
110
+ export function subscribe<
111
+ FALLBACK_TYPES = unknown,
112
+ TYPED_GQL_STRING extends string = string
113
+ >(
114
+ document: TYPED_GQL_STRING,
115
+ queryParams?: GraphqlSubscriptionParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
116
+ additionalHeaders?: { [key: string]: string }
117
+ ): Observable<GraphqlSubscriptionResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
118
+ return (
119
+ GraphQLAPI.graphql(
120
+ {
121
+ ...((queryParams || {}) as any),
122
+ query: document,
123
+ },
124
+ additionalHeaders
125
+ ) as Observable<any>
126
+ ).map(raw => {
127
+ const messageKey = raw.value.data
128
+ ? Object.keys(raw.value.data)[0]
129
+ : undefined;
130
+ return {
131
+ value: {
132
+ data: messageKey ? raw.value.data[messageKey] : undefined,
133
+ provider: raw.provider,
134
+ },
135
+ };
136
+ }) as unknown as Observable<
137
+ GraphqlSubscriptionResult<TYPED_GQL_STRING, FALLBACK_TYPES>
138
+ >;
139
+ }
@@ -0,0 +1,100 @@
1
+ import { GraphQLAPI } from './GraphQLAPI';
2
+ import { Observable } from 'zen-observable-ts';
3
+ import { parse, print, OperationDefinitionNode } from 'graphql';
4
+
5
+ /**
6
+ * `GraphQLOptions` currently includes `{ query: string | DocumentNode, ... }`, but I'm thinking
7
+ * we swap that for `query: string` or `query: string | T extends Whatever`.
8
+ */
9
+ import { GraphQLOptions, GraphQLResult } from './types';
10
+
11
+ import {
12
+ GraphqlQueryParams,
13
+ GraphqlQueryResult,
14
+ GraphqlMutationParams,
15
+ GraphqlMutationResult,
16
+ GraphqlSubscriptionParams,
17
+ GraphqlSubscriptionResult,
18
+ } from './types/facade-no-flattening';
19
+
20
+ export function graphql<T = any>(
21
+ options: GraphQLOptions,
22
+ additionalHeaders?: { [key: string]: string }
23
+ ): Observable<GraphqlSubscriptionResult<'', T>> | Promise<GraphQLResult<T>> {
24
+ const parsedQuery =
25
+ typeof options.query === 'string'
26
+ ? parse(options.query)
27
+ : parse(print(options.query));
28
+
29
+ const [operationDef = {}] = parsedQuery.definitions.filter(
30
+ def => def.kind === 'OperationDefinition'
31
+ );
32
+ const { operation: operationType } = operationDef as OperationDefinitionNode;
33
+
34
+ const childOptions = {
35
+ ...options,
36
+ query: print(parsedQuery),
37
+ } as any; // todo
38
+
39
+ switch (operationType) {
40
+ case 'query':
41
+ return query(childOptions.query, childOptions, additionalHeaders);
42
+ case 'mutation':
43
+ return mutate(childOptions.query, childOptions, additionalHeaders);
44
+ case 'subscription':
45
+ return subscribe(childOptions.query, childOptions, additionalHeaders);
46
+ default:
47
+ throw new Error(`invalid operation type: ${operationType}`);
48
+ }
49
+ }
50
+
51
+ export function query<
52
+ FALLBACK_TYPES = unknown,
53
+ TYPED_GQL_STRING extends string = string
54
+ >(
55
+ document: TYPED_GQL_STRING,
56
+ queryParams?: GraphqlQueryParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
57
+ additionalHeaders?: { [key: string]: string }
58
+ ): Promise<GraphqlQueryResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
59
+ return GraphQLAPI.graphql(
60
+ {
61
+ ...((queryParams || {}) as any), // todo
62
+ query: document,
63
+ },
64
+ additionalHeaders
65
+ ) as Promise<GraphqlQueryResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
66
+ }
67
+
68
+ export function mutate<
69
+ FALLBACK_TYPES = unknown,
70
+ TYPED_GQL_STRING extends string = string
71
+ >(
72
+ document: TYPED_GQL_STRING,
73
+ queryParams?: GraphqlMutationParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
74
+ additionalHeaders?: { [key: string]: string }
75
+ ): Promise<GraphqlMutationResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
76
+ return GraphQLAPI.graphql(
77
+ {
78
+ ...((queryParams || {}) as any), // todo
79
+ query: document,
80
+ },
81
+ additionalHeaders
82
+ ) as Promise<GraphqlMutationResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
83
+ }
84
+
85
+ export function subscribe<
86
+ FALLBACK_TYPES = unknown,
87
+ TYPED_GQL_STRING extends string = string
88
+ >(
89
+ document: TYPED_GQL_STRING,
90
+ queryParams?: GraphqlSubscriptionParams<TYPED_GQL_STRING, FALLBACK_TYPES>,
91
+ additionalHeaders?: { [key: string]: string }
92
+ ): Observable<GraphqlSubscriptionResult<TYPED_GQL_STRING, FALLBACK_TYPES>> {
93
+ return GraphQLAPI.graphql(
94
+ {
95
+ ...((queryParams || {}) as any), // todo
96
+ query: document,
97
+ },
98
+ additionalHeaders
99
+ ) as Observable<GraphqlSubscriptionResult<TYPED_GQL_STRING, FALLBACK_TYPES>>;
100
+ }