@devticon-os/graphql-codegen-axios 0.3.0-test → 0.3.1

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 (49) hide show
  1. package/lib/_types.js +3 -0
  2. package/lib/_types.js.map +1 -0
  3. package/lib/enums.js +138 -0
  4. package/lib/enums.js.map +1 -0
  5. package/lib/fragments.js +49 -0
  6. package/lib/fragments.js.map +1 -0
  7. package/lib/functions.js +71 -0
  8. package/lib/functions.js.map +1 -0
  9. package/lib/graphql.js +208 -0
  10. package/lib/graphql.js.map +1 -0
  11. package/lib/helpers.js +69 -0
  12. package/lib/helpers.js.map +1 -0
  13. package/lib/index.js +86 -0
  14. package/lib/index.js.map +1 -0
  15. package/lib/input.js +142 -0
  16. package/lib/input.js.map +1 -0
  17. package/lib/operation.js +315 -0
  18. package/lib/operation.js.map +1 -0
  19. package/lib/prettier.js +17 -0
  20. package/lib/prettier.js.map +1 -0
  21. package/lib/print.js +231 -0
  22. package/lib/print.js.map +1 -0
  23. package/lib/query.js +66 -0
  24. package/lib/query.js.map +1 -0
  25. package/lib/render.js +186 -0
  26. package/lib/render.js.map +1 -0
  27. package/lib/results.js +64 -0
  28. package/lib/results.js.map +1 -0
  29. package/lib/scalar.js +9 -0
  30. package/lib/scalar.js.map +1 -0
  31. package/lib/types.js +81 -0
  32. package/lib/types.js.map +1 -0
  33. package/lib/utils.js +6 -0
  34. package/lib/utils.js.map +1 -0
  35. package/lib/variables.js +44 -0
  36. package/lib/variables.js.map +1 -0
  37. package/package.json +4 -3
  38. package/templates/helpers.ts +95 -0
  39. package/src/_types.ts +0 -60
  40. package/src/enums.ts +0 -50
  41. package/src/fragments.ts +0 -15
  42. package/src/graphql.ts +0 -101
  43. package/src/index.ts +0 -70
  44. package/src/input.ts +0 -65
  45. package/src/operation.ts +0 -212
  46. package/src/prettier.ts +0 -13
  47. package/src/print.ts +0 -139
  48. package/src/scalar.ts +0 -5
  49. package/src/utils.ts +0 -1
