@graphql-tools/utils 10.0.0 → 10.0.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.
package/cjs/Interfaces.js CHANGED
@@ -28,4 +28,4 @@ var MapperKind;
28
28
  MapperKind["INPUT_OBJECT_FIELD"] = "MapperKind.INPUT_OBJECT_FIELD";
29
29
  MapperKind["ARGUMENT"] = "MapperKind.ARGUMENT";
30
30
  MapperKind["ENUM_VALUE"] = "MapperKind.ENUM_VALUE";
31
- })(MapperKind = exports.MapperKind || (exports.MapperKind = {}));
31
+ })(MapperKind || (exports.MapperKind = MapperKind = {}));
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.astFromValue = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const jsutils_js_1 = require("./jsutils.js");
6
+ const inspect_js_1 = require("./inspect.js");
7
+ const astFromValueUntyped_js_1 = require("./astFromValueUntyped.js");
8
+ /**
9
+ * Produces a GraphQL Value AST given a JavaScript object.
10
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
11
+ * by using suggested GraphQLInputType. For example:
12
+ *
13
+ * astFromValue("value", GraphQLString)
14
+ *
15
+ * A GraphQL type must be provided, which will be used to interpret different
16
+ * JavaScript values.
17
+ *
18
+ * | JSON Value | GraphQL Value |
19
+ * | ------------- | -------------------- |
20
+ * | Object | Input Object |
21
+ * | Array | List |
22
+ * | Boolean | Boolean |
23
+ * | String | String / Enum Value |
24
+ * | Number | Int / Float |
25
+ * | BigInt | Int |
26
+ * | Unknown | Enum Value |
27
+ * | null | NullValue |
28
+ *
29
+ */
30
+ function astFromValue(value, type) {
31
+ if ((0, graphql_1.isNonNullType)(type)) {
32
+ const astValue = astFromValue(value, type.ofType);
33
+ if (astValue?.kind === graphql_1.Kind.NULL) {
34
+ return null;
35
+ }
36
+ return astValue;
37
+ }
38
+ // only explicit null, not undefined, NaN
39
+ if (value === null) {
40
+ return { kind: graphql_1.Kind.NULL };
41
+ }
42
+ // undefined
43
+ if (value === undefined) {
44
+ return null;
45
+ }
46
+ // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
47
+ // the value is not an array, convert the value using the list's item type.
48
+ if ((0, graphql_1.isListType)(type)) {
49
+ const itemType = type.ofType;
50
+ if ((0, jsutils_js_1.isIterableObject)(value)) {
51
+ const valuesNodes = [];
52
+ for (const item of value) {
53
+ const itemNode = astFromValue(item, itemType);
54
+ if (itemNode != null) {
55
+ valuesNodes.push(itemNode);
56
+ }
57
+ }
58
+ return { kind: graphql_1.Kind.LIST, values: valuesNodes };
59
+ }
60
+ return astFromValue(value, itemType);
61
+ }
62
+ // Populate the fields of the input object by creating ASTs from each value
63
+ // in the JavaScript object according to the fields in the input type.
64
+ if ((0, graphql_1.isInputObjectType)(type)) {
65
+ if (!(0, jsutils_js_1.isObjectLike)(value)) {
66
+ return null;
67
+ }
68
+ const fieldNodes = [];
69
+ for (const field of Object.values(type.getFields())) {
70
+ const fieldValue = astFromValue(value[field.name], field.type);
71
+ if (fieldValue) {
72
+ fieldNodes.push({
73
+ kind: graphql_1.Kind.OBJECT_FIELD,
74
+ name: { kind: graphql_1.Kind.NAME, value: field.name },
75
+ value: fieldValue,
76
+ });
77
+ }
78
+ }
79
+ return { kind: graphql_1.Kind.OBJECT, fields: fieldNodes };
80
+ }
81
+ if ((0, graphql_1.isLeafType)(type)) {
82
+ // Since value is an internally represented value, it must be serialized
83
+ // to an externally represented value before converting into an AST.
84
+ const serialized = type.serialize(value);
85
+ if (serialized == null) {
86
+ return null;
87
+ }
88
+ if ((0, graphql_1.isEnumType)(type)) {
89
+ return { kind: graphql_1.Kind.ENUM, value: serialized };
90
+ }
91
+ // ID types can use Int literals.
92
+ if (type.name === 'ID' && typeof serialized === 'string' && integerStringRegExp.test(serialized)) {
93
+ return { kind: graphql_1.Kind.INT, value: serialized };
94
+ }
95
+ return (0, astFromValueUntyped_js_1.astFromValueUntyped)(serialized);
96
+ }
97
+ /* c8 ignore next 3 */
98
+ // Not reachable, all possible types have been considered.
99
+ console.assert(false, 'Unexpected input type: ' + (0, inspect_js_1.inspect)(type));
100
+ }
101
+ exports.astFromValue = astFromValue;
102
+ /**
103
+ * IntValue:
104
+ * - NegativeSign? 0
105
+ * - NegativeSign? NonZeroDigit ( Digit+ )?
106
+ */
107
+ const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
@@ -14,6 +14,7 @@ const graphql_1 = require("graphql");
14
14
  * | Boolean | Boolean |
