@devticon-os/graphql-codegen-axios 0.2.14 → 0.2.16

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devticon-os/graphql-codegen-axios",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "repository": "https://github.com/devticon/graphql-codegen-axios",
package/src/functions.js CHANGED
@@ -21,6 +21,11 @@ const getFunctionChain = (operation, useSingleResults) => {
21
21
  return chain;
22
22
  };
23
23
 
24
- const isSingleResultOperation = operation => operation.selectionSet.selections.length === 1;
24
+ const isSingleResultOperation = (operation, config) => {
25
+ if (config.autoSingleResult === undefined || config.autoSingleResult === true) {
26
+ return operation.selectionSet.selections.length === 1;
27
+ }
28
+ return operation.directives.some(d => d.name.value === 'singleResult');
29
+ };
25
30
 
26
31
  module.exports = { getFunctionChain, isSingleResultOperation };
package/src/helpers.ts CHANGED
@@ -33,7 +33,7 @@ const nonNullable = (key: string, reqParams: GraphqlRequestParams) => (data: any
33
33
  return data;
34
34
  };
35
35
 
36
- export const handleResponse = <T>({ data }: AxiosResponse<GraphqlResponse<T>>) => {
36
+ export const handleResponse = ({ data }: AxiosResponse<GraphqlResponse<any>>): any => {
37
37
  const errors = data.errors;
38
38
  if (errors && errors.length > 0) {
39
39
  throw new GraphqlError('Request failed', errors);
package/src/index.js CHANGED
@@ -2,7 +2,7 @@ const fs = require('fs');
2
2
  const path = require('path');
3
3
  const { findUsageInputs } = require('./input');
4
4
  const { getVariablesFields } = require('./variables');
5
- const { getResultsFields, getResultType } = require('./results');
5
+ const { getResultType } = require('./results');
6
6
  const { findScalars } = require('./scalar');
7
7
  const {
8
8
  renderType,
@@ -19,9 +19,14 @@ const { getFunctionChain, isSingleResultOperation } = require('./functions');
19
19
  const { capitalize } = require('./utils');
20
20
 
21
21
  const helpers = fs.readFileSync(path.join(__dirname, 'helpers.ts'), 'utf-8');
22
+ const directives = `directive @first on FIELD
23
+ directive @firstOrFail on FIELD
24
+ directive @singleResult on QUERY | MUTATION
25
+ directive @nonNullable on FIELD`;
22
26
  module.exports = {
23
27
  plugin(schema, documents, config) {
24
28
  try {
29
+ fs.writeFileSync(path.join(config.directivesFilePath || '', 'directives.graphql'), directives);
25
30
  const functions = [];
26
31
  const queries = [];
27
32
  const inputs = findUsageInputs(documents, schema);
@@ -35,7 +40,7 @@ module.exports = {
35
40
  continue;
36
41
  }
37
42
  const name = definition.name.value;
38
- const useSingleResults = isSingleResultOperation(definition);
43
+ const useSingleResults = isSingleResultOperation(definition, config);
39
44
 
40
45
  const results = getResultType(definition, schema, document, useSingleResults);
41
46
  types.push(results);
@@ -85,9 +90,5 @@ module.exports = {
85
90
  process.exit(1);
86
91
  }
87
92
  },
88
- addToSchema: /* GraphQL */ `
89
- directive @first on OBJECT
90
- directive @firstOrFail on OBJECT
91
- directive @nonNullable on OBJECT
92
- `,
93
+ addToSchema: directives,
93
94
  };
package/src/render.js CHANGED
@@ -2,6 +2,7 @@ const { print } = require('graphql/index');
2
2
  const { getUsedFragments } = require('./query');
3
3
  const { GraphQLInputObjectType, GraphQLEnumType } = require('graphql/type');
4
4
  const { get } = require('axios');
5
+ const { capitalize } = require('./utils');
5
6
 
6
7
  const getName = (name, type, config) => {
7
8
  const suffix = config.suffix ? config.suffix[type] || '' : '';
@@ -27,7 +28,7 @@ const renderType = ({ name, fields, union, isList, isNullable, gqlType }, config
27
28
  const renderHeader = text => `/** \n ${text} \n **/`;
28
29
  const renderTypeField = (fields, config) => {
29
30
  return fields
30
- .map(({ isList, isNullable, typeName, name, fields, isScalar, union, type, inLine, gqlType }) => {
31
+ .map(({ isList, isNullable, typeName, name, fields, isScalar, union, type, inLine, gqlType, alias }) => {
31
32
  let tsType = '';
32
33
  if (union && union.length) {
33
34
  tsType += [...union.map(u => getName(u, 'fragment', config)), ''].join(' & ');
@@ -40,7 +41,7 @@ const renderTypeField = (fields, config) => {
40
41
  if (type instanceof GraphQLInputObjectType || type instanceof GraphQLEnumType) {
41
42
  tsType += gqlType ? getName(typeName, gqlType, config) : typeName;
42
43
  } else {
43
- tsType += `{${renderTypeField(fields)}}`;
44
+ tsType += `{${renderTypeField(fields, config)}}`;
44
45
  }
45
46
  }
46
47
  tsType = `(${tsType})`;
@@ -50,7 +51,7 @@ const renderTypeField = (fields, config) => {
50
51
  if (isNullable) {
51
52
  tsType = `Nullable<${tsType}>`;
52
53
  }
53
- return `${name}${isNullable ? '?' : ''}: ${tsType}`;
54
+ return `${alias || name}${isNullable ? '?' : ''}: ${tsType}`;
54
55
  })
55
56
  .join(',\n');
56
57
  };
@@ -72,7 +73,11 @@ const renderFunction = ({ name, variables, results, chain }) => {
72
73
  };
73
74
 
74
75
  const renderQuery = ({ name, ast, allFragments }) => {
75
- const raw = print(ast).replace('@firstOrFail', '').replace('@first', '').replace('@nonNullable', '');
76
+ const raw = print(ast)
77
+ .replace(/@firstOrFail/g, '')
78
+ .replace(/@first/g, '')
79
+ .replace(/@nonNullable/g, '')
80
+ .replace(/@singleResult/g, '');
76
81
 
77
82
  let fragments = getUsedFragments(raw, allFragments);
78
83
  fragments = [...new Set(fragments)].map(f => `\${${f.name}FragmentQuery}`);
@@ -86,7 +91,7 @@ const getScalarTsType = name => `Scalar["${name}"]`;
86
91
  const renderEnum = (e, config) => {
87
92
  let str = `export enum ${getName(e.name, 'enum', config)} {`;
88
93
  for (let { name, value } of e.values) {
89
- str += `${name} = "${value}",`;
94
+ str += `${capitalize(name.toLowerCase())} = "${value}",`;
90
95
  }
91
96
  str += `};`;
92
97
  return str;
package/src/results.js CHANGED
@@ -36,6 +36,7 @@ const getField = (parent, field, schema, document) => {
36
36
  ...type,
37
37
  fields,
38
38
  union,
39
+ alias: field.alias?.value,
39
40
  };
40
41
  };
41
42