package/lib/types.js ADDED
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __values = (this && this.__values) || function(o) {
14
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
15
+ if (m) return m.call(o);
16
+ if (o && typeof o.length === "number") return {
17
+ next: function () {
18
+ if (o && i >= o.length) o = void 0;
19
+ return { value: o && o[i++], done: !o };
20
+ }
21
+ };
22
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.assignDirectivesToType = exports.getGraphqlTypeInfo = void 0;
26
+ var type_1 = require("graphql/type");
27
+ var getGraphqlTypeInfo = function (type, isList, isNullable) {
28
+ if (isList === void 0) { isList = false; }
29
+ if (isNullable === void 0) { isNullable = true; }
30
+ if (type instanceof type_1.GraphQLList) {
31
+ isList = true;
32
+ return (0, exports.getGraphqlTypeInfo)(type.ofType, isList, isNullable);
33
+ }
34
+ if (type instanceof type_1.GraphQLNonNull) {
35
+ isNullable = false;
36
+ return (0, exports.getGraphqlTypeInfo)(type.ofType, isList, isNullable);
37
+ }
38
+ var isScalar = type instanceof type_1.GraphQLScalarType;
39
+ return { type: type, isList: isList, isNullable: isNullable, typeName: type.name, isScalar: isScalar, gqlType: getGraphqlType(type) };
40
+ };
41
+ exports.getGraphqlTypeInfo = getGraphqlTypeInfo;
42
+ var getGraphqlType = function (type) {
43
+ if (type instanceof type_1.GraphQLEnumType) {
44
+ return 'enum';
45
+ }
46
+ if (type instanceof type_1.GraphQLInputObjectType) {
47
+ return 'input';
48
+ }
49
+ };
50
+ var assignDirectivesToType = function (typeInfo, directives) {
51
+ var e_1, _a;
52
+ var newType = __assign({}, typeInfo);
53
+ try {
54
+ for (var directives_1 = __values(directives), directives_1_1 = directives_1.next(); !directives_1_1.done; directives_1_1 = directives_1.next()) {
55
+ var directive = directives_1_1.value;
56
+ switch (directive.name.value) {
57
+ case 'firstOrFail':
58
+ newType.isList = false;
59
+ newType.isNullable = false;
60
+ break;
61
+ case 'first':
62
+ newType.isList = false;
63
+ newType.isNullable = true;
64
+ break;
65
+ case 'nonNullable':
66
+ newType.isNullable = false;
67
+ break;
68
+ }
69
+ }
70
+ }
71
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
72
+ finally {
73
+ try {
74
+ if (directives_1_1 && !directives_1_1.done && (_a = directives_1.return)) _a.call(directives_1);
75
+ }
76
+ finally { if (e_1) throw e_1.error; }
77
+ }
78
+ return newType;
79
+ };
80
+ exports.assignDirectivesToType = assignDirectivesToType;
81
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAAuH;AAKhH,IAAM,kBAAkB,GAAG,UAChC,IAAiB,EACjB,MAAc,EACd,UAAiB;IADjB,uBAAA,EAAA,cAAc;IACd,2BAAA,EAAA,iBAAiB;IAEjB,IAAI,IAAI,YAAY,kBAAW,EAAE;QAC/B,MAAM,GAAG,IAAI,CAAC;QACd,OAAO,IAAA,0BAAkB,EAAC,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;KAC5D;IACD,IAAI,IAAI,YAAY,qBAAc,EAAE;QAClC,UAAU,GAAG,KAAK,CAAC;QACnB,OAAO,IAAA,0BAAkB,EAAC,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;KAC5D;IACD,IAAM,QAAQ,GAAG,IAAI,YAAY,wBAAiB,CAAC;IACnD,OAAO,EAAE,IAAI,MAAA,EAAE,MAAM,QAAA,EAAE,UAAU,YAAA,EAAE,QAAQ,EAAE,IAAI,CAAC,IAAI,EAAE,QAAQ,UAAA,EAAE,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC;AACpG,CAAC,CAAC;AAfW,QAAA,kBAAkB,sBAe7B;AAEF,IAAM,cAAc,GAAG,UAAC,IAA0C;IAChE,IAAI,IAAI,YAAY,sBAAe,EAAE;QACnC,OAAO,MAAM,CAAC;KACf;IACD,IAAI,IAAI,YAAY,6BAAsB,EAAE;QAC1C,OAAO,OAAO,CAAC;KAChB;AACH,CAAC,CAAC;AACK,IAAM,sBAAsB,GAAG,UACpC,QAA6E,EAC7E,UAAoC;;IAEpC,IAAM,OAAO,gBAAQ,QAAQ,CAAE,CAAC;;QAChC,KAAsB,IAAA,eAAA,SAAA,UAAU,CAAA,sCAAA,8DAAE;YAA7B,IAAI,SAAS,uBAAA;YAChB,QAAQ,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE;gBAC5B,KAAK,aAAa;oBAChB,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC;oBACvB,OAAO,CAAC,UAAU,GAAG,KAAK,CAAC;oBAC3B,MAAM;gBACR,KAAK,OAAO;oBACV,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC;oBACvB,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;oBAC1B,MAAM;gBACR,KAAK,aAAa;oBAChB,OAAO,CAAC,UAAU,GAAG,KAAK,CAAC;oBAC3B,MAAM;aACT;SACF;;;;;;;;;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AArBW,QAAA,sBAAsB,0BAqBjC"}
package/lib/utils.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.capitalize = void 0;
4
+ var capitalize = function (str) { return str.charAt(0).toUpperCase() + str.slice(1); };
5
+ exports.capitalize = capitalize;
6
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAO,IAAM,UAAU,GAAG,UAAC,GAAW,IAAK,OAAA,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAA1C,CAA0C,CAAC;AAAzE,QAAA,UAAU,cAA+D"}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.getVariablesFields = void 0;
15
+ var input_1 = require("./input");
16
+ var enums_1 = require("./enums");
17
+ var language_1 = require("graphql/language");
18
+ var getVariablesFields = function (definition, schema) {
19
+ return definition.variableDefinitions.map(function (variable) { return (__assign(__assign({ name: variable.variable.name.value, alias: variable.variable.name.value, union: [] }, getVariableType(variable.type, schema)), { fields: [] })); });
20
+ };
21
+ exports.getVariablesFields = getVariablesFields;
22
+ var getVariableType = function (type, schema, isList, isNullable) {
23
+ if (isList === void 0) { isList = false; }
24
+ if (isNullable === void 0) { isNullable = true; }
25
+ if (type.kind === language_1.Kind.LIST_TYPE) {
26
+ isList = true;
27
+ return getVariableType(type.type, schema, isList, isNullable);
28
+ }
29
+ if (type.kind === language_1.Kind.NON_NULL_TYPE) {
30
+ isNullable = false;
31
+ return getVariableType(type.type, schema, isList, isNullable);
32
+ }
33
+ var typeName = type.name.value;
34
+ var isScalar = !(0, input_1.findInputInSchema)(typeName, schema) && !(0, enums_1.findEnumInSchema)(typeName, schema);
35
+ return {
36
+ isList: isList,
37
+ isNullable: isNullable,
38
+ typeName: typeName,
39
+ isScalar: isScalar,
40
+ inLine: !isScalar,
41
+ gqlType: isScalar ? 'scalar' : 'input',
42
+ };
43
+ };
44
+ //# sourceMappingURL=variables.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"variables.js","sourceRoot":"","sources":["../src/variables.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iCAA4C;AAC5C,iCAA2C;AAG3C,6CAAwC;AAGjC,IAAM,kBAAkB,GAAG,UAAC,UAAmC,EAAE,MAAqB;IAC3F,OAAO,UAAU,CAAC,mBAAmB,CAAC,GAAG,CAAC,UAAA,QAAQ,IAAI,OAAA,qBACpD,IAAI,EAAE,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EAClC,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EACnC,KAAK,EAAE,EAAE,IACN,eAAe,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,KACzC,MAAM,EAAE,EAAE,IACV,EANoD,CAMpD,CAAC,CAAC;AACN,CAAC,CAAC;AARW,QAAA,kBAAkB,sBAQ7B;AAEF,IAAM,eAAe,GAAG,UACtB,IAAc,EACd,MAAqB,EACrB,MAAc,EACd,UAAiB;IADjB,uBAAA,EAAA,cAAc;IACd,2BAAA,EAAA,iBAAiB;IAEjB,IAAI,IAAI,CAAC,IAAI,KAAK,eAAI,CAAC,SAAS,EAAE;QAChC,MAAM,GAAG,IAAI,CAAC;QACd,OAAO,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;KAC/D;IACD,IAAI,IAAI,CAAC,IAAI,KAAK,eAAI,CAAC,aAAa,EAAE;QACpC,UAAU,GAAG,KAAK,CAAC;QACnB,OAAO,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;KAC/D;IACD,IAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;IACjC,IAAM,QAAQ,GAAG,CAAC,IAAA,yBAAiB,EAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,IAAA,wBAAgB,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC7F,OAAO;QACL,MAAM,QAAA;QACN,UAAU,YAAA;QACV,QAAQ,UAAA;QACR,QAAQ,UAAA;QACR,MAAM,EAAE,CAAC,QAAQ;QACjB,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO;KACvC,CAAC;AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "@devticon-os/graphql-codegen-axios",
3
- "version": "0.3.0-test",
3
+ "version": "0.3.1",
4
4
  "license": "MIT",
5
- "main": "src/index.ts",
5
+ "main": "lib/index.js",
6
6
  "repository": "https://github.com/devticon/graphql-codegen-axios",
7
7
  "author": {
8
8
  "name": "krs",
9
9
  "email": "k.kamasa@gmail.com"
10
10
  },
11
11
  "files": [
12
- "src"
12
+ "lib",
13
+ "templates"
13
14
  ],
14
15
  "scripts": {
15
16
  "start": "node src/index.ts",
@@ -0,0 +1,95 @@
1
+ import { AxiosResponse, AxiosInstance, AxiosRequestConfig } from 'axios';
2
+ import { GraphQLError } from 'graphql';
3
+
4
+ type Nullable<T> = T | undefined;
5
+
6
+ type GraphqlResponse<T> = {
7
+ data: T;
8
+ errors?: GraphQLError[];
9
+ };
10
+
11
+ type GraphqlRequestParams = {
12
+ query: string;
13
+ variables?: any;
14
+ };
15
+ const first = (key: string) => (data: any) => {
16
+ data[key] = data[key][0];
17
+ return data;
18
+ };
19
+
20
+ const firstOrFail = (key: string) => (data: any) => {
21
+ data[key] = (data as any)[key][0];
22
+ if (!data[key]) {
23
+ throw {
24
+ message: `Empty list for ${key} `,
25
+ code: 'EMPTY_LIST',
26
+ };
27
+ }
28
+ return data;
29
+ };
30
+
31
+ const required = (path: string) => (data: any) => {
32
+ const p = path.split('.');
33
+ let d: any = data;
34
+ while (p.length) {
35
+ const k = p.shift();
36
+ const f = d[k!];
37
+ if (Array.isArray(f)) {
38
+ for (let i of f) {
39
+ required(p.join('.'))(i);
40
+ }
41
+ } else if (p.length > 0) {
42
+ d = f;
43
+ } else if (!f) {
44
+ throw {
45
+ message: `missing required field: ${path}`,
46
+ code: 'MISSING_FIELD',
47
+ };
48
+ }
49
+ }
50
+ return data;
51
+ };
52
+
53
+ const execute = (
54
+ client: AxiosInstance,
55
+ body: GraphqlRequestParams,
56
+ functions: ((data: any) => any)[],
57
+ config?: AxiosRequestConfig,
58
+ ) =>
59
+ client.post('', body, config).then(({ data }: AxiosResponse<GraphqlResponse<any>>) => {
60
+ const errors = data.errors;
61
+ if (errors && errors.length > 0) {
62
+ throw new GraphqlError('Request failed', errors);
63
+ }
64
+ let d = data.data;
65
+ for (let func of functions) {
66
+ try {
67
+ d = func(d);
68
+ } catch (e: any) {
69
+ if (e.code) {
70
+ throw new QueryError(e.message, e.code, body);
71
+ } else {
72
+ throw e;
73
+ }
74
+ }
75
+ }
76
+ return d;
77
+ });
78
+
79
+ export const singleResult = (key: string) => (data: any) => data[key];
80
+
81
+ export class GraphqlError extends Error {
82
+ constructor(message: string, public gqlErrors: GraphQLError[]) {
83
+ super(`${message} ${gqlErrors.map(e => e.message).join('\n')}`);
84
+ }
85
+ }
86
+
87
+ export class QueryError extends Error {
88
+ query: string;
89
+ variables: any;
90
+ constructor(message: string, code: string, params: GraphqlRequestParams) {
91
+ super(message);
92
+ this.query = params.query;
93
+ this.variables = params.variables;
94
+ }
95
+ }
package/src/_types.ts DELETED
@@ -1,60 +0,0 @@
1
- import { DocumentNode, OperationDefinitionNode } from 'graphql/language/ast';
2
- import { GraphQLSchema } from 'graphql/type';
3
-
4
- export type TsType = {
5
- fields: TsTypeField[];
6
- unions?: string[];
7
- };
8
-
9
- export type NamedTsType = TsType & {
10
- name: string;
11
- };
12
- export type TsTypeFieldBase = {
13
- name: string;
14
- isList: boolean;
15
- isNullable: boolean;
16
- };
17
- export type TsTypeFieldInLine = TsTypeFieldBase & { kind: 'inLine'; type: string };
18
- export type TsTypeFieldObject = TsTypeFieldBase & { kind: 'object' } & TsType;
19
- export type TsTypeField = TsTypeFieldInLine | TsTypeFieldObject;
20
- export type Query = {
21
- name: string;
22
- ast: OperationDefinitionNode;
23
- allFragments: TsType[];
24
- };
25
- export type Config = {
26
- autoSingleResult?: boolean;
27
- prettier?: boolean;
28
- emitDirectives?: string | boolean;
29
- emitSchema?: string | boolean;
30
-
31
- suffix?: {
32
- fragment?: string;
33
- input?: string;
34
- enum?: string;
35
- };
36
- scalars?: Record<string, string>;
37
- };
38
- export type CodegenDocuments = { document: DocumentNode }[];
39
- export type CodegenPlugin = {
40
- plugin: (schema: GraphQLSchema, documents: CodegenDocuments, config: Config) => string | Promise<string>;
41
- addToSchema?: string;
42
- };
43
-
44
- export type Operation = {
45
- name: string;
46
- definition: OperationDefinitionNode;
47
- singleResultKey?: string;
48
- variables?: NamedTsType;
49
- results: NamedTsType;
50
- fragments: string[];
51
- directives: Directive[];
52
- };
53
-
54
- export type Directive = {
55
- name: string;
56
- path: string;
57
- args: Record<string, string>;
58
- };
59
-
60
- export type FieldsMap = Record<string, TsTypeFieldBase & { fields: FieldsMap }>;
package/src/enums.ts DELETED
@@ -1,50 +0,0 @@
1
- import {
2
- assertEnumType,
3
- assertObjectType,
4
- GraphQLEnumType,
5
- GraphQLInputObjectType,
6
- GraphQLSchema,
7
- isEnumType,
8
- } from 'graphql/type';
9
- import { getNamedType } from 'graphql/type/definition';
10
- import { FragmentDefinitionNode } from 'graphql/language/ast';
11
- import { Kind } from 'graphql/language';
12
- import { Operation } from './_types';
13
-
14
- export const findUsageEnums = (
15
- inputs: GraphQLInputObjectType[],
16
- fragments: FragmentDefinitionNode[],
17
- operations: Operation[],
18
- schema: GraphQLSchema,
19
- ): GraphQLEnumType[] => {
20
- const enums = new Set<GraphQLEnumType>();
21
- for (let input of inputs) {
22
- for (let field of Object.values(input.getFields())) {
23
- if (isEnumType(getNamedType(field.type))) {
24
- enums.add(assertEnumType(getNamedType(field.type)));
25
- }
26
- }
27
- }
28
- for (let fragment of fragments) {
29
- const parent = assertObjectType(schema.getType(fragment.typeCondition.name.value));
30
- for (let selection of fragment.selectionSet.selections) {
31
- if (selection.kind === Kind.FIELD) {
32
- const parentField = parent.getFields()[selection.name.value];
33
- if (isEnumType(getNamedType(parentField.type))) {
34
- enums.add(assertEnumType(getNamedType(parentField.type)));
35
- }
36
- }
37
- }
38
- }
39
- for (let operation of operations) {
40
- for (let field of operation.variables?.fields || []) {
41
- if (field.kind === 'inLine') {
42
- const type = schema.getType(field.type);
43
- if (isEnumType(getNamedType(type))) {
44
- enums.add(assertEnumType(getNamedType(type)));
45
- }
46
- }
47
- }
48
- }
49
- return [...enums];
50
- };
package/src/fragments.ts DELETED
@@ -1,15 +0,0 @@
1
- import { FragmentDefinitionNode } from 'graphql/language/ast';
2
- import { CodegenDocuments } from './_types';
3
- import { Kind } from 'graphql/language';
4
-
5
- export const findUsageFragments = (documents: CodegenDocuments) => {
6
- const fragments: FragmentDefinitionNode[] = [];
7
- for (let { document } of documents) {
8
- for (const definition of document.definitions) {
9
- if (definition.kind === Kind.FRAGMENT_DEFINITION) {
10
- fragments.push(definition);
11
- }
12
- }
13
- }
14
- return fragments;
15
- };
package/src/graphql.ts DELETED
@@ -1,101 +0,0 @@
1
- import { SelectionNode, TypeNode } from 'graphql/language/ast';
2
- import { Config, TsType } from './_types';
3
- import { getNamedType, GraphQLType } from 'graphql/type/definition';
4
- import {
5
- assertObjectType,
6
- getNullableType,
7
- GraphQLList,
8
- GraphQLNonNull,
9
- GraphQLObjectType,
10
- GraphQLScalarType,
11
- GraphQLSchema,
12
- isListType,
13
- isNonNullType,
14
- isNullableType,
15
- } from 'graphql/type';
16
- import { Kind } from 'graphql/language';
17
-
18
- export const selectionSetToTsType = (
19
- parent: GraphQLObjectType,
20
- selections: SelectionNode[] | readonly SelectionNode[],
21
- config: Config,
22
- ): TsType => {
23
- const type: TsType = {
24
- fields: [],
25
- unions: [],
26
- };
27
- for (let selection of selections) {
28
- if (selection.kind === Kind.FRAGMENT_SPREAD) {
29
- type.unions.push(selection.name.value);
30
- }
31
-
32
- if (selection.kind === Kind.FIELD) {
33
- const parentField = parent.getFields()[selection.name.value];
34
- if (selection.selectionSet && selection.selectionSet.selections.length) {
35
- const nestedParent = assertObjectType(getNamedType(parentField.type));
36
- type.fields.push({
37
- kind: 'object',
38
- name: selection.alias?.value || selection.name.value,
39
- ...selectionSetToTsType(nestedParent, selection.selectionSet.selections, config),
40
- ...getGraphqlTypeWrappers(parentField.type),
41
- });
42
- } else {
43
- type.fields.push({
44
- kind: 'inLine',
45
- name: selection.alias?.value || selection.name.value,
46
- type: graphqlTypeToTypescript(parentField.type, config),
47
- ...getGraphqlTypeWrappers(parentField.type),
48
- });
49
- }
50
- }
51
- }
52
- return type;
53
- };
54
-
55
- export const getGraphqlTypeWrappers = (type: GraphQLType) => {
56
- return {
57
- isList: isListType(isNonNullType(type) ? getNullableType(type) : type),
58
- isNullable: isNullableType(type),
59
- };
60
- };
61
-
62
- export const graphqlTypeToTypescript = (type: GraphQLType, config: Config) => {
63
- const namedType = getNamedType(type);
64
-
65
- if (namedType instanceof GraphQLScalarType) {
66
- return `Scalar["${namedType.name}"]`;
67
- }
68
-
69
- switch (namedType.astNode.kind) {
70
- case Kind.ENUM_TYPE_DEFINITION:
71
- return namedType.name + (config?.suffix?.enum || '');
72
- case Kind.INPUT_OBJECT_TYPE_DEFINITION:
73
- return namedType.name + (config?.suffix?.input || '');
74
- default:
75
- throw new Error(`unsupported kind: ${namedType.astNode.kind}`);
76
- }
77
- };
78
-
79
- const typeNodeToGraphqlType = (
80
- type: TypeNode,
81
- schema: GraphQLSchema,
82
- isNullable = true,
83
- isList = false,
84
- ): GraphQLType => {
85
- if (type.kind === Kind.NON_NULL_TYPE) {
86
- return typeNodeToGraphqlType(type.type, schema, false, isList);
87
- }
88
- if (type.kind === Kind.LIST_TYPE) {
89
- return typeNodeToGraphqlType(type.type, schema, isNullable, true);
90
- }
91
-
92
- let gqlType: GraphQLType = schema.getType(type.name.value);
93
- if (isList) {
94
- gqlType = new GraphQLList(gqlType);
95
- }
96
- if (!isNullable) {
97
- gqlType = new GraphQLNonNull(gqlType);
98
- }
99
-
100
- return gqlType;
101
- };
package/src/index.ts DELETED
@@ -1,70 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as path from 'path';
3
- import { findUsageInputs } from './input';
4
- import { CodegenPlugin, Config } from './_types';
5
- import {
6
- printCreateSdkFunction,
7
- printEnum,
8
- printFragmentGql,
9
- printFragmentType,
10
- printHelpers,
11
- printInput,
12
- printOperationTypes,
13
- printScalars,
14
- } from './print';
15
- import { findUsageEnums } from './enums';
16
- import { findUsageFragments } from './fragments';
17
- import { findUsageOperation, pluginDirectives } from './operation';
18
- import { findScalars } from './scalar';
19
- import { runPrettierIfExists } from './prettier';
20
- import { printSchema } from 'graphql/utilities';
21
-
22
- const configDefaults: Partial<Config> = {
23
- autoSingleResult: true,
24
- prettier: true,
25
- };
26
- const directives = pluginDirectives.map(d => `directive @${d} on FIELD`).join('\n');
27
- const plugin: CodegenPlugin = {
28
- plugin(schema, documents, config) {
29
- try {
30
- config = {
31
- ...configDefaults,
32
- ...config,
33
- };
34
- if (config.emitDirectives) {
35
- const directivesPath = typeof config.emitDirectives === 'string' ? config.emitDirectives : 'directives.graphql';
36
- fs.writeFileSync(path.join(directivesPath), directives);
37
- }
38
- if (config.emitSchema) {
39
- const schemaPath = typeof config.emitSchema === 'string' ? config.emitSchema : 'schema.graphql';
40
- fs.writeFileSync(path.join(schemaPath), printSchema(schema));
41
- }
42
-
43
- const imports = findUsageInputs(documents, schema);
44
- const fragments = findUsageFragments(documents);
45
- const operations = findUsageOperation(documents, schema, config);
46
- const enums = findUsageEnums(imports, fragments, operations, schema);
47
- const scalars = findScalars(schema);
48
-
49
- return runPrettierIfExists(
50
- config,
51
- [
52
- printHelpers(),
53
- printScalars(scalars, config),
54
- ...enums.map(e => printEnum(e, config)),
55
- ...imports.map(i => printInput(i, config)),
56
- ...fragments.map(f => printFragmentType(f, schema, config)),
57
- ...fragments.map(f => printFragmentGql(f)),
58
- ...operations.map(o => printOperationTypes(o, config)),
59
- printCreateSdkFunction(operations, config),
60
- ].join('\n'),
61
- );
62
- } catch (e) {
63
- console.log(e);
64
- process.exit(1);
65
- }
66
- },
67
- addToSchema: directives,
68
- };
69
-
70
- module.exports = plugin;
package/src/input.ts DELETED
@@ -1,65 +0,0 @@
1
- import { GraphQLInputObjectType, GraphQLList, GraphQLNonNull, GraphQLSchema } from 'graphql/type';
2
- import { getNamedType, GraphQLEnumType, GraphQLInputType, GraphQLScalarType } from 'graphql/type/definition';
3
- import { CodegenDocuments, TsType } from './_types';
4
- import { NamedTypeNode, TypeNode } from 'graphql/language/ast';
5
- import { Kind } from 'graphql/language';
6
-
7
- export const findUsageInputs = (documents: CodegenDocuments, schema: GraphQLSchema) => {
8
- const inputs = new Set<GraphQLInputObjectType>();
9
- for (let { document } of documents) {
10
- for (let definition of document.definitions) {
11
- if (definition.kind === Kind.OPERATION_DEFINITION) {
12
- for (const variableDefinition of definition.variableDefinitions) {
13
- const type = unpackVariableType(variableDefinition.type);
14
- const name = type.name.value;
15
- const input = findInputInSchema(name, schema);
16
- if (input) {
17
- inputs.add(input);
18
- findDeepInputs(input, inputs);
19
- }
20
- }
21
- }
22
- }
23
- }
24
-
25
- return [...inputs];
26
- };
27
-
28
- const findDeepInputs = (parent: GraphQLInputObjectType, imports = new Set<GraphQLInputObjectType>()) => {
29
- for (let field of Object.values(parent.getFields())) {
30
- const fieldType = getNamedType(field.type);
31
- if (fieldType instanceof GraphQLInputObjectType) {
32
- imports.add(fieldType);
33
- findDeepInputs(fieldType, imports);
34
- }
35
- }
36
- return imports;
37
- };
38
-
39
- export const findInputInSchema = (name: string, schema: GraphQLSchema) => {
40
- const type = schema.getType(name);
41
- if (type instanceof GraphQLInputObjectType) {
42
- return type;
43
- }
44
- };
45
-
46
- const unpackInputType = (type: GraphQLInputType): GraphQLScalarType | GraphQLEnumType | GraphQLInputObjectType => {
47
- if (type instanceof GraphQLNonNull) {
48
- return unpackInputType(type.ofType);
49
- }
50
- if (type instanceof GraphQLList) {
51
- return unpackInputType(type.ofType);
52
- }
53
- return type;
54
- };
55
-
56
- const unpackVariableType = (type: TypeNode): NamedTypeNode => {
57
- if (type.kind === Kind.NON_NULL_TYPE) {
58
- return unpackVariableType(type.type);
59
- }
60
- if (type.kind === Kind.LIST_TYPE) {
61
- return unpackVariableType(type.type);
62
- }
63
-
64
- return type;
65
- };