15
15
  * | String | String |
16
16
  * | Number | Int / Float |
17
+ * | BigInt | Int |
17
18
  * | null | NullValue |
18
19
  *
19
20
  */
@@ -57,6 +58,9 @@ function astFromValueUntyped(value) {
57
58
  if (typeof value === 'boolean') {
58
59
  return { kind: graphql_1.Kind.BOOLEAN, value };
59
60
  }
61
+ if (typeof value === 'bigint') {
62
+ return { kind: graphql_1.Kind.INT, value: String(value) };
63
+ }
60
64
  // JavaScript numbers can be Int or Float values.
61
65
  if (typeof value === 'number' && isFinite(value)) {
62
66
  const stringNum = String(value);
@@ -7,6 +7,7 @@ const get_directives_js_1 = require("./get-directives.js");
7
7
  const astFromValueUntyped_js_1 = require("./astFromValueUntyped.js");
8
8
  const helpers_js_1 = require("./helpers.js");
9
9
  const rootTypes_js_1 = require("./rootTypes.js");
10
+ const astFromValue_js_1 = require("./astFromValue.js");
10
11
  function getDocumentNodeFromSchema(schema, options = {}) {
11
12
  const pathToDirectivesInExtensions = options.pathToDirectivesInExtensions;
12
13
  const typesMap = schema.getTypeMap();
@@ -215,7 +216,7 @@ function astFromArg(arg, schema, pathToDirectivesInExtensions) {
215
216
  },
216
217
  type: (0, astFromType_js_1.astFromType)(arg.type),
217
218
  // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
218
- defaultValue: arg.defaultValue !== undefined ? (0, graphql_1.astFromValue)(arg.defaultValue, arg.type) ?? undefined : undefined,
219
+ defaultValue: arg.defaultValue !== undefined ? (0, astFromValue_js_1.astFromValue)(arg.defaultValue, arg.type) ?? undefined : undefined,
219
220
  directives: getDeprecatableDirectiveNodes(arg, schema, pathToDirectivesInExtensions),
220
221
  };
221
222
  }
@@ -399,7 +400,7 @@ function astFromInputField(field, schema, pathToDirectivesInExtensions) {
399
400
  type: (0, astFromType_js_1.astFromType)(field.type),
400
401
  // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
401
402
  directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
402
- defaultValue: (0, graphql_1.astFromValue)(field.defaultValue, field.type) ?? undefined,
403
+ defaultValue: (0, astFromValue_js_1.astFromValue)(field.defaultValue, field.type) ?? undefined,
403
404
  };
404
405
  }
405
406
  exports.astFromInputField = astFromInputField;
@@ -434,7 +435,7 @@ function makeDirectiveNode(name, args, directive) {
434
435
  const argName = arg.name;
435
436
  const argValue = args[argName];
436
437
  if (argValue !== undefined) {
437
- const value = (0, graphql_1.astFromValue)(argValue, arg.type);
438
+ const value = (0, astFromValue_js_1.astFromValue)(argValue, arg.type);
438
439
  if (value) {
439
440
  directiveArguments.push({
440
441
  kind: graphql_1.Kind.ARGUMENT,
package/cjs/types.js CHANGED
@@ -24,4 +24,4 @@ var DirectiveLocation;
24
24
  DirectiveLocation["ENUM_VALUE"] = "ENUM_VALUE";
25
25
  DirectiveLocation["INPUT_OBJECT"] = "INPUT_OBJECT";
26
26
  DirectiveLocation["INPUT_FIELD_DEFINITION"] = "INPUT_FIELD_DEFINITION";
27
- })(DirectiveLocation = exports.DirectiveLocation || (exports.DirectiveLocation = {}));
27
+ })(DirectiveLocation || (exports.DirectiveLocation = DirectiveLocation = {}));
@@ -0,0 +1,103 @@
1
+ import { Kind, isEnumType, isInputObjectType, isLeafType, isListType, isNonNullType, } from 'graphql';
2
+ import { isIterableObject, isObjectLike } from './jsutils.js';
3
+ import { inspect } from './inspect.js';
4
+ import { astFromValueUntyped } from './astFromValueUntyped.js';
5
+ /**
6
+ * Produces a GraphQL Value AST given a JavaScript object.
7
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
8
+ * by using suggested GraphQLInputType. For example:
9
+ *
10
+ * astFromValue("value", GraphQLString)
11
+ *
12
+ * A GraphQL type must be provided, which will be used to interpret different
13
+ * JavaScript values.
14
+ *
15
+ * | JSON Value | GraphQL Value |
16
+ * | ------------- | -------------------- |
17
+ * | Object | Input Object |
18
+ * | Array | List |
19
+ * | Boolean | Boolean |
20
+ * | String | String / Enum Value |
21
+ * | Number | Int / Float |
22
+ * | BigInt | Int |
23
+ * | Unknown | Enum Value |
24
+ * | null | NullValue |
25
+ *
26
+ */
27
+ export function astFromValue(value, type) {
28
+ if (isNonNullType(type)) {
29
+ const astValue = astFromValue(value, type.ofType);
30
+ if (astValue?.kind === Kind.NULL) {
31
+ return null;
32
+ }
33
+ return astValue;
34
+ }
35
+ // only explicit null, not undefined, NaN
36
+ if (value === null) {
37
+ return { kind: Kind.NULL };
38
+ }
39
+ // undefined
40
+ if (value === undefined) {
41
+ return null;
42
+ }
43
+ // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
44
+ // the value is not an array, convert the value using the list's item type.
45
+ if (isListType(type)) {
46
+ const itemType = type.ofType;
47
+ if (isIterableObject(value)) {
48
+ const valuesNodes = [];
49
+ for (const item of value) {
50
+ const itemNode = astFromValue(item, itemType);
51
+ if (itemNode != null) {
52
+ valuesNodes.push(itemNode);
53
+ }
54
+ }
55
+ return { kind: Kind.LIST, values: valuesNodes };
56
+ }
57
+ return astFromValue(value, itemType);
58
+ }
59
+ // Populate the fields of the input object by creating ASTs from each value
60
+ // in the JavaScript object according to the fields in the input type.
61
+ if (isInputObjectType(type)) {
62
+ if (!isObjectLike(value)) {
63
+ return null;
64
+ }
65
+ const fieldNodes = [];
66
+ for (const field of Object.values(type.getFields())) {
67
+ const fieldValue = astFromValue(value[field.name], field.type);
68
+ if (fieldValue) {
69
+ fieldNodes.push({
70
+ kind: Kind.OBJECT_FIELD,
71
+ name: { kind: Kind.NAME, value: field.name },
72
+ value: fieldValue,
73
+ });
74
+ }
75
+ }
76
+ return { kind: Kind.OBJECT, fields: fieldNodes };
77
+ }
78
+ if (isLeafType(type)) {
79
+ // Since value is an internally represented value, it must be serialized
80
+ // to an externally represented value before converting into an AST.
81
+ const serialized = type.serialize(value);
82
+ if (serialized == null) {
83
+ return null;
84
+ }
85
+ if (isEnumType(type)) {
86
+ return { kind: Kind.ENUM, value: serialized };
87
+ }
88
+ // ID types can use Int literals.
89
+ if (type.name === 'ID' && typeof serialized === 'string' && integerStringRegExp.test(serialized)) {
90
+ return { kind: Kind.INT, value: serialized };
91
+ }
92
+ return astFromValueUntyped(serialized);
93
+ }
94
+ /* c8 ignore next 3 */
95
+ // Not reachable, all possible types have been considered.
96
+ console.assert(false, 'Unexpected input type: ' + inspect(type));
97
+ }
98
+ /**
99
+ * IntValue:
100
+ * - NegativeSign? 0
101
+ * - NegativeSign? NonZeroDigit ( Digit+ )?
102
+ */
103
+ const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
@@ -11,6 +11,7 @@ import { Kind } from 'graphql';
11
11
  * | Boolean | Boolean |
12
12
  * | String | String |
13
13
  * | Number | Int / Float |
14
+ * | BigInt | Int |
14
15
  * | null | NullValue |
15
16
  *
16
17
  */
@@ -54,6 +55,9 @@ export function astFromValueUntyped(value) {
54
55
  if (typeof value === 'boolean') {
55
56
  return { kind: Kind.BOOLEAN, value };
56
57
  }
58
+ if (typeof value === 'bigint') {
59
+ return { kind: Kind.INT, value: String(value) };
60
+ }
57
61
  // JavaScript numbers can be Int or Float values.
58
62
  if (typeof value === 'number' && isFinite(value)) {
59
63
  const stringNum = String(value);
@@ -1,9 +1,10 @@
1
- import { print, Kind, isSpecifiedScalarType, isIntrospectionType, isSpecifiedDirective, astFromValue, GraphQLDeprecatedDirective, isObjectType, isInterfaceType, isUnionType, isInputObjectType, isEnumType, isScalarType, } from 'graphql';
1
+ import { print, Kind, isSpecifiedScalarType, isIntrospectionType, isSpecifiedDirective, GraphQLDeprecatedDirective, isObjectType, isInterfaceType, isUnionType, isInputObjectType, isEnumType, isScalarType, } from 'graphql';
2
2
  import { astFromType } from './astFromType.js';
3
3
  import { getDirectivesInExtensions } from './get-directives.js';
4
4
  import { astFromValueUntyped } from './astFromValueUntyped.js';
5
5
  import { isSome } from './helpers.js';
6
6
  import { getRootTypeMap } from './rootTypes.js';
7
+ import { astFromValue } from './astFromValue.js';
7
8
  export function getDocumentNodeFromSchema(schema, options = {}) {
8
9
  const pathToDirectivesInExtensions = options.pathToDirectivesInExtensions;
9
10
  const typesMap = schema.getTypeMap();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/utils",
3
- "version": "10.0.0",
3
+ "version": "10.0.1",
4
4
  "description": "Common package containing utils and types for GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -0,0 +1,25 @@
1
+ import { GraphQLInputType, ValueNode } from 'graphql';
2
+ import { Maybe } from './types.cjs';
3
+ /**
4
+ * Produces a GraphQL Value AST given a JavaScript object.
5
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
6
+ * by using suggested GraphQLInputType. For example:
7
+ *
8
+ * astFromValue("value", GraphQLString)
9
+ *
10
+ * A GraphQL type must be provided, which will be used to interpret different
11
+ * JavaScript values.
12
+ *
13
+ * | JSON Value | GraphQL Value |
14
+ * | ------------- | -------------------- |
15
+ * | Object | Input Object |
16
+ * | Array | List |
17
+ * | Boolean | Boolean |
18
+ * | String | String / Enum Value |
19
+ * | Number | Int / Float |
20
+ * | BigInt | Int |
21
+ * | Unknown | Enum Value |
22
+ * | null | NullValue |
23
+ *
24
+ */
25
+ export declare function astFromValue(value: unknown, type: GraphQLInputType): Maybe<ValueNode>;
@@ -0,0 +1,25 @@
1
+ import { GraphQLInputType, ValueNode } from 'graphql';
2
+ import { Maybe } from './types.js';
3
+ /**
4
+ * Produces a GraphQL Value AST given a JavaScript object.
5
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
6
+ * by using suggested GraphQLInputType. For example:
7
+ *
8
+ * astFromValue("value", GraphQLString)
9
+ *
10
+ * A GraphQL type must be provided, which will be used to interpret different
11
+ * JavaScript values.
12
+ *
13
+ * | JSON Value | GraphQL Value |
14
+ * | ------------- | -------------------- |
15
+ * | Object | Input Object |
16
+ * | Array | List |
17
+ * | Boolean | Boolean |
18
+ * | String | String / Enum Value |
19
+ * | Number | Int / Float |
20
+ * | BigInt | Int |
21
+ * | Unknown | Enum Value |
22
+ * | null | NullValue |
23
+ *
24
+ */
25
+ export declare function astFromValue(value: unknown, type: GraphQLInputType): Maybe<ValueNode>;
@@ -11,6 +11,7 @@ import { ValueNode } from 'graphql';
11
11
  * | Boolean | Boolean |
12
12
  * | String | String |
13
13
  * | Number | Int / Float |
14
+ * | BigInt | Int |
14
15
  * | null | NullValue |
15
16
  *
16
17
  */
@@ -11,6 +11,7 @@ import { ValueNode } from 'graphql';
11
11
  * | Boolean | Boolean |
12
12
  * | String | String |
13
13
  * | Number | Int / Float |
14
+ * | BigInt | Int |
14
15
  * | null | NullValue |
15
16
  *
16
17
  */