From 72dadfe558a18e6ac41da28f5965eb740f4f368e Mon Sep 17 00:00:00 2001 From: Saihajpreet Singh Date: Thu, 1 Sep 2022 10:59:24 -0400 Subject: [PATCH] create new graphql package (#4688) * create new graphql package * b uild * esm * skip docs for fork * chore(dependencies): updated changesets for modified dependencies * remove Co-authored-by: github-actions[bot] --- .changeset/five-gifts-lie.md | 5 + .eslintrc.json | 20 + packages/graphql/README.md | 3 + packages/graphql/package.json | 65 + .../__testUtils__/__tests__/dedent-test.ts | 102 + .../__tests__/genFuzzStrings-test.ts | 38 + .../__tests__/inspectStr-test.ts | 16 + .../__tests__/resolveOnNextTick-test.ts | 18 + packages/graphql/src/__testUtils__/dedent.ts | 38 + .../graphql/src/__testUtils__/expectJSON.ts | 51 + .../src/__testUtils__/genFuzzStrings.ts | 29 + .../graphql/src/__testUtils__/inspectStr.ts | 11 + .../src/__testUtils__/kitchenSinkQuery.ts | 83 + .../src/__testUtils__/kitchenSinkSDL.ts | 158 + .../src/__testUtils__/resolveOnNextTick.ts | 3 + .../graphql/src/__tests__/starWarsData.ts | 156 + .../__tests__/starWarsIntrospection-test.ts | 363 +++ .../src/__tests__/starWarsQuery-test.ts | 494 ++++ .../graphql/src/__tests__/starWarsSchema.ts | 306 ++ .../src/__tests__/starWarsValidation-test.ts | 116 + packages/graphql/src/error/GraphQLError.ts | 296 ++ .../src/error/__tests__/GraphQLError-test.ts | 322 ++ .../src/error/__tests__/locatedError-test.ts | 46 + packages/graphql/src/error/index.ts | 3 + packages/graphql/src/error/locatedError.ts | 36 + packages/graphql/src/error/syntaxError.ts | 14 + .../src/execution/__tests__/abstract-test.ts | 633 ++++ .../execution/__tests__/directives-test.ts | 308 ++ .../src/execution/__tests__/executor-test.ts | 1203 ++++++++ .../src/execution/__tests__/lists-test.ts | 392 +++ .../__tests__/mapAsyncIterator-test.ts | 329 +++ .../src/execution/__tests__/mutations-test.ts | 191 ++ .../src/execution/__tests__/nonnull-test.ts | 697 +++++ .../src/execution/__tests__/resolve-test.ts | 124 + .../src/execution/__tests__/schema-test.ts | 176 ++ .../execution/__tests__/simplePubSub-test.ts | 52 + .../src/execution/__tests__/simplePubSub.ts | 77 + .../src/execution/__tests__/subscribe-test.ts | 1033 +++++++ .../src/execution/__tests__/sync-test.ts | 174 ++ .../__tests__/union-interface-test.ts | 536 ++++ .../src/execution/__tests__/variables-test.ts | 1038 +++++++ .../graphql/src/execution/collectFields.ts | 186 ++ packages/graphql/src/execution/execute.ts | 1130 +++++++ packages/graphql/src/execution/index.ts | 5 + .../graphql/src/execution/mapAsyncIterator.ts | 55 + packages/graphql/src/execution/subscribe.ts | 225 ++ packages/graphql/src/execution/values.ts | 227 ++ packages/graphql/src/graphql.ts | 124 + packages/graphql/src/index.ts | 9 + .../graphql/src/jsutils/AccumulatorMap.ts | 17 + packages/graphql/src/jsutils/Maybe.ts | 2 + packages/graphql/src/jsutils/ObjMap.ts | 11 + packages/graphql/src/jsutils/Path.ts | 27 + .../graphql/src/jsutils/PromiseOrValue.ts | 1 + .../jsutils/__tests__/AccumulatorMap-test.ts | 31 + .../src/jsutils/__tests__/Path-test.ts | 33 + .../src/jsutils/__tests__/capitalize-test.ts | 18 + .../src/jsutils/__tests__/didYouMean-test.ts | 27 + .../jsutils/__tests__/identityFunc-test.ts | 13 + .../src/jsutils/__tests__/inspect-test.ts | 173 ++ .../src/jsutils/__tests__/invariant-test.ts | 11 + .../jsutils/__tests__/isAsyncIterable-test.ts | 49 + .../__tests__/isIterableObject-test.ts | 67 + .../jsutils/__tests__/isObjectLike-test.ts | 19 + .../jsutils/__tests__/naturalCompare-test.ts | 69 + .../jsutils/__tests__/suggestionList-test.ts | 52 + .../src/jsutils/__tests__/toObjMap-test.ts | 58 + packages/graphql/src/jsutils/capitalize.ts | 6 + packages/graphql/src/jsutils/devAssert.ts | 5 + packages/graphql/src/jsutils/didYouMean.ts | 26 + packages/graphql/src/jsutils/formatList.ts | 30 + packages/graphql/src/jsutils/groupBy.ts | 12 + packages/graphql/src/jsutils/identityFunc.ts | 6 + packages/graphql/src/jsutils/inspect.ts | 107 + packages/graphql/src/jsutils/invariant.ts | 5 + .../graphql/src/jsutils/isAsyncIterable.ts | 7 + .../graphql/src/jsutils/isIterableObject.ts | 20 + packages/graphql/src/jsutils/isObjectLike.ts | 7 + packages/graphql/src/jsutils/isPromise.ts | 7 + packages/graphql/src/jsutils/keyMap.ts | 36 + packages/graphql/src/jsutils/keyValMap.ts | 26 + packages/graphql/src/jsutils/mapValue.ts | 14 + packages/graphql/src/jsutils/memoize1.ts | 16 + packages/graphql/src/jsutils/memoize3.ts | 34 + .../graphql/src/jsutils/naturalCompare.ts | 58 + .../graphql/src/jsutils/printPathArray.ts | 6 + .../graphql/src/jsutils/promiseForObject.ts | 18 + packages/graphql/src/jsutils/promiseReduce.ts | 23 + .../graphql/src/jsutils/suggestionList.ts | 134 + packages/graphql/src/jsutils/toError.ts | 18 + packages/graphql/src/jsutils/toObjMap.ts | 18 + .../language/__tests__/blockString-fuzz.ts | 45 + .../language/__tests__/blockString-test.ts | 208 ++ .../src/language/__tests__/lexer-test.ts | 1170 ++++++++ .../src/language/__tests__/parser-test.ts | 781 +++++ .../src/language/__tests__/predicates-test.ts | 139 + .../language/__tests__/printLocation-test.ts | 68 + .../language/__tests__/printString-test.ts | 79 + .../src/language/__tests__/printer-test.ts | 224 ++ .../language/__tests__/schema-parser-test.ts | 1057 +++++++ .../language/__tests__/schema-printer-test.ts | 172 ++ .../src/language/__tests__/source-test.ts | 29 + .../src/language/__tests__/visitor-test.ts | 1466 +++++++++ packages/graphql/src/language/ast.ts | 739 +++++ packages/graphql/src/language/blockString.ts | 157 + .../graphql/src/language/characterClasses.ts | 64 + .../graphql/src/language/directiveLocation.ts | 33 + packages/graphql/src/language/index.ts | 15 + packages/graphql/src/language/kinds.ts | 81 + packages/graphql/src/language/lexer.ts | 788 +++++ packages/graphql/src/language/location.ts | 33 + packages/graphql/src/language/parser.ts | 1534 ++++++++++ packages/graphql/src/language/predicates.ts | 94 + .../graphql/src/language/printLocation.ts | 68 + packages/graphql/src/language/printString.ts | 38 + packages/graphql/src/language/printer.ts | 279 ++ packages/graphql/src/language/source.ts | 43 + packages/graphql/src/language/tokenKind.ts | 36 + packages/graphql/src/language/visitor.ts | 379 +++ packages/graphql/src/subscription/index.ts | 21 + .../src/type/__tests__/assertName-test.ts | 49 + .../src/type/__tests__/definition-test.ts | 672 +++++ .../src/type/__tests__/directive-test.ts | 106 + .../src/type/__tests__/enumType-test.ts | 388 +++ .../src/type/__tests__/extensions-test.ts | 386 +++ .../src/type/__tests__/introspection-test.ts | 1629 ++++++++++ .../src/type/__tests__/predicate-test.ts | 682 +++++ .../src/type/__tests__/scalars-test.ts | 427 +++ .../graphql/src/type/__tests__/schema-test.ts | 483 +++ .../src/type/__tests__/validation-test.ts | 2631 +++++++++++++++++ packages/graphql/src/type/assertName.ts | 36 + packages/graphql/src/type/definition.ts | 1543 ++++++++++ packages/graphql/src/type/directives.ts | 193 ++ packages/graphql/src/type/index.ts | 8 + packages/graphql/src/type/introspection.ts | 532 ++++ packages/graphql/src/type/scalars.ts | 245 ++ packages/graphql/src/type/schema.ts | 431 +++ packages/graphql/src/type/validate.ts | 581 ++++ packages/graphql/src/utilities/TypeInfo.ts | 324 ++ .../src/utilities/__tests__/TypeInfo-test.ts | 491 +++ .../utilities/__tests__/astFromValue-test.ts | 345 +++ .../__tests__/buildASTSchema-test.ts | 1056 +++++++ .../__tests__/buildClientSchema-test.ts | 917 ++++++ .../__tests__/coerceInputValue-test.ts | 405 +++ .../src/utilities/__tests__/concatAST-test.ts | 37 + .../utilities/__tests__/extendSchema-test.ts | 1279 ++++++++ .../__tests__/findBreakingChanges-test.ts | 1187 ++++++++ .../__tests__/getIntrospectionQuery-test.ts | 98 + .../__tests__/getOperationAST-test.ts | 65 + .../__tests__/getOperationRootType-test.ts | 145 + .../__tests__/introspectionFromSchema-test.ts | 63 + .../__tests__/lexicographicSortSchema-test.ts | 356 +++ .../utilities/__tests__/printSchema-test.ts | 859 ++++++ .../__tests__/separateOperations-test.ts | 255 ++ .../utilities/__tests__/sortValueNode-test.ts | 31 + .../__tests__/stripIgnoredCharacters-fuzz.ts | 223 ++ .../__tests__/stripIgnoredCharacters-test.ts | 225 ++ .../__tests__/typeComparators-test.ts | 136 + .../utilities/__tests__/valueFromAST-test.ts | 223 ++ .../__tests__/valueFromASTUntyped-test.ts | 56 + .../src/utilities/addResolversToSchema.ts | 125 + .../graphql/src/utilities/assertValidName.ts | 40 + .../graphql/src/utilities/astFromSchema.ts | 738 +++++ .../graphql/src/utilities/astFromValue.ts | 141 + .../graphql/src/utilities/buildASTSchema.ts | 95 + .../src/utilities/buildClientSchema.ts | 341 +++ .../graphql/src/utilities/coerceInputValue.ts | 151 + packages/graphql/src/utilities/concatAST.ts | 15 + .../graphql/src/utilities/extendSchema.ts | 629 ++++ .../src/utilities/findBreakingChanges.ts | 504 ++++ .../utilities/getDocumentNodeFromSchema.ts | 82 + .../src/utilities/getIntrospectionQuery.ts | 300 ++ .../graphql/src/utilities/getOperationAST.ts | 31 + .../src/utilities/getOperationRootType.ts | 46 + .../graphql/src/utilities/getRootTypeMap.ts | 26 + packages/graphql/src/utilities/index.ts | 28 + .../src/utilities/introspectionFromSchema.ts | 34 + .../src/utilities/lexicographicSortSchema.ts | 174 ++ packages/graphql/src/utilities/printSchema.ts | 289 ++ .../utilities/printSchemaWithDirectives.ts | 27 + .../src/utilities/separateOperations.ts | 83 + .../graphql/src/utilities/sortValueNode.ts | 43 + .../src/utilities/stripIgnoredCharacters.ts | 101 + .../graphql/src/utilities/typeComparators.ts | 107 + packages/graphql/src/utilities/typeFromAST.ts | 32 + .../src/utilities/typedQueryDocumentNode.ts | 17 + .../graphql/src/utilities/valueFromAST.ts | 150 + .../src/utilities/valueFromASTUntyped.ts | 47 + .../src/validation/ValidationContext.ts | 245 ++ .../ExecutableDefinitionsRule-test.ts | 92 + .../__tests__/FieldsOnCorrectTypeRule-test.ts | 430 +++ .../FragmentsOnCompositeTypesRule-test.ts | 121 + .../__tests__/KnownArgumentNamesRule-test.ts | 317 ++ .../__tests__/KnownDirectivesRule-test.ts | 438 +++ .../__tests__/KnownFragmentNamesRule-test.ts | 69 + .../__tests__/KnownTypeNamesRule-test.ts | 356 +++ .../LoneAnonymousOperationRule-test.ts | 104 + .../LoneSchemaDefinitionRule-test.ts | 156 + .../__tests__/NoDeprecatedCustomRule-test.ts | 261 ++ .../__tests__/NoFragmentCyclesRule-test.ts | 255 ++ .../NoSchemaIntrospectionCustomRule-test.ts | 128 + .../NoUndefinedVariablesRule-test.ts | 405 +++ .../__tests__/NoUnusedFragmentsRule-test.ts | 161 + .../__tests__/NoUnusedVariablesRule-test.ts | 231 ++ .../OverlappingFieldsCanBeMergedRule-test.ts | 1132 +++++++ .../PossibleFragmentSpreadsRule-test.ts | 294 ++ .../PossibleTypeExtensionsRule-test.ts | 267 ++ .../ProvidedRequiredArgumentsRule-test.ts | 339 +++ .../__tests__/ScalarLeafsRule-test.ts | 120 + .../SingleFieldSubscriptionsRule-test.ts | 291 ++ .../UniqueArgumentDefinitionNamesRule-test.ts | 164 + .../__tests__/UniqueArgumentNamesRule-test.ts | 152 + .../UniqueDirectiveNamesRule-test.ts | 97 + .../UniqueDirectivesPerLocationRule-test.ts | 356 +++ .../UniqueEnumValueNamesRule-test.ts | 192 ++ .../UniqueFieldDefinitionNamesRule-test.ts | 429 +++ .../__tests__/UniqueFragmentNamesRule-test.ts | 117 + .../UniqueInputFieldNamesRule-test.ts | 108 + .../UniqueOperationNamesRule-test.ts | 133 + .../UniqueOperationTypesRule-test.ts | 373 +++ .../__tests__/UniqueTypeNamesRule-test.ts | 154 + .../__tests__/UniqueVariableNamesRule-test.ts | 51 + .../__tests__/ValidationContext-test.ts | 27 + .../__tests__/ValuesOfCorrectTypeRule-test.ts | 1175 ++++++++ .../VariablesAreInputTypesRule-test.ts | 50 + .../VariablesInAllowedPositionRule-test.ts | 349 +++ .../src/validation/__tests__/harness.ts | 136 + .../validation/__tests__/validation-test.ts | 167 ++ packages/graphql/src/validation/index.ts | 4 + .../rules/ExecutableDefinitionsRule.ts | 36 + .../rules/FieldsOnCorrectTypeRule.ts | 118 + .../rules/FragmentsOnCompositeTypesRule.ts | 47 + .../rules/KnownArgumentNamesRule.ts | 91 + .../validation/rules/KnownDirectivesRule.ts | 127 + .../rules/KnownFragmentNamesRule.ts | 29 + .../validation/rules/KnownTypeNamesRule.ts | 63 + .../rules/LoneAnonymousOperationRule.ts | 30 + .../rules/LoneSchemaDefinitionRule.ts | 35 + .../validation/rules/NoFragmentCyclesRule.ts | 84 + .../rules/NoUndefinedVariablesRule.ts | 45 + .../validation/rules/NoUnusedFragmentsRule.ts | 51 + .../validation/rules/NoUnusedVariablesRule.ts | 51 + .../rules/OverlappingFieldsCanBeMergedRule.ts | 745 +++++ .../rules/PossibleFragmentSpreadsRule.ts | 70 + .../rules/PossibleTypeExtensionsRule.ts | 139 + .../rules/ProvidedRequiredArgumentsRule.ts | 113 + .../src/validation/rules/ScalarLeafsRule.ts | 48 + .../rules/SingleFieldSubscriptionsRule.ts | 71 + .../UniqueArgumentDefinitionNamesRule.ts | 69 + .../rules/UniqueArgumentNamesRule.ts | 41 + .../rules/UniqueDirectiveNamesRule.ts | 42 + .../rules/UniqueDirectivesPerLocationRule.ts | 77 + .../rules/UniqueEnumValueNamesRule.ts | 61 + .../rules/UniqueFieldDefinitionNamesRule.ts | 75 + .../rules/UniqueFragmentNamesRule.ts | 32 + .../rules/UniqueInputFieldNamesRule.ts | 48 + .../rules/UniqueOperationNamesRule.ts | 34 + .../rules/UniqueOperationTypesRule.ts | 57 + .../validation/rules/UniqueTypeNamesRule.ts | 51 + .../rules/UniqueVariableNamesRule.ts | 34 + .../rules/ValuesOfCorrectTypeRule.ts | 138 + .../rules/VariablesAreInputTypesRule.ts | 36 + .../rules/VariablesInAllowedPositionRule.ts | 90 + .../rules/custom/NoDeprecatedCustomRule.ts | 91 + .../custom/NoSchemaIntrospectionCustomRule.ts | 35 + .../src/validation/rules/custom/index.ts | 2 + .../graphql/src/validation/rules/index.ts | 35 + .../graphql/src/validation/specifiedRules.ts | 121 + packages/graphql/src/validation/validate.ts | 119 + packages/graphql/src/version.ts | 17 + scripts/build-api-docs.ts | 2 + yarn.lock | 5 + 272 files changed, 60818 insertions(+) create mode 100644 .changeset/five-gifts-lie.md create mode 100644 packages/graphql/README.md create mode 100644 packages/graphql/package.json create mode 100644 packages/graphql/src/__testUtils__/__tests__/dedent-test.ts create mode 100644 packages/graphql/src/__testUtils__/__tests__/genFuzzStrings-test.ts create mode 100644 packages/graphql/src/__testUtils__/__tests__/inspectStr-test.ts create mode 100644 packages/graphql/src/__testUtils__/__tests__/resolveOnNextTick-test.ts create mode 100644 packages/graphql/src/__testUtils__/dedent.ts create mode 100644 packages/graphql/src/__testUtils__/expectJSON.ts create mode 100644 packages/graphql/src/__testUtils__/genFuzzStrings.ts create mode 100644 packages/graphql/src/__testUtils__/inspectStr.ts create mode 100644 packages/graphql/src/__testUtils__/kitchenSinkQuery.ts create mode 100644 packages/graphql/src/__testUtils__/kitchenSinkSDL.ts create mode 100644 packages/graphql/src/__testUtils__/resolveOnNextTick.ts create mode 100644 packages/graphql/src/__tests__/starWarsData.ts create mode 100644 packages/graphql/src/__tests__/starWarsIntrospection-test.ts create mode 100644 packages/graphql/src/__tests__/starWarsQuery-test.ts create mode 100644 packages/graphql/src/__tests__/starWarsSchema.ts create mode 100644 packages/graphql/src/__tests__/starWarsValidation-test.ts create mode 100644 packages/graphql/src/error/GraphQLError.ts create mode 100644 packages/graphql/src/error/__tests__/GraphQLError-test.ts create mode 100644 packages/graphql/src/error/__tests__/locatedError-test.ts create mode 100644 packages/graphql/src/error/index.ts create mode 100644 packages/graphql/src/error/locatedError.ts create mode 100644 packages/graphql/src/error/syntaxError.ts create mode 100644 packages/graphql/src/execution/__tests__/abstract-test.ts create mode 100644 packages/graphql/src/execution/__tests__/directives-test.ts create mode 100644 packages/graphql/src/execution/__tests__/executor-test.ts create mode 100644 packages/graphql/src/execution/__tests__/lists-test.ts create mode 100644 packages/graphql/src/execution/__tests__/mapAsyncIterator-test.ts create mode 100644 packages/graphql/src/execution/__tests__/mutations-test.ts create mode 100644 packages/graphql/src/execution/__tests__/nonnull-test.ts create mode 100644 packages/graphql/src/execution/__tests__/resolve-test.ts create mode 100644 packages/graphql/src/execution/__tests__/schema-test.ts create mode 100644 packages/graphql/src/execution/__tests__/simplePubSub-test.ts create mode 100644 packages/graphql/src/execution/__tests__/simplePubSub.ts create mode 100644 packages/graphql/src/execution/__tests__/subscribe-test.ts create mode 100644 packages/graphql/src/execution/__tests__/sync-test.ts create mode 100644 packages/graphql/src/execution/__tests__/union-interface-test.ts create mode 100644 packages/graphql/src/execution/__tests__/variables-test.ts create mode 100644 packages/graphql/src/execution/collectFields.ts create mode 100644 packages/graphql/src/execution/execute.ts create mode 100644 packages/graphql/src/execution/index.ts create mode 100644 packages/graphql/src/execution/mapAsyncIterator.ts create mode 100644 packages/graphql/src/execution/subscribe.ts create mode 100644 packages/graphql/src/execution/values.ts create mode 100644 packages/graphql/src/graphql.ts create mode 100644 packages/graphql/src/index.ts create mode 100644 packages/graphql/src/jsutils/AccumulatorMap.ts create mode 100644 packages/graphql/src/jsutils/Maybe.ts create mode 100644 packages/graphql/src/jsutils/ObjMap.ts create mode 100644 packages/graphql/src/jsutils/Path.ts create mode 100644 packages/graphql/src/jsutils/PromiseOrValue.ts create mode 100644 packages/graphql/src/jsutils/__tests__/AccumulatorMap-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/Path-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/capitalize-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/didYouMean-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/identityFunc-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/inspect-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/invariant-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/isAsyncIterable-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/isIterableObject-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/isObjectLike-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/naturalCompare-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/suggestionList-test.ts create mode 100644 packages/graphql/src/jsutils/__tests__/toObjMap-test.ts create mode 100644 packages/graphql/src/jsutils/capitalize.ts create mode 100644 packages/graphql/src/jsutils/devAssert.ts create mode 100644 packages/graphql/src/jsutils/didYouMean.ts create mode 100644 packages/graphql/src/jsutils/formatList.ts create mode 100644 packages/graphql/src/jsutils/groupBy.ts create mode 100644 packages/graphql/src/jsutils/identityFunc.ts create mode 100644 packages/graphql/src/jsutils/inspect.ts create mode 100644 packages/graphql/src/jsutils/invariant.ts create mode 100644 packages/graphql/src/jsutils/isAsyncIterable.ts create mode 100644 packages/graphql/src/jsutils/isIterableObject.ts create mode 100644 packages/graphql/src/jsutils/isObjectLike.ts create mode 100644 packages/graphql/src/jsutils/isPromise.ts create mode 100644 packages/graphql/src/jsutils/keyMap.ts create mode 100644 packages/graphql/src/jsutils/keyValMap.ts create mode 100644 packages/graphql/src/jsutils/mapValue.ts create mode 100644 packages/graphql/src/jsutils/memoize1.ts create mode 100644 packages/graphql/src/jsutils/memoize3.ts create mode 100644 packages/graphql/src/jsutils/naturalCompare.ts create mode 100644 packages/graphql/src/jsutils/printPathArray.ts create mode 100644 packages/graphql/src/jsutils/promiseForObject.ts create mode 100644 packages/graphql/src/jsutils/promiseReduce.ts create mode 100644 packages/graphql/src/jsutils/suggestionList.ts create mode 100644 packages/graphql/src/jsutils/toError.ts create mode 100644 packages/graphql/src/jsutils/toObjMap.ts create mode 100644 packages/graphql/src/language/__tests__/blockString-fuzz.ts create mode 100644 packages/graphql/src/language/__tests__/blockString-test.ts create mode 100644 packages/graphql/src/language/__tests__/lexer-test.ts create mode 100644 packages/graphql/src/language/__tests__/parser-test.ts create mode 100644 packages/graphql/src/language/__tests__/predicates-test.ts create mode 100644 packages/graphql/src/language/__tests__/printLocation-test.ts create mode 100644 packages/graphql/src/language/__tests__/printString-test.ts create mode 100644 packages/graphql/src/language/__tests__/printer-test.ts create mode 100644 packages/graphql/src/language/__tests__/schema-parser-test.ts create mode 100644 packages/graphql/src/language/__tests__/schema-printer-test.ts create mode 100644 packages/graphql/src/language/__tests__/source-test.ts create mode 100644 packages/graphql/src/language/__tests__/visitor-test.ts create mode 100644 packages/graphql/src/language/ast.ts create mode 100644 packages/graphql/src/language/blockString.ts create mode 100644 packages/graphql/src/language/characterClasses.ts create mode 100644 packages/graphql/src/language/directiveLocation.ts create mode 100644 packages/graphql/src/language/index.ts create mode 100644 packages/graphql/src/language/kinds.ts create mode 100644 packages/graphql/src/language/lexer.ts create mode 100644 packages/graphql/src/language/location.ts create mode 100644 packages/graphql/src/language/parser.ts create mode 100644 packages/graphql/src/language/predicates.ts create mode 100644 packages/graphql/src/language/printLocation.ts create mode 100644 packages/graphql/src/language/printString.ts create mode 100644 packages/graphql/src/language/printer.ts create mode 100644 packages/graphql/src/language/source.ts create mode 100644 packages/graphql/src/language/tokenKind.ts create mode 100644 packages/graphql/src/language/visitor.ts create mode 100644 packages/graphql/src/subscription/index.ts create mode 100644 packages/graphql/src/type/__tests__/assertName-test.ts create mode 100644 packages/graphql/src/type/__tests__/definition-test.ts create mode 100644 packages/graphql/src/type/__tests__/directive-test.ts create mode 100644 packages/graphql/src/type/__tests__/enumType-test.ts create mode 100644 packages/graphql/src/type/__tests__/extensions-test.ts create mode 100644 packages/graphql/src/type/__tests__/introspection-test.ts create mode 100644 packages/graphql/src/type/__tests__/predicate-test.ts create mode 100644 packages/graphql/src/type/__tests__/scalars-test.ts create mode 100644 packages/graphql/src/type/__tests__/schema-test.ts create mode 100644 packages/graphql/src/type/__tests__/validation-test.ts create mode 100644 packages/graphql/src/type/assertName.ts create mode 100644 packages/graphql/src/type/definition.ts create mode 100644 packages/graphql/src/type/directives.ts create mode 100644 packages/graphql/src/type/index.ts create mode 100644 packages/graphql/src/type/introspection.ts create mode 100644 packages/graphql/src/type/scalars.ts create mode 100644 packages/graphql/src/type/schema.ts create mode 100644 packages/graphql/src/type/validate.ts create mode 100644 packages/graphql/src/utilities/TypeInfo.ts create mode 100644 packages/graphql/src/utilities/__tests__/TypeInfo-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/astFromValue-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/buildASTSchema-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/buildClientSchema-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/coerceInputValue-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/concatAST-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/extendSchema-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/findBreakingChanges-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/getIntrospectionQuery-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/getOperationAST-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/getOperationRootType-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/introspectionFromSchema-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/lexicographicSortSchema-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/printSchema-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/separateOperations-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/sortValueNode-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/stripIgnoredCharacters-fuzz.ts create mode 100644 packages/graphql/src/utilities/__tests__/stripIgnoredCharacters-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/typeComparators-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/valueFromAST-test.ts create mode 100644 packages/graphql/src/utilities/__tests__/valueFromASTUntyped-test.ts create mode 100644 packages/graphql/src/utilities/addResolversToSchema.ts create mode 100644 packages/graphql/src/utilities/assertValidName.ts create mode 100644 packages/graphql/src/utilities/astFromSchema.ts create mode 100644 packages/graphql/src/utilities/astFromValue.ts create mode 100644 packages/graphql/src/utilities/buildASTSchema.ts create mode 100644 packages/graphql/src/utilities/buildClientSchema.ts create mode 100644 packages/graphql/src/utilities/coerceInputValue.ts create mode 100644 packages/graphql/src/utilities/concatAST.ts create mode 100644 packages/graphql/src/utilities/extendSchema.ts create mode 100644 packages/graphql/src/utilities/findBreakingChanges.ts create mode 100644 packages/graphql/src/utilities/getDocumentNodeFromSchema.ts create mode 100644 packages/graphql/src/utilities/getIntrospectionQuery.ts create mode 100644 packages/graphql/src/utilities/getOperationAST.ts create mode 100644 packages/graphql/src/utilities/getOperationRootType.ts create mode 100644 packages/graphql/src/utilities/getRootTypeMap.ts create mode 100644 packages/graphql/src/utilities/index.ts create mode 100644 packages/graphql/src/utilities/introspectionFromSchema.ts create mode 100644 packages/graphql/src/utilities/lexicographicSortSchema.ts create mode 100644 packages/graphql/src/utilities/printSchema.ts create mode 100644 packages/graphql/src/utilities/printSchemaWithDirectives.ts create mode 100644 packages/graphql/src/utilities/separateOperations.ts create mode 100644 packages/graphql/src/utilities/sortValueNode.ts create mode 100644 packages/graphql/src/utilities/stripIgnoredCharacters.ts create mode 100644 packages/graphql/src/utilities/typeComparators.ts create mode 100644 packages/graphql/src/utilities/typeFromAST.ts create mode 100644 packages/graphql/src/utilities/typedQueryDocumentNode.ts create mode 100644 packages/graphql/src/utilities/valueFromAST.ts create mode 100644 packages/graphql/src/utilities/valueFromASTUntyped.ts create mode 100644 packages/graphql/src/validation/ValidationContext.ts create mode 100644 packages/graphql/src/validation/__tests__/ExecutableDefinitionsRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/FieldsOnCorrectTypeRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/FragmentsOnCompositeTypesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/KnownArgumentNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/KnownDirectivesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/KnownFragmentNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/KnownTypeNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/LoneAnonymousOperationRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/LoneSchemaDefinitionRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/NoDeprecatedCustomRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/NoFragmentCyclesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/NoSchemaIntrospectionCustomRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/NoUndefinedVariablesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/NoUnusedFragmentsRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/NoUnusedVariablesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/PossibleFragmentSpreadsRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/PossibleTypeExtensionsRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/ProvidedRequiredArgumentsRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/ScalarLeafsRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/SingleFieldSubscriptionsRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueArgumentDefinitionNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueArgumentNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueDirectiveNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueDirectivesPerLocationRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueEnumValueNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueFieldDefinitionNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueFragmentNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueInputFieldNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueOperationNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueOperationTypesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueTypeNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/UniqueVariableNamesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/ValidationContext-test.ts create mode 100644 packages/graphql/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/VariablesAreInputTypesRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/VariablesInAllowedPositionRule-test.ts create mode 100644 packages/graphql/src/validation/__tests__/harness.ts create mode 100644 packages/graphql/src/validation/__tests__/validation-test.ts create mode 100644 packages/graphql/src/validation/index.ts create mode 100644 packages/graphql/src/validation/rules/ExecutableDefinitionsRule.ts create mode 100644 packages/graphql/src/validation/rules/FieldsOnCorrectTypeRule.ts create mode 100644 packages/graphql/src/validation/rules/FragmentsOnCompositeTypesRule.ts create mode 100644 packages/graphql/src/validation/rules/KnownArgumentNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/KnownDirectivesRule.ts create mode 100644 packages/graphql/src/validation/rules/KnownFragmentNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/KnownTypeNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/LoneAnonymousOperationRule.ts create mode 100644 packages/graphql/src/validation/rules/LoneSchemaDefinitionRule.ts create mode 100644 packages/graphql/src/validation/rules/NoFragmentCyclesRule.ts create mode 100644 packages/graphql/src/validation/rules/NoUndefinedVariablesRule.ts create mode 100644 packages/graphql/src/validation/rules/NoUnusedFragmentsRule.ts create mode 100644 packages/graphql/src/validation/rules/NoUnusedVariablesRule.ts create mode 100644 packages/graphql/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts create mode 100644 packages/graphql/src/validation/rules/PossibleFragmentSpreadsRule.ts create mode 100644 packages/graphql/src/validation/rules/PossibleTypeExtensionsRule.ts create mode 100644 packages/graphql/src/validation/rules/ProvidedRequiredArgumentsRule.ts create mode 100644 packages/graphql/src/validation/rules/ScalarLeafsRule.ts create mode 100644 packages/graphql/src/validation/rules/SingleFieldSubscriptionsRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueArgumentDefinitionNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueArgumentNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueDirectiveNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueDirectivesPerLocationRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueEnumValueNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueFieldDefinitionNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueFragmentNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueInputFieldNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueOperationNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueOperationTypesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueTypeNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/UniqueVariableNamesRule.ts create mode 100644 packages/graphql/src/validation/rules/ValuesOfCorrectTypeRule.ts create mode 100644 packages/graphql/src/validation/rules/VariablesAreInputTypesRule.ts create mode 100644 packages/graphql/src/validation/rules/VariablesInAllowedPositionRule.ts create mode 100644 packages/graphql/src/validation/rules/custom/NoDeprecatedCustomRule.ts create mode 100644 packages/graphql/src/validation/rules/custom/NoSchemaIntrospectionCustomRule.ts create mode 100644 packages/graphql/src/validation/rules/custom/index.ts create mode 100644 packages/graphql/src/validation/rules/index.ts create mode 100644 packages/graphql/src/validation/specifiedRules.ts create mode 100644 packages/graphql/src/validation/validate.ts create mode 100644 packages/graphql/src/version.ts diff --git a/.changeset/five-gifts-lie.md b/.changeset/five-gifts-lie.md new file mode 100644 index 00000000000..fb72b881777 --- /dev/null +++ b/.changeset/five-gifts-lie.md @@ -0,0 +1,5 @@ +--- +'@graphql-tools/graphql': patch +--- + +initial release diff --git a/.eslintrc.json b/.eslintrc.json index 0614be53b4a..57323ad3daf 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -47,6 +47,26 @@ "@typescript-eslint/no-unused-vars": "off", "import/no-extraneous-dependencies": "off" } + }, + { + "files": ["packages/graphql/**"], + "env": { + "jest": true + }, + "rules": { + "unicorn/filename-case": "off", // we keep the same file names as GraphQL.js + // TODO: Enable us incrementally + "no-use-before-define": "off", + "@typescript-eslint/prefer-as-const": "off", + "@typescript-eslint/ban-ts-comment": "off", + "@typescript-eslint/no-inferrable-types": "off", + "unicorn/no-lonely-if": "off", + "@typescript-eslint/no-unused-vars": "off", + "prefer-rest-params": "off", + "no-throw-literal": "off", + "promise/param-names": "off", + "eqeqeq": "off" + } } ], "ignorePatterns": [ diff --git a/packages/graphql/README.md b/packages/graphql/README.md new file mode 100644 index 00000000000..bdcedf072ab --- /dev/null +++ b/packages/graphql/README.md @@ -0,0 +1,3 @@ +## `@graphql-tools/graphql` + +Fork of GraphQL.js diff --git a/packages/graphql/package.json b/packages/graphql/package.json new file mode 100644 index 00000000000..9db9c9510ab --- /dev/null +++ b/packages/graphql/package.json @@ -0,0 +1,65 @@ +{ + "name": "@graphql-tools/graphql", + "version": "0.0.0", + "author": "Saihajpreet Singh ", + "license": "MIT", + "sideEffects": false, + "repository": { + "type": "git", + "url": "https://github.com/ardatan/graphql-tools.git", + "directory": "packages/graphql" + }, + "keywords": [ + "gql", + "graphql", + "typescript" + ], + "main": "dist/cjs/index.js", + "module": "dist/esm/index.js", + "exports": { + ".": { + "require": { + "types": "./dist/typings/index.d.cts", + "default": "./dist/cjs/index.js" + }, + "import": { + "types": "./dist/typings/index.d.ts", + "default": "./dist/esm/index.js" + }, + "default": { + "types": "./dist/typings/index.d.ts", + "default": "./dist/esm/index.js" + } + }, + "./*": { + "require": { + "types": "./dist/typings/*.d.cts", + "default": "./dist/cjs/*.js" + }, + "import": { + "types": "./dist/typings/*.d.ts", + "default": "./dist/esm/*.js" + }, + "default": { + "types": "./dist/typings/*.d.ts", + "default": "./dist/esm/*.js" + } + }, + "./package.json": "./package.json" + }, + "typings": "dist/typings/index.d.ts", + "typescript": { + "definition": "dist/typings/index.d.ts" + }, + "devDependencies": { + "typescript": "4.7.4" + }, + "buildOptions": { + "input": "./src/index.ts" + }, + "publishConfig": { + "directory": "dist", + "access": "public" + }, + "type": "module" +} diff --git a/packages/graphql/src/__testUtils__/__tests__/dedent-test.ts b/packages/graphql/src/__testUtils__/__tests__/dedent-test.ts new file mode 100644 index 00000000000..d08d754d1b6 --- /dev/null +++ b/packages/graphql/src/__testUtils__/__tests__/dedent-test.ts @@ -0,0 +1,102 @@ +import { dedent, dedentString } from '../dedent.js'; + +describe('dedentString', () => { + it('removes indentation in typical usage', () => { + const output = dedentString(` + type Query { + me: User + } + + type User { + id: ID + name: String + } + `); + expect(output).toEqual( + ['type Query {', ' me: User', '}', '', 'type User {', ' id: ID', ' name: String', '}'].join('\n') + ); + }); + + it('removes only the first level of indentation', () => { + const output = dedentString(` + first + second + third + fourth + `); + expect(output).toEqual(['first', ' second', ' third', ' fourth'].join('\n')); + }); + + it('does not escape special characters', () => { + const output = dedentString(` + type Root { + field(arg: String = "wi\th de\fault"): String + } + `); + expect(output).toEqual(['type Root {', ' field(arg: String = "wi\th de\fault"): String', '}'].join('\n')); + }); + + it('also removes indentation using tabs', () => { + const output = dedentString(` + \t\t type Query { + \t\t me: User + \t\t } + `); + expect(output).toEqual(['type Query {', ' me: User', '}'].join('\n')); + }); + + it('removes leading and trailing newlines', () => { + const output = dedentString(` + + + type Query { + me: User + } + + + `); + expect(output).toEqual(['type Query {', ' me: User', '}'].join('\n')); + }); + + it('removes all trailing spaces and tabs', () => { + const output = dedentString(` + type Query { + me: User + } + \t\t \t `); + expect(output).toEqual(['type Query {', ' me: User', '}'].join('\n')); + }); + + it('works on text without leading newline', () => { + const output = dedentString(` type Query { + me: User + } + `); + expect(output).toEqual(['type Query {', ' me: User', '}'].join('\n')); + }); +}); + +describe('dedent', () => { + it('removes indentation in typical usage', () => { + const output = dedent` + type Query { + me: User + } + `; + expect(output).toEqual(['type Query {', ' me: User', '}'].join('\n')); + }); + + it('supports expression interpolation', () => { + const name = 'John'; + const surname = 'Doe'; + const output = dedent` + { + "me": { + "name": "${name}", + "surname": "${surname}" + } + } + `; + expect(output).toEqual(['{', ' "me": {', ' "name": "John",', ' "surname": "Doe"', ' }', '}'].join('\n')); + }); +}); diff --git a/packages/graphql/src/__testUtils__/__tests__/genFuzzStrings-test.ts b/packages/graphql/src/__testUtils__/__tests__/genFuzzStrings-test.ts new file mode 100644 index 00000000000..b5d02cc9bf7 --- /dev/null +++ b/packages/graphql/src/__testUtils__/__tests__/genFuzzStrings-test.ts @@ -0,0 +1,38 @@ +import { genFuzzStrings } from '../genFuzzStrings.js'; + +function expectFuzzStrings(options: { allowedChars: ReadonlyArray; maxLength: number }) { + return expect([...genFuzzStrings(options)]); +} + +describe('genFuzzStrings', () => { + it('always provide empty string', () => { + expectFuzzStrings({ allowedChars: [], maxLength: 0 }).toEqual(['']); + expectFuzzStrings({ allowedChars: [], maxLength: 1 }).toEqual(['']); + expectFuzzStrings({ allowedChars: ['a'], maxLength: 0 }).toEqual(['']); + }); + + it('generate strings with single character', () => { + expectFuzzStrings({ allowedChars: ['a'], maxLength: 1 }).toEqual(['', 'a']); + + expectFuzzStrings({ + allowedChars: ['a', 'b', 'c'], + maxLength: 1, + }).toEqual(['', 'a', 'b', 'c']); + }); + + it('generate strings with multiple character', () => { + expectFuzzStrings({ allowedChars: ['a'], maxLength: 2 }).toEqual(['', 'a', 'aa']); + + expectFuzzStrings({ + allowedChars: ['a', 'b', 'c'], + maxLength: 2, + }).toEqual(['', 'a', 'b', 'c', 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']); + }); + + it('generate strings longer than possible number of characters', () => { + expectFuzzStrings({ + allowedChars: ['a', 'b'], + maxLength: 3, + }).toEqual(['', 'a', 'b', 'aa', 'ab', 'ba', 'bb', 'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bba', 'bbb']); + }); +}); diff --git a/packages/graphql/src/__testUtils__/__tests__/inspectStr-test.ts b/packages/graphql/src/__testUtils__/__tests__/inspectStr-test.ts new file mode 100644 index 00000000000..c6d336a442c --- /dev/null +++ b/packages/graphql/src/__testUtils__/__tests__/inspectStr-test.ts @@ -0,0 +1,16 @@ +import { inspectStr } from '../inspectStr.js'; + +describe('inspectStr', () => { + it('handles null and undefined values', () => { + expect(inspectStr(null)).toEqual('null'); + expect(inspectStr(undefined)).toEqual('null'); + }); + + it('correctly print various strings', () => { + expect(inspectStr('')).toEqual('``'); + expect(inspectStr('a')).toEqual('`a`'); + expect(inspectStr('"')).toEqual('`"`'); + expect(inspectStr("'")).toEqual("`'`"); + expect(inspectStr('\\"')).toEqual('`\\"`'); + }); +}); diff --git a/packages/graphql/src/__testUtils__/__tests__/resolveOnNextTick-test.ts b/packages/graphql/src/__testUtils__/__tests__/resolveOnNextTick-test.ts new file mode 100644 index 00000000000..663487a4163 --- /dev/null +++ b/packages/graphql/src/__testUtils__/__tests__/resolveOnNextTick-test.ts @@ -0,0 +1,18 @@ +import { resolveOnNextTick } from '../resolveOnNextTick.js'; + +describe('resolveOnNextTick', () => { + it('resolves promise on the next tick', async () => { + const output = []; + + const promise1 = resolveOnNextTick().then(() => { + output.push('second'); + }); + const promise2 = resolveOnNextTick().then(() => { + output.push('third'); + }); + output.push('first'); + + await Promise.all([promise1, promise2]); + expect(output).toEqual(['first', 'second', 'third']); + }); +}); diff --git a/packages/graphql/src/__testUtils__/dedent.ts b/packages/graphql/src/__testUtils__/dedent.ts new file mode 100644 index 00000000000..9a8145e552e --- /dev/null +++ b/packages/graphql/src/__testUtils__/dedent.ts @@ -0,0 +1,38 @@ +export function dedentString(string: string): string { + const trimmedStr = string + .replace(/^\n*/m, '') // remove leading newline + .replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs + + // fixes indentation by removing leading spaces and tabs from each line + let indent = ''; + for (const char of trimmedStr) { + if (char !== ' ' && char !== '\t') { + break; + } + indent += char; + } + + return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent +} + +/** + * An ES6 string tag that fixes indentation and also trims string. + * + * Example usage: + * ```ts + * const str = dedent` + * { + * test + * } + * `; + * str === "{\n test\n}"; + * ``` + */ +export function dedent(strings: ReadonlyArray, ...values: ReadonlyArray): string { + let str = strings[0]; + + for (let i = 1; i < strings.length; ++i) { + str += values[i - 1] + strings[i]; // interpolation + } + return dedentString(str); +} diff --git a/packages/graphql/src/__testUtils__/expectJSON.ts b/packages/graphql/src/__testUtils__/expectJSON.ts new file mode 100644 index 00000000000..6f700453d33 --- /dev/null +++ b/packages/graphql/src/__testUtils__/expectJSON.ts @@ -0,0 +1,51 @@ +import { isObjectLike } from '../jsutils/isObjectLike.js'; +import { mapValue } from '../jsutils/mapValue.js'; + +/** + * Deeply transforms an arbitrary value to a JSON-safe value by calling toJSON + * on any nested value which defines it. + */ +function toJSONDeep(value: unknown): unknown { + if (!isObjectLike(value)) { + return value; + } + + // @ts-expect-error: toJSON is not defined on all objects + if (typeof value.toJSON === 'function') { + // @ts-expect-error: toJSON is not defined on all objects + return value.toJSON(); + } + + if (Array.isArray(value)) { + return value.map(toJSONDeep); + } + + return mapValue(value, toJSONDeep); +} + +export function expectJSON(actual: unknown) { + const actualJSON = toJSONDeep(actual); + + return { + toDeepEqual(expected: unknown) { + const expectedJSON = toJSONDeep(expected); + expect(actualJSON).toMatchObject(expectedJSON as any); + }, + toDeepNestedProperty(path: string, expected: unknown) { + const expectedJSON = toJSONDeep(expected); + expect(actualJSON).toHaveProperty(path, expectedJSON); + }, + }; +} + +export function expectToThrowJSON(fn: () => unknown) { + function mapException(): unknown { + try { + return fn(); + } catch (error) { + return error; + } + } + + return expect(mapException()); +} diff --git a/packages/graphql/src/__testUtils__/genFuzzStrings.ts b/packages/graphql/src/__testUtils__/genFuzzStrings.ts new file mode 100644 index 00000000000..f29e1bb860b --- /dev/null +++ b/packages/graphql/src/__testUtils__/genFuzzStrings.ts @@ -0,0 +1,29 @@ +/** + * Generator that produces all possible combinations of allowed characters. + */ +export function* genFuzzStrings(options: { + allowedChars: ReadonlyArray; + maxLength: number; +}): Generator { + const { allowedChars, maxLength } = options; + const numAllowedChars = allowedChars.length; + + let numCombinations = 0; + for (let length = 1; length <= maxLength; ++length) { + numCombinations += numAllowedChars ** length; + } + + yield ''; // special case for empty string + for (let combination = 0; combination < numCombinations; ++combination) { + let permutation = ''; + + let leftOver = combination; + while (leftOver >= 0) { + const reminder = leftOver % numAllowedChars; + permutation = allowedChars[reminder] + permutation; + leftOver = (leftOver - reminder) / numAllowedChars - 1; + } + + yield permutation; + } +} diff --git a/packages/graphql/src/__testUtils__/inspectStr.ts b/packages/graphql/src/__testUtils__/inspectStr.ts new file mode 100644 index 00000000000..da94f721747 --- /dev/null +++ b/packages/graphql/src/__testUtils__/inspectStr.ts @@ -0,0 +1,11 @@ +import type { Maybe } from '../jsutils/Maybe.js'; + +/** + * Special inspect function to produce readable string literal for error messages in tests + */ +export function inspectStr(str: Maybe): string { + if (str == null) { + return 'null'; + } + return JSON.stringify(str).replace(/^"|"$/g, '`').replace(/\\"/g, '"').replace(/\\\\/g, '\\'); +} diff --git a/packages/graphql/src/__testUtils__/kitchenSinkQuery.ts b/packages/graphql/src/__testUtils__/kitchenSinkQuery.ts new file mode 100644 index 00000000000..ff989d4b46b --- /dev/null +++ b/packages/graphql/src/__testUtils__/kitchenSinkQuery.ts @@ -0,0 +1,83 @@ +export const kitchenSinkQuery: string = String.raw` +query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery { + whoever123is: node(id: [123, 456]) { + id + ... on User @onInlineFragment { + field2 { + id + alias: field1(first: 10, after: $foo) @include(if: $foo) { + id + ...frag @onFragmentSpread + } + } + + field3! + field4? + requiredField5: field5! + requiredSelectionSet(first: 10)! @directive { + field + } + + unsetListItemsRequiredList: listField[]! + requiredListItemsUnsetList: listField[!] + requiredListItemsRequiredList: listField[!]! + unsetListItemsOptionalList: listField[]? + optionalListItemsUnsetList: listField[?] + optionalListItemsOptionalList: listField[?]? + multidimensionalList: listField[[[!]!]!]! + } + ... @skip(unless: $foo) { + id + } + ... { + id + } + } +} + +mutation likeStory @onMutation { + like(story: 123) @onField { + story { + id @onField + } + } +} + +subscription StoryLikeSubscription( + $input: StoryLikeSubscribeInput @onVariableDefinition +) + @onSubscription { + storyLikeSubscribe(input: $input) { + story { + likers { + count + } + likeSentence { + text + } + } + } +} + +fragment frag on Friend @onFragmentDefinition { + foo( + size: $size + bar: $b + obj: { + key: "value" + block: """ + block string uses \""" + """ + } + ) +} + +{ + unnamed(truthy: true, falsy: false, nullish: null) + query +} + +query { + __typename +} +`; diff --git a/packages/graphql/src/__testUtils__/kitchenSinkSDL.ts b/packages/graphql/src/__testUtils__/kitchenSinkSDL.ts new file mode 100644 index 00000000000..cdf2f9afcea --- /dev/null +++ b/packages/graphql/src/__testUtils__/kitchenSinkSDL.ts @@ -0,0 +1,158 @@ +export const kitchenSinkSDL = ` +"""This is a description of the schema as a whole.""" +schema { + query: QueryType + mutation: MutationType +} + +""" +This is a description +of the \`Foo\` type. +""" +type Foo implements Bar & Baz & Two { + "Description of the \`one\` field." + one: Type + """ + This is a description of the \`two\` field. + """ + two( + """ + This is a description of the \`argument\` argument. + """ + argument: InputType! + ): Type + """This is a description of the \`three\` field.""" + three(argument: InputType, other: String): Int + four(argument: String = "string"): String + five(argument: [String] = ["string", "string"]): String + six(argument: InputType = {key: "value"}): Type + seven(argument: Int = null): Type +} + +type AnnotatedObject @onObject(arg: "value") { + annotatedField(arg: Type = "default" @onArgumentDefinition): Type @onField +} + +type UndefinedType + +extend type Foo { + seven(argument: [String]): Type +} + +extend type Foo @onType + +interface Bar { + one: Type + four(argument: String = "string"): String +} + +interface AnnotatedInterface @onInterface { + annotatedField(arg: Type @onArgumentDefinition): Type @onField +} + +interface UndefinedInterface + +extend interface Bar implements Two { + two(argument: InputType!): Type +} + +extend interface Bar @onInterface + +interface Baz implements Bar & Two { + one: Type + two(argument: InputType!): Type + four(argument: String = "string"): String +} + +union Feed = + | Story + | Article + | Advert + +union AnnotatedUnion @onUnion = A | B + +union AnnotatedUnionTwo @onUnion = | A | B + +union UndefinedUnion + +extend union Feed = Photo | Video + +extend union Feed @onUnion + +scalar CustomScalar + +scalar AnnotatedScalar @onScalar + +extend scalar CustomScalar @onScalar + +enum Site { + """ + This is a description of the \`DESKTOP\` value + """ + DESKTOP + + """This is a description of the \`MOBILE\` value""" + MOBILE + + "This is a description of the \`WEB\` value" + WEB +} + +enum AnnotatedEnum @onEnum { + ANNOTATED_VALUE @onEnumValue + OTHER_VALUE +} + +enum UndefinedEnum + +extend enum Site { + VR +} + +extend enum Site @onEnum + +input InputType { + key: String! + answer: Int = 42 +} + +input AnnotatedInput @onInputObject { + annotatedField: Type @onInputFieldDefinition +} + +input UndefinedInput + +extend input InputType { + other: Float = 1.23e4 @onInputFieldDefinition +} + +extend input InputType @onInputObject + +""" +This is a description of the \`@skip\` directive +""" +directive @skip( + """This is a description of the \`if\` argument""" + if: Boolean! @onArgumentDefinition +) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT + +directive @include(if: Boolean!) + on FIELD + | FRAGMENT_SPREAD + | INLINE_FRAGMENT + +directive @include2(if: Boolean!) on + | FIELD + | FRAGMENT_SPREAD + | INLINE_FRAGMENT + +directive @myRepeatableDir(name: String!) repeatable on + | OBJECT + | INTERFACE + +extend schema @onSchema + +extend schema @onSchema { + subscription: SubscriptionType +} +`; diff --git a/packages/graphql/src/__testUtils__/resolveOnNextTick.ts b/packages/graphql/src/__testUtils__/resolveOnNextTick.ts new file mode 100644 index 00000000000..6dd50b39822 --- /dev/null +++ b/packages/graphql/src/__testUtils__/resolveOnNextTick.ts @@ -0,0 +1,3 @@ +export function resolveOnNextTick(): Promise { + return Promise.resolve(undefined); +} diff --git a/packages/graphql/src/__tests__/starWarsData.ts b/packages/graphql/src/__tests__/starWarsData.ts new file mode 100644 index 00000000000..32a82ff4d7c --- /dev/null +++ b/packages/graphql/src/__tests__/starWarsData.ts @@ -0,0 +1,156 @@ +/** + * These are types which correspond to the schema. + * They represent the shape of the data visited during field resolution. + */ +export interface Character { + id: string; + name: string; + friends: ReadonlyArray; + appearsIn: ReadonlyArray; +} + +export interface Human { + type: 'Human'; + id: string; + name: string; + friends: ReadonlyArray; + appearsIn: ReadonlyArray; + homePlanet?: string; +} + +export interface Droid { + type: 'Droid'; + id: string; + name: string; + friends: ReadonlyArray; + appearsIn: ReadonlyArray; + primaryFunction: string; +} + +/** + * This defines a basic set of data for our Star Wars Schema. + * + * This data is hard coded for the sake of the demo, but you could imagine + * fetching this data from a backend service rather than from hardcoded + * JSON objects in a more complex demo. + */ + +const luke: Human = { + type: 'Human', + id: '1000', + name: 'Luke Skywalker', + friends: ['1002', '1003', '2000', '2001'], + appearsIn: [4, 5, 6], + homePlanet: 'Tatooine', +}; + +const vader: Human = { + type: 'Human', + id: '1001', + name: 'Darth Vader', + friends: ['1004'], + appearsIn: [4, 5, 6], + homePlanet: 'Tatooine', +}; + +const han: Human = { + type: 'Human', + id: '1002', + name: 'Han Solo', + friends: ['1000', '1003', '2001'], + appearsIn: [4, 5, 6], +}; + +const leia: Human = { + type: 'Human', + id: '1003', + name: 'Leia Organa', + friends: ['1000', '1002', '2000', '2001'], + appearsIn: [4, 5, 6], + homePlanet: 'Alderaan', +}; + +const tarkin: Human = { + type: 'Human', + id: '1004', + name: 'Wilhuff Tarkin', + friends: ['1001'], + appearsIn: [4], +}; + +const humanData: { [id: string]: Human } = { + [luke.id]: luke, + [vader.id]: vader, + [han.id]: han, + [leia.id]: leia, + [tarkin.id]: tarkin, +}; + +const threepio: Droid = { + type: 'Droid', + id: '2000', + name: 'C-3PO', + friends: ['1000', '1002', '1003', '2001'], + appearsIn: [4, 5, 6], + primaryFunction: 'Protocol', +}; + +const artoo: Droid = { + type: 'Droid', + id: '2001', + name: 'R2-D2', + friends: ['1000', '1002', '1003'], + appearsIn: [4, 5, 6], + primaryFunction: 'Astromech', +}; + +const droidData: { [id: string]: Droid } = { + [threepio.id]: threepio, + [artoo.id]: artoo, +}; + +/** + * Helper function to get a character by ID. + */ +function getCharacter(id: string): Promise { + // Returning a promise just to illustrate that GraphQL.js supports it. + return Promise.resolve(humanData[id] ?? droidData[id]); +} + +/** + * Allows us to query for a character's friends. + */ +export function getFriends(character: Character): Array> { + // Notice that GraphQL accepts Arrays of Promises. + return character.friends.map(id => getCharacter(id)); +} + +/** + * Allows us to fetch the undisputed hero of the Star Wars trilogy, R2-D2. + */ +export function getHero(episode: number): Character { + if (episode === 5) { + // Luke is the hero of Episode V. + return luke; + } + // Artoo is the hero otherwise. + return artoo; +} + +/** + * Allows us to query for the human with the given id. + */ +export function getHuman(id: string): Human | null { + return humanData[id]; +} + +/** + * Allows us to query for the droid with the given id. + */ +export function getDroid(id: string): Droid | null { + return droidData[id]; +} + +describe.skip('no starWarsDataError', () => { + it.todo('nope'); +}); diff --git a/packages/graphql/src/__tests__/starWarsIntrospection-test.ts b/packages/graphql/src/__tests__/starWarsIntrospection-test.ts new file mode 100644 index 00000000000..50b2c0a6ac1 --- /dev/null +++ b/packages/graphql/src/__tests__/starWarsIntrospection-test.ts @@ -0,0 +1,363 @@ +import { graphqlSync } from '../graphql.js'; + +import { StarWarsSchema } from './starWarsSchema.js'; + +function queryStarWars(source: string) { + const result = graphqlSync({ schema: StarWarsSchema, source }); + expect(Object.keys(result)).toEqual(['data']); + return result.data; +} + +describe('Star Wars Introspection Tests', () => { + describe('Basic Introspection', () => { + it('Allows querying the schema for types', () => { + const data = queryStarWars(` + { + __schema { + types { + name + } + } + } + `); + + // Include all types used by StarWars schema, introspection types and + // standard directives. For example, `Boolean` is used in `@skip`, + // `@include` and also inside introspection types. + expect(data).toEqual({ + __schema: { + types: [ + { name: 'Human' }, + { name: 'Character' }, + { name: 'String' }, + { name: 'Episode' }, + { name: 'Droid' }, + { name: 'Query' }, + { name: 'Boolean' }, + { name: '__Schema' }, + { name: '__Type' }, + { name: '__TypeKind' }, + { name: '__Field' }, + { name: '__InputValue' }, + { name: '__EnumValue' }, + { name: '__Directive' }, + { name: '__DirectiveLocation' }, + ], + }, + }); + }); + + it('Allows querying the schema for query type', () => { + const data = queryStarWars(` + { + __schema { + queryType { + name + } + } + } + `); + + expect(data).toEqual({ + __schema: { + queryType: { + name: 'Query', + }, + }, + }); + }); + + it('Allows querying the schema for a specific type', () => { + const data = queryStarWars(` + { + __type(name: "Droid") { + name + } + } + `); + + expect(data).toEqual({ + __type: { + name: 'Droid', + }, + }); + }); + + it('Allows querying the schema for an object kind', () => { + const data = queryStarWars(` + { + __type(name: "Droid") { + name + kind + } + } + `); + + expect(data).toEqual({ + __type: { + name: 'Droid', + kind: 'OBJECT', + }, + }); + }); + + it('Allows querying the schema for an interface kind', () => { + const data = queryStarWars(` + { + __type(name: "Character") { + name + kind + } + } + `); + + expect(data).toEqual({ + __type: { + name: 'Character', + kind: 'INTERFACE', + }, + }); + }); + + it('Allows querying the schema for object fields', () => { + const data = queryStarWars(` + { + __type(name: "Droid") { + name + fields { + name + type { + name + kind + } + } + } + } + `); + + expect(data).toEqual({ + __type: { + name: 'Droid', + fields: [ + { + name: 'id', + type: { name: null, kind: 'NON_NULL' }, + }, + { + name: 'name', + type: { name: 'String', kind: 'SCALAR' }, + }, + { + name: 'friends', + type: { name: null, kind: 'LIST' }, + }, + { + name: 'appearsIn', + type: { name: null, kind: 'LIST' }, + }, + { + name: 'secretBackstory', + type: { name: 'String', kind: 'SCALAR' }, + }, + { + name: 'primaryFunction', + type: { name: 'String', kind: 'SCALAR' }, + }, + ], + }, + }); + }); + + it('Allows querying the schema for nested object fields', () => { + const data = queryStarWars(` + { + __type(name: "Droid") { + name + fields { + name + type { + name + kind + ofType { + name + kind + } + } + } + } + } + `); + + expect(data).toEqual({ + __type: { + name: 'Droid', + fields: [ + { + name: 'id', + type: { + name: null, + kind: 'NON_NULL', + ofType: { + name: 'String', + kind: 'SCALAR', + }, + }, + }, + { + name: 'name', + type: { + name: 'String', + kind: 'SCALAR', + ofType: null, + }, + }, + { + name: 'friends', + type: { + name: null, + kind: 'LIST', + ofType: { + name: 'Character', + kind: 'INTERFACE', + }, + }, + }, + { + name: 'appearsIn', + type: { + name: null, + kind: 'LIST', + ofType: { + name: 'Episode', + kind: 'ENUM', + }, + }, + }, + { + name: 'secretBackstory', + type: { + name: 'String', + kind: 'SCALAR', + ofType: null, + }, + }, + { + name: 'primaryFunction', + type: { + name: 'String', + kind: 'SCALAR', + ofType: null, + }, + }, + ], + }, + }); + }); + + it('Allows querying the schema for field args', () => { + const data = queryStarWars(` + { + __schema { + queryType { + fields { + name + args { + name + description + type { + name + kind + ofType { + name + kind + } + } + defaultValue + } + } + } + } + } + `); + + expect(data).toEqual({ + __schema: { + queryType: { + fields: [ + { + name: 'hero', + args: [ + { + defaultValue: null, + description: + 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.', + name: 'episode', + type: { + kind: 'ENUM', + name: 'Episode', + ofType: null, + }, + }, + ], + }, + { + name: 'human', + args: [ + { + name: 'id', + description: 'id of the human', + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + }, + }, + defaultValue: null, + }, + ], + }, + { + name: 'droid', + args: [ + { + name: 'id', + description: 'id of the droid', + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + }, + }, + defaultValue: null, + }, + ], + }, + ], + }, + }, + }); + }); + + it('Allows querying the schema for documentation', () => { + const data = queryStarWars(` + { + __type(name: "Droid") { + name + description + } + } + `); + + expect(data).toEqual({ + __type: { + name: 'Droid', + description: 'A mechanical creature in the Star Wars universe.', + }, + }); + }); + }); +}); diff --git a/packages/graphql/src/__tests__/starWarsQuery-test.ts b/packages/graphql/src/__tests__/starWarsQuery-test.ts new file mode 100644 index 00000000000..1996dc6474d --- /dev/null +++ b/packages/graphql/src/__tests__/starWarsQuery-test.ts @@ -0,0 +1,494 @@ +import { expectJSON } from '../__testUtils__/expectJSON.js'; + +import { graphql } from '../graphql.js'; + +import { StarWarsSchema as schema } from './starWarsSchema.js'; + +describe('Star Wars Query Tests', () => { + describe('Basic Queries', () => { + it('Correctly identifies R2-D2 as the hero of the Star Wars Saga', async () => { + const source = ` + query HeroNameQuery { + hero { + name + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + hero: { + name: 'R2-D2', + }, + }, + }); + }); + + it('Allows us to query for the ID and friends of R2-D2', async () => { + const source = ` + query HeroNameAndFriendsQuery { + hero { + id + name + friends { + name + } + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + hero: { + id: '2001', + name: 'R2-D2', + friends: [ + { + name: 'Luke Skywalker', + }, + { + name: 'Han Solo', + }, + { + name: 'Leia Organa', + }, + ], + }, + }, + }); + }); + }); + + describe('Nested Queries', () => { + it('Allows us to query for the friends of friends of R2-D2', async () => { + const source = ` + query NestedQuery { + hero { + name + friends { + name + appearsIn + friends { + name + } + } + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + hero: { + name: 'R2-D2', + friends: [ + { + name: 'Luke Skywalker', + appearsIn: ['NEW_HOPE', 'EMPIRE', 'JEDI'], + friends: [ + { + name: 'Han Solo', + }, + { + name: 'Leia Organa', + }, + { + name: 'C-3PO', + }, + { + name: 'R2-D2', + }, + ], + }, + { + name: 'Han Solo', + appearsIn: ['NEW_HOPE', 'EMPIRE', 'JEDI'], + friends: [ + { + name: 'Luke Skywalker', + }, + { + name: 'Leia Organa', + }, + { + name: 'R2-D2', + }, + ], + }, + { + name: 'Leia Organa', + appearsIn: ['NEW_HOPE', 'EMPIRE', 'JEDI'], + friends: [ + { + name: 'Luke Skywalker', + }, + { + name: 'Han Solo', + }, + { + name: 'C-3PO', + }, + { + name: 'R2-D2', + }, + ], + }, + ], + }, + }, + }); + }); + }); + + describe('Using IDs and query parameters to refetch objects', () => { + it('Allows us to query characters directly, using their IDs', async () => { + const source = ` + query FetchLukeAndC3POQuery { + human(id: "1000") { + name + } + droid(id: "2000") { + name + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + human: { + name: 'Luke Skywalker', + }, + droid: { + name: 'C-3PO', + }, + }, + }); + }); + + it('Allows us to create a generic query, then use it to fetch Luke Skywalker using his ID', async () => { + const source = ` + query FetchSomeIDQuery($someId: String!) { + human(id: $someId) { + name + } + } + `; + const variableValues = { someId: '1000' }; + + const result = await graphql({ schema, source, variableValues }); + expect(result).toEqual({ + data: { + human: { + name: 'Luke Skywalker', + }, + }, + }); + }); + + it('Allows us to create a generic query, then use it to fetch Han Solo using his ID', async () => { + const source = ` + query FetchSomeIDQuery($someId: String!) { + human(id: $someId) { + name + } + } + `; + const variableValues = { someId: '1002' }; + + const result = await graphql({ schema, source, variableValues }); + expect(result).toEqual({ + data: { + human: { + name: 'Han Solo', + }, + }, + }); + }); + + it('Allows us to create a generic query, then pass an invalid ID to get null back', async () => { + const source = ` + query humanQuery($id: String!) { + human(id: $id) { + name + } + } + `; + const variableValues = { id: 'not a valid id' }; + + const result = await graphql({ schema, source, variableValues }); + expect(result).toEqual({ + data: { + human: null, + }, + }); + }); + }); + + describe('Using aliases to change the key in the response', () => { + it('Allows us to query for Luke, changing his key with an alias', async () => { + const source = ` + query FetchLukeAliased { + luke: human(id: "1000") { + name + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + luke: { + name: 'Luke Skywalker', + }, + }, + }); + }); + + it('Allows us to query for both Luke and Leia, using two root fields and an alias', async () => { + const source = ` + query FetchLukeAndLeiaAliased { + luke: human(id: "1000") { + name + } + leia: human(id: "1003") { + name + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + luke: { + name: 'Luke Skywalker', + }, + leia: { + name: 'Leia Organa', + }, + }, + }); + }); + }); + + describe('Uses fragments to express more complex queries', () => { + it('Allows us to query using duplicated content', async () => { + const source = ` + query DuplicateFields { + luke: human(id: "1000") { + name + homePlanet + } + leia: human(id: "1003") { + name + homePlanet + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + luke: { + name: 'Luke Skywalker', + homePlanet: 'Tatooine', + }, + leia: { + name: 'Leia Organa', + homePlanet: 'Alderaan', + }, + }, + }); + }); + + it('Allows us to use a fragment to avoid duplicating content', async () => { + const source = ` + query UseFragment { + luke: human(id: "1000") { + ...HumanFragment + } + leia: human(id: "1003") { + ...HumanFragment + } + } + + fragment HumanFragment on Human { + name + homePlanet + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + luke: { + name: 'Luke Skywalker', + homePlanet: 'Tatooine', + }, + leia: { + name: 'Leia Organa', + homePlanet: 'Alderaan', + }, + }, + }); + }); + }); + + describe('Using __typename to find the type of an object', () => { + it('Allows us to verify that R2-D2 is a droid', async () => { + const source = ` + query CheckTypeOfR2 { + hero { + __typename + name + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + hero: { + __typename: 'Droid', + name: 'R2-D2', + }, + }, + }); + }); + + it('Allows us to verify that Luke is a human', async () => { + const source = ` + query CheckTypeOfLuke { + hero(episode: EMPIRE) { + __typename + name + } + } + `; + + const result = await graphql({ schema, source }); + expect(result).toEqual({ + data: { + hero: { + __typename: 'Human', + name: 'Luke Skywalker', + }, + }, + }); + }); + }); + + describe('Reporting errors raised in resolvers', () => { + it('Correctly reports error on accessing secretBackstory', async () => { + const source = ` + query HeroNameQuery { + hero { + name + secretBackstory + } + } + `; + + const result = await graphql({ schema, source }); + expectJSON(result).toDeepEqual({ + data: { + hero: { + name: 'R2-D2', + secretBackstory: null, + }, + }, + errors: [ + { + message: 'secretBackstory is secret.', + locations: [{ line: 5, column: 13 }], + path: ['hero', 'secretBackstory'], + }, + ], + }); + }); + + it('Correctly reports error on accessing secretBackstory in a list', async () => { + const source = ` + query HeroNameQuery { + hero { + name + friends { + name + secretBackstory + } + } + } + `; + + const result = await graphql({ schema, source }); + expectJSON(result).toDeepEqual({ + data: { + hero: { + name: 'R2-D2', + friends: [ + { + name: 'Luke Skywalker', + secretBackstory: null, + }, + { + name: 'Han Solo', + secretBackstory: null, + }, + { + name: 'Leia Organa', + secretBackstory: null, + }, + ], + }, + }, + errors: [ + { + message: 'secretBackstory is secret.', + locations: [{ line: 7, column: 15 }], + path: ['hero', 'friends', 0, 'secretBackstory'], + }, + { + message: 'secretBackstory is secret.', + locations: [{ line: 7, column: 15 }], + path: ['hero', 'friends', 1, 'secretBackstory'], + }, + { + message: 'secretBackstory is secret.', + locations: [{ line: 7, column: 15 }], + path: ['hero', 'friends', 2, 'secretBackstory'], + }, + ], + }); + }); + + it('Correctly reports error on accessing through an alias', async () => { + const source = ` + query HeroNameQuery { + mainHero: hero { + name + story: secretBackstory + } + } + `; + + const result = await graphql({ schema, source }); + expectJSON(result).toDeepEqual({ + data: { + mainHero: { + name: 'R2-D2', + story: null, + }, + }, + errors: [ + { + message: 'secretBackstory is secret.', + locations: [{ line: 5, column: 13 }], + path: ['mainHero', 'story'], + }, + ], + }); + }); + }); +}); diff --git a/packages/graphql/src/__tests__/starWarsSchema.ts b/packages/graphql/src/__tests__/starWarsSchema.ts new file mode 100644 index 00000000000..c7ec027956b --- /dev/null +++ b/packages/graphql/src/__tests__/starWarsSchema.ts @@ -0,0 +1,306 @@ +import { + GraphQLEnumType, + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, +} from '../type/definition.js'; +import { GraphQLString } from '../type/scalars.js'; +import { GraphQLSchema } from '../type/schema.js'; + +import { getDroid, getFriends, getHero, getHuman } from './starWarsData.js'; + +/** + * This is designed to be an end-to-end test, demonstrating + * the full GraphQL stack. + * + * We will create a GraphQL schema that describes the major + * characters in the original Star Wars trilogy. + * + * NOTE: This may contain spoilers for the original Star + * Wars trilogy. + */ + +/** + * Using our shorthand to describe type systems, the type system for our + * Star Wars example is: + * + * ```graphql + * enum Episode { NEW_HOPE, EMPIRE, JEDI } + * + * interface Character { + * id: String! + * name: String + * friends: [Character] + * appearsIn: [Episode] + * } + * + * type Human implements Character { + * id: String! + * name: String + * friends: [Character] + * appearsIn: [Episode] + * homePlanet: String + * } + * + * type Droid implements Character { + * id: String! + * name: String + * friends: [Character] + * appearsIn: [Episode] + * primaryFunction: String + * } + * + * type Query { + * hero(episode: Episode): Character + * human(id: String!): Human + * droid(id: String!): Droid + * } + * ``` + * + * We begin by setting up our schema. + */ + +/** + * The original trilogy consists of three movies. + * + * This implements the following type system shorthand: + * ```graphql + * enum Episode { NEW_HOPE, EMPIRE, JEDI } + * ``` + */ +const episodeEnum = new GraphQLEnumType({ + name: 'Episode', + description: 'One of the films in the Star Wars Trilogy', + values: { + NEW_HOPE: { + value: 4, + description: 'Released in 1977.', + }, + EMPIRE: { + value: 5, + description: 'Released in 1980.', + }, + JEDI: { + value: 6, + description: 'Released in 1983.', + }, + }, +}); + +/** + * Characters in the Star Wars trilogy are either humans or droids. + * + * This implements the following type system shorthand: + * ```graphql + * interface Character { + * id: String! + * name: String + * friends: [Character] + * appearsIn: [Episode] + * secretBackstory: String + * } + * ``` + */ +const characterInterface: GraphQLInterfaceType = new GraphQLInterfaceType({ + name: 'Character', + description: 'A character in the Star Wars Trilogy', + fields: () => ({ + id: { + type: new GraphQLNonNull(GraphQLString), + description: 'The id of the character.', + }, + name: { + type: GraphQLString, + description: 'The name of the character.', + }, + friends: { + type: new GraphQLList(characterInterface), + description: 'The friends of the character, or an empty list if they have none.', + }, + appearsIn: { + type: new GraphQLList(episodeEnum), + description: 'Which movies they appear in.', + }, + secretBackstory: { + type: GraphQLString, + description: 'All secrets about their past.', + }, + }), + resolveType(character) { + switch (character.type) { + case 'Human': + return humanType.name; + case 'Droid': + return droidType.name; + default: + throw new Error(`Unknown character type: ${character.type}`); + } + }, +}); + +/** + * We define our human type, which implements the character interface. + * + * This implements the following type system shorthand: + * ```graphql + * type Human : Character { + * id: String! + * name: String + * friends: [Character] + * appearsIn: [Episode] + * secretBackstory: String + * } + * ``` + */ +const humanType = new GraphQLObjectType({ + name: 'Human', + description: 'A humanoid creature in the Star Wars universe.', + fields: () => ({ + id: { + type: new GraphQLNonNull(GraphQLString), + description: 'The id of the human.', + }, + name: { + type: GraphQLString, + description: 'The name of the human.', + }, + friends: { + type: new GraphQLList(characterInterface), + description: 'The friends of the human, or an empty list if they have none.', + resolve: human => getFriends(human), + }, + appearsIn: { + type: new GraphQLList(episodeEnum), + description: 'Which movies they appear in.', + }, + homePlanet: { + type: GraphQLString, + description: 'The home planet of the human, or null if unknown.', + }, + secretBackstory: { + type: GraphQLString, + description: 'Where are they from and how they came to be who they are.', + resolve() { + throw new Error('secretBackstory is secret.'); + }, + }, + }), + interfaces: [characterInterface], +}); + +/** + * The other type of character in Star Wars is a droid. + * + * This implements the following type system shorthand: + * ```graphql + * type Droid : Character { + * id: String! + * name: String + * friends: [Character] + * appearsIn: [Episode] + * secretBackstory: String + * primaryFunction: String + * } + * ``` + */ +const droidType = new GraphQLObjectType({ + name: 'Droid', + description: 'A mechanical creature in the Star Wars universe.', + fields: () => ({ + id: { + type: new GraphQLNonNull(GraphQLString), + description: 'The id of the droid.', + }, + name: { + type: GraphQLString, + description: 'The name of the droid.', + }, + friends: { + type: new GraphQLList(characterInterface), + description: 'The friends of the droid, or an empty list if they have none.', + resolve: droid => getFriends(droid), + }, + appearsIn: { + type: new GraphQLList(episodeEnum), + description: 'Which movies they appear in.', + }, + secretBackstory: { + type: GraphQLString, + description: 'Construction date and the name of the designer.', + resolve() { + throw new Error('secretBackstory is secret.'); + }, + }, + primaryFunction: { + type: GraphQLString, + description: 'The primary function of the droid.', + }, + }), + interfaces: [characterInterface], +}); + +/** + * This is the type that will be the root of our query, and the + * entry point into our schema. It gives us the ability to fetch + * objects by their IDs, as well as to fetch the undisputed hero + * of the Star Wars trilogy, R2-D2, directly. + * + * This implements the following type system shorthand: + * ```graphql + * type Query { + * hero(episode: Episode): Character + * human(id: String!): Human + * droid(id: String!): Droid + * } + * ``` + */ +const queryType = new GraphQLObjectType({ + name: 'Query', + fields: () => ({ + hero: { + type: characterInterface, + args: { + episode: { + description: + 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.', + type: episodeEnum, + }, + }, + resolve: (_source, { episode }) => getHero(episode), + }, + human: { + type: humanType, + args: { + id: { + description: 'id of the human', + type: new GraphQLNonNull(GraphQLString), + }, + }, + resolve: (_source, { id }) => getHuman(id), + }, + droid: { + type: droidType, + args: { + id: { + description: 'id of the droid', + type: new GraphQLNonNull(GraphQLString), + }, + }, + resolve: (_source, { id }) => getDroid(id), + }, + }), +}); + +/** + * Finally, we construct our schema (whose starting query type is the query + * type we defined above) and export it. + */ +export const StarWarsSchema: GraphQLSchema = new GraphQLSchema({ + query: queryType, + types: [humanType, droidType], +}); + +describe.skip('no starWarsSchema', () => { + it.todo('nope'); +}); diff --git a/packages/graphql/src/__tests__/starWarsValidation-test.ts b/packages/graphql/src/__tests__/starWarsValidation-test.ts new file mode 100644 index 00000000000..8c0f71aab94 --- /dev/null +++ b/packages/graphql/src/__tests__/starWarsValidation-test.ts @@ -0,0 +1,116 @@ +import { parse } from '../language/parser.js'; +import { Source } from '../language/source.js'; + +import { validate } from '../validation/validate.js'; + +import { StarWarsSchema } from './starWarsSchema.js'; + +/** + * Helper function to test a query and the expected response. + */ +function validationErrors(query: string) { + const source = new Source(query, 'StarWars.graphql'); + const ast = parse(source); + return validate(StarWarsSchema, ast); +} + +describe('Star Wars Validation Tests', () => { + describe('Basic Queries', () => { + it('Validates a complex but valid query', () => { + const query = ` + query NestedQueryWithFragment { + hero { + ...NameAndAppearances + friends { + ...NameAndAppearances + friends { + ...NameAndAppearances + } + } + } + } + + fragment NameAndAppearances on Character { + name + appearsIn + } + `; + return expect(validationErrors(query)).toHaveLength(0); + }); + + it('Notes that non-existent fields are invalid', () => { + const query = ` + query HeroSpaceshipQuery { + hero { + favoriteSpaceship + } + } + `; + return expect(validationErrors(query)).not.toHaveLength(0); + }); + + it('Requires fields on objects', () => { + const query = ` + query HeroNoFieldsQuery { + hero + } + `; + return expect(validationErrors(query)).not.toHaveLength(0); + }); + + it('Disallows fields on scalars', () => { + const query = ` + query HeroFieldsOnScalarQuery { + hero { + name { + firstCharacterOfName + } + } + } + `; + return expect(validationErrors(query)).not.toHaveLength(0); + }); + + it('Disallows object fields on interfaces', () => { + const query = ` + query DroidFieldOnCharacter { + hero { + name + primaryFunction + } + } + `; + return expect(validationErrors(query)).not.toHaveLength(0); + }); + + it('Allows object fields in fragments', () => { + const query = ` + query DroidFieldInFragment { + hero { + name + ...DroidFields + } + } + + fragment DroidFields on Droid { + primaryFunction + } + `; + return expect(validationErrors(query)).toHaveLength(0); + }); + + it('Allows object fields in inline fragments', () => { + const query = ` + query DroidFieldInFragment { + hero { + name + ... on Droid { + primaryFunction + } + } + } + `; + return expect(validationErrors(query)).toHaveLength(0); + }); + }); +}); diff --git a/packages/graphql/src/error/GraphQLError.ts b/packages/graphql/src/error/GraphQLError.ts new file mode 100644 index 00000000000..4f1e7a05cda --- /dev/null +++ b/packages/graphql/src/error/GraphQLError.ts @@ -0,0 +1,296 @@ +import { isObjectLike } from '../jsutils/isObjectLike.js'; +import type { Maybe } from '../jsutils/Maybe.js'; + +import type { ASTNode, Location } from '../language/ast.js'; +import type { SourceLocation } from '../language/location.js'; +import { getLocation } from '../language/location.js'; +import { printLocation, printSourceLocation } from '../language/printLocation.js'; +import type { Source } from '../language/source.js'; + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLErrorExtensions { + [attributeName: string]: unknown; +} + +export interface GraphQLErrorOptions { + nodes?: ReadonlyArray | ASTNode | null; + source?: Maybe; + positions?: Maybe>; + path?: Maybe>; + originalError?: Maybe; + extensions?: Maybe; +} + +type BackwardsCompatibleArgs = + | [options?: GraphQLErrorOptions] + | [ + nodes?: GraphQLErrorOptions['nodes'], + source?: GraphQLErrorOptions['source'], + positions?: GraphQLErrorOptions['positions'], + path?: GraphQLErrorOptions['path'], + originalError?: GraphQLErrorOptions['originalError'], + extensions?: GraphQLErrorOptions['extensions'] + ]; + +function toNormalizedOptions(args: BackwardsCompatibleArgs): GraphQLErrorOptions { + const firstArg = args[0]; + if (firstArg == null || 'kind' in firstArg || 'length' in firstArg) { + return { + nodes: firstArg, + source: args[1], + positions: args[2], + path: args[3], + originalError: args[4], + extensions: args[5], + }; + } + return firstArg; +} + +const isGraphQLErrorSymbol = Symbol.for('GraphQLError'); + +export function isGraphQLError(error: unknown): error is GraphQLError { + return typeof error === 'object' && error != null && isGraphQLErrorSymbol in error; +} + +/** + * A GraphQLError describes an Error found during the parse, validate, or + * execute phases of performing a GraphQL operation. In addition to a message + * and stack trace, it also includes information about the locations in a + * GraphQL document and/or execution result that correspond to the Error. + */ +export class GraphQLError extends Error { + readonly [isGraphQLErrorSymbol]: true = true; + /** + * An array of `{ line, column }` locations within the source GraphQL document + * which correspond to this error. + * + * Errors during validation often contain multiple locations, for example to + * point out two things with the same name. Errors during execution include a + * single location, the field which produced the error. + * + * Enumerable, and appears in the result of JSON.stringify(). + */ + readonly locations: ReadonlyArray | undefined; + + /** + * An array describing the JSON-path into the execution response which + * corresponds to this error. Only included for errors during execution. + * + * Enumerable, and appears in the result of JSON.stringify(). + */ + readonly path: ReadonlyArray | undefined; + + /** + * An array of GraphQL AST Nodes corresponding to this error. + */ + readonly nodes: ReadonlyArray | undefined; + + /** + * The source GraphQL document for the first location of this error. + * + * Note that if this Error represents more than one node, the source may not + * represent nodes after the first node. + */ + readonly source: Source | undefined; + + /** + * An array of character offsets within the source GraphQL document + * which correspond to this error. + */ + readonly positions: ReadonlyArray | undefined; + + /** + * The original error thrown from a field resolver during execution. + */ + readonly originalError: Error | undefined; + + /** + * Extension fields to add to the formatted error. + */ + readonly extensions: GraphQLErrorExtensions; + + constructor(message: string, options?: GraphQLErrorOptions); + /** + * @deprecated Please use the `GraphQLErrorOptions` constructor overload instead. + */ + constructor( + message: string, + nodes?: ReadonlyArray | ASTNode | null, + source?: Maybe, + positions?: Maybe>, + path?: Maybe>, + originalError?: Maybe, + extensions?: Maybe + ); + + constructor(message: string, ...rawArgs: BackwardsCompatibleArgs) { + const { nodes, source, positions, path, originalError, extensions } = toNormalizedOptions(rawArgs); + super(message); + + this.name = 'GraphQLError'; + this.path = path ?? undefined; + this.originalError = originalError ?? undefined; + + // Compute list of blame nodes. + this.nodes = undefinedIfEmpty(Array.isArray(nodes) ? nodes : nodes ? [nodes] : undefined); + + const nodeLocations = undefinedIfEmpty( + this.nodes?.map(node => node.loc).filter((loc): loc is Location => loc != null) + ); + + // Compute locations in the source for the given nodes/positions. + this.source = source ?? nodeLocations?.[0]?.source; + + this.positions = positions ?? nodeLocations?.map(loc => loc.start); + + this.locations = + positions && source + ? positions.map(pos => getLocation(source, pos)) + : nodeLocations?.map(loc => getLocation(loc.source, loc.start)); + + const originalExtensions = isObjectLike(originalError?.extensions) ? originalError?.extensions : undefined; + this.extensions = extensions ?? originalExtensions ?? Object.create(null); + + // Only properties prescribed by the spec should be enumerable. + // Keep the rest as non-enumerable. + Object.defineProperties(this, { + message: { + writable: true, + enumerable: true, + }, + name: { enumerable: false }, + nodes: { enumerable: false }, + source: { enumerable: false }, + positions: { enumerable: false }, + originalError: { enumerable: false }, + }); + + // Include (non-enumerable) stack trace. + /* c8 ignore start */ + // FIXME: https://github.com/graphql/graphql-js/issues/2317 + if (originalError?.stack) { + Object.defineProperty(this, 'stack', { + value: originalError.stack, + writable: true, + configurable: true, + }); + } else if (Error.captureStackTrace) { + Error.captureStackTrace(this, GraphQLError); + } else { + Object.defineProperty(this, 'stack', { + value: Error().stack, + writable: true, + configurable: true, + }); + } + /* c8 ignore stop */ + } + + get [Symbol.toStringTag](): string { + return 'GraphQLError'; + } + + toString(): string { + let output = this.message; + + if (this.nodes) { + for (const node of this.nodes) { + if (node.loc) { + output += '\n\n' + printLocation(node.loc); + } + } + } else if (this.source && this.locations) { + for (const location of this.locations) { + output += '\n\n' + printSourceLocation(this.source, location); + } + } + + return output; + } + + toJSON(): GraphQLFormattedError { + type WritableFormattedError = { + -readonly [P in keyof GraphQLFormattedError]: GraphQLFormattedError[P]; + }; + + const formattedError: WritableFormattedError = { + message: this.message, + }; + + if (this.locations != null) { + formattedError.locations = this.locations; + } + + if (this.path != null) { + formattedError.path = this.path; + } + + if (this.extensions != null && Object.keys(this.extensions).length > 0) { + formattedError.extensions = this.extensions; + } + + return formattedError; + } +} + +function undefinedIfEmpty(array: Array | undefined): Array | undefined { + return array === undefined || array.length === 0 ? undefined : array; +} + +/** + * See: https://spec.graphql.org/draft/#sec-Errors + */ +export interface GraphQLFormattedError { + /** + * A short, human-readable summary of the problem that **SHOULD NOT** change + * from occurrence to occurrence of the problem, except for purposes of + * localization. + */ + readonly message: string; + /** + * If an error can be associated to a particular point in the requested + * GraphQL document, it should contain a list of locations. + */ + readonly locations?: ReadonlyArray; + /** + * If an error can be associated to a particular field in the GraphQL result, + * it _must_ contain an entry with the key `path` that details the path of + * the response field which experienced the error. This allows clients to + * identify whether a null result is intentional or caused by a runtime error. + */ + readonly path?: ReadonlyArray; + /** + * Reserved for implementors to extend the protocol however they see fit, + * and hence there are no additional restrictions on its contents. + */ + readonly extensions?: { [key: string]: unknown }; +} + +/** + * Prints a GraphQLError to a string, representing useful location information + * about the error's position in the source. + * + * @deprecated Please use `error.toString` instead. Will be removed in v17 + */ +export function printError(error: GraphQLError): string { + return error.toString(); +} + +/** + * Given a GraphQLError, format it according to the rules described by the + * Response Format, Errors section of the GraphQL Specification. + * + * @deprecated Please use `error.toJSON` instead. Will be removed in v17 + */ +export function formatError(error: GraphQLError): GraphQLFormattedError { + return error.toJSON(); +} diff --git a/packages/graphql/src/error/__tests__/GraphQLError-test.ts b/packages/graphql/src/error/__tests__/GraphQLError-test.ts new file mode 100644 index 00000000000..99c97fc5cbe --- /dev/null +++ b/packages/graphql/src/error/__tests__/GraphQLError-test.ts @@ -0,0 +1,322 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import { Kind } from '../../language/kinds.js'; +import { parse } from '../../language/parser.js'; +import { Source } from '../../language/source.js'; + +import { formatError, GraphQLError } from '../GraphQLError.js'; + +const source = new Source(dedent` + { + field + } +`); +const ast = parse(source); +const operationNode = ast.definitions[0]; +expect(operationNode.kind === Kind.OPERATION_DEFINITION).toBeTruthy(); +// @ts-expect-error +const fieldNode = operationNode.selectionSet.selections[0]; +expect(fieldNode != null).toBeTruthy(); + +describe('GraphQLError', () => { + it('is a class and is a subclass of Error', () => { + expect(new GraphQLError('str')).toBeInstanceOf(Error); + expect(new GraphQLError('str')).toBeInstanceOf(GraphQLError); + }); + + it('has a name, message, extensions, and stack trace', () => { + const e = new GraphQLError('msg'); + + expect(e).toMatchObject({ + name: 'GraphQLError', + message: 'msg', + extensions: {}, + }); + expect(typeof e.stack === 'string').toBeTruthy(); + }); + + it('enumerate only properties prescribed by the spec', () => { + const e = new GraphQLError('msg' /* message */, { + nodes: [fieldNode], + source, + positions: [1, 2, 3], + path: ['a', 'b', 'c'], + originalError: new Error('test'), + extensions: { foo: 'bar' }, + }); + + expect(Object.keys(e)).toEqual(['message', 'locations', 'path', 'extensions']); + }); + + it('uses the stack of an original error', () => { + const original = new Error('original'); + const e = new GraphQLError('msg', { + originalError: original, + }); + + expect(e).toMatchObject({ + name: 'GraphQLError', + message: 'msg', + stack: original.stack, + originalError: original, + }); + }); + + it('creates new stack if original error has no stack', () => { + const original = new Error('original'); + const e = new GraphQLError('msg', { originalError: original }); + + expect(e).toMatchObject({ + name: 'GraphQLError', + message: 'msg', + originalError: original, + }); + expect(typeof e.stack === 'string').toBeTruthy(); + }); + + it('converts nodes to positions and locations', () => { + const e = new GraphQLError('msg', { nodes: [fieldNode] }); + expect(e).toMatchObject({ + source, + nodes: [fieldNode], + positions: [4], + locations: [{ line: 2, column: 3 }], + }); + }); + + it('converts single node to positions and locations', () => { + const e = new GraphQLError('msg', { nodes: fieldNode }); // Non-array value. + expect(e).toMatchObject({ + source, + nodes: [fieldNode], + positions: [4], + locations: [{ line: 2, column: 3 }], + }); + }); + + it('converts node with loc.start === 0 to positions and locations', () => { + const e = new GraphQLError('msg', { nodes: operationNode }); + expect(e).toMatchObject({ + source, + nodes: [operationNode], + positions: [0], + locations: [{ line: 1, column: 1 }], + }); + }); + + it('converts node without location to undefined source, positions and locations', () => { + const fieldNodeNoLocation = { + ...fieldNode, + loc: undefined, + }; + + const e = new GraphQLError('msg', { nodes: fieldNodeNoLocation }); + expect(e).toMatchObject({ + nodes: [fieldNodeNoLocation], + source: undefined, + positions: undefined, + locations: undefined, + }); + }); + + it('converts source and positions to locations', () => { + const e = new GraphQLError('msg', { source, positions: [6] }); + expect(e).toMatchObject({ + source, + nodes: undefined, + positions: [6], + locations: [{ line: 2, column: 5 }], + }); + }); + + it('defaults to original error extension only if extensions argument is not passed', () => { + class ErrorWithExtensions extends Error { + extensions: unknown; + + constructor(message: string) { + super(message); + this.extensions = { original: 'extensions' }; + } + } + + const original = new ErrorWithExtensions('original'); + const inheritedExtensions = new GraphQLError('InheritedExtensions', { + originalError: original, + }); + + expect(inheritedExtensions).toMatchObject({ + message: 'InheritedExtensions', + originalError: original, + extensions: { original: 'extensions' }, + }); + + const ownExtensions = new GraphQLError('OwnExtensions', { + originalError: original, + extensions: { own: 'extensions' }, + }); + + expect(ownExtensions).toMatchObject({ + message: 'OwnExtensions', + originalError: original, + extensions: { own: 'extensions' }, + }); + + const ownEmptyExtensions = new GraphQLError('OwnEmptyExtensions', { + originalError: original, + extensions: {}, + }); + + expect(ownEmptyExtensions).toMatchObject({ + message: 'OwnEmptyExtensions', + originalError: original, + extensions: {}, + }); + }); + + it('serializes to include all standard fields', () => { + const eShort = new GraphQLError('msg'); + expect(JSON.stringify(eShort, null, 2)).toEqual(dedent` + { + "message": "msg" + } + `); + + const path = ['path', 2, 'field']; + const extensions = { foo: 'bar' }; + const eFull = new GraphQLError('msg', { + nodes: fieldNode, + path, + extensions, + }); + + // We should try to keep order of fields stable + // Changing it wouldn't be breaking change but will fail some tests in other libraries. + expect(JSON.stringify(eFull, null, 2)).toEqual(dedent` + { + "message": "msg", + "locations": [ + { + "line": 2, + "column": 3 + } + ], + "path": [ + "path", + 2, + "field" + ], + "extensions": { + "foo": "bar" + } + } + `); + }); +}); + +describe('toString', () => { + it('prints an error without location', () => { + const error = new GraphQLError('Error without location'); + expect(error.toString()).toEqual('Error without location'); + }); + + it('prints an error using node without location', () => { + const error = new GraphQLError('Error attached to node without location', { + nodes: parse('{ foo }', { noLocation: true }), + }); + expect(error.toString()).toEqual('Error attached to node without location'); + }); + + it('prints an error with nodes from different sources', () => { + const docA = parse( + new Source( + dedent` + type Foo { + field: String + } + `, + 'SourceA' + ) + ); + const opA = docA.definitions[0]; + expect(opA.kind === Kind.OBJECT_TYPE_DEFINITION && opA.fields != null).toBeTruthy(); + // @ts-expect-error + const fieldA = opA.fields[0]; + + const docB = parse( + new Source( + dedent` + type Foo { + field: Int + } + `, + 'SourceB' + ) + ); + const opB = docB.definitions[0]; + expect(opB.kind === Kind.OBJECT_TYPE_DEFINITION && opB.fields != null).toBeTruthy(); + // @ts-expect-error + const fieldB = opB.fields[0]; + + const error = new GraphQLError('Example error with two nodes', { + nodes: [fieldA.type, fieldB.type], + }); + + expect(error.toString()).toEqual(dedent` + Example error with two nodes + + SourceA:2:10 + 1 | type Foo { + 2 | field: String + | ^ + 3 | } + + SourceB:2:10 + 1 | type Foo { + 2 | field: Int + | ^ + 3 | } + `); + }); +}); + +describe('toJSON', () => { + it('Deprecated: format an error using formatError', () => { + const error = new GraphQLError('Example Error'); + expect(formatError(error)).toMatchObject({ + message: 'Example Error', + }); + }); + + it('includes path', () => { + const error = new GraphQLError('msg', { path: ['path', 3, 'to', 'field'] }); + + expect(error.toJSON()).toMatchObject({ + message: 'msg', + path: ['path', 3, 'to', 'field'], + }); + }); + + it('includes extension fields', () => { + const error = new GraphQLError('msg', { + extensions: { foo: 'bar' }, + }); + + expect(error.toJSON()).toMatchObject({ + message: 'msg', + extensions: { foo: 'bar' }, + }); + }); + + it('can be created with the legacy argument list', () => { + const error = new GraphQLError('msg', [operationNode], source, [6], ['path', 2, 'a'], new Error('I like turtles'), { + hee: 'I like turtles', + }); + + expect(error.toJSON()).toMatchObject({ + message: 'msg', + locations: [{ column: 5, line: 2 }], + path: ['path', 2, 'a'], + extensions: { hee: 'I like turtles' }, + }); + }); +}); diff --git a/packages/graphql/src/error/__tests__/locatedError-test.ts b/packages/graphql/src/error/__tests__/locatedError-test.ts new file mode 100644 index 00000000000..bf134b3451a --- /dev/null +++ b/packages/graphql/src/error/__tests__/locatedError-test.ts @@ -0,0 +1,46 @@ +import { GraphQLError } from '../GraphQLError.js'; +import { locatedError } from '../locatedError.js'; + +describe('locatedError', () => { + it('passes GraphQLError through', () => { + const e = new GraphQLError('msg', { path: ['path', 3, 'to', 'field'] }); + + expect(locatedError(e, [], [])).toEqual(e); + }); + + it('wraps non-errors', () => { + const testObject = Object.freeze({}); + const error = locatedError(testObject, [], []); + + expect(error).toBeInstanceOf(GraphQLError); + expect(error.originalError).toMatchObject({ + name: 'NonErrorThrown', + thrownValue: testObject, + }); + }); + + it('passes GraphQLError-ish through', () => { + const e = new Error(); + // @ts-expect-error + e.locations = []; + // @ts-expect-error + e.path = []; + // @ts-expect-error + e.nodes = []; + // @ts-expect-error + e.source = null; + // @ts-expect-error + e.positions = []; + e.name = 'GraphQLError'; + + expect(locatedError(e, [], [])).toEqual(e); + }); + + it('does not pass through elasticsearch-like errors', () => { + const e = new Error('I am from elasticsearch'); + // @ts-expect-error + e.path = '/something/feed/_search'; + + expect(locatedError(e, [], [])).toEqual(e); + }); +}); diff --git a/packages/graphql/src/error/index.ts b/packages/graphql/src/error/index.ts new file mode 100644 index 00000000000..3defa260a2d --- /dev/null +++ b/packages/graphql/src/error/index.ts @@ -0,0 +1,3 @@ +export * from './GraphQLError.js'; +export * from './syntaxError.js'; +export * from './locatedError.js'; diff --git a/packages/graphql/src/error/locatedError.ts b/packages/graphql/src/error/locatedError.ts new file mode 100644 index 00000000000..fb1312fc9d9 --- /dev/null +++ b/packages/graphql/src/error/locatedError.ts @@ -0,0 +1,36 @@ +import type { Maybe } from '../jsutils/Maybe.js'; +import { toError } from '../jsutils/toError.js'; + +import type { ASTNode } from '../language/ast.js'; + +import { GraphQLError } from './GraphQLError.js'; + +/** + * Given an arbitrary value, presumably thrown while attempting to execute a + * GraphQL operation, produce a new GraphQLError aware of the location in the + * document responsible for the original Error. + */ +export function locatedError( + rawOriginalError: unknown, + nodes: ASTNode | ReadonlyArray | undefined | null, + path?: Maybe> +): GraphQLError { + const originalError = toError(rawOriginalError); + + // Note: this uses a brand-check to support GraphQL errors originating from other contexts. + if (isLocatedGraphQLError(originalError)) { + return originalError; + } + + return new GraphQLError(originalError.message, { + nodes: (originalError as GraphQLError).nodes ?? nodes, + source: (originalError as GraphQLError).source, + positions: (originalError as GraphQLError).positions, + path, + originalError, + }); +} + +function isLocatedGraphQLError(error: any): error is GraphQLError { + return Array.isArray(error.path); +} diff --git a/packages/graphql/src/error/syntaxError.ts b/packages/graphql/src/error/syntaxError.ts new file mode 100644 index 00000000000..a47d95f0f9d --- /dev/null +++ b/packages/graphql/src/error/syntaxError.ts @@ -0,0 +1,14 @@ +import type { Source } from '../language/source.js'; + +import { GraphQLError } from './GraphQLError.js'; + +/** + * Produces a GraphQLError representing a syntax error, containing useful + * descriptive information about the syntax error's position in the source. + */ +export function syntaxError(source: Source, position: number, description: string): GraphQLError { + return new GraphQLError(`Syntax Error: ${description}`, { + source, + positions: [position], + }); +} diff --git a/packages/graphql/src/execution/__tests__/abstract-test.ts b/packages/graphql/src/execution/__tests__/abstract-test.ts new file mode 100644 index 00000000000..7b30f3173a3 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/abstract-test.ts @@ -0,0 +1,633 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { parse } from '../../language/parser.js'; + +import { + assertInterfaceType, + GraphQLInterfaceType, + GraphQLList, + GraphQLObjectType, + GraphQLUnionType, +} from '../../type/definition.js'; +import { GraphQLBoolean, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { execute, executeSync } from '../execute.js'; + +async function executeQuery(args: { schema: GraphQLSchema; query: string; rootValue?: unknown }) { + const { schema, query, rootValue } = args; + const document = parse(query); + const result = executeSync({ + schema, + document, + rootValue, + contextValue: { async: false }, + }); + const asyncResult = await execute({ + schema, + document, + rootValue, + contextValue: { async: true }, + }); + + expectJSON(result).toDeepEqual(asyncResult); + return result; +} + +class Dog { + name: string; + woofs: boolean; + + constructor(name: string, woofs: boolean) { + this.name = name; + this.woofs = woofs; + } +} + +class Cat { + name: string; + meows: boolean; + + constructor(name: string, meows: boolean) { + this.name = name; + this.meows = meows; + } +} + +describe('Execute: Handles execution of abstract types', () => { + it('isTypeOf used to resolve runtime type for Interface', async () => { + const PetType = new GraphQLInterfaceType({ + name: 'Pet', + fields: { + name: { type: GraphQLString }, + }, + }); + + const DogType = new GraphQLObjectType({ + name: 'Dog', + interfaces: [PetType], + isTypeOf(obj, context) { + const isDog = obj instanceof Dog; + return context.async ? Promise.resolve(isDog) : isDog; + }, + fields: { + name: { type: GraphQLString }, + woofs: { type: GraphQLBoolean }, + }, + }); + + const CatType = new GraphQLObjectType({ + name: 'Cat', + interfaces: [PetType], + isTypeOf(obj, context) { + const isCat = obj instanceof Cat; + return context.async ? Promise.resolve(isCat) : isCat; + }, + fields: { + name: { type: GraphQLString }, + meows: { type: GraphQLBoolean }, + }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + pets: { + type: new GraphQLList(PetType), + resolve() { + return [new Dog('Odie', true), new Cat('Garfield', false)]; + }, + }, + }, + }), + types: [CatType, DogType], + }); + + const query = ` + { + pets { + name + ... on Dog { + woofs + } + ... on Cat { + meows + } + } + } + `; + + expect(await executeQuery({ schema, query })).toEqual({ + data: { + pets: [ + { + name: 'Odie', + woofs: true, + }, + { + name: 'Garfield', + meows: false, + }, + ], + }, + }); + }); + + it('isTypeOf can throw', async () => { + const PetType = new GraphQLInterfaceType({ + name: 'Pet', + fields: { + name: { type: GraphQLString }, + }, + }); + + const DogType = new GraphQLObjectType({ + name: 'Dog', + interfaces: [PetType], + isTypeOf(_source, context) { + const error = new Error('We are testing this error'); + if (context.async) { + return Promise.reject(error); + } + throw error; + }, + fields: { + name: { type: GraphQLString }, + woofs: { type: GraphQLBoolean }, + }, + }); + + const CatType = new GraphQLObjectType({ + name: 'Cat', + interfaces: [PetType], + isTypeOf: undefined, + fields: { + name: { type: GraphQLString }, + meows: { type: GraphQLBoolean }, + }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + pets: { + type: new GraphQLList(PetType), + resolve() { + return [new Dog('Odie', true), new Cat('Garfield', false)]; + }, + }, + }, + }), + types: [DogType, CatType], + }); + + const query = ` + { + pets { + name + ... on Dog { + woofs + } + ... on Cat { + meows + } + } + } + `; + + expectJSON(await executeQuery({ schema, query })).toDeepEqual({ + data: { + pets: [null, null], + }, + errors: [ + { + message: 'We are testing this error', + locations: [{ line: 3, column: 9 }], + path: ['pets', 0], + }, + { + message: 'We are testing this error', + locations: [{ line: 3, column: 9 }], + path: ['pets', 1], + }, + ], + }); + }); + + it('isTypeOf can return false', async () => { + const PetType = new GraphQLInterfaceType({ + name: 'Pet', + fields: { + name: { type: GraphQLString }, + }, + }); + + const DogType = new GraphQLObjectType({ + name: 'Dog', + interfaces: [PetType], + isTypeOf(_source, context) { + return context.async ? Promise.resolve(false) : false; + }, + fields: { + name: { type: GraphQLString }, + woofs: { type: GraphQLBoolean }, + }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + pet: { + type: PetType, + resolve: () => ({}), + }, + }, + }), + types: [DogType], + }); + + const query = ` + { + pet { + name + } + } + `; + + expectJSON(await executeQuery({ schema, query })).toDeepEqual({ + data: { pet: null }, + errors: [ + { + message: + 'Abstract type "Pet" must resolve to an Object type at runtime for field "Query.pet". Either the "Pet" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.', + locations: [{ line: 3, column: 9 }], + path: ['pet'], + }, + ], + }); + }); + + it('isTypeOf used to resolve runtime type for Union', async () => { + const DogType = new GraphQLObjectType({ + name: 'Dog', + isTypeOf(obj, context) { + const isDog = obj instanceof Dog; + return context.async ? Promise.resolve(isDog) : isDog; + }, + fields: { + name: { type: GraphQLString }, + woofs: { type: GraphQLBoolean }, + }, + }); + + const CatType = new GraphQLObjectType({ + name: 'Cat', + isTypeOf(obj, context) { + const isCat = obj instanceof Cat; + return context.async ? Promise.resolve(isCat) : isCat; + }, + fields: { + name: { type: GraphQLString }, + meows: { type: GraphQLBoolean }, + }, + }); + + const PetType = new GraphQLUnionType({ + name: 'Pet', + types: [DogType, CatType], + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + pets: { + type: new GraphQLList(PetType), + resolve() { + return [new Dog('Odie', true), new Cat('Garfield', false)]; + }, + }, + }, + }), + }); + + const query = `{ + pets { + ... on Dog { + name + woofs + } + ... on Cat { + name + meows + } + } + }`; + + expect(await executeQuery({ schema, query })).toEqual({ + data: { + pets: [ + { + name: 'Odie', + woofs: true, + }, + { + name: 'Garfield', + meows: false, + }, + ], + }, + }); + }); + + it('resolveType can throw', async () => { + const PetType = new GraphQLInterfaceType({ + name: 'Pet', + resolveType(_source, context) { + const error = new Error('We are testing this error'); + if (context.async) { + return Promise.reject(error); + } + throw error; + }, + fields: { + name: { type: GraphQLString }, + }, + }); + + const DogType = new GraphQLObjectType({ + name: 'Dog', + interfaces: [PetType], + fields: { + name: { type: GraphQLString }, + woofs: { type: GraphQLBoolean }, + }, + }); + + const CatType = new GraphQLObjectType({ + name: 'Cat', + interfaces: [PetType], + fields: { + name: { type: GraphQLString }, + meows: { type: GraphQLBoolean }, + }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + pets: { + type: new GraphQLList(PetType), + resolve() { + return [new Dog('Odie', true), new Cat('Garfield', false)]; + }, + }, + }, + }), + types: [CatType, DogType], + }); + + const query = ` + { + pets { + name + ... on Dog { + woofs + } + ... on Cat { + meows + } + } + } + `; + + expectJSON(await executeQuery({ schema, query })).toDeepEqual({ + data: { + pets: [null, null], + }, + errors: [ + { + message: 'We are testing this error', + locations: [{ line: 3, column: 9 }], + path: ['pets', 0], + }, + { + message: 'We are testing this error', + locations: [{ line: 3, column: 9 }], + path: ['pets', 1], + }, + ], + }); + }); + + it('resolve Union type using __typename on source object', async () => { + const schema = buildSchema(` + type Query { + pets: [Pet] + } + + union Pet = Cat | Dog + + type Cat { + name: String + meows: Boolean + } + + type Dog { + name: String + woofs: Boolean + } + `); + + const query = ` + { + pets { + name + ... on Dog { + woofs + } + ... on Cat { + meows + } + } + } + `; + + const rootValue = { + pets: [ + { + __typename: 'Dog', + name: 'Odie', + woofs: true, + }, + { + __typename: 'Cat', + name: 'Garfield', + meows: false, + }, + ], + }; + + expect(await executeQuery({ schema, query, rootValue })).toEqual({ + data: { + pets: [ + { + name: 'Odie', + woofs: true, + }, + { + name: 'Garfield', + meows: false, + }, + ], + }, + }); + }); + + it('resolve Interface type using __typename on source object', async () => { + const schema = buildSchema(` + type Query { + pets: [Pet] + } + + interface Pet { + name: String + } + + type Cat implements Pet { + name: String + meows: Boolean + } + + type Dog implements Pet { + name: String + woofs: Boolean + } + `); + + const query = ` + { + pets { + name + ... on Dog { + woofs + } + ... on Cat { + meows + } + } + } + `; + + const rootValue = { + pets: [ + { + __typename: 'Dog', + name: 'Odie', + woofs: true, + }, + { + __typename: 'Cat', + name: 'Garfield', + meows: false, + }, + ], + }; + + expect(await executeQuery({ schema, query, rootValue })).toEqual({ + data: { + pets: [ + { + name: 'Odie', + woofs: true, + }, + { + name: 'Garfield', + meows: false, + }, + ], + }, + }); + }); + + it('resolveType on Interface yields useful error', () => { + const schema = buildSchema(` + type Query { + pet: Pet + } + + interface Pet { + name: String + } + + type Cat implements Pet { + name: String + } + + type Dog implements Pet { + name: String + } + `); + + const document = parse(` + { + pet { + name + } + } + `); + + function expectError({ forTypeName }: { forTypeName: unknown }) { + const rootValue = { pet: { __typename: forTypeName } }; + const result = executeSync({ schema, document, rootValue }); + return { + toEqual(message: string) { + expectJSON(result).toDeepEqual({ + data: { pet: null }, + errors: [ + { + message, + locations: [{ line: 3, column: 9 }], + path: ['pet'], + }, + ], + }); + }, + }; + } + + expectError({ forTypeName: undefined }).toEqual( + 'Abstract type "Pet" must resolve to an Object type at runtime for field "Query.pet". Either the "Pet" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.' + ); + + expectError({ forTypeName: 'Human' }).toEqual( + 'Abstract type "Pet" was resolved to a type "Human" that does not exist inside the schema.' + ); + + expectError({ forTypeName: 'String' }).toEqual('Abstract type "Pet" was resolved to a non-object type "String".'); + + expectError({ forTypeName: '__Schema' }).toEqual( + 'Runtime Object type "__Schema" is not a possible type for "Pet".' + ); + + // FIXME: workaround since we can't inject resolveType into SDL + // @ts-expect-error + assertInterfaceType(schema.getType('Pet')).resolveType = () => []; + expectError({ forTypeName: undefined }).toEqual( + 'Abstract type "Pet" must resolve to an Object type at runtime for field "Query.pet" with value { __typename: undefined }, received "[]".' + ); + + // FIXME: workaround since we can't inject resolveType into SDL + // @ts-expect-error + assertInterfaceType(schema.getType('Pet')).resolveType = () => schema.getType('Cat'); + expectError({ forTypeName: undefined }).toEqual( + 'Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.' + ); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/directives-test.ts b/packages/graphql/src/execution/__tests__/directives-test.ts new file mode 100644 index 00000000000..9bd84dfb6ea --- /dev/null +++ b/packages/graphql/src/execution/__tests__/directives-test.ts @@ -0,0 +1,308 @@ +import { parse } from '../../language/parser.js'; + +import { GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { executeSync } from '../execute.js'; + +const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'TestType', + fields: { + a: { type: GraphQLString }, + b: { type: GraphQLString }, + }, + }), +}); + +const rootValue = { + a() { + return 'a'; + }, + b() { + return 'b'; + }, +}; + +function executeTestQuery(query: string) { + const document = parse(query); + return executeSync({ schema, document, rootValue }); +} + +describe('Execute: handles directives', () => { + describe('works without directives', () => { + it('basic query works', () => { + const result = executeTestQuery('{ a, b }'); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + }); + + describe('works on scalars', () => { + it('if true includes scalar', () => { + const result = executeTestQuery('{ a, b @include(if: true) }'); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('if false omits on scalar', () => { + const result = executeTestQuery('{ a, b @include(if: false) }'); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + + it('unless false includes scalar', () => { + const result = executeTestQuery('{ a, b @skip(if: false) }'); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('unless true omits scalar', () => { + const result = executeTestQuery('{ a, b @skip(if: true) }'); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + }); + + describe('works on fragment spreads', () => { + it('if false omits fragment spread', () => { + const result = executeTestQuery(` + query { + a + ...Frag @include(if: false) + } + fragment Frag on TestType { + b + } + `); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + + it('if true includes fragment spread', () => { + const result = executeTestQuery(` + query { + a + ...Frag @include(if: true) + } + fragment Frag on TestType { + b + } + `); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('unless false includes fragment spread', () => { + const result = executeTestQuery(` + query { + a + ...Frag @skip(if: false) + } + fragment Frag on TestType { + b + } + `); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('unless true omits fragment spread', () => { + const result = executeTestQuery(` + query { + a + ...Frag @skip(if: true) + } + fragment Frag on TestType { + b + } + `); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + }); + + describe('works on inline fragment', () => { + it('if false omits inline fragment', () => { + const result = executeTestQuery(` + query { + a + ... on TestType @include(if: false) { + b + } + } + `); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + + it('if true includes inline fragment', () => { + const result = executeTestQuery(` + query { + a + ... on TestType @include(if: true) { + b + } + } + `); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('unless false includes inline fragment', () => { + const result = executeTestQuery(` + query { + a + ... on TestType @skip(if: false) { + b + } + } + `); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('unless true includes inline fragment', () => { + const result = executeTestQuery(` + query { + a + ... on TestType @skip(if: true) { + b + } + } + `); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + }); + + describe('works on anonymous inline fragment', () => { + it('if false omits anonymous inline fragment', () => { + const result = executeTestQuery(` + query { + a + ... @include(if: false) { + b + } + } + `); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + + it('if true includes anonymous inline fragment', () => { + const result = executeTestQuery(` + query { + a + ... @include(if: true) { + b + } + } + `); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('unless false includes anonymous inline fragment', () => { + const result = executeTestQuery(` + query Q { + a + ... @skip(if: false) { + b + } + } + `); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('unless true includes anonymous inline fragment', () => { + const result = executeTestQuery(` + query { + a + ... @skip(if: true) { + b + } + } + `); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + }); + + describe('works with skip and include directives', () => { + it('include and no skip', () => { + const result = executeTestQuery(` + { + a + b @include(if: true) @skip(if: false) + } + `); + + expect(result).toEqual({ + data: { a: 'a', b: 'b' }, + }); + }); + + it('include and skip', () => { + const result = executeTestQuery(` + { + a + b @include(if: true) @skip(if: true) + } + `); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + + it('no include or skip', () => { + const result = executeTestQuery(` + { + a + b @include(if: false) @skip(if: false) + } + `); + + expect(result).toEqual({ + data: { a: 'a' }, + }); + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/executor-test.ts b/packages/graphql/src/execution/__tests__/executor-test.ts new file mode 100644 index 00000000000..7485e03a53c --- /dev/null +++ b/packages/graphql/src/execution/__tests__/executor-test.ts @@ -0,0 +1,1203 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { inspect } from '../../jsutils/inspect.js'; + +import { Kind } from '../../language/kinds.js'; +import { parse } from '../../language/parser.js'; + +import { + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, +} from '../../type/definition.js'; +import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { execute, executeSync } from '../execute.js'; + +describe('Execute: Handles basic execution tasks', () => { + it('executes arbitrary code', async () => { + const data = { + a: () => 'Apple', + b: () => 'Banana', + c: () => 'Cookie', + d: () => 'Donut', + e: () => 'Egg', + f: 'Fish', + // Called only by DataType::pic static resolver + pic: (size: number) => 'Pic of size: ' + size, + deep: () => deepData, + promise: promiseData, + }; + + const deepData = { + a: () => 'Already Been Done', + b: () => 'Boring', + c: () => ['Contrived', undefined, 'Confusing'], + deeper: () => [data, null, data], + }; + + function promiseData() { + return Promise.resolve(data); + } + + const DataType: GraphQLObjectType = new GraphQLObjectType({ + name: 'DataType', + fields: () => ({ + a: { type: GraphQLString }, + b: { type: GraphQLString }, + c: { type: GraphQLString }, + d: { type: GraphQLString }, + e: { type: GraphQLString }, + f: { type: GraphQLString }, + pic: { + args: { size: { type: GraphQLInt } }, + type: GraphQLString, + resolve: (obj, { size }) => obj.pic(size), + }, + deep: { type: DeepDataType }, + promise: { type: DataType }, + }), + }); + + const DeepDataType = new GraphQLObjectType({ + name: 'DeepDataType', + fields: { + a: { type: GraphQLString }, + b: { type: GraphQLString }, + c: { type: new GraphQLList(GraphQLString) }, + deeper: { type: new GraphQLList(DataType) }, + }, + }); + + const document = parse(` + query ($size: Int) { + a, + b, + x: c + ...c + f + ...on DataType { + pic(size: $size) + promise { + a + } + } + deep { + a + b + c + deeper { + a + b + } + } + } + + fragment c on DataType { + d + e + } + `); + + const result = await execute({ + schema: new GraphQLSchema({ query: DataType }), + document, + rootValue: data, + variableValues: { size: 100 }, + }); + + expect(result).toEqual({ + data: { + a: 'Apple', + b: 'Banana', + x: 'Cookie', + d: 'Donut', + e: 'Egg', + f: 'Fish', + pic: 'Pic of size: 100', + promise: { a: 'Apple' }, + deep: { + a: 'Already Been Done', + b: 'Boring', + c: ['Contrived', null, 'Confusing'], + deeper: [{ a: 'Apple', b: 'Banana' }, null, { a: 'Apple', b: 'Banana' }], + }, + }, + }); + }); + + it('merges parallel fragments', () => { + const Type: GraphQLObjectType = new GraphQLObjectType({ + name: 'Type', + fields: () => ({ + a: { type: GraphQLString, resolve: () => 'Apple' }, + b: { type: GraphQLString, resolve: () => 'Banana' }, + c: { type: GraphQLString, resolve: () => 'Cherry' }, + deep: { type: Type, resolve: () => ({}) }, + }), + }); + const schema = new GraphQLSchema({ query: Type }); + + const document = parse(` + { a, ...FragOne, ...FragTwo } + + fragment FragOne on Type { + b + deep { b, deeper: deep { b } } + } + + fragment FragTwo on Type { + c + deep { c, deeper: deep { c } } + } + `); + + const result = executeSync({ schema, document }); + expect(result).toEqual({ + data: { + a: 'Apple', + b: 'Banana', + c: 'Cherry', + deep: { + b: 'Banana', + c: 'Cherry', + deeper: { + b: 'Banana', + c: 'Cherry', + }, + }, + }, + }); + }); + + it('provides info about current execution state', () => { + let resolvedInfo; + const testType = new GraphQLObjectType({ + name: 'Test', + fields: { + test: { + type: GraphQLString, + resolve(_val, _args, _ctx, info) { + resolvedInfo = info; + }, + }, + }, + }); + const schema = new GraphQLSchema({ query: testType }); + + const document = parse('query ($var: String) { result: test }'); + const rootValue = { root: 'val' }; + const variableValues = { var: 'abc' }; + + executeSync({ schema, document, rootValue, variableValues }); + + // @ts-expect-error + expect(Object.keys(resolvedInfo)).toEqual([ + 'fieldName', + 'fieldNodes', + 'returnType', + 'parentType', + 'path', + 'schema', + 'fragments', + 'rootValue', + 'operation', + 'variableValues', + ]); + + const operation = document.definitions[0]; + expect(operation.kind === Kind.OPERATION_DEFINITION).toBeTruthy(); + + expect(resolvedInfo).toMatchObject({ + fieldName: 'test', + returnType: GraphQLString, + parentType: testType, + schema, + rootValue, + operation, + }); + + // @ts-expect-error + const field = operation.selectionSet.selections[0]; + expect(resolvedInfo).toMatchObject({ + fieldNodes: [field], + path: { prev: undefined, key: 'result', typename: 'Test' }, + variableValues: { var: 'abc' }, + }); + }); + + it('populates path correctly with complex types', () => { + let path; + const someObject = new GraphQLObjectType({ + name: 'SomeObject', + fields: { + test: { + type: GraphQLString, + resolve(_val, _args, _ctx, info) { + path = info.path; + }, + }, + }, + }); + const someUnion = new GraphQLUnionType({ + name: 'SomeUnion', + types: [someObject], + resolveType() { + return 'SomeObject'; + }, + }); + const testType = new GraphQLObjectType({ + name: 'SomeQuery', + fields: { + test: { + type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(someUnion))), + }, + }, + }); + const schema = new GraphQLSchema({ query: testType }); + const rootValue = { test: [{}] }; + const document = parse(` + query { + l1: test { + ... on SomeObject { + l2: test + } + } + } + `); + + executeSync({ schema, document, rootValue }); + + expect(path).toEqual({ + key: 'l2', + typename: 'SomeObject', + prev: { + key: 0, + typename: undefined, + prev: { + key: 'l1', + typename: 'SomeQuery', + prev: undefined, + }, + }, + }); + }); + + it('threads root value context correctly', () => { + let resolvedRootValue; + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { + type: GraphQLString, + resolve(rootValueArg) { + resolvedRootValue = rootValueArg; + }, + }, + }, + }), + }); + + const document = parse('query Example { a }'); + const rootValue = { contextThing: 'thing' }; + + executeSync({ schema, document, rootValue }); + expect(resolvedRootValue).toEqual(rootValue); + }); + + it('correctly threads arguments', () => { + let resolvedArgs; + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + b: { + args: { + numArg: { type: GraphQLInt }, + stringArg: { type: GraphQLString }, + }, + type: GraphQLString, + resolve(_, args) { + resolvedArgs = args; + }, + }, + }, + }), + }); + + const document = parse(` + query Example { + b(numArg: 123, stringArg: "foo") + } + `); + + executeSync({ schema, document }); + expect(resolvedArgs).toEqual({ numArg: 123, stringArg: 'foo' }); + }); + + it('nulls out error subtrees', async () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + sync: { type: GraphQLString }, + syncError: { type: GraphQLString }, + syncRawError: { type: GraphQLString }, + syncReturnError: { type: GraphQLString }, + syncReturnErrorList: { type: new GraphQLList(GraphQLString) }, + async: { type: GraphQLString }, + asyncReject: { type: GraphQLString }, + asyncRejectWithExtensions: { type: GraphQLString }, + asyncRawReject: { type: GraphQLString }, + asyncEmptyReject: { type: GraphQLString }, + asyncError: { type: GraphQLString }, + asyncRawError: { type: GraphQLString }, + asyncReturnError: { type: GraphQLString }, + asyncReturnErrorWithExtensions: { type: GraphQLString }, + }, + }), + }); + + const document = parse(` + { + sync + syncError + syncRawError + syncReturnError + syncReturnErrorList + async + asyncReject + asyncRawReject + asyncEmptyReject + asyncError + asyncRawError + asyncReturnError + asyncReturnErrorWithExtensions + } + `); + + const rootValue = { + sync() { + return 'sync'; + }, + syncError() { + throw new Error('Error getting syncError'); + }, + syncRawError() { + throw 'Error getting syncRawError'; + }, + syncReturnError() { + return new Error('Error getting syncReturnError'); + }, + syncReturnErrorList() { + return [ + 'sync0', + new Error('Error getting syncReturnErrorList1'), + 'sync2', + new Error('Error getting syncReturnErrorList3'), + ]; + }, + async() { + return new Promise(resolve => resolve('async')); + }, + asyncReject() { + return new Promise((_, reject) => reject(new Error('Error getting asyncReject'))); + }, + asyncRawReject() { + // eslint-disable-next-line prefer-promise-reject-errors + return Promise.reject('Error getting asyncRawReject'); + }, + asyncEmptyReject() { + // eslint-disable-next-line prefer-promise-reject-errors + return Promise.reject(); + }, + asyncError() { + return new Promise(() => { + throw new Error('Error getting asyncError'); + }); + }, + asyncRawError() { + return new Promise(() => { + throw 'Error getting asyncRawError'; + }); + }, + asyncReturnError() { + return Promise.resolve(new Error('Error getting asyncReturnError')); + }, + asyncReturnErrorWithExtensions() { + const error = new Error('Error getting asyncReturnErrorWithExtensions'); + // @ts-expect-error + error.extensions = { foo: 'bar' }; + + return Promise.resolve(error); + }, + }; + + const result = await execute({ schema, document, rootValue }); + expectJSON(result).toDeepEqual({ + data: { + sync: 'sync', + syncError: null, + syncRawError: null, + syncReturnError: null, + syncReturnErrorList: ['sync0', null, 'sync2', null], + async: 'async', + asyncReject: null, + asyncRawReject: null, + asyncEmptyReject: null, + asyncError: null, + asyncRawError: null, + asyncReturnError: null, + asyncReturnErrorWithExtensions: null, + }, + errors: [ + { + message: 'Error getting syncError', + locations: [{ line: 4, column: 9 }], + path: ['syncError'], + }, + { + message: 'Unexpected error value: "Error getting syncRawError"', + locations: [{ line: 5, column: 9 }], + path: ['syncRawError'], + }, + { + message: 'Error getting syncReturnError', + locations: [{ line: 6, column: 9 }], + path: ['syncReturnError'], + }, + { + message: 'Error getting syncReturnErrorList1', + locations: [{ line: 7, column: 9 }], + path: ['syncReturnErrorList', 1], + }, + { + message: 'Error getting syncReturnErrorList3', + locations: [{ line: 7, column: 9 }], + path: ['syncReturnErrorList', 3], + }, + { + message: 'Error getting asyncReject', + locations: [{ line: 9, column: 9 }], + path: ['asyncReject'], + }, + { + message: 'Unexpected error value: "Error getting asyncRawReject"', + locations: [{ line: 10, column: 9 }], + path: ['asyncRawReject'], + }, + { + message: 'Unexpected error value: undefined', + locations: [{ line: 11, column: 9 }], + path: ['asyncEmptyReject'], + }, + { + message: 'Error getting asyncError', + locations: [{ line: 12, column: 9 }], + path: ['asyncError'], + }, + { + message: 'Unexpected error value: "Error getting asyncRawError"', + locations: [{ line: 13, column: 9 }], + path: ['asyncRawError'], + }, + { + message: 'Error getting asyncReturnError', + locations: [{ line: 14, column: 9 }], + path: ['asyncReturnError'], + }, + { + message: 'Error getting asyncReturnErrorWithExtensions', + locations: [{ line: 15, column: 9 }], + path: ['asyncReturnErrorWithExtensions'], + extensions: { foo: 'bar' }, + }, + ], + }); + }); + + it('nulls error subtree for promise rejection #1071', async () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + foods: { + type: new GraphQLList( + new GraphQLObjectType({ + name: 'Food', + fields: { + name: { type: GraphQLString }, + }, + }) + ), + resolve() { + return Promise.reject(new Error('Oops')); + }, + }, + }, + }), + }); + + const document = parse(` + query { + foods { + name + } + } + `); + + const result = await execute({ schema, document }); + + expectJSON(result).toDeepEqual({ + data: { foods: null }, + errors: [ + { + locations: [{ column: 9, line: 3 }], + message: 'Oops', + path: ['foods'], + }, + ], + }); + }); + + it('Full response path is included for non-nullable fields', () => { + const A: GraphQLObjectType = new GraphQLObjectType({ + name: 'A', + fields: () => ({ + nullableA: { + type: A, + resolve: () => ({}), + }, + nonNullA: { + type: new GraphQLNonNull(A), + resolve: () => ({}), + }, + throws: { + type: new GraphQLNonNull(GraphQLString), + resolve: () => { + throw new Error('Catch me if you can'); + }, + }, + }), + }); + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'query', + fields: () => ({ + nullableA: { + type: A, + resolve: () => ({}), + }, + }), + }), + }); + + const document = parse(` + query { + nullableA { + aliasedA: nullableA { + nonNullA { + anotherA: nonNullA { + throws + } + } + } + } + } + `); + + const result = executeSync({ schema, document }); + expectJSON(result).toDeepEqual({ + data: { + nullableA: { + aliasedA: null, + }, + }, + errors: [ + { + message: 'Catch me if you can', + locations: [{ line: 7, column: 17 }], + path: ['nullableA', 'aliasedA', 'nonNullA', 'anotherA', 'throws'], + }, + ], + }); + }); + + it('uses the inline operation if no operation name is provided', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse('{ a }'); + const rootValue = { a: 'b' }; + + const result = executeSync({ schema, document, rootValue }); + expect(result).toEqual({ data: { a: 'b' } }); + }); + + it('uses the only operation if no operation name is provided', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse('query Example { a }'); + const rootValue = { a: 'b' }; + + const result = executeSync({ schema, document, rootValue }); + expect(result).toEqual({ data: { a: 'b' } }); + }); + + it('uses the named operation if operation name is provided', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + + const document = parse(` + query Example { first: a } + query OtherExample { second: a } + `); + const rootValue = { a: 'b' }; + const operationName = 'OtherExample'; + + const result = executeSync({ schema, document, rootValue, operationName }); + expect(result).toEqual({ data: { second: 'b' } }); + }); + + it('provides error if no operation is provided', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse('fragment Example on Type { a }'); + const rootValue = { a: 'b' }; + + const result = executeSync({ schema, document, rootValue }); + expectJSON(result).toDeepEqual({ + errors: [{ message: 'Must provide an operation.' }], + }); + }); + + it('errors if no op name is provided with multiple operations', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse(` + query Example { a } + query OtherExample { a } + `); + + const result = executeSync({ schema, document }); + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Must provide operation name if query contains multiple operations.', + }, + ], + }); + }); + + it('errors if unknown operation name is provided', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse(` + query Example { a } + query OtherExample { a } + `); + const operationName = 'UnknownExample'; + + const result = executeSync({ schema, document, operationName }); + expectJSON(result).toDeepEqual({ + errors: [{ message: 'Unknown operation named "UnknownExample".' }], + }); + }); + + it('errors if empty string is provided as operation name', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse('{ a }'); + const operationName = ''; + + const result = executeSync({ schema, document, operationName }); + expectJSON(result).toDeepEqual({ + errors: [{ message: 'Unknown operation named "".' }], + }); + }); + + it('uses the query schema for queries', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Q', + fields: { + a: { type: GraphQLString }, + }, + }), + mutation: new GraphQLObjectType({ + name: 'M', + fields: { + c: { type: GraphQLString }, + }, + }), + subscription: new GraphQLObjectType({ + name: 'S', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse(` + query Q { a } + mutation M { c } + subscription S { a } + `); + const rootValue = { a: 'b', c: 'd' }; + const operationName = 'Q'; + + const result = executeSync({ schema, document, rootValue, operationName }); + expect(result).toEqual({ data: { a: 'b' } }); + }); + + it('uses the mutation schema for mutations', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Q', + fields: { + a: { type: GraphQLString }, + }, + }), + mutation: new GraphQLObjectType({ + name: 'M', + fields: { + c: { type: GraphQLString }, + }, + }), + }); + const document = parse(` + query Q { a } + mutation M { c } + `); + const rootValue = { a: 'b', c: 'd' }; + const operationName = 'M'; + + const result = executeSync({ schema, document, rootValue, operationName }); + expect(result).toEqual({ data: { c: 'd' } }); + }); + + it('uses the subscription schema for subscriptions', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Q', + fields: { + a: { type: GraphQLString }, + }, + }), + subscription: new GraphQLObjectType({ + name: 'S', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse(` + query Q { a } + subscription S { a } + `); + const rootValue = { a: 'b', c: 'd' }; + const operationName = 'S'; + + const result = executeSync({ schema, document, rootValue, operationName }); + expect(result).toEqual({ data: { a: 'b' } }); + }); + + it('resolves to an error if schema does not support operation', () => { + const schema = new GraphQLSchema({ assumeValid: true }); + + const document = parse(` + query Q { __typename } + mutation M { __typename } + subscription S { __typename } + `); + + expectJSON(executeSync({ schema, document, operationName: 'Q' })).toDeepEqual({ + data: null, + errors: [ + { + message: 'Schema is not configured to execute query operation.', + locations: [{ line: 2, column: 7 }], + }, + ], + }); + + expectJSON(executeSync({ schema, document, operationName: 'M' })).toDeepEqual({ + data: null, + errors: [ + { + message: 'Schema is not configured to execute mutation operation.', + locations: [{ line: 3, column: 7 }], + }, + ], + }); + + expectJSON(executeSync({ schema, document, operationName: 'S' })).toDeepEqual({ + data: null, + errors: [ + { + message: 'Schema is not configured to execute subscription operation.', + locations: [{ line: 4, column: 7 }], + }, + ], + }); + }); + + it('correct field ordering despite execution order', async () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + b: { type: GraphQLString }, + c: { type: GraphQLString }, + d: { type: GraphQLString }, + e: { type: GraphQLString }, + }, + }), + }); + const document = parse('{ a, b, c, d, e }'); + const rootValue = { + a: () => 'a', + b: () => new Promise(resolve => resolve('b')), + c: () => 'c', + d: () => new Promise(resolve => resolve('d')), + e: () => 'e', + }; + + const result = await execute({ schema, document, rootValue }); + expect(result).toEqual({ + data: { a: 'a', b: 'b', c: 'c', d: 'd', e: 'e' }, + }); + }); + + it('Avoids recursion', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse(` + { + a + ...Frag + ...Frag + } + + fragment Frag on Type { + a, + ...Frag + } + `); + const rootValue = { a: 'b' }; + + const result = executeSync({ schema, document, rootValue }); + expect(result).toEqual({ + data: { a: 'b' }, + }); + }); + + it('ignores missing sub selections on fields', () => { + const someType = new GraphQLObjectType({ + name: 'SomeType', + fields: { + b: { type: GraphQLString }, + }, + }); + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + a: { type: someType }, + }, + }), + }); + const document = parse('{ a }'); + const rootValue = { a: { b: 'c' } }; + + const result = executeSync({ schema, document, rootValue }); + expect(result).toEqual({ + data: { a: {} }, + }); + }); + + it('does not include illegal fields in output', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Q', + fields: { + a: { type: GraphQLString }, + }, + }), + }); + const document = parse('{ thisIsIllegalDoNotIncludeMe }'); + + const result = executeSync({ schema, document }); + expect(result).toEqual({ + data: {}, + }); + }); + + it('does not include arguments that were not set', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Type', + fields: { + field: { + type: GraphQLString, + resolve: (_source, args) => inspect(args), + args: { + a: { type: GraphQLBoolean }, + b: { type: GraphQLBoolean }, + c: { type: GraphQLBoolean }, + d: { type: GraphQLInt }, + e: { type: GraphQLInt }, + }, + }, + }, + }), + }); + const document = parse('{ field(a: true, c: false, e: 0) }'); + + const result = executeSync({ schema, document }); + expect(result).toEqual({ + data: { + field: '{ a: true, c: false, e: 0 }', + }, + }); + }); + + it('fails when an isTypeOf check is not met', async () => { + class Special { + value: string; + + constructor(value: string) { + this.value = value; + } + } + + class NotSpecial { + value: string; + + constructor(value: string) { + this.value = value; + } + } + + const SpecialType = new GraphQLObjectType({ + name: 'SpecialType', + isTypeOf(obj, context) { + const result = obj instanceof Special; + return context?.async ? Promise.resolve(result) : result; + }, + fields: { value: { type: GraphQLString } }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + specials: { type: new GraphQLList(SpecialType) }, + }, + }), + }); + + const document = parse('{ specials { value } }'); + const rootValue = { + specials: [new Special('foo'), new NotSpecial('bar')], + }; + + const result = executeSync({ schema, document, rootValue }); + expectJSON(result).toDeepEqual({ + data: { + specials: [{ value: 'foo' }, null], + }, + errors: [ + { + message: 'Expected value of type "SpecialType" but got: { value: "bar" }.', + locations: [{ line: 1, column: 3 }], + path: ['specials', 1], + }, + ], + }); + + const contextValue = { async: true }; + const asyncResult = await execute({ + schema, + document, + rootValue, + contextValue, + }); + expect(asyncResult).toEqual(result); + }); + + it('fails when serialize of custom scalar does not return a value', () => { + const customScalar = new GraphQLScalarType({ + name: 'CustomScalar', + serialize() { + /* returns nothing */ + }, + }); + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + customScalar: { + type: customScalar, + resolve: () => 'CUSTOM_VALUE', + }, + }, + }), + }); + + const result = executeSync({ schema, document: parse('{ customScalar }') }); + expectJSON(result).toDeepEqual({ + data: { customScalar: null }, + errors: [ + { + message: + 'Expected `CustomScalar.serialize("CUSTOM_VALUE")` to return non-nullable value, returned: undefined', + locations: [{ line: 1, column: 3 }], + path: ['customScalar'], + }, + ], + }); + }); + + it('executes ignoring invalid non-executable definitions', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + foo: { type: GraphQLString }, + }, + }), + }); + + const document = parse(` + { foo } + + type Query { bar: String } + `); + + const result = executeSync({ schema, document }); + expect(result).toEqual({ data: { foo: null } }); + }); + + it('uses a custom field resolver', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + foo: { type: GraphQLString }, + }, + }), + }); + const document = parse('{ foo }'); + + const result = executeSync({ + schema, + document, + fieldResolver(_source, _args, _context, info) { + // For the purposes of test, just return the name of the field! + return info.fieldName; + }, + }); + + expect(result).toEqual({ data: { foo: 'foo' } }); + }); + + it('uses a custom type resolver', () => { + const document = parse('{ foo { bar } }'); + + const fooInterface = new GraphQLInterfaceType({ + name: 'FooInterface', + fields: { + bar: { type: GraphQLString }, + }, + }); + + const fooObject = new GraphQLObjectType({ + name: 'FooObject', + interfaces: [fooInterface], + fields: { + bar: { type: GraphQLString }, + }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + foo: { type: fooInterface }, + }, + }), + types: [fooObject], + }); + + const rootValue = { foo: { bar: 'bar' } }; + + let possibleTypes; + const result = executeSync({ + schema, + document, + rootValue, + typeResolver(_source, _context, info, abstractType) { + // Resolver should be able to figure out all possible types on its own + possibleTypes = info.schema.getPossibleTypes(abstractType); + + return 'FooObject'; + }, + }); + + expect(result).toEqual({ data: { foo: { bar: 'bar' } } }); + expect(possibleTypes).toEqual([fooObject]); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/lists-test.ts b/packages/graphql/src/execution/__tests__/lists-test.ts new file mode 100644 index 00000000000..907ee0309c7 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/lists-test.ts @@ -0,0 +1,392 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import type { PromiseOrValue } from '../../jsutils/PromiseOrValue.js'; + +import { parse } from '../../language/parser.js'; + +import type { GraphQLFieldResolver } from '../../type/definition.js'; +import { GraphQLList, GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import type { ExecutionResult } from '../execute.js'; +import { execute, executeSync } from '../execute.js'; + +describe('Execute: Accepts any iterable as list value', () => { + function complete(rootValue: unknown) { + return executeSync({ + schema: buildSchema('type Query { listField: [String] }'), + document: parse('{ listField }'), + rootValue, + }); + } + + it('Accepts a Set as a List value', () => { + const listField = new Set(['apple', 'banana', 'apple', 'coconut']); + + expect(complete({ listField })).toEqual({ + data: { listField: ['apple', 'banana', 'coconut'] }, + }); + }); + + it('Accepts an Generator function as a List value', () => { + function* listField() { + yield 'one'; + yield 2; + yield true; + } + + expect(complete({ listField })).toEqual({ + data: { listField: ['one', '2', 'true'] }, + }); + }); + + it('Accepts function arguments as a List value', () => { + function getArgs(..._args: ReadonlyArray) { + return arguments; + } + const listField = getArgs('one', 'two'); + + expect(complete({ listField })).toEqual({ + data: { listField: ['one', 'two'] }, + }); + }); + + it('Does not accept (Iterable) String-literal as a List value', () => { + const listField = 'Singular'; + + expectJSON(complete({ listField })).toDeepEqual({ + data: { listField: null }, + errors: [ + { + message: 'Expected Iterable, but did not find one for field "Query.listField".', + locations: [{ line: 1, column: 3 }], + path: ['listField'], + }, + ], + }); + }); +}); + +describe('Execute: Accepts async iterables as list value', () => { + function complete(rootValue: unknown, as: string = '[String]') { + return execute({ + schema: buildSchema(`type Query { listField: ${as} }`), + document: parse('{ listField }'), + rootValue, + }); + } + + function completeObjectList( + resolve: GraphQLFieldResolver<{ index: number }, unknown> + ): PromiseOrValue { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + listField: { + resolve: async function* listField() { + yield await Promise.resolve({ index: 0 }); + yield await Promise.resolve({ index: 1 }); + yield await Promise.resolve({ index: 2 }); + }, + type: new GraphQLList( + new GraphQLObjectType({ + name: 'ObjectWrapper', + fields: { + index: { + type: GraphQLString, + resolve, + }, + }, + }) + ), + }, + }, + }), + }); + return execute({ + schema, + document: parse('{ listField { index } }'), + }); + } + + it('Accepts an AsyncGenerator function as a List value', async () => { + async function* listField() { + yield await Promise.resolve('two'); + yield await Promise.resolve(4); + yield await Promise.resolve(false); + } + + expectJSON(await complete({ listField })).toDeepEqual({ + data: { listField: ['two', '4', 'false'] }, + }); + }); + + it('Handles an AsyncGenerator function that throws', async () => { + async function* listField() { + yield await Promise.resolve('two'); + yield await Promise.resolve(4); + throw new Error('bad'); + } + + expectJSON(await complete({ listField })).toDeepEqual({ + data: { listField: ['two', '4', null] }, + errors: [ + { + message: 'bad', + locations: [{ line: 1, column: 3 }], + path: ['listField', 2], + }, + ], + }); + }); + + it('Handles an AsyncGenerator function where an intermediate value triggers an error', async () => { + async function* listField() { + yield await Promise.resolve('two'); + yield await Promise.resolve({}); + yield await Promise.resolve(4); + } + + expectJSON(await complete({ listField })).toDeepEqual({ + data: { listField: ['two', null, '4'] }, + errors: [ + { + message: 'String cannot represent value: {}', + locations: [{ line: 1, column: 3 }], + path: ['listField', 1], + }, + ], + }); + }); + + it('Handles errors from `completeValue` in AsyncIterables', async () => { + async function* listField() { + yield await Promise.resolve('two'); + yield await Promise.resolve({}); + } + + expectJSON(await complete({ listField })).toDeepEqual({ + data: { listField: ['two', null] }, + errors: [ + { + message: 'String cannot represent value: {}', + locations: [{ line: 1, column: 3 }], + path: ['listField', 1], + }, + ], + }); + }); + + it('Handles promises from `completeValue` in AsyncIterables', async () => { + expectJSON(await completeObjectList(({ index }) => Promise.resolve(index))).toDeepEqual({ + data: { listField: [{ index: '0' }, { index: '1' }, { index: '2' }] }, + }); + }); + + it('Handles rejected promises from `completeValue` in AsyncIterables', async () => { + expectJSON( + await completeObjectList(({ index }) => { + if (index === 2) { + return Promise.reject(new Error('bad')); + } + return Promise.resolve(index); + }) + ).toDeepEqual({ + data: { listField: [{ index: '0' }, { index: '1' }, { index: null }] }, + errors: [ + { + message: 'bad', + locations: [{ line: 1, column: 15 }], + path: ['listField', 2, 'index'], + }, + ], + }); + }); + it('Handles nulls yielded by async generator', async () => { + async function* listField() { + yield await Promise.resolve(1); + yield await Promise.resolve(null); + yield await Promise.resolve(2); + } + const errors = [ + { + message: 'Cannot return null for non-nullable field Query.listField.', + locations: [{ line: 1, column: 3 }], + path: ['listField', 1], + }, + ]; + + expect(await complete({ listField }, '[Int]')).toEqual({ + data: { listField: [1, null, 2] }, + }); + expect(await complete({ listField }, '[Int]!')).toEqual({ + data: { listField: [1, null, 2] }, + }); + expectJSON(await complete({ listField }, '[Int!]')).toDeepEqual({ + data: { listField: null }, + errors, + }); + expectJSON(await complete({ listField }, '[Int!]!')).toDeepEqual({ + data: null, + errors, + }); + }); +}); + +describe('Execute: Handles list nullability', () => { + async function complete(args: { listField: unknown; as: string }) { + const { listField, as } = args; + const schema = buildSchema(`type Query { listField: ${as} }`); + const document = parse('{ listField }'); + + const result = await executeQuery(listField); + // Promise> === Array + expectJSON(await executeQuery(promisify(listField))).toDeepEqual(result); + if (Array.isArray(listField)) { + const listOfPromises = listField.map(promisify); + + // Array> === Array + expectJSON(await executeQuery(listOfPromises)).toDeepEqual(result); + // Promise>> === Array + expectJSON(await executeQuery(promisify(listOfPromises))).toDeepEqual(result); + } + return result; + + function executeQuery(listValue: unknown) { + return execute({ schema, document, rootValue: { listField: listValue } }); + } + + function promisify(value: unknown): Promise { + return value instanceof Error ? Promise.reject(value) : Promise.resolve(value); + } + } + + it('Contains values', async () => { + const listField = [1, 2]; + + expect(await complete({ listField, as: '[Int]' })).toEqual({ + data: { listField: [1, 2] }, + }); + expect(await complete({ listField, as: '[Int]!' })).toEqual({ + data: { listField: [1, 2] }, + }); + expect(await complete({ listField, as: '[Int!]' })).toEqual({ + data: { listField: [1, 2] }, + }); + expect(await complete({ listField, as: '[Int!]!' })).toEqual({ + data: { listField: [1, 2] }, + }); + }); + + it('Contains null', async () => { + const listField = [1, null, 2]; + const errors = [ + { + message: 'Cannot return null for non-nullable field Query.listField.', + locations: [{ line: 1, column: 3 }], + path: ['listField', 1], + }, + ]; + + expect(await complete({ listField, as: '[Int]' })).toEqual({ + data: { listField: [1, null, 2] }, + }); + expect(await complete({ listField, as: '[Int]!' })).toEqual({ + data: { listField: [1, null, 2] }, + }); + expectJSON(await complete({ listField, as: '[Int!]' })).toDeepEqual({ + data: { listField: null }, + errors, + }); + expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({ + data: null, + errors, + }); + }); + + it('Returns null', async () => { + const listField = null; + const errors = [ + { + message: 'Cannot return null for non-nullable field Query.listField.', + locations: [{ line: 1, column: 3 }], + path: ['listField'], + }, + ]; + + expect(await complete({ listField, as: '[Int]' })).toEqual({ + data: { listField: null }, + }); + expectJSON(await complete({ listField, as: '[Int]!' })).toDeepEqual({ + data: null, + errors, + }); + expect(await complete({ listField, as: '[Int!]' })).toEqual({ + data: { listField: null }, + }); + expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({ + data: null, + errors, + }); + }); + + it('Contains error', async () => { + const listField = [1, new Error('bad'), 2]; + const errors = [ + { + message: 'bad', + locations: [{ line: 1, column: 3 }], + path: ['listField', 1], + }, + ]; + + expectJSON(await complete({ listField, as: '[Int]' })).toDeepEqual({ + data: { listField: [1, null, 2] }, + errors, + }); + expectJSON(await complete({ listField, as: '[Int]!' })).toDeepEqual({ + data: { listField: [1, null, 2] }, + errors, + }); + expectJSON(await complete({ listField, as: '[Int!]' })).toDeepEqual({ + data: { listField: null }, + errors, + }); + expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({ + data: null, + errors, + }); + }); + + it('Results in error', async () => { + const listField = new Error('bad'); + const errors = [ + { + message: 'bad', + locations: [{ line: 1, column: 3 }], + path: ['listField'], + }, + ]; + + expectJSON(await complete({ listField, as: '[Int]' })).toDeepEqual({ + data: { listField: null }, + errors, + }); + expectJSON(await complete({ listField, as: '[Int]!' })).toDeepEqual({ + data: null, + errors, + }); + expectJSON(await complete({ listField, as: '[Int!]' })).toDeepEqual({ + data: { listField: null }, + errors, + }); + expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({ + data: null, + errors, + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/mapAsyncIterator-test.ts b/packages/graphql/src/execution/__tests__/mapAsyncIterator-test.ts new file mode 100644 index 00000000000..23cc31521b4 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/mapAsyncIterator-test.ts @@ -0,0 +1,329 @@ +import { mapAsyncIterator } from '../mapAsyncIterator.js'; + +describe('mapAsyncIterator', () => { + it('maps over async generator', async () => { + async function* source() { + yield 1; + yield 2; + yield 3; + } + + const doubles = mapAsyncIterator(source(), x => x + x); + + expect(await doubles.next()).toEqual({ value: 2, done: false }); + expect(await doubles.next()).toEqual({ value: 4, done: false }); + expect(await doubles.next()).toEqual({ value: 6, done: false }); + expect(await doubles.next()).toEqual({ + value: undefined, + done: true, + }); + }); + + it('maps over async iterable', async () => { + const items = [1, 2, 3]; + + const iterable = { + [Symbol.asyncIterator]() { + return this; + }, + + next(): Promise> { + if (items.length > 0) { + const value = items[0]; + items.shift(); + return Promise.resolve({ done: false, value }); + } + + return Promise.resolve({ done: true, value: undefined }); + }, + }; + + const doubles = mapAsyncIterator(iterable, x => x + x); + + expect(await doubles.next()).toEqual({ value: 2, done: false }); + expect(await doubles.next()).toEqual({ value: 4, done: false }); + expect(await doubles.next()).toEqual({ value: 6, done: false }); + expect(await doubles.next()).toEqual({ + value: undefined, + done: true, + }); + }); + + it('compatible with for-await-of', async () => { + async function* source() { + yield 1; + yield 2; + yield 3; + } + + const doubles = mapAsyncIterator(source(), x => x + x); + + const result = []; + for await (const x of doubles) { + result.push(x); + } + expect(result).toEqual([2, 4, 6]); + }); + + it('maps over async values with async function', async () => { + async function* source() { + yield 1; + yield 2; + yield 3; + } + + const doubles = mapAsyncIterator(source(), x => Promise.resolve(x + x)); + + expect(await doubles.next()).toEqual({ value: 2, done: false }); + expect(await doubles.next()).toEqual({ value: 4, done: false }); + expect(await doubles.next()).toEqual({ value: 6, done: false }); + expect(await doubles.next()).toEqual({ + value: undefined, + done: true, + }); + }); + + it('allows returning early from mapped async generator', async () => { + async function* source() { + try { + yield 1; + /* c8 ignore next 3 */ + yield 2; + yield 3; // Shouldn't be reached. + } finally { + // eslint-disable-next-line no-unsafe-finally + return 'The End'; + } + } + + const doubles = mapAsyncIterator(source(), x => x + x); + + expect(await doubles.next()).toEqual({ value: 2, done: false }); + expect(await doubles.next()).toEqual({ value: 4, done: false }); + + // Early return + expect(await doubles.return('')).toEqual({ + value: 'The End', + done: true, + }); + + // Subsequent next calls + expect(await doubles.next()).toEqual({ + value: undefined, + done: true, + }); + expect(await doubles.next()).toEqual({ + value: undefined, + done: true, + }); + }); + + it('allows returning early from mapped async iterable', async () => { + const items = [1, 2, 3]; + + const iterable = { + [Symbol.asyncIterator]() { + return this; + }, + next() { + const value = items[0]; + items.shift(); + return Promise.resolve({ + done: items.length === 0, + value, + }); + }, + }; + + const doubles = mapAsyncIterator(iterable, x => x + x); + + expect(await doubles.next()).toEqual({ value: 2, done: false }); + expect(await doubles.next()).toEqual({ value: 4, done: false }); + + // Early return + expect(await doubles.return(0)).toEqual({ + value: undefined, + done: true, + }); + }); + + it('passes through early return from async values', async () => { + async function* source() { + try { + yield 'a'; + /* c8 ignore next 3 */ + yield 'b'; + yield 'c'; // Shouldn't be reached. + } finally { + yield 'Done'; + yield 'Last'; + } + } + + const doubles = mapAsyncIterator(source(), x => x + x); + + expect(await doubles.next()).toEqual({ value: 'aa', done: false }); + expect(await doubles.next()).toEqual({ value: 'bb', done: false }); + + // Early return + expect(await doubles.return()).toEqual({ + value: 'DoneDone', + done: false, + }); + + // Subsequent next calls may yield from finally block + expect(await doubles.next()).toEqual({ + value: 'LastLast', + done: false, + }); + expect(await doubles.next()).toEqual({ + value: undefined, + done: true, + }); + }); + + it('allows throwing errors through async iterable', async () => { + const items = [1, 2, 3]; + + const iterable = { + [Symbol.asyncIterator]() { + return this; + }, + next() { + const value = items[0]; + items.shift(); + return Promise.resolve({ + done: items.length === 0, + value, + }); + }, + }; + + const doubles = mapAsyncIterator(iterable, x => x + x); + + expect(await doubles.next()).toEqual({ value: 2, done: false }); + expect(await doubles.next()).toEqual({ value: 4, done: false }); + + // Throw error + let caughtError; + try { + /* c8 ignore next 2 */ + await doubles.throw('ouch'); + } catch (e) { + caughtError = e; + } + expect(caughtError).toEqual('ouch'); + }); + + it('passes through caught errors through async generators', async () => { + async function* source() { + try { + yield 1; + /* c8 ignore next 2 */ + yield 2; + yield 3; // Shouldn't be reached. + } catch (e) { + yield e; + } + } + + // @ts-expect-error + const doubles = mapAsyncIterator(source(), (x: number) => x + x); + + expect(await doubles.next()).toEqual({ value: 2, done: false }); + expect(await doubles.next()).toEqual({ value: 4, done: false }); + + // Throw error + expect(await doubles.throw('Ouch')).toEqual({ + value: 'OuchOuch', + done: false, + }); + + expect(await doubles.next()).toEqual({ + value: undefined, + done: true, + }); + expect(await doubles.next()).toEqual({ + value: undefined, + done: true, + }); + }); + + it('does not normally map over thrown errors', async () => { + async function* source() { + yield 'Hello'; + throw new Error('Goodbye'); + } + + const doubles = mapAsyncIterator(source(), x => x + x); + + expect(await doubles.next()).toEqual({ + value: 'HelloHello', + done: false, + }); + + let caughtError; + try { + /* c8 ignore next 2 */ + await doubles.next(); + } catch (e) { + caughtError = e; + } + + expect(caughtError).toBeInstanceOf(Error); + expect(caughtError).toHaveProperty('message', 'Goodbye'); + }); + + async function testClosesSourceWithMapper(mapper: (value: number) => T) { + let didVisitFinally = false; + + async function* source() { + try { + yield 1; + /* c8 ignore next 3 */ + yield 2; + yield 3; // Shouldn't be reached. + } finally { + didVisitFinally = true; + yield 1000; + } + } + + const throwOver1 = mapAsyncIterator(source(), mapper); + + expect(await throwOver1.next()).toEqual({ value: 1, done: false }); + + let expectedError; + try { + /* c8 ignore next 2 */ + await throwOver1.next(); + } catch (error) { + expectedError = error; + } + + expect(expectedError).toBeInstanceOf(Error); + expect(expectedError).toHaveProperty('message', 'Cannot count to 2'); + + expect(await throwOver1.next()).toEqual({ + value: undefined, + done: true, + }); + + expect(didVisitFinally).toEqual(true); + } + + it('closes source if mapper throws an error', async () => { + await testClosesSourceWithMapper(x => { + if (x > 1) { + throw new Error('Cannot count to ' + x); + } + return x; + }); + }); + + it('closes source if mapper rejects', async () => { + await testClosesSourceWithMapper(x => + x > 1 ? Promise.reject(new Error('Cannot count to ' + x)) : Promise.resolve(x) + ); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/mutations-test.ts b/packages/graphql/src/execution/__tests__/mutations-test.ts new file mode 100644 index 00000000000..a05c7161fe6 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/mutations-test.ts @@ -0,0 +1,191 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; +import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js'; + +import { parse } from '../../language/parser.js'; + +import { GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLInt } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { execute, executeSync } from '../execute.js'; + +class NumberHolder { + theNumber: number; + + constructor(originalNumber: number) { + this.theNumber = originalNumber; + } +} + +class Root { + numberHolder: NumberHolder; + + constructor(originalNumber: number) { + this.numberHolder = new NumberHolder(originalNumber); + } + + immediatelyChangeTheNumber(newNumber: number): NumberHolder { + this.numberHolder.theNumber = newNumber; + return this.numberHolder; + } + + async promiseToChangeTheNumber(newNumber: number): Promise { + await resolveOnNextTick(); + return this.immediatelyChangeTheNumber(newNumber); + } + + failToChangeTheNumber(): NumberHolder { + throw new Error('Cannot change the number'); + } + + async promiseAndFailToChangeTheNumber(): Promise { + await resolveOnNextTick(); + throw new Error('Cannot change the number'); + } +} + +const numberHolderType = new GraphQLObjectType({ + fields: { + theNumber: { type: GraphQLInt }, + }, + name: 'NumberHolder', +}); + +const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + fields: { + numberHolder: { type: numberHolderType }, + }, + name: 'Query', + }), + mutation: new GraphQLObjectType({ + fields: { + immediatelyChangeTheNumber: { + type: numberHolderType, + args: { newNumber: { type: GraphQLInt } }, + resolve(obj, { newNumber }) { + return obj.immediatelyChangeTheNumber(newNumber); + }, + }, + promiseToChangeTheNumber: { + type: numberHolderType, + args: { newNumber: { type: GraphQLInt } }, + resolve(obj, { newNumber }) { + return obj.promiseToChangeTheNumber(newNumber); + }, + }, + failToChangeTheNumber: { + type: numberHolderType, + args: { newNumber: { type: GraphQLInt } }, + resolve(obj, { newNumber }) { + return obj.failToChangeTheNumber(newNumber); + }, + }, + promiseAndFailToChangeTheNumber: { + type: numberHolderType, + args: { newNumber: { type: GraphQLInt } }, + resolve(obj, { newNumber }) { + return obj.promiseAndFailToChangeTheNumber(newNumber); + }, + }, + }, + name: 'Mutation', + }), +}); + +describe('Execute: Handles mutation execution ordering', () => { + it('evaluates mutations serially', async () => { + const document = parse(` + mutation M { + first: immediatelyChangeTheNumber(newNumber: 1) { + theNumber + }, + second: promiseToChangeTheNumber(newNumber: 2) { + theNumber + }, + third: immediatelyChangeTheNumber(newNumber: 3) { + theNumber + } + fourth: promiseToChangeTheNumber(newNumber: 4) { + theNumber + }, + fifth: immediatelyChangeTheNumber(newNumber: 5) { + theNumber + } + } + `); + + const rootValue = new Root(6); + const mutationResult = await execute({ schema, document, rootValue }); + + expect(mutationResult).toEqual({ + data: { + first: { theNumber: 1 }, + second: { theNumber: 2 }, + third: { theNumber: 3 }, + fourth: { theNumber: 4 }, + fifth: { theNumber: 5 }, + }, + }); + }); + + it('does not include illegal mutation fields in output', () => { + const document = parse('mutation { thisIsIllegalDoNotIncludeMe }'); + + const result = executeSync({ schema, document }); + expect(result).toEqual({ + data: {}, + }); + }); + + it('evaluates mutations correctly in the presence of a failed mutation', async () => { + const document = parse(` + mutation M { + first: immediatelyChangeTheNumber(newNumber: 1) { + theNumber + }, + second: promiseToChangeTheNumber(newNumber: 2) { + theNumber + }, + third: failToChangeTheNumber(newNumber: 3) { + theNumber + } + fourth: promiseToChangeTheNumber(newNumber: 4) { + theNumber + }, + fifth: immediatelyChangeTheNumber(newNumber: 5) { + theNumber + } + sixth: promiseAndFailToChangeTheNumber(newNumber: 6) { + theNumber + } + } + `); + + const rootValue = new Root(6); + const result = await execute({ schema, document, rootValue }); + + expectJSON(result).toDeepEqual({ + data: { + first: { theNumber: 1 }, + second: { theNumber: 2 }, + third: null, + fourth: { theNumber: 4 }, + fifth: { theNumber: 5 }, + sixth: null, + }, + errors: [ + { + message: 'Cannot change the number', + locations: [{ line: 9, column: 9 }], + path: ['third'], + }, + { + message: 'Cannot change the number', + locations: [{ line: 18, column: 9 }], + path: ['sixth'], + }, + ], + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/nonnull-test.ts b/packages/graphql/src/execution/__tests__/nonnull-test.ts new file mode 100644 index 00000000000..84893df61cf --- /dev/null +++ b/packages/graphql/src/execution/__tests__/nonnull-test.ts @@ -0,0 +1,697 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { parse } from '../../language/parser.js'; + +import { GraphQLNonNull, GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import type { ExecutionResult } from '../execute.js'; +import { execute, executeSync } from '../execute.js'; + +const syncError = new Error('sync'); +const syncNonNullError = new Error('syncNonNull'); +const promiseError = new Error('promise'); +const promiseNonNullError = new Error('promiseNonNull'); + +const throwingData = { + sync() { + throw syncError; + }, + syncNonNull() { + throw syncNonNullError; + }, + promise() { + return new Promise(() => { + throw promiseError; + }); + }, + promiseNonNull() { + return new Promise(() => { + throw promiseNonNullError; + }); + }, + syncNest() { + return throwingData; + }, + syncNonNullNest() { + return throwingData; + }, + promiseNest() { + return new Promise(resolve => { + resolve(throwingData); + }); + }, + promiseNonNullNest() { + return new Promise(resolve => { + resolve(throwingData); + }); + }, +}; + +const nullingData = { + sync() { + return null; + }, + syncNonNull() { + return null; + }, + promise() { + return new Promise(resolve => { + resolve(null); + }); + }, + promiseNonNull() { + return new Promise(resolve => { + resolve(null); + }); + }, + syncNest() { + return nullingData; + }, + syncNonNullNest() { + return nullingData; + }, + promiseNest() { + return new Promise(resolve => { + resolve(nullingData); + }); + }, + promiseNonNullNest() { + return new Promise(resolve => { + resolve(nullingData); + }); + }, +}; + +const schema = buildSchema(` + type DataType { + sync: String + syncNonNull: String! + promise: String + promiseNonNull: String! + syncNest: DataType + syncNonNullNest: DataType! + promiseNest: DataType + promiseNonNullNest: DataType! + } + + schema { + query: DataType + } +`); + +function executeQuery(query: string, rootValue: unknown): ExecutionResult | Promise { + return execute({ schema, document: parse(query), rootValue }); +} + +function patch(str: string): string { + return str.replace(/\bsync\b/g, 'promise').replace(/\bsyncNonNull\b/g, 'promiseNonNull'); +} + +// avoids also doing any nests +function patchData(data: ExecutionResult): ExecutionResult { + return JSON.parse(patch(JSON.stringify(data))); +} + +async function executeSyncAndAsync(query: string, rootValue: unknown) { + const syncResult = executeSync({ schema, document: parse(query), rootValue }); + const asyncResult = await execute({ + schema, + document: parse(patch(query)), + rootValue, + }); + + expectJSON(asyncResult).toDeepEqual(patchData(syncResult)); + return syncResult; +} + +describe('Execute: handles non-nullable types', () => { + describe('nulls a nullable field', () => { + const query = ` + { + sync + } + `; + + it('that returns null', async () => { + const result = await executeSyncAndAsync(query, nullingData); + expect(result).toEqual({ + data: { sync: null }, + }); + }); + + it('that throws', async () => { + const result = await executeSyncAndAsync(query, throwingData); + expectJSON(result).toDeepEqual({ + data: { sync: null }, + errors: [ + { + message: syncError.message, + path: ['sync'], + locations: [{ line: 3, column: 9 }], + }, + ], + }); + }); + }); + + describe('nulls a returned object that contains a non-nullable field', () => { + const query = ` + { + syncNest { + syncNonNull, + } + } + `; + + it('that returns null', async () => { + const result = await executeSyncAndAsync(query, nullingData); + expectJSON(result).toDeepEqual({ + data: { syncNest: null }, + errors: [ + { + message: 'Cannot return null for non-nullable field DataType.syncNonNull.', + path: ['syncNest', 'syncNonNull'], + locations: [{ line: 4, column: 11 }], + }, + ], + }); + }); + + it('that throws', async () => { + const result = await executeSyncAndAsync(query, throwingData); + expectJSON(result).toDeepEqual({ + data: { syncNest: null }, + errors: [ + { + message: syncNonNullError.message, + path: ['syncNest', 'syncNonNull'], + locations: [{ line: 4, column: 11 }], + }, + ], + }); + }); + }); + + describe('nulls a complex tree of nullable fields, each', () => { + const query = ` + { + syncNest { + sync + promise + syncNest { sync promise } + promiseNest { sync promise } + } + promiseNest { + sync + promise + syncNest { sync promise } + promiseNest { sync promise } + } + } + `; + const data = { + syncNest: { + sync: null, + promise: null, + syncNest: { sync: null, promise: null }, + promiseNest: { sync: null, promise: null }, + }, + promiseNest: { + sync: null, + promise: null, + syncNest: { sync: null, promise: null }, + promiseNest: { sync: null, promise: null }, + }, + }; + + it('that returns null', async () => { + const result = await executeQuery(query, nullingData); + expect(result).toEqual({ data }); + }); + + it('that throws', async () => { + const result = await executeQuery(query, throwingData); + expectJSON(result).toDeepEqual({ + data, + errors: [ + { + message: syncError.message, + path: ['syncNest', 'sync'], + locations: [{ line: 4, column: 11 }], + }, + { + message: syncError.message, + path: ['syncNest', 'syncNest', 'sync'], + locations: [{ line: 6, column: 22 }], + }, + { + message: syncError.message, + path: ['syncNest', 'promiseNest', 'sync'], + locations: [{ line: 7, column: 25 }], + }, + { + message: syncError.message, + path: ['promiseNest', 'sync'], + locations: [{ line: 10, column: 11 }], + }, + { + message: syncError.message, + path: ['promiseNest', 'syncNest', 'sync'], + locations: [{ line: 12, column: 22 }], + }, + { + message: promiseError.message, + path: ['syncNest', 'promise'], + locations: [{ line: 5, column: 11 }], + }, + { + message: promiseError.message, + path: ['syncNest', 'syncNest', 'promise'], + locations: [{ line: 6, column: 27 }], + }, + { + message: syncError.message, + path: ['promiseNest', 'promiseNest', 'sync'], + locations: [{ line: 13, column: 25 }], + }, + { + message: promiseError.message, + path: ['syncNest', 'promiseNest', 'promise'], + locations: [{ line: 7, column: 30 }], + }, + { + message: promiseError.message, + path: ['promiseNest', 'promise'], + locations: [{ line: 11, column: 11 }], + }, + { + message: promiseError.message, + path: ['promiseNest', 'syncNest', 'promise'], + locations: [{ line: 12, column: 27 }], + }, + { + message: promiseError.message, + path: ['promiseNest', 'promiseNest', 'promise'], + locations: [{ line: 13, column: 30 }], + }, + ], + }); + }); + }); + + describe('nulls the first nullable object after a field in a long chain of non-null fields', () => { + const query = ` + { + syncNest { + syncNonNullNest { + promiseNonNullNest { + syncNonNullNest { + promiseNonNullNest { + syncNonNull + } + } + } + } + } + promiseNest { + syncNonNullNest { + promiseNonNullNest { + syncNonNullNest { + promiseNonNullNest { + syncNonNull + } + } + } + } + } + anotherNest: syncNest { + syncNonNullNest { + promiseNonNullNest { + syncNonNullNest { + promiseNonNullNest { + promiseNonNull + } + } + } + } + } + anotherPromiseNest: promiseNest { + syncNonNullNest { + promiseNonNullNest { + syncNonNullNest { + promiseNonNullNest { + promiseNonNull + } + } + } + } + } + } + `; + const data = { + syncNest: null, + promiseNest: null, + anotherNest: null, + anotherPromiseNest: null, + }; + + it('that returns null', async () => { + const result = await executeQuery(query, nullingData); + expectJSON(result).toDeepEqual({ + data, + errors: [ + { + message: 'Cannot return null for non-nullable field DataType.syncNonNull.', + path: [ + 'syncNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNull', + ], + locations: [{ line: 8, column: 19 }], + }, + { + message: 'Cannot return null for non-nullable field DataType.syncNonNull.', + path: [ + 'promiseNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNull', + ], + locations: [{ line: 19, column: 19 }], + }, + { + message: 'Cannot return null for non-nullable field DataType.promiseNonNull.', + path: [ + 'anotherNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'promiseNonNull', + ], + locations: [{ line: 30, column: 19 }], + }, + { + message: 'Cannot return null for non-nullable field DataType.promiseNonNull.', + path: [ + 'anotherPromiseNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'promiseNonNull', + ], + locations: [{ line: 41, column: 19 }], + }, + ], + }); + }); + + it('that throws', async () => { + const result = await executeQuery(query, throwingData); + expectJSON(result).toDeepEqual({ + data, + errors: [ + { + message: syncNonNullError.message, + path: [ + 'syncNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNull', + ], + locations: [{ line: 8, column: 19 }], + }, + { + message: syncNonNullError.message, + path: [ + 'promiseNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNull', + ], + locations: [{ line: 19, column: 19 }], + }, + { + message: promiseNonNullError.message, + path: [ + 'anotherNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'promiseNonNull', + ], + locations: [{ line: 30, column: 19 }], + }, + { + message: promiseNonNullError.message, + path: [ + 'anotherPromiseNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'syncNonNullNest', + 'promiseNonNullNest', + 'promiseNonNull', + ], + locations: [{ line: 41, column: 19 }], + }, + ], + }); + }); + }); + + describe('nulls the top level if non-nullable field', () => { + const query = ` + { + syncNonNull + } + `; + + it('that returns null', async () => { + const result = await executeSyncAndAsync(query, nullingData); + expectJSON(result).toDeepEqual({ + data: null, + errors: [ + { + message: 'Cannot return null for non-nullable field DataType.syncNonNull.', + path: ['syncNonNull'], + locations: [{ line: 3, column: 9 }], + }, + ], + }); + }); + + it('that throws', async () => { + const result = await executeSyncAndAsync(query, throwingData); + expectJSON(result).toDeepEqual({ + data: null, + errors: [ + { + message: syncNonNullError.message, + path: ['syncNonNull'], + locations: [{ line: 3, column: 9 }], + }, + ], + }); + }); + }); + + describe('Handles non-null argument', () => { + const schemaWithNonNullArg = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + withNonNullArg: { + type: GraphQLString, + args: { + cannotBeNull: { + type: new GraphQLNonNull(GraphQLString), + }, + }, + resolve: (_, args) => 'Passed: ' + String(args.cannotBeNull), + }, + }, + }), + }); + + it('succeeds when passed non-null literal value', () => { + const result = executeSync({ + schema: schemaWithNonNullArg, + document: parse(` + query { + withNonNullArg (cannotBeNull: "literal value") + } + `), + }); + + expect(result).toEqual({ + data: { + withNonNullArg: 'Passed: literal value', + }, + }); + }); + + it('succeeds when passed non-null variable value', () => { + const result = executeSync({ + schema: schemaWithNonNullArg, + document: parse(` + query ($testVar: String!) { + withNonNullArg (cannotBeNull: $testVar) + } + `), + variableValues: { + testVar: 'variable value', + }, + }); + + expect(result).toEqual({ + data: { + withNonNullArg: 'Passed: variable value', + }, + }); + }); + + it('succeeds when missing variable has default value', () => { + const result = executeSync({ + schema: schemaWithNonNullArg, + document: parse(` + query ($testVar: String = "default value") { + withNonNullArg (cannotBeNull: $testVar) + } + `), + variableValues: { + // Intentionally missing variable + }, + }); + + expect(result).toEqual({ + data: { + withNonNullArg: 'Passed: default value', + }, + }); + }); + + it('field error when missing non-null arg', () => { + // Note: validation should identify this issue first (missing args rule) + // however execution should still protect against this. + const result = executeSync({ + schema: schemaWithNonNullArg, + document: parse(` + query { + withNonNullArg + } + `), + }); + + expectJSON(result).toDeepEqual({ + data: { + withNonNullArg: null, + }, + errors: [ + { + message: 'Argument "cannotBeNull" of required type "String!" was not provided.', + locations: [{ line: 3, column: 13 }], + path: ['withNonNullArg'], + }, + ], + }); + }); + + it('field error when non-null arg provided null', () => { + // Note: validation should identify this issue first (values of correct + // type rule) however execution should still protect against this. + const result = executeSync({ + schema: schemaWithNonNullArg, + document: parse(` + query { + withNonNullArg(cannotBeNull: null) + } + `), + }); + + expectJSON(result).toDeepEqual({ + data: { + withNonNullArg: null, + }, + errors: [ + { + message: 'Argument "cannotBeNull" of non-null type "String!" must not be null.', + locations: [{ line: 3, column: 42 }], + path: ['withNonNullArg'], + }, + ], + }); + }); + + it('field error when non-null arg not provided variable value', () => { + // Note: validation should identify this issue first (variables in allowed + // position rule) however execution should still protect against this. + const result = executeSync({ + schema: schemaWithNonNullArg, + document: parse(` + query ($testVar: String) { + withNonNullArg(cannotBeNull: $testVar) + } + `), + variableValues: { + // Intentionally missing variable + }, + }); + + expectJSON(result).toDeepEqual({ + data: { + withNonNullArg: null, + }, + errors: [ + { + message: + 'Argument "cannotBeNull" of required type "String!" was provided the variable "$testVar" which was not provided a runtime value.', + locations: [{ line: 3, column: 42 }], + path: ['withNonNullArg'], + }, + ], + }); + }); + + it('field error when non-null arg provided variable with explicit null value', () => { + const result = executeSync({ + schema: schemaWithNonNullArg, + document: parse(` + query ($testVar: String = "default value") { + withNonNullArg (cannotBeNull: $testVar) + } + `), + variableValues: { + testVar: null, + }, + }); + + expectJSON(result).toDeepEqual({ + data: { + withNonNullArg: null, + }, + errors: [ + { + message: 'Argument "cannotBeNull" of non-null type "String!" must not be null.', + locations: [{ line: 3, column: 43 }], + path: ['withNonNullArg'], + }, + ], + }); + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/resolve-test.ts b/packages/graphql/src/execution/__tests__/resolve-test.ts new file mode 100644 index 00000000000..5537d3b8c68 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/resolve-test.ts @@ -0,0 +1,124 @@ +import { parse } from '../../language/parser.js'; + +import type { GraphQLFieldConfig } from '../../type/definition.js'; +import { GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { executeSync } from '../execute.js'; + +describe('Execute: resolve function', () => { + function testSchema(testField: GraphQLFieldConfig) { + return new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + test: testField, + }, + }), + }); + } + + it('default function accesses properties', () => { + const result = executeSync({ + schema: testSchema({ type: GraphQLString }), + document: parse('{ test }'), + rootValue: { test: 'testValue' }, + }); + + expect(result).toEqual({ + data: { + test: 'testValue', + }, + }); + }); + + it('default function calls methods', () => { + const rootValue = { + _secret: 'secretValue', + test() { + return this._secret; + }, + }; + + const result = executeSync({ + schema: testSchema({ type: GraphQLString }), + document: parse('{ test }'), + rootValue, + }); + expect(result).toEqual({ + data: { + test: 'secretValue', + }, + }); + }); + + it('default function passes args and context', () => { + class Adder { + _num: number; + + constructor(num: number) { + this._num = num; + } + + test(args: { addend1: number }, context: { addend2: number }) { + return this._num + args.addend1 + context.addend2; + } + } + const rootValue = new Adder(700); + + const schema = testSchema({ + type: GraphQLInt, + args: { + addend1: { type: GraphQLInt }, + }, + }); + const contextValue = { addend2: 9 }; + const document = parse('{ test(addend1: 80) }'); + + const result = executeSync({ schema, document, rootValue, contextValue }); + expect(result).toEqual({ + data: { test: 789 }, + }); + }); + + it('uses provided resolve function', () => { + const schema = testSchema({ + type: GraphQLString, + args: { + aStr: { type: GraphQLString }, + aInt: { type: GraphQLInt }, + }, + resolve: (source, args) => JSON.stringify([source, args]), + }); + + function executeQuery(query: string, rootValue?: unknown) { + const document = parse(query); + return executeSync({ schema, document, rootValue }); + } + + expect(executeQuery('{ test }')).toEqual({ + data: { + test: '[null,{}]', + }, + }); + + expect(executeQuery('{ test }', 'Source!')).toEqual({ + data: { + test: '["Source!",{}]', + }, + }); + + expect(executeQuery('{ test(aStr: "String!") }', 'Source!')).toEqual({ + data: { + test: '["Source!",{"aStr":"String!"}]', + }, + }); + + expect(executeQuery('{ test(aInt: -123, aStr: "String!") }', 'Source!')).toEqual({ + data: { + test: '["Source!",{"aStr":"String!","aInt":-123}]', + }, + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/schema-test.ts b/packages/graphql/src/execution/__tests__/schema-test.ts new file mode 100644 index 00000000000..fe948fb6dff --- /dev/null +++ b/packages/graphql/src/execution/__tests__/schema-test.ts @@ -0,0 +1,176 @@ +import { parse } from '../../language/parser.js'; + +import { GraphQLList, GraphQLNonNull, GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLBoolean, GraphQLID, GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { executeSync } from '../execute.js'; + +describe('Execute: Handles execution with a complex schema', () => { + it('executes using a schema', () => { + const BlogImage = new GraphQLObjectType({ + name: 'Image', + fields: { + url: { type: GraphQLString }, + width: { type: GraphQLInt }, + height: { type: GraphQLInt }, + }, + }); + + const BlogAuthor: GraphQLObjectType = new GraphQLObjectType({ + name: 'Author', + fields: () => ({ + id: { type: GraphQLString }, + name: { type: GraphQLString }, + pic: { + args: { width: { type: GraphQLInt }, height: { type: GraphQLInt } }, + type: BlogImage, + resolve: (obj, { width, height }) => obj.pic(width, height), + }, + recentArticle: { type: BlogArticle }, + }), + }); + + const BlogArticle = new GraphQLObjectType({ + name: 'Article', + fields: { + id: { type: new GraphQLNonNull(GraphQLString) }, + isPublished: { type: GraphQLBoolean }, + author: { type: BlogAuthor }, + title: { type: GraphQLString }, + body: { type: GraphQLString }, + keywords: { type: new GraphQLList(GraphQLString) }, + }, + }); + + const BlogQuery = new GraphQLObjectType({ + name: 'Query', + fields: { + article: { + type: BlogArticle, + args: { id: { type: GraphQLID } }, + resolve: (_, { id }) => article(id), + }, + feed: { + type: new GraphQLList(BlogArticle), + resolve: () => [ + article(1), + article(2), + article(3), + article(4), + article(5), + article(6), + article(7), + article(8), + article(9), + article(10), + ], + }, + }, + }); + + const BlogSchema = new GraphQLSchema({ + query: BlogQuery, + }); + + function article(id: number) { + return { + id, + isPublished: true, + author: { + id: 123, + name: 'John Smith', + pic: (width: number, height: number) => getPic(123, width, height), + recentArticle: () => article(1), + }, + title: 'My Article ' + id, + body: 'This is a post', + hidden: 'This data is not exposed in the schema', + keywords: ['foo', 'bar', 1, true, null], + }; + } + + function getPic(uid: number, width: number, height: number) { + return { + url: `cdn://${uid}`, + width: `${width}`, + height: `${height}`, + }; + } + + const document = parse(` + { + feed { + id, + title + }, + article(id: "1") { + ...articleFields, + author { + id, + name, + pic(width: 640, height: 480) { + url, + width, + height + }, + recentArticle { + ...articleFields, + keywords + } + } + } + } + + fragment articleFields on Article { + id, + isPublished, + title, + body, + hidden, + notDefined + } + `); + + // Note: this is intentionally not validating to ensure appropriate + // behavior occurs when executing an invalid query. + expect(executeSync({ schema: BlogSchema, document })).toEqual({ + data: { + feed: [ + { id: '1', title: 'My Article 1' }, + { id: '2', title: 'My Article 2' }, + { id: '3', title: 'My Article 3' }, + { id: '4', title: 'My Article 4' }, + { id: '5', title: 'My Article 5' }, + { id: '6', title: 'My Article 6' }, + { id: '7', title: 'My Article 7' }, + { id: '8', title: 'My Article 8' }, + { id: '9', title: 'My Article 9' }, + { id: '10', title: 'My Article 10' }, + ], + article: { + id: '1', + isPublished: true, + title: 'My Article 1', + body: 'This is a post', + author: { + id: '123', + name: 'John Smith', + pic: { + url: 'cdn://123', + width: 640, + height: 480, + }, + recentArticle: { + id: '1', + isPublished: true, + title: 'My Article 1', + body: 'This is a post', + keywords: ['foo', 'bar', '1', 'true', null], + }, + }, + }, + }, + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/simplePubSub-test.ts b/packages/graphql/src/execution/__tests__/simplePubSub-test.ts new file mode 100644 index 00000000000..54a736e2320 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/simplePubSub-test.ts @@ -0,0 +1,52 @@ +import { SimplePubSub } from './simplePubSub.js'; + +describe('SimplePubSub', () => { + it('subscribe async-iterator mock', async () => { + const pubsub = new SimplePubSub(); + const iterator = pubsub.getSubscriber(x => x); + + // Queue up publishes + expect(pubsub.emit('Apple')).toEqual(true); + expect(pubsub.emit('Banana')).toEqual(true); + + // Read payloads + expect(await iterator.next()).toEqual({ + done: false, + value: 'Apple', + }); + expect(await iterator.next()).toEqual({ + done: false, + value: 'Banana', + }); + + // Read ahead + const i3 = iterator.next().then(x => x); + const i4 = iterator.next().then(x => x); + + // Publish + expect(pubsub.emit('Coconut')).toEqual(true); + expect(pubsub.emit('Durian')).toEqual(true); + + // Await out of order to get correct results + expect(await i4).toEqual({ done: false, value: 'Durian' }); + expect(await i3).toEqual({ done: false, value: 'Coconut' }); + + // Read ahead + const i5 = iterator.next().then(x => x); + + // Terminate queue + await iterator.return(); + + // Publish is not caught after terminate + expect(pubsub.emit('Fig')).toEqual(false); + + // Find that cancelled read-ahead got a "done" result + expect(await i5).toEqual({ done: true, value: undefined }); + + // And next returns empty completion value + expect(await iterator.next()).toEqual({ + done: true, + value: undefined, + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/simplePubSub.ts b/packages/graphql/src/execution/__tests__/simplePubSub.ts new file mode 100644 index 00000000000..a3ffc21928b --- /dev/null +++ b/packages/graphql/src/execution/__tests__/simplePubSub.ts @@ -0,0 +1,77 @@ +/** + * Create an AsyncIterator from an EventEmitter. Useful for mocking a + * PubSub system for tests. + */ +export class SimplePubSub { + private _subscribers: Set<(value: T) => void>; + + constructor() { + this._subscribers = new Set(); + } + + emit(event: T): boolean { + for (const subscriber of this._subscribers) { + subscriber(event); + } + return this._subscribers.size > 0; + } + + getSubscriber(transform: (value: T) => R): AsyncGenerator { + const pullQueue: Array<(result: IteratorResult) => void> = []; + const pushQueue: Array = []; + let listening = true; + this._subscribers.add(pushValue); + + const emptyQueue = () => { + listening = false; + this._subscribers.delete(pushValue); + for (const resolve of pullQueue) { + resolve({ value: undefined, done: true }); + } + pullQueue.length = 0; + pushQueue.length = 0; + }; + + return { + next(): Promise> { + if (!listening) { + return Promise.resolve({ value: undefined, done: true }); + } + + if (pushQueue.length > 0) { + const value = pushQueue[0]; + pushQueue.shift(); + return Promise.resolve({ value, done: false }); + } + return new Promise(resolve => pullQueue.push(resolve)); + }, + return(): Promise> { + emptyQueue(); + return Promise.resolve({ value: undefined, done: true }); + }, + throw(error: unknown) { + emptyQueue(); + return Promise.reject(error); + }, + [Symbol.asyncIterator]() { + return this; + }, + }; + + function pushValue(event: T): void { + const value: R = transform(event); + if (pullQueue.length > 0) { + const receiver = pullQueue.shift(); + expect(receiver != null).toBeTruthy(); + // @ts-expect-error + receiver({ value, done: false }); + } else { + pushQueue.push(value); + } + } + } +} + +describe.skip('no simplePubSub tests', () => { + it.todo('nothing to test'); +}); diff --git a/packages/graphql/src/execution/__tests__/subscribe-test.ts b/packages/graphql/src/execution/__tests__/subscribe-test.ts new file mode 100644 index 00000000000..18b9c9dbd36 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/subscribe-test.ts @@ -0,0 +1,1033 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; +import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js'; + +import { isAsyncIterable } from '../../jsutils/isAsyncIterable.js'; +import { isPromise } from '../../jsutils/isPromise.js'; +import type { PromiseOrValue } from '../../jsutils/PromiseOrValue.js'; + +import { parse } from '../../language/parser.js'; + +import { GraphQLList, GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import type { ExecutionArgs, ExecutionResult } from '../execute.js'; +import { createSourceEventStream, subscribe } from '../execute.js'; + +import { SimplePubSub } from './simplePubSub.js'; + +interface Email { + from: string; + subject: string; + message: string; + unread: boolean; +} + +const EmailType = new GraphQLObjectType({ + name: 'Email', + fields: { + from: { type: GraphQLString }, + subject: { type: GraphQLString }, + message: { type: GraphQLString }, + unread: { type: GraphQLBoolean }, + }, +}); + +const InboxType = new GraphQLObjectType({ + name: 'Inbox', + fields: { + total: { + type: GraphQLInt, + resolve: inbox => inbox.emails.length, + }, + unread: { + type: GraphQLInt, + resolve: inbox => inbox.emails.filter((email: any) => email.unread).length, + }, + emails: { type: new GraphQLList(EmailType) }, + }, +}); + +const QueryType = new GraphQLObjectType({ + name: 'Query', + fields: { + inbox: { type: InboxType }, + }, +}); + +const EmailEventType = new GraphQLObjectType({ + name: 'EmailEvent', + fields: { + email: { type: EmailType }, + inbox: { type: InboxType }, + }, +}); + +const emailSchema = new GraphQLSchema({ + query: QueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + importantEmail: { + type: EmailEventType, + args: { + priority: { type: GraphQLInt }, + }, + }, + }, + }), +}); + +function createSubscription(pubsub: SimplePubSub) { + const document = parse(` + subscription ($priority: Int = 0) { + importantEmail(priority: $priority) { + email { + from + subject + } + inbox { + unread + total + } + } + } + `); + + const emails = [ + { + from: 'joe@graphql.org', + subject: 'Hello', + message: 'Hello World', + unread: false, + }, + ]; + + const data: any = { + inbox: { emails }, + // FIXME: we shouldn't use mapAsyncIterator here since it makes tests way more complex + importantEmail: pubsub.getSubscriber(newEmail => { + emails.push(newEmail); + + return { + importantEmail: { + email: newEmail, + inbox: data.inbox, + }, + }; + }), + }; + + return subscribe({ schema: emailSchema, document, rootValue: data }); +} + +// TODO: consider adding this method to testUtils (with tests) +function expectPromise(maybePromise: unknown) { + expect(isPromise(maybePromise)).toBeTruthy(); + + return { + toResolve() { + return maybePromise; + }, + async toRejectWith(message: string) { + let caughtError: Error; + + try { + /* c8 ignore next 2 */ + await maybePromise; + } catch (error) { + caughtError = error as Error; + expect(caughtError).toBeInstanceOf(Error); + expect(caughtError).toHaveProperty('message', message); + } + }, + }; +} + +// TODO: consider adding this method to testUtils (with tests) +function expectEqualPromisesOrValues(value1: PromiseOrValue, value2: PromiseOrValue): PromiseOrValue { + if (isPromise(value1)) { + expect(isPromise(value2)).toBeTruthy(); + return Promise.all([value1, value2]).then(resolved => { + expectJSON(resolved[1]).toDeepEqual(resolved[0]); + return resolved[0]; + }); + } + + expect(!isPromise(value2)).toBeTruthy(); + expectJSON(value2).toDeepEqual(value1); + return value1; +} + +const DummyQueryType = new GraphQLObjectType({ + name: 'Query', + fields: { + dummy: { type: GraphQLString }, + }, +}); + +function subscribeWithBadFn(subscribeFn: () => unknown): PromiseOrValue> { + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { type: GraphQLString, subscribe: subscribeFn }, + }, + }), + }); + const document = parse('subscription { foo }'); + + return subscribeWithBadArgs({ schema, document }); +} + +function subscribeWithBadArgs(args: ExecutionArgs): PromiseOrValue> { + return expectEqualPromisesOrValues(subscribe(args), createSourceEventStream(args)); +} + +// Check all error cases when initializing the subscription. +describe('Subscription Initialization Phase', () => { + it('accepts multiple subscription fields defined in schema', async () => { + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { type: GraphQLString }, + bar: { type: GraphQLString }, + }, + }), + }); + + async function* fooGenerator() { + yield { foo: 'FooValue' }; + } + + const subscription = subscribe({ + schema, + document: parse('subscription { foo }'), + rootValue: { foo: fooGenerator }, + }); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: false, + value: { data: { foo: 'FooValue' } }, + }); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: true, + value: undefined, + }); + }); + + it('accepts type definition with sync subscribe function', async () => { + async function* fooGenerator() { + yield { foo: 'FooValue' }; + } + + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { + type: GraphQLString, + subscribe: fooGenerator, + }, + }, + }), + }); + + const subscription = subscribe({ + schema, + document: parse('subscription { foo }'), + }); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: false, + value: { data: { foo: 'FooValue' } }, + }); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: true, + value: undefined, + }); + }); + + it('accepts type definition with async subscribe function', async () => { + async function* fooGenerator() { + yield { foo: 'FooValue' }; + } + + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { + type: GraphQLString, + async subscribe() { + await resolveOnNextTick(); + return fooGenerator(); + }, + }, + }, + }), + }); + + const promise = subscribe({ + schema, + document: parse('subscription { foo }'), + }); + expect(isPromise(promise)).toBeTruthy(); + + const subscription = await promise; + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: false, + value: { data: { foo: 'FooValue' } }, + }); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: true, + value: undefined, + }); + }); + + it('uses a custom default subscribeFieldResolver', async () => { + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { type: GraphQLString }, + }, + }), + }); + + async function* fooGenerator() { + yield { foo: 'FooValue' }; + } + + const subscription = subscribe({ + schema, + document: parse('subscription { foo }'), + rootValue: { customFoo: fooGenerator }, + subscribeFieldResolver: root => root.customFoo(), + }); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: false, + value: { data: { foo: 'FooValue' } }, + }); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: true, + value: undefined, + }); + }); + + it('should only resolve the first field of invalid multi-field', async () => { + async function* fooGenerator() { + yield { foo: 'FooValue' }; + } + + let didResolveFoo = false; + let didResolveBar = false; + + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { + type: GraphQLString, + subscribe() { + didResolveFoo = true; + return fooGenerator(); + }, + }, + bar: { + type: GraphQLString, + /* c8 ignore next 3 */ + subscribe() { + didResolveBar = true; + }, + }, + }, + }), + }); + + const subscription = subscribe({ + schema, + document: parse('subscription { foo bar }'), + }); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + expect(didResolveFoo).toEqual(true); + expect(didResolveBar).toEqual(false); + + // @ts-expect-error + expect(await subscription.next()).toHaveProperty('done', false); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: true, + value: undefined, + }); + }); + + it('resolves to an error if schema does not support subscriptions', async () => { + const schema = new GraphQLSchema({ query: DummyQueryType }); + const document = parse('subscription { unknownField }'); + + const result = subscribeWithBadArgs({ schema, document }); + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Schema is not configured to execute subscription operation.', + locations: [{ line: 1, column: 1 }], + }, + ], + }); + }); + + it('resolves to an error for unknown subscription field', async () => { + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { type: GraphQLString }, + }, + }), + }); + const document = parse('subscription { unknownField }'); + + const result = subscribeWithBadArgs({ schema, document }); + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'The subscription field "unknownField" is not defined.', + locations: [{ line: 1, column: 16 }], + }, + ], + }); + }); + + it('should pass through unexpected errors thrown in subscribe', async () => { + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { type: GraphQLString }, + }, + }), + }); + + // @ts-expect-error + expect(() => subscribeWithBadArgs({ schema, document: {} })).toThrow(); + }); + + it('throws an error if subscribe does not return an iterator', async () => { + const expectedResult = { + errors: [ + { + message: 'Subscription field must return Async Iterable. Received: "test".', + locations: [{ line: 1, column: 16 }], + path: ['foo'], + }, + ], + }; + + expectJSON(subscribeWithBadFn(() => 'test')).toDeepEqual(expectedResult); + + expectJSON(await expectPromise(subscribeWithBadFn(() => Promise.resolve('test'))).toResolve()).toDeepEqual( + expectedResult + ); + }); + + it('resolves to an error for subscription resolver errors', async () => { + const expectedResult = { + errors: [ + { + message: 'test error', + locations: [{ line: 1, column: 16 }], + path: ['foo'], + }, + ], + }; + + expectJSON( + // Returning an error + subscribeWithBadFn(() => new Error('test error')) + ).toDeepEqual(expectedResult); + + expectJSON( + // Throwing an error + subscribeWithBadFn(() => { + throw new Error('test error'); + }) + ).toDeepEqual(expectedResult); + + expectJSON( + // Resolving to an error + await expectPromise(subscribeWithBadFn(() => Promise.resolve(new Error('test error')))).toResolve() + ).toDeepEqual(expectedResult); + + expectJSON( + // Rejecting with an error + await expectPromise(subscribeWithBadFn(() => Promise.reject(new Error('test error')))).toResolve() + ).toDeepEqual(expectedResult); + }); + + it('resolves to an error if variables were wrong type', async () => { + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + foo: { + type: GraphQLString, + args: { arg: { type: GraphQLInt } }, + }, + }, + }), + }); + + const variableValues = { arg: 'meow' }; + const document = parse(` + subscription ($arg: Int) { + foo(arg: $arg) + } + `); + + // If we receive variables that cannot be coerced correctly, subscribe() will + // resolve to an ExecutionResult that contains an informative error description. + const result = subscribeWithBadArgs({ schema, document, variableValues }); + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$arg" got invalid value "meow"; Int cannot represent non-integer value: "meow"', + locations: [{ line: 2, column: 21 }], + }, + ], + }); + }); +}); + +// Once a subscription returns a valid AsyncIterator, it can still yield errors. +describe('Subscription Publish Phase', () => { + it('produces a payload for multiple subscribe in same subscription', async () => { + const pubsub = new SimplePubSub(); + + const subscription = createSubscription(pubsub); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + const secondSubscription = createSubscription(pubsub); + expect(isAsyncIterable(secondSubscription)).toBeTruthy(); + + // @ts-expect-error + const payload1 = subscription.next(); + // @ts-expect-error + const payload2 = secondSubscription.next(); + + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Alright', + message: 'Tests are good', + unread: true, + }) + ).toEqual(true); + + const expectedPayload = { + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'yuzhi@graphql.org', + subject: 'Alright', + }, + inbox: { + unread: 1, + total: 2, + }, + }, + }, + }, + }; + + expect(await payload1).toEqual(expectedPayload); + expect(await payload2).toEqual(expectedPayload); + }); + + it('produces a payload per subscription event', async () => { + const pubsub = new SimplePubSub(); + const subscription = createSubscription(pubsub); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // Wait for the next subscription payload. + // @ts-expect-error + const payload = subscription.next(); + + // A new email arrives! + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Alright', + message: 'Tests are good', + unread: true, + }) + ).toEqual(true); + + // The previously waited on payload now has a value. + expect(await payload).toEqual({ + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'yuzhi@graphql.org', + subject: 'Alright', + }, + inbox: { + unread: 1, + total: 2, + }, + }, + }, + }, + }); + + // Another new email arrives, before subscription.next() is called. + expect( + pubsub.emit({ + from: 'hyo@graphql.org', + subject: 'Tools', + message: 'I <3 making things', + unread: true, + }) + ).toEqual(true); + + // The next waited on payload will have a value. + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'hyo@graphql.org', + subject: 'Tools', + }, + inbox: { + unread: 2, + total: 3, + }, + }, + }, + }, + }); + + // The client decides to disconnect. + // @ts-expect-error + expect(await subscription.return()).toEqual({ + done: true, + value: undefined, + }); + + // Which may result in disconnecting upstream services as well. + expect( + pubsub.emit({ + from: 'adam@graphql.org', + subject: 'Important', + message: 'Read me please', + unread: true, + }) + ).toEqual(false); // No more listeners. + + // Awaiting a subscription after closing it results in completed results. + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: true, + value: undefined, + }); + }); + + it('produces a payload when there are multiple events', async () => { + const pubsub = new SimplePubSub(); + const subscription = createSubscription(pubsub); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + let payload = subscription.next(); + + // A new email arrives! + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Alright', + message: 'Tests are good', + unread: true, + }) + ).toEqual(true); + + expect(await payload).toEqual({ + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'yuzhi@graphql.org', + subject: 'Alright', + }, + inbox: { + unread: 1, + total: 2, + }, + }, + }, + }, + }); + + // @ts-expect-error + payload = subscription.next(); + + // A new email arrives! + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Alright 2', + message: 'Tests are good 2', + unread: true, + }) + ).toEqual(true); + + expect(await payload).toEqual({ + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'yuzhi@graphql.org', + subject: 'Alright 2', + }, + inbox: { + unread: 2, + total: 3, + }, + }, + }, + }, + }); + }); + + it('should not trigger when subscription is already done', async () => { + const pubsub = new SimplePubSub(); + const subscription = createSubscription(pubsub); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + let payload = subscription.next(); + + // A new email arrives! + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Alright', + message: 'Tests are good', + unread: true, + }) + ).toEqual(true); + + expect(await payload).toEqual({ + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'yuzhi@graphql.org', + subject: 'Alright', + }, + inbox: { + unread: 1, + total: 2, + }, + }, + }, + }, + }); + + // @ts-expect-error + payload = subscription.next(); + // @ts-expect-error + await subscription.return(); + + // A new email arrives! + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Alright 2', + message: 'Tests are good 2', + unread: true, + }) + ).toEqual(false); + + expect(await payload).toEqual({ + done: true, + value: undefined, + }); + }); + + it('should not trigger when subscription is thrown', async () => { + const pubsub = new SimplePubSub(); + const subscription = createSubscription(pubsub); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + let payload = subscription.next(); + + // A new email arrives! + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Alright', + message: 'Tests are good', + unread: true, + }) + ).toEqual(true); + + expect(await payload).toEqual({ + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'yuzhi@graphql.org', + subject: 'Alright', + }, + inbox: { + unread: 1, + total: 2, + }, + }, + }, + }, + }); + + // @ts-expect-error + payload = subscription.next(); + + // Throw error + let caughtError; + try { + /* c8 ignore next 2 */ + // @ts-expect-error + await subscription.throw('ouch'); + } catch (e) { + caughtError = e; + } + expect(caughtError).toEqual('ouch'); + + expect(await payload).toEqual({ + done: true, + value: undefined, + }); + }); + + it('event order is correct for multiple publishes', async () => { + const pubsub = new SimplePubSub(); + const subscription = createSubscription(pubsub); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + let payload = subscription.next(); + + // A new email arrives! + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Message', + message: 'Tests are good', + unread: true, + }) + ).toEqual(true); + + // A new email arrives! + expect( + pubsub.emit({ + from: 'yuzhi@graphql.org', + subject: 'Message 2', + message: 'Tests are good 2', + unread: true, + }) + ).toEqual(true); + + expect(await payload).toEqual({ + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'yuzhi@graphql.org', + subject: 'Message', + }, + inbox: { + unread: 2, + total: 3, + }, + }, + }, + }, + }); + + // @ts-expect-error + payload = subscription.next(); + + expect(await payload).toEqual({ + done: false, + value: { + data: { + importantEmail: { + email: { + from: 'yuzhi@graphql.org', + subject: 'Message 2', + }, + inbox: { + unread: 2, + total: 3, + }, + }, + }, + }, + }); + }); + + it('should handle error during execution of source event', async () => { + async function* generateMessages() { + yield 'Hello'; + yield 'Goodbye'; + yield 'Bonjour'; + } + + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + newMessage: { + type: GraphQLString, + subscribe: generateMessages, + resolve(message) { + if (message === 'Goodbye') { + throw new Error('Never leave.'); + } + return message; + }, + }, + }, + }), + }); + + const document = parse('subscription { newMessage }'); + const subscription = subscribe({ schema, document }); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: false, + value: { + data: { newMessage: 'Hello' }, + }, + }); + + // An error in execution is presented as such. + // @ts-expect-error + expectJSON(await subscription.next()).toDeepEqual({ + done: false, + value: { + data: { newMessage: null }, + errors: [ + { + message: 'Never leave.', + locations: [{ line: 1, column: 16 }], + path: ['newMessage'], + }, + ], + }, + }); + + // However that does not close the response event stream. + // Subsequent events are still executed. + // @ts-expect-error + expectJSON(await subscription.next()).toDeepEqual({ + done: false, + value: { + data: { newMessage: 'Bonjour' }, + }, + }); + + // @ts-expect-error + expectJSON(await subscription.next()).toDeepEqual({ + done: true, + value: undefined, + }); + }); + + it('should pass through error thrown in source event stream', async () => { + async function* generateMessages() { + yield 'Hello'; + throw new Error('test error'); + } + + const schema = new GraphQLSchema({ + query: DummyQueryType, + subscription: new GraphQLObjectType({ + name: 'Subscription', + fields: { + newMessage: { + type: GraphQLString, + resolve: message => message, + subscribe: generateMessages, + }, + }, + }), + }); + + const document = parse('subscription { newMessage }'); + const subscription = subscribe({ schema, document }); + expect(isAsyncIterable(subscription)).toBeTruthy(); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: false, + value: { + data: { newMessage: 'Hello' }, + }, + }); + + // @ts-expect-error + await expectPromise(subscription.next()).toRejectWith('test error'); + + // @ts-expect-error + expect(await subscription.next()).toEqual({ + done: true, + value: undefined, + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/sync-test.ts b/packages/graphql/src/execution/__tests__/sync-test.ts new file mode 100644 index 00000000000..14a1514aa1b --- /dev/null +++ b/packages/graphql/src/execution/__tests__/sync-test.ts @@ -0,0 +1,174 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { parse } from '../../language/parser.js'; + +import { GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { validate } from '../../validation/validate.js'; + +import { graphqlSync } from '../../graphql.js'; + +import { execute, executeSync } from '../execute.js'; + +describe('Execute: synchronously when possible', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + syncField: { + type: GraphQLString, + resolve(rootValue) { + return rootValue; + }, + }, + asyncField: { + type: GraphQLString, + resolve(rootValue) { + return Promise.resolve(rootValue); + }, + }, + }, + }), + mutation: new GraphQLObjectType({ + name: 'Mutation', + fields: { + syncMutationField: { + type: GraphQLString, + resolve(rootValue) { + return rootValue; + }, + }, + }, + }), + }); + + it('does not return a Promise for initial errors', () => { + const doc = 'fragment Example on Query { syncField }'; + const result = execute({ + schema, + document: parse(doc), + rootValue: 'rootValue', + }); + expectJSON(result).toDeepEqual({ + errors: [{ message: 'Must provide an operation.' }], + }); + }); + + it('does not return a Promise if fields are all synchronous', () => { + const doc = 'query Example { syncField }'; + const result = execute({ + schema, + document: parse(doc), + rootValue: 'rootValue', + }); + expect(result).toEqual({ data: { syncField: 'rootValue' } }); + }); + + it('does not return a Promise if mutation fields are all synchronous', () => { + const doc = 'mutation Example { syncMutationField }'; + const result = execute({ + schema, + document: parse(doc), + rootValue: 'rootValue', + }); + expect(result).toEqual({ data: { syncMutationField: 'rootValue' } }); + }); + + it('returns a Promise if any field is asynchronous', async () => { + const doc = 'query Example { syncField, asyncField }'; + const result = execute({ + schema, + document: parse(doc), + rootValue: 'rootValue', + }); + expect(result).toBeInstanceOf(Promise); + expect(await result).toEqual({ + data: { syncField: 'rootValue', asyncField: 'rootValue' }, + }); + }); + + describe('executeSync', () => { + it('does not return a Promise for sync execution', () => { + const doc = 'query Example { syncField }'; + const result = executeSync({ + schema, + document: parse(doc), + rootValue: 'rootValue', + }); + expect(result).toEqual({ data: { syncField: 'rootValue' } }); + }); + + it('throws if encountering async execution', () => { + const doc = 'query Example { syncField, asyncField }'; + expect(() => { + executeSync({ + schema, + document: parse(doc), + rootValue: 'rootValue', + }); + }).toThrow('GraphQL execution failed to complete synchronously.'); + }); + }); + + describe('graphqlSync', () => { + it('report errors raised during schema validation', () => { + const badSchema = new GraphQLSchema({}); + const result = graphqlSync({ + schema: badSchema, + source: '{ __typename }', + }); + expectJSON(result).toDeepEqual({ + errors: [{ message: 'Query root type must be provided.' }], + }); + }); + + it('does not return a Promise for syntax errors', () => { + const doc = 'fragment Example on Query { { { syncField }'; + const result = graphqlSync({ + schema, + source: doc, + }); + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Syntax Error: Expected Name, found "{".', + locations: [{ line: 1, column: 29 }], + }, + ], + }); + }); + + it('does not return a Promise for validation errors', () => { + const doc = 'fragment Example on Query { unknownField }'; + const validationErrors = validate(schema, parse(doc)); + const result = graphqlSync({ + schema, + source: doc, + }); + expect(result).toEqual({ errors: validationErrors }); + }); + + it('does not return a Promise for sync execution', () => { + const doc = 'query Example { syncField }'; + const result = graphqlSync({ + schema, + source: doc, + rootValue: 'rootValue', + }); + expect(result).toEqual({ data: { syncField: 'rootValue' } }); + }); + + it('throws if encountering async execution', () => { + const doc = 'query Example { syncField, asyncField }'; + expect(() => { + graphqlSync({ + schema, + source: doc, + rootValue: 'rootValue', + }); + }).toThrow('GraphQL execution failed to complete synchronously.'); + }); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/union-interface-test.ts b/packages/graphql/src/execution/__tests__/union-interface-test.ts new file mode 100644 index 00000000000..271c50f8967 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/union-interface-test.ts @@ -0,0 +1,536 @@ +import { parse } from '../../language/parser.js'; + +import { GraphQLInterfaceType, GraphQLList, GraphQLObjectType, GraphQLUnionType } from '../../type/definition.js'; +import { GraphQLBoolean, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { executeSync } from '../execute.js'; + +class Dog { + name: string; + barks: boolean; + mother?: Dog; + father?: Dog; + progeny: ReadonlyArray; + + constructor(name: string, barks: boolean) { + this.name = name; + this.barks = barks; + this.progeny = []; + } +} + +class Cat { + name: string; + meows: boolean; + mother?: Cat; + father?: Cat; + progeny: ReadonlyArray; + + constructor(name: string, meows: boolean) { + this.name = name; + this.meows = meows; + this.progeny = []; + } +} + +class Person { + name: string; + pets?: ReadonlyArray; + friends?: ReadonlyArray; + + constructor(name: string, pets?: ReadonlyArray, friends?: ReadonlyArray) { + this.name = name; + this.pets = pets; + this.friends = friends; + } +} + +const NamedType = new GraphQLInterfaceType({ + name: 'Named', + fields: { + name: { type: GraphQLString }, + }, +}); + +const LifeType: GraphQLInterfaceType = new GraphQLInterfaceType({ + name: 'Life', + fields: () => ({ + progeny: { type: new GraphQLList(LifeType) }, + }), +}); + +const MammalType: GraphQLInterfaceType = new GraphQLInterfaceType({ + name: 'Mammal', + interfaces: [LifeType], + fields: () => ({ + progeny: { type: new GraphQLList(MammalType) }, + mother: { type: MammalType }, + father: { type: MammalType }, + }), +}); + +const DogType: GraphQLObjectType = new GraphQLObjectType({ + name: 'Dog', + interfaces: [MammalType, LifeType, NamedType], + fields: () => ({ + name: { type: GraphQLString }, + barks: { type: GraphQLBoolean }, + progeny: { type: new GraphQLList(DogType) }, + mother: { type: DogType }, + father: { type: DogType }, + }), + isTypeOf: value => value instanceof Dog, +}); + +const CatType: GraphQLObjectType = new GraphQLObjectType({ + name: 'Cat', + interfaces: [MammalType, LifeType, NamedType], + fields: () => ({ + name: { type: GraphQLString }, + meows: { type: GraphQLBoolean }, + progeny: { type: new GraphQLList(CatType) }, + mother: { type: CatType }, + father: { type: CatType }, + }), + isTypeOf: value => value instanceof Cat, +}); + +const PetType = new GraphQLUnionType({ + name: 'Pet', + types: [DogType, CatType], + resolveType(value) { + if (value instanceof Dog) { + return DogType.name; + } + if (value instanceof Cat) { + return CatType.name; + } + /* c8 ignore next 3 */ + // Not reachable, all possible types have been considered. + return undefined; + }, +}); + +const PersonType: GraphQLObjectType = new GraphQLObjectType({ + name: 'Person', + interfaces: [NamedType, MammalType, LifeType], + fields: () => ({ + name: { type: GraphQLString }, + pets: { type: new GraphQLList(PetType) }, + friends: { type: new GraphQLList(NamedType) }, + progeny: { type: new GraphQLList(PersonType) }, + mother: { type: PersonType }, + father: { type: PersonType }, + }), + isTypeOf: value => value instanceof Person, +}); + +const schema = new GraphQLSchema({ + query: PersonType, + types: [PetType], +}); + +const garfield = new Cat('Garfield', false); +garfield.mother = new Cat("Garfield's Mom", false); +garfield.mother.progeny = [garfield]; + +const odie = new Dog('Odie', true); +odie.mother = new Dog("Odie's Mom", true); +odie.mother.progeny = [odie]; + +const liz = new Person('Liz'); +const john = new Person('John', [garfield, odie], [liz, odie]); + +describe('Execute: Union and intersection types', () => { + it('can introspect on union and intersection types', () => { + const document = parse(` + { + Named: __type(name: "Named") { + kind + name + fields { name } + interfaces { name } + possibleTypes { name } + enumValues { name } + inputFields { name } + } + Mammal: __type(name: "Mammal") { + kind + name + fields { name } + interfaces { name } + possibleTypes { name } + enumValues { name } + inputFields { name } + } + Pet: __type(name: "Pet") { + kind + name + fields { name } + interfaces { name } + possibleTypes { name } + enumValues { name } + inputFields { name } + } + } + `); + + expect(executeSync({ schema, document })).toEqual({ + data: { + Named: { + kind: 'INTERFACE', + name: 'Named', + fields: [{ name: 'name' }], + interfaces: [], + possibleTypes: [{ name: 'Dog' }, { name: 'Cat' }, { name: 'Person' }], + enumValues: null, + inputFields: null, + }, + Mammal: { + kind: 'INTERFACE', + name: 'Mammal', + fields: [{ name: 'progeny' }, { name: 'mother' }, { name: 'father' }], + interfaces: [{ name: 'Life' }], + possibleTypes: [{ name: 'Dog' }, { name: 'Cat' }, { name: 'Person' }], + enumValues: null, + inputFields: null, + }, + Pet: { + kind: 'UNION', + name: 'Pet', + fields: null, + interfaces: null, + possibleTypes: [{ name: 'Dog' }, { name: 'Cat' }], + enumValues: null, + inputFields: null, + }, + }, + }); + }); + + it('executes using union types', () => { + // NOTE: This is an *invalid* query, but it should be an *executable* query. + const document = parse(` + { + __typename + name + pets { + __typename + name + barks + meows + } + } + `); + + expect(executeSync({ schema, document, rootValue: john })).toEqual({ + data: { + __typename: 'Person', + name: 'John', + pets: [ + { + __typename: 'Cat', + name: 'Garfield', + meows: false, + }, + { + __typename: 'Dog', + name: 'Odie', + barks: true, + }, + ], + }, + }); + }); + + it('executes union types with inline fragments', () => { + // This is the valid version of the query in the above test. + const document = parse(` + { + __typename + name + pets { + __typename + ... on Dog { + name + barks + } + ... on Cat { + name + meows + } + } + } + `); + + expect(executeSync({ schema, document, rootValue: john })).toEqual({ + data: { + __typename: 'Person', + name: 'John', + pets: [ + { + __typename: 'Cat', + name: 'Garfield', + meows: false, + }, + { + __typename: 'Dog', + name: 'Odie', + barks: true, + }, + ], + }, + }); + }); + + it('executes using interface types', () => { + // NOTE: This is an *invalid* query, but it should be an *executable* query. + const document = parse(` + { + __typename + name + friends { + __typename + name + barks + meows + } + } + `); + + expect(executeSync({ schema, document, rootValue: john })).toEqual({ + data: { + __typename: 'Person', + name: 'John', + friends: [ + { __typename: 'Person', name: 'Liz' }, + { __typename: 'Dog', name: 'Odie', barks: true }, + ], + }, + }); + }); + + it('executes interface types with inline fragments', () => { + // This is the valid version of the query in the above test. + const document = parse(` + { + __typename + name + friends { + __typename + name + ... on Dog { + barks + } + ... on Cat { + meows + } + + ... on Mammal { + mother { + __typename + ... on Dog { + name + barks + } + ... on Cat { + name + meows + } + } + } + } + } + `); + + expect(executeSync({ schema, document, rootValue: john })).toEqual({ + data: { + __typename: 'Person', + name: 'John', + friends: [ + { + __typename: 'Person', + name: 'Liz', + mother: null, + }, + { + __typename: 'Dog', + name: 'Odie', + barks: true, + mother: { __typename: 'Dog', name: "Odie's Mom", barks: true }, + }, + ], + }, + }); + }); + + it('executes interface types with named fragments', () => { + const document = parse(` + { + __typename + name + friends { + __typename + name + ...DogBarks + ...CatMeows + } + } + + fragment DogBarks on Dog { + barks + } + + fragment CatMeows on Cat { + meows + } + `); + + expect(executeSync({ schema, document, rootValue: john })).toEqual({ + data: { + __typename: 'Person', + name: 'John', + friends: [ + { + __typename: 'Person', + name: 'Liz', + }, + { + __typename: 'Dog', + name: 'Odie', + barks: true, + }, + ], + }, + }); + }); + + it('allows fragment conditions to be abstract types', () => { + const document = parse(` + { + __typename + name + pets { + ...PetFields, + ...on Mammal { + mother { + ...ProgenyFields + } + } + } + friends { ...FriendFields } + } + + fragment PetFields on Pet { + __typename + ... on Dog { + name + barks + } + ... on Cat { + name + meows + } + } + + fragment FriendFields on Named { + __typename + name + ... on Dog { + barks + } + ... on Cat { + meows + } + } + + fragment ProgenyFields on Life { + progeny { + __typename + } + } + `); + + expect(executeSync({ schema, document, rootValue: john })).toEqual({ + data: { + __typename: 'Person', + name: 'John', + pets: [ + { + __typename: 'Cat', + name: 'Garfield', + meows: false, + mother: { progeny: [{ __typename: 'Cat' }] }, + }, + { + __typename: 'Dog', + name: 'Odie', + barks: true, + mother: { progeny: [{ __typename: 'Dog' }] }, + }, + ], + friends: [ + { + __typename: 'Person', + name: 'Liz', + }, + { + __typename: 'Dog', + name: 'Odie', + barks: true, + }, + ], + }, + }); + }); + + it('gets execution info in resolver', () => { + let encounteredContext; + let encounteredSchema; + let encounteredRootValue; + + const NamedType2: GraphQLInterfaceType = new GraphQLInterfaceType({ + name: 'Named', + fields: { + name: { type: GraphQLString }, + }, + resolveType(_source, context, info) { + encounteredContext = context; + encounteredSchema = info.schema; + encounteredRootValue = info.rootValue; + return PersonType2.name; + }, + }); + + const PersonType2: GraphQLObjectType = new GraphQLObjectType({ + name: 'Person', + interfaces: [NamedType2], + fields: { + name: { type: GraphQLString }, + friends: { type: new GraphQLList(NamedType2) }, + }, + }); + const schema2 = new GraphQLSchema({ query: PersonType2 }); + const document = parse('{ name, friends { name } }'); + const rootValue = new Person('John', [], [liz]); + const contextValue = { authToken: '123abc' }; + + const result = executeSync({ + schema: schema2, + document, + rootValue, + contextValue, + }); + expect(result).toEqual({ + data: { + name: 'John', + friends: [{ name: 'Liz' }], + }, + }); + + expect(encounteredSchema).toEqual(schema2); + expect(encounteredRootValue).toEqual(rootValue); + expect(encounteredContext).toEqual(contextValue); + }); +}); diff --git a/packages/graphql/src/execution/__tests__/variables-test.ts b/packages/graphql/src/execution/__tests__/variables-test.ts new file mode 100644 index 00000000000..e660c2421b9 --- /dev/null +++ b/packages/graphql/src/execution/__tests__/variables-test.ts @@ -0,0 +1,1038 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; +import { inspect } from '../../jsutils/inspect.js'; +import { Kind } from '../../language/kinds.js'; +import { parse } from '../../language/parser.js'; + +import type { GraphQLArgumentConfig, GraphQLFieldConfig } from '../../type/definition.js'; +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLScalarType, +} from '../../type/definition.js'; +import { GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { executeSync } from '../execute.js'; +import { getVariableValues } from '../values.js'; + +const TestComplexScalar = new GraphQLScalarType({ + name: 'ComplexScalar', + parseValue(value) { + expect(value).toEqual('SerializedValue'); + return 'DeserializedValue'; + }, + parseLiteral(ast) { + expect(ast).toMatchObject({ kind: 'StringValue', value: 'SerializedValue' }); + return 'DeserializedValue'; + }, +}); + +const TestInputObject = new GraphQLInputObjectType({ + name: 'TestInputObject', + fields: { + a: { type: GraphQLString }, + b: { type: new GraphQLList(GraphQLString) }, + c: { type: new GraphQLNonNull(GraphQLString) }, + d: { type: TestComplexScalar }, + }, +}); + +const TestNestedInputObject = new GraphQLInputObjectType({ + name: 'TestNestedInputObject', + fields: { + na: { type: new GraphQLNonNull(TestInputObject) }, + nb: { type: new GraphQLNonNull(GraphQLString) }, + }, +}); + +const TestEnum = new GraphQLEnumType({ + name: 'TestEnum', + values: { + NULL: { value: null }, + UNDEFINED: { value: undefined }, + NAN: { value: NaN }, + FALSE: { value: false }, + CUSTOM: { value: 'custom value' }, + DEFAULT_VALUE: {}, + }, +}); + +function fieldWithInputArg(inputArg: GraphQLArgumentConfig): GraphQLFieldConfig { + return { + type: GraphQLString, + args: { input: inputArg }, + resolve(_, args) { + if ('input' in args) { + return inspect(args.input); + } + return undefined; + }, + }; +} + +const TestType = new GraphQLObjectType({ + name: 'TestType', + fields: { + fieldWithEnumInput: fieldWithInputArg({ type: TestEnum }), + fieldWithNonNullableEnumInput: fieldWithInputArg({ + type: new GraphQLNonNull(TestEnum), + }), + fieldWithObjectInput: fieldWithInputArg({ type: TestInputObject }), + fieldWithNullableStringInput: fieldWithInputArg({ type: GraphQLString }), + fieldWithNonNullableStringInput: fieldWithInputArg({ + type: new GraphQLNonNull(GraphQLString), + }), + fieldWithDefaultArgumentValue: fieldWithInputArg({ + type: GraphQLString, + defaultValue: 'Hello World', + }), + fieldWithNonNullableStringInputAndDefaultArgumentValue: fieldWithInputArg({ + type: new GraphQLNonNull(GraphQLString), + defaultValue: 'Hello World', + }), + fieldWithNestedInputObject: fieldWithInputArg({ + type: TestNestedInputObject, + defaultValue: 'Hello World', + }), + list: fieldWithInputArg({ type: new GraphQLList(GraphQLString) }), + nnList: fieldWithInputArg({ + type: new GraphQLNonNull(new GraphQLList(GraphQLString)), + }), + listNN: fieldWithInputArg({ + type: new GraphQLList(new GraphQLNonNull(GraphQLString)), + }), + nnListNN: fieldWithInputArg({ + type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString))), + }), + }, +}); + +const schema = new GraphQLSchema({ query: TestType }); + +function executeQuery(query: string, variableValues?: { [variable: string]: unknown }) { + const document = parse(query); + return executeSync({ schema, document, variableValues }); +} + +describe('Execute: Handles inputs', () => { + describe('Handles objects and nullability', () => { + describe('using inline structs', () => { + it('executes with complex input', () => { + const result = executeQuery(` + { + fieldWithObjectInput(input: {a: "foo", b: ["bar"], c: "baz"}) + } + `); + + expect(result).toEqual({ + data: { + fieldWithObjectInput: '{ a: "foo", b: ["bar"], c: "baz" }', + }, + }); + }); + + it('properly parses single value to list', () => { + const result = executeQuery(` + { + fieldWithObjectInput(input: {a: "foo", b: "bar", c: "baz"}) + } + `); + + expect(result).toEqual({ + data: { + fieldWithObjectInput: '{ a: "foo", b: ["bar"], c: "baz" }', + }, + }); + }); + + it('properly parses null value to null', () => { + const result = executeQuery(` + { + fieldWithObjectInput(input: {a: null, b: null, c: "C", d: null}) + } + `); + + expect(result).toMatchObject({ + data: { + fieldWithObjectInput: '{ a: null, b: null, c: "C", d: null }', + }, + }); + }); + + it('properly parses null value in list', () => { + const result = executeQuery(` + { + fieldWithObjectInput(input: {b: ["A",null,"C"], c: "C"}) + } + `); + + expect(result).toEqual({ + data: { + fieldWithObjectInput: '{ b: ["A", null, "C"], c: "C" }', + }, + }); + }); + + it('does not use incorrect value', () => { + const result = executeQuery(` + { + fieldWithObjectInput(input: ["foo", "bar", "baz"]) + } + `); + + expectJSON(result).toDeepEqual({ + data: { + fieldWithObjectInput: null, + }, + errors: [ + { + message: 'Argument "input" has invalid value ["foo", "bar", "baz"].', + path: ['fieldWithObjectInput'], + locations: [{ line: 3, column: 41 }], + }, + ], + }); + }); + + it('properly runs parseLiteral on complex scalar types', () => { + const result = executeQuery(` + { + fieldWithObjectInput(input: {c: "foo", d: "SerializedValue"}) + } + `); + + expect(result).toMatchObject({ + data: { + fieldWithObjectInput: '{ c: "foo", d: "DeserializedValue" }', + }, + }); + }); + }); + + describe('using variables', () => { + const doc = ` + query ($input: TestInputObject) { + fieldWithObjectInput(input: $input) + } + `; + + it('executes with complex input', () => { + const params = { input: { a: 'foo', b: ['bar'], c: 'baz' } }; + const result = executeQuery(doc, params); + + expect(result).toEqual({ + data: { + fieldWithObjectInput: '{ a: "foo", b: ["bar"], c: "baz" }', + }, + }); + }); + + it('uses undefined when variable not provided', () => { + const result = executeQuery( + ` + query q($input: String) { + fieldWithNullableStringInput(input: $input) + }`, + { + // Intentionally missing variable values. + } + ); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: null, + }, + }); + }); + + it('uses null when variable provided explicit null value', () => { + const result = executeQuery( + ` + query q($input: String) { + fieldWithNullableStringInput(input: $input) + }`, + { input: null } + ); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: 'null', + }, + }); + }); + + it('uses default value when not provided', () => { + const result = executeQuery(` + query ($input: TestInputObject = {a: "foo", b: ["bar"], c: "baz"}) { + fieldWithObjectInput(input: $input) + } + `); + + expect(result).toEqual({ + data: { + fieldWithObjectInput: '{ a: "foo", b: ["bar"], c: "baz" }', + }, + }); + }); + + it('does not use default value when provided', () => { + const result = executeQuery( + ` + query q($input: String = "Default value") { + fieldWithNullableStringInput(input: $input) + } + `, + { input: 'Variable value' } + ); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: '"Variable value"', + }, + }); + }); + + it('uses explicit null value instead of default value', () => { + const result = executeQuery( + ` + query q($input: String = "Default value") { + fieldWithNullableStringInput(input: $input) + }`, + { input: null } + ); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: 'null', + }, + }); + }); + + it('uses null default value when not provided', () => { + const result = executeQuery( + ` + query q($input: String = null) { + fieldWithNullableStringInput(input: $input) + }`, + { + // Intentionally missing variable values. + } + ); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: 'null', + }, + }); + }); + + it('properly parses single value to list', () => { + const params = { input: { a: 'foo', b: 'bar', c: 'baz' } }; + const result = executeQuery(doc, params); + + expect(result).toEqual({ + data: { + fieldWithObjectInput: '{ a: "foo", b: ["bar"], c: "baz" }', + }, + }); + }); + + it('executes with complex scalar input', () => { + const params = { input: { c: 'foo', d: 'SerializedValue' } }; + const result = executeQuery(doc, params); + + expect(result).toEqual({ + data: { + fieldWithObjectInput: '{ c: "foo", d: "DeserializedValue" }', + }, + }); + }); + + it('errors on null for nested non-null', () => { + const params = { input: { a: 'foo', b: 'bar', c: null } }; + const result = executeQuery(doc, params); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: + 'Variable "$input" got invalid value null at "input.c"; Expected non-nullable type "String!" not to be null.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('errors on incorrect type', () => { + const result = executeQuery(doc, { input: 'foo bar' }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: + 'Variable "$input" got invalid value "foo bar"; Expected type "TestInputObject" to be an object.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('errors on omission of nested non-null', () => { + const result = executeQuery(doc, { input: { a: 'foo', b: 'bar' } }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: + 'Variable "$input" got invalid value { a: "foo", b: "bar" }; Field "c" of required type "String!" was not provided.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('errors on deep nested errors and with many errors', () => { + const nestedDoc = ` + query ($input: TestNestedInputObject) { + fieldWithNestedObjectInput(input: $input) + } + `; + const result = executeQuery(nestedDoc, { input: { na: { a: 'foo' } } }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: + 'Variable "$input" got invalid value { a: "foo" } at "input.na"; Field "c" of required type "String!" was not provided.', + locations: [{ line: 2, column: 18 }], + }, + { + message: + 'Variable "$input" got invalid value { na: { a: "foo" } }; Field "nb" of required type "String!" was not provided.', + locations: [{ line: 2, column: 18 }], + }, + ], + }); + }); + + it('errors on addition of unknown input field', () => { + const params = { + input: { a: 'foo', b: 'bar', c: 'baz', extra: 'dog' }, + }; + const result = executeQuery(doc, params); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: + 'Variable "$input" got invalid value { a: "foo", b: "bar", c: "baz", extra: "dog" }; Field "extra" is not defined by type "TestInputObject".', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + }); + }); + + describe('Handles custom enum values', () => { + it('allows custom enum values as inputs', () => { + const result = executeQuery(` + { + null: fieldWithEnumInput(input: NULL) + NaN: fieldWithEnumInput(input: NAN) + false: fieldWithEnumInput(input: FALSE) + customValue: fieldWithEnumInput(input: CUSTOM) + defaultValue: fieldWithEnumInput(input: DEFAULT_VALUE) + } + `); + + expect(result).toEqual({ + data: { + null: 'null', + NaN: 'NaN', + false: 'false', + customValue: '"custom value"', + defaultValue: '"DEFAULT_VALUE"', + }, + }); + }); + + it('allows non-nullable inputs to have null as enum custom value', () => { + const result = executeQuery(` + { + fieldWithNonNullableEnumInput(input: NULL) + } + `); + + expect(result).toEqual({ + data: { + fieldWithNonNullableEnumInput: 'null', + }, + }); + }); + }); + + describe('Handles nullable scalars', () => { + it('allows nullable inputs to be omitted', () => { + const result = executeQuery(` + { + fieldWithNullableStringInput + } + `); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: null, + }, + }); + }); + + it('allows nullable inputs to be omitted in a variable', () => { + const result = executeQuery(` + query ($value: String) { + fieldWithNullableStringInput(input: $value) + } + `); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: null, + }, + }); + }); + + it('allows nullable inputs to be omitted in an unlisted variable', () => { + const result = executeQuery(` + query { + fieldWithNullableStringInput(input: $value) + } + `); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: null, + }, + }); + }); + + it('allows nullable inputs to be set to null in a variable', () => { + const doc = ` + query ($value: String) { + fieldWithNullableStringInput(input: $value) + } + `; + const result = executeQuery(doc, { value: null }); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: 'null', + }, + }); + }); + + it('allows nullable inputs to be set to a value in a variable', () => { + const doc = ` + query ($value: String) { + fieldWithNullableStringInput(input: $value) + } + `; + const result = executeQuery(doc, { value: 'a' }); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: '"a"', + }, + }); + }); + + it('allows nullable inputs to be set to a value directly', () => { + const result = executeQuery(` + { + fieldWithNullableStringInput(input: "a") + } + `); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: '"a"', + }, + }); + }); + }); + + describe('Handles non-nullable scalars', () => { + it('allows non-nullable variable to be omitted given a default', () => { + const result = executeQuery(` + query ($value: String! = "default") { + fieldWithNullableStringInput(input: $value) + } + `); + + expect(result).toEqual({ + data: { + fieldWithNullableStringInput: '"default"', + }, + }); + }); + + it('allows non-nullable inputs to be omitted given a default', () => { + const result = executeQuery(` + query ($value: String = "default") { + fieldWithNonNullableStringInput(input: $value) + } + `); + + expect(result).toEqual({ + data: { + fieldWithNonNullableStringInput: '"default"', + }, + }); + }); + + it('does not allow non-nullable inputs to be omitted in a variable', () => { + const result = executeQuery(` + query ($value: String!) { + fieldWithNonNullableStringInput(input: $value) + } + `); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$value" of required type "String!" was not provided.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('does not allow non-nullable inputs to be set to null in a variable', () => { + const doc = ` + query ($value: String!) { + fieldWithNonNullableStringInput(input: $value) + } + `; + const result = executeQuery(doc, { value: null }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$value" of non-null type "String!" must not be null.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('allows non-nullable inputs to be set to a value in a variable', () => { + const doc = ` + query ($value: String!) { + fieldWithNonNullableStringInput(input: $value) + } + `; + const result = executeQuery(doc, { value: 'a' }); + + expect(result).toEqual({ + data: { + fieldWithNonNullableStringInput: '"a"', + }, + }); + }); + + it('allows non-nullable inputs to be set to a value directly', () => { + const result = executeQuery(` + { + fieldWithNonNullableStringInput(input: "a") + } + `); + + expect(result).toEqual({ + data: { + fieldWithNonNullableStringInput: '"a"', + }, + }); + }); + + it('reports error for missing non-nullable inputs', () => { + const result = executeQuery('{ fieldWithNonNullableStringInput }'); + + expectJSON(result).toDeepEqual({ + data: { + fieldWithNonNullableStringInput: null, + }, + errors: [ + { + message: 'Argument "input" of required type "String!" was not provided.', + locations: [{ line: 1, column: 3 }], + path: ['fieldWithNonNullableStringInput'], + }, + ], + }); + }); + + it('reports error for array passed into string input', () => { + const doc = ` + query ($value: String!) { + fieldWithNonNullableStringInput(input: $value) + } + `; + const result = executeQuery(doc, { value: [1, 2, 3] }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: + 'Variable "$value" got invalid value [1, 2, 3]; String cannot represent a non string value: [1, 2, 3]', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + + expect(result).toHaveProperty('errors[0].originalError'); + }); + + it('reports error for non-provided variables for non-nullable inputs', () => { + // Note: this test would typically fail validation before encountering + // this execution error, however for queries which previously validated + // and are being run against a new schema which have introduced a breaking + // change to make a formerly non-required argument required, this asserts + // failure before allowing the underlying code to receive a non-null value. + const result = executeQuery(` + { + fieldWithNonNullableStringInput(input: $foo) + } + `); + + expectJSON(result).toDeepEqual({ + data: { + fieldWithNonNullableStringInput: null, + }, + errors: [ + { + message: + 'Argument "input" of required type "String!" was provided the variable "$foo" which was not provided a runtime value.', + locations: [{ line: 3, column: 50 }], + path: ['fieldWithNonNullableStringInput'], + }, + ], + }); + }); + }); + + describe('Handles lists and nullability', () => { + it('allows lists to be null', () => { + const doc = ` + query ($input: [String]) { + list(input: $input) + } + `; + const result = executeQuery(doc, { input: null }); + + expect(result).toEqual({ data: { list: 'null' } }); + }); + + it('allows lists to contain values', () => { + const doc = ` + query ($input: [String]) { + list(input: $input) + } + `; + const result = executeQuery(doc, { input: ['A'] }); + + expect(result).toEqual({ data: { list: '["A"]' } }); + }); + + it('allows lists to contain null', () => { + const doc = ` + query ($input: [String]) { + list(input: $input) + } + `; + const result = executeQuery(doc, { input: ['A', null, 'B'] }); + + expect(result).toEqual({ data: { list: '["A", null, "B"]' } }); + }); + + it('does not allow non-null lists to be null', () => { + const doc = ` + query ($input: [String]!) { + nnList(input: $input) + } + `; + const result = executeQuery(doc, { input: null }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$input" of non-null type "[String]!" must not be null.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('allows non-null lists to contain values', () => { + const doc = ` + query ($input: [String]!) { + nnList(input: $input) + } + `; + const result = executeQuery(doc, { input: ['A'] }); + + expect(result).toEqual({ data: { nnList: '["A"]' } }); + }); + + it('allows non-null lists to contain null', () => { + const doc = ` + query ($input: [String]!) { + nnList(input: $input) + } + `; + const result = executeQuery(doc, { input: ['A', null, 'B'] }); + + expect(result).toEqual({ data: { nnList: '["A", null, "B"]' } }); + }); + + it('allows lists of non-nulls to be null', () => { + const doc = ` + query ($input: [String!]) { + listNN(input: $input) + } + `; + const result = executeQuery(doc, { input: null }); + + expect(result).toEqual({ data: { listNN: 'null' } }); + }); + + it('allows lists of non-nulls to contain values', () => { + const doc = ` + query ($input: [String!]) { + listNN(input: $input) + } + `; + const result = executeQuery(doc, { input: ['A'] }); + + expect(result).toEqual({ data: { listNN: '["A"]' } }); + }); + + it('does not allow lists of non-nulls to contain null', () => { + const doc = ` + query ($input: [String!]) { + listNN(input: $input) + } + `; + const result = executeQuery(doc, { input: ['A', null, 'B'] }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: + 'Variable "$input" got invalid value null at "input[1]"; Expected non-nullable type "String!" not to be null.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('does not allow non-null lists of non-nulls to be null', () => { + const doc = ` + query ($input: [String!]!) { + nnListNN(input: $input) + } + `; + const result = executeQuery(doc, { input: null }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$input" of non-null type "[String!]!" must not be null.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('allows non-null lists of non-nulls to contain values', () => { + const doc = ` + query ($input: [String!]!) { + nnListNN(input: $input) + } + `; + const result = executeQuery(doc, { input: ['A'] }); + + expect(result).toEqual({ data: { nnListNN: '["A"]' } }); + }); + + it('does not allow non-null lists of non-nulls to contain null', () => { + const doc = ` + query ($input: [String!]!) { + nnListNN(input: $input) + } + `; + const result = executeQuery(doc, { input: ['A', null, 'B'] }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: + 'Variable "$input" got invalid value null at "input[1]"; Expected non-nullable type "String!" not to be null.', + locations: [{ line: 2, column: 16 }], + }, + ], + }); + }); + + it('does not allow invalid types to be used as values', () => { + const doc = ` + query ($input: TestType!) { + fieldWithObjectInput(input: $input) + } + `; + const result = executeQuery(doc, { input: { list: ['A', 'B'] } }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$input" expected value of type "TestType!" which cannot be used as an input type.', + locations: [{ line: 2, column: 24 }], + }, + ], + }); + }); + + it('does not allow unknown types to be used as values', () => { + const doc = ` + query ($input: UnknownType!) { + fieldWithObjectInput(input: $input) + } + `; + const result = executeQuery(doc, { input: 'WhoKnows' }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$input" expected value of type "UnknownType!" which cannot be used as an input type.', + locations: [{ line: 2, column: 24 }], + }, + ], + }); + }); + }); + + describe('Execute: Uses argument default values', () => { + it('when no argument provided', () => { + const result = executeQuery('{ fieldWithDefaultArgumentValue }'); + + expect(result).toEqual({ + data: { + fieldWithDefaultArgumentValue: '"Hello World"', + }, + }); + }); + + it('when omitted variable provided', () => { + const result = executeQuery(` + query ($optional: String) { + fieldWithDefaultArgumentValue(input: $optional) + } + `); + + expect(result).toEqual({ + data: { + fieldWithDefaultArgumentValue: '"Hello World"', + }, + }); + }); + + it('not when argument cannot be coerced', () => { + const result = executeQuery(` + { + fieldWithDefaultArgumentValue(input: WRONG_TYPE) + } + `); + + expectJSON(result).toDeepEqual({ + data: { + fieldWithDefaultArgumentValue: null, + }, + errors: [ + { + message: 'Argument "input" has invalid value WRONG_TYPE.', + locations: [{ line: 3, column: 48 }], + path: ['fieldWithDefaultArgumentValue'], + }, + ], + }); + }); + + it('when no runtime value is provided to a non-null argument', () => { + const result = executeQuery(` + query optionalVariable($optional: String) { + fieldWithNonNullableStringInputAndDefaultArgumentValue(input: $optional) + } + `); + + expect(result).toEqual({ + data: { + fieldWithNonNullableStringInputAndDefaultArgumentValue: '"Hello World"', + }, + }); + }); + }); + + describe('getVariableValues: limit maximum number of coercion errors', () => { + const doc = parse(` + query ($input: [String!]) { + listNN(input: $input) + } + `); + + const operation = doc.definitions[0]; + expect(operation.kind === Kind.OPERATION_DEFINITION).toBeTruthy(); + // @ts-expect-error + const { variableDefinitions } = operation; + expect(variableDefinitions != null).toBeTruthy(); + + const inputValue = { input: [0, 1, 2] }; + + function invalidValueError(value: number, index: number) { + return { + message: `Variable "$input" got invalid value ${value} at "input[${index}]"; String cannot represent a non string value: ${value}`, + locations: [{ line: 2, column: 14 }], + }; + } + + it('return all errors by default', () => { + const result = getVariableValues(schema, variableDefinitions, inputValue); + + expectJSON(result).toDeepEqual({ + errors: [invalidValueError(0, 0), invalidValueError(1, 1), invalidValueError(2, 2)], + }); + }); + + it('when maxErrors is equal to number of errors', () => { + const result = getVariableValues(schema, variableDefinitions, inputValue, { maxErrors: 3 }); + + expectJSON(result).toDeepEqual({ + errors: [invalidValueError(0, 0), invalidValueError(1, 1), invalidValueError(2, 2)], + }); + }); + + it('when maxErrors is less than number of errors', () => { + const result = getVariableValues(schema, variableDefinitions, inputValue, { maxErrors: 2 }); + + expectJSON(result).toDeepEqual({ + errors: [ + invalidValueError(0, 0), + invalidValueError(1, 1), + { + message: 'Too many errors processing variables, error limit reached. Execution aborted.', + }, + ], + }); + }); + }); +}); diff --git a/packages/graphql/src/execution/collectFields.ts b/packages/graphql/src/execution/collectFields.ts new file mode 100644 index 00000000000..35d1692c0d5 --- /dev/null +++ b/packages/graphql/src/execution/collectFields.ts @@ -0,0 +1,186 @@ +import { AccumulatorMap } from '../jsutils/AccumulatorMap.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; + +import type { + FieldNode, + FragmentDefinitionNode, + FragmentSpreadNode, + InlineFragmentNode, + SelectionSetNode, +} from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +import type { GraphQLObjectType } from '../type/definition.js'; +import { isAbstractType } from '../type/definition.js'; +import { GraphQLIncludeDirective, GraphQLSkipDirective } from '../type/directives.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +import { typeFromAST } from '../utilities/typeFromAST.js'; + +import { getDirectiveValues } from './values.js'; + +/** + * Given a selectionSet, collects all of the fields and returns them. + * + * CollectFields requires the "runtime type" of an object. For a field that + * returns an Interface or Union type, the "runtime type" will be the actual + * object type returned by that field. + * + * @internal + */ +export function collectFields( + schema: GraphQLSchema, + fragments: ObjMap, + variableValues: { [variable: string]: unknown }, + runtimeType: GraphQLObjectType, + selectionSet: SelectionSetNode +): Map> { + const fields = new AccumulatorMap(); + collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, new Set()); + return fields; +} + +/** + * Given an array of field nodes, collects all of the subfields of the passed + * in fields, and returns them at the end. + * + * CollectSubFields requires the "return type" of an object. For a field that + * returns an Interface or Union type, the "return type" will be the actual + * object type returned by that field. + * + * @internal + */ +export function collectSubfields( + schema: GraphQLSchema, + fragments: ObjMap, + variableValues: { [variable: string]: unknown }, + returnType: GraphQLObjectType, + fieldNodes: ReadonlyArray +): Map> { + const subFieldNodes = new AccumulatorMap(); + const visitedFragmentNames = new Set(); + for (const node of fieldNodes) { + if (node.selectionSet) { + collectFieldsImpl( + schema, + fragments, + variableValues, + returnType, + node.selectionSet, + subFieldNodes, + visitedFragmentNames + ); + } + } + return subFieldNodes; +} + +function collectFieldsImpl( + schema: GraphQLSchema, + fragments: ObjMap, + variableValues: { [variable: string]: unknown }, + runtimeType: GraphQLObjectType, + selectionSet: SelectionSetNode, + fields: AccumulatorMap, + visitedFragmentNames: Set +): void { + for (const selection of selectionSet.selections) { + switch (selection.kind) { + case Kind.FIELD: { + if (!shouldIncludeNode(variableValues, selection)) { + continue; + } + fields.add(getFieldEntryKey(selection), selection); + break; + } + case Kind.INLINE_FRAGMENT: { + if ( + !shouldIncludeNode(variableValues, selection) || + !doesFragmentConditionMatch(schema, selection, runtimeType) + ) { + continue; + } + collectFieldsImpl( + schema, + fragments, + variableValues, + runtimeType, + selection.selectionSet, + fields, + visitedFragmentNames + ); + break; + } + case Kind.FRAGMENT_SPREAD: { + const fragName = selection.name.value; + if (visitedFragmentNames.has(fragName) || !shouldIncludeNode(variableValues, selection)) { + continue; + } + visitedFragmentNames.add(fragName); + const fragment = fragments[fragName]; + if (!fragment || !doesFragmentConditionMatch(schema, fragment, runtimeType)) { + continue; + } + collectFieldsImpl( + schema, + fragments, + variableValues, + runtimeType, + fragment.selectionSet, + fields, + visitedFragmentNames + ); + break; + } + } + } +} + +/** + * Determines if a field should be included based on the `@include` and `@skip` + * directives, where `@skip` has higher precedence than `@include`. + */ +function shouldIncludeNode( + variableValues: { [variable: string]: unknown }, + node: FragmentSpreadNode | FieldNode | InlineFragmentNode +): boolean { + const skip = getDirectiveValues(GraphQLSkipDirective, node, variableValues); + if (skip?.['if'] === true) { + return false; + } + + const include = getDirectiveValues(GraphQLIncludeDirective, node, variableValues); + if (include?.['if'] === false) { + return false; + } + return true; +} + +/** + * Determines if a fragment is applicable to the given type. + */ +function doesFragmentConditionMatch( + schema: GraphQLSchema, + fragment: FragmentDefinitionNode | InlineFragmentNode, + type: GraphQLObjectType +): boolean { + const typeConditionNode = fragment.typeCondition; + if (!typeConditionNode) { + return true; + } + const conditionalType = typeFromAST(schema, typeConditionNode); + if (conditionalType === type) { + return true; + } + if (isAbstractType(conditionalType)) { + return schema.isSubType(conditionalType, type); + } + return false; +} + +/** + * Implements the logic to compute the key of a given field's entry + */ +function getFieldEntryKey(node: FieldNode): string { + return node.alias ? node.alias.value : node.name.value; +} diff --git a/packages/graphql/src/execution/execute.ts b/packages/graphql/src/execution/execute.ts new file mode 100644 index 00000000000..5c06e7beb65 --- /dev/null +++ b/packages/graphql/src/execution/execute.ts @@ -0,0 +1,1130 @@ +import { devAssert } from '../jsutils/devAssert.js'; +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; +import { isAsyncIterable } from '../jsutils/isAsyncIterable.js'; +import { isIterableObject } from '../jsutils/isIterableObject.js'; +import { isObjectLike } from '../jsutils/isObjectLike.js'; +import { isPromise } from '../jsutils/isPromise.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import { memoize3 } from '../jsutils/memoize3.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; +import type { Path } from '../jsutils/Path.js'; +import { addPath, pathToArray } from '../jsutils/Path.js'; +import { promiseForObject } from '../jsutils/promiseForObject.js'; +import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js'; +import { promiseReduce } from '../jsutils/promiseReduce.js'; + +import type { GraphQLFormattedError } from '../error/GraphQLError.js'; +import { GraphQLError } from '../error/GraphQLError.js'; +import { locatedError } from '../error/locatedError.js'; + +import type { DocumentNode, FieldNode, FragmentDefinitionNode, OperationDefinitionNode } from '../language/ast.js'; +import { OperationTypeNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +import type { + GraphQLAbstractType, + GraphQLField, + GraphQLFieldResolver, + GraphQLLeafType, + GraphQLList, + GraphQLObjectType, + GraphQLOutputType, + GraphQLResolveInfo, + GraphQLTypeResolver, +} from '../type/definition.js'; +import { isAbstractType, isLeafType, isListType, isNonNullType, isObjectType } from '../type/definition.js'; +import type { GraphQLSchema } from '../type/schema.js'; +import { assertValidSchema } from '../type/validate.js'; +import { SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef } from '../type/introspection.js'; + +import { collectFields, collectSubfields as _collectSubfields } from './collectFields.js'; +import { mapAsyncIterator } from './mapAsyncIterator.js'; +import { getArgumentValues, getVariableValues } from './values.js'; + +// This file contains a lot of such errors but we plan to refactor it anyway +// so just disable it for entire file. + +/** + * A memoized collection of relevant subfields with regard to the return + * type. Memoizing ensures the subfields are not repeatedly calculated, which + * saves overhead when resolving lists of values. + */ +const collectSubfields = memoize3( + (exeContext: ExecutionContext, returnType: GraphQLObjectType, fieldNodes: ReadonlyArray) => + _collectSubfields(exeContext.schema, exeContext.fragments, exeContext.variableValues, returnType, fieldNodes) +); + +/** + * Terminology + * + * "Definitions" are the generic name for top-level statements in the document. + * Examples of this include: + * 1) Operations (such as a query) + * 2) Fragments + * + * "Operations" are a generic name for requests in the document. + * Examples of this include: + * 1) query, + * 2) mutation + * + * "Selections" are the definitions that can appear legally and at + * single level of the query. These include: + * 1) field references e.g `a` + * 2) fragment "spreads" e.g. `...c` + * 3) inline fragment "spreads" e.g. `...on Type { a }` + */ + +/** + * Data that must be available at all points during query execution. + * + * Namely, schema of the type system that is currently executing, + * and the fragments defined in the query document + */ +export interface ExecutionContext { + schema: GraphQLSchema; + fragments: ObjMap; + rootValue: unknown; + contextValue: unknown; + operation: OperationDefinitionNode; + variableValues: { [variable: string]: unknown }; + fieldResolver: GraphQLFieldResolver; + typeResolver: GraphQLTypeResolver; + subscribeFieldResolver: GraphQLFieldResolver; + errors: Array; +} + +/** + * The result of GraphQL execution. + * + * - `errors` is included when any errors occurred as a non-empty array. + * - `data` is the result of a successful execution of the query. + * - `extensions` is reserved for adding non-standard properties. + */ +export interface ExecutionResult, TExtensions = ObjMap> { + errors?: ReadonlyArray; + data?: TData | null; + extensions?: TExtensions; +} + +export interface FormattedExecutionResult, TExtensions = ObjMap> { + errors?: ReadonlyArray; + data?: TData | null; + extensions?: TExtensions; +} + +export interface ExecutionArgs { + schema: GraphQLSchema; + document: DocumentNode; + rootValue?: unknown; + contextValue?: unknown; + variableValues?: Maybe<{ readonly [variable: string]: unknown }>; + operationName?: Maybe; + fieldResolver?: Maybe>; + typeResolver?: Maybe>; + subscribeFieldResolver?: Maybe>; +} + +/** + * Implements the "Executing requests" section of the GraphQL specification. + * + * Returns either a synchronous ExecutionResult (if all encountered resolvers + * are synchronous), or a Promise of an ExecutionResult that will eventually be + * resolved and never rejected. + * + * If the arguments to this function do not result in a legal execution context, + * a GraphQLError will be thrown immediately explaining the invalid input. + */ +export function execute(args: ExecutionArgs): PromiseOrValue { + // If a valid execution context cannot be created due to incorrect arguments, + // a "Response" with only errors is returned. + const exeContext = buildExecutionContext(args); + + // Return early errors if execution context failed. + if (!('schema' in exeContext)) { + return { errors: exeContext }; + } + + return executeImpl(exeContext); +} + +function executeImpl(exeContext: ExecutionContext): PromiseOrValue { + // Return a Promise that will eventually resolve to the data described by + // The "Response" section of the GraphQL specification. + // + // If errors are encountered while executing a GraphQL field, only that + // field and its descendants will be omitted, and sibling fields will still + // be executed. An execution which encounters errors will still result in a + // resolved Promise. + // + // Errors from sub-fields of a NonNull type may propagate to the top level, + // at which point we still log the error and null the parent field, which + // in this case is the entire response. + try { + const result = executeOperation(exeContext); + if (isPromise(result)) { + return result.then( + data => buildResponse(data, exeContext.errors), + error => { + exeContext.errors.push(error); + return buildResponse(null, exeContext.errors); + } + ); + } + return buildResponse(result, exeContext.errors); + } catch (error) { + exeContext.errors.push(error as GraphQLError); + return buildResponse(null, exeContext.errors); + } +} + +/** + * Also implements the "Executing requests" section of the GraphQL specification. + * However, it guarantees to complete synchronously (or throw an error) assuming + * that all field resolvers are also synchronous. + */ +export function executeSync(args: ExecutionArgs): ExecutionResult { + const result = execute(args); + + // Assert that the execution was synchronous. + if (isPromise(result)) { + throw new Error('GraphQL execution failed to complete synchronously.'); + } + + return result; +} + +/** + * Given a completed execution context and data, build the `{ errors, data }` + * response defined by the "Response" section of the GraphQL specification. + */ +function buildResponse(data: ObjMap | null, errors: ReadonlyArray): ExecutionResult { + return errors.length === 0 ? { data } : { errors, data }; +} + +/** + * Essential assertions before executing to provide developer feedback for + * improper use of the GraphQL library. + * + * @internal + */ +export function assertValidExecutionArguments( + schema: GraphQLSchema, + document: DocumentNode, + rawVariableValues: Maybe<{ readonly [variable: string]: unknown }> +): void { + devAssert(!!document, 'Must provide document.'); + + // If the schema used for execution is invalid, throw an error. + assertValidSchema(schema); + + // Variables, if provided, must be an object. + devAssert( + rawVariableValues == null || isObjectLike(rawVariableValues), + 'Variables must be provided as an Object where each property is a variable value. Perhaps look to see if an unparsed JSON string was provided.' + ); +} + +/** + * Constructs a ExecutionContext object from the arguments passed to + * execute, which we will pass throughout the other execution methods. + * + * Throws a GraphQLError if a valid execution context cannot be created. + * + * TODO: consider no longer exporting this function + * @internal + */ +export function buildExecutionContext(args: ExecutionArgs): ReadonlyArray | ExecutionContext { + const { + schema, + document, + rootValue, + contextValue, + variableValues: rawVariableValues, + operationName, + fieldResolver, + typeResolver, + subscribeFieldResolver, + } = args; + + // If the schema used for execution is invalid, throw an error. + assertValidSchema(schema); + + let operation: OperationDefinitionNode | undefined; + const fragments: ObjMap = Object.create(null); + for (const definition of document.definitions) { + switch (definition.kind) { + case Kind.OPERATION_DEFINITION: + if (operationName == null) { + if (operation !== undefined) { + return [new GraphQLError('Must provide operation name if query contains multiple operations.')]; + } + operation = definition; + } else if (definition.name?.value === operationName) { + operation = definition; + } + break; + case Kind.FRAGMENT_DEFINITION: + fragments[definition.name.value] = definition; + break; + default: + // ignore non-executable definitions + } + } + + if (!operation) { + if (operationName != null) { + return [new GraphQLError(`Unknown operation named "${operationName}".`)]; + } + return [new GraphQLError('Must provide an operation.')]; + } + + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const variableDefinitions = operation.variableDefinitions ?? []; + + const coercedVariableValues = getVariableValues(schema, variableDefinitions, rawVariableValues ?? {}, { + maxErrors: 50, + }); + + if (coercedVariableValues.errors) { + return coercedVariableValues.errors; + } + + return { + schema, + fragments, + rootValue, + contextValue, + operation, + variableValues: coercedVariableValues.coerced, + fieldResolver: fieldResolver ?? defaultFieldResolver, + typeResolver: typeResolver ?? defaultTypeResolver, + subscribeFieldResolver: subscribeFieldResolver ?? defaultFieldResolver, + errors: [], + }; +} + +function buildPerEventExecutionContext(exeContext: ExecutionContext, payload: unknown): ExecutionContext { + return { + ...exeContext, + rootValue: payload, + errors: [], + }; +} + +/** + * Implements the "Executing operations" section of the spec. + */ +function executeOperation(exeContext: ExecutionContext): PromiseOrValue> { + const { operation, schema, fragments, variableValues, rootValue } = exeContext; + const rootType = schema.getRootType(operation.operation); + if (rootType == null) { + throw new GraphQLError(`Schema is not configured to execute ${operation.operation} operation.`, { + nodes: operation, + }); + } + + const rootFields = collectFields(schema, fragments, variableValues, rootType, operation.selectionSet); + const path = undefined; + + switch (operation.operation) { + case OperationTypeNode.QUERY: + return executeFields(exeContext, rootType, rootValue, path, rootFields); + case OperationTypeNode.MUTATION: + return executeFieldsSerially(exeContext, rootType, rootValue, path, rootFields); + case OperationTypeNode.SUBSCRIPTION: + // TODO: deprecate `subscribe` and move all logic here + // Temporary solution until we finish merging execute and subscribe together + return executeFields(exeContext, rootType, rootValue, path, rootFields); + } +} + +/** + * Implements the "Executing selection sets" section of the spec + * for fields that must be executed serially. + */ +function executeFieldsSerially( + exeContext: ExecutionContext, + parentType: GraphQLObjectType, + sourceValue: unknown, + path: Path | undefined, + fields: Map> +): PromiseOrValue> { + return promiseReduce( + fields.entries(), + (results, [responseName, fieldNodes]) => { + const fieldPath = addPath(path, responseName, parentType.name); + const result = executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath); + if (result === undefined) { + return results; + } + if (isPromise(result)) { + return result.then(resolvedResult => { + results[responseName] = resolvedResult; + return results; + }); + } + results[responseName] = result; + return results; + }, + Object.create(null) + ); +} + +/** + * Implements the "Executing selection sets" section of the spec + * for fields that may be executed in parallel. + */ +function executeFields( + exeContext: ExecutionContext, + parentType: GraphQLObjectType, + sourceValue: unknown, + path: Path | undefined, + fields: Map> +): PromiseOrValue> { + const results = Object.create(null); + let containsPromise = false; + + for (const [responseName, fieldNodes] of fields.entries()) { + const fieldPath = addPath(path, responseName, parentType.name); + const result = executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath); + + if (result !== undefined) { + results[responseName] = result; + if (isPromise(result)) { + containsPromise = true; + } + } + } + + // If there are no promises, we can just return the object + if (!containsPromise) { + return results; + } + + // Otherwise, results is a map from field name to the result of resolving that + // field, which is possibly a promise. Return a promise that will return this + // same map, but with any promises replaced with the values they resolved to. + return promiseForObject(results); +} + +/** + * Implements the "Executing fields" section of the spec + * In particular, this function figures out the value that the field returns by + * calling its resolve function, then calls completeValue to complete promises, + * serialize scalars, or execute the sub-selection-set for objects. + */ +function executeField( + exeContext: ExecutionContext, + parentType: GraphQLObjectType, + source: unknown, + fieldNodes: ReadonlyArray, + path: Path +): PromiseOrValue { + const fieldName = fieldNodes[0].name.value; + const fieldDef = exeContext.schema.getField(parentType, fieldName); + if (!fieldDef) { + return; + } + + const returnType = fieldDef.type; + const resolveFn = fieldDef.resolve ?? exeContext.fieldResolver; + + const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path); + + // Get the resolve function, regardless of if its result is normal or abrupt (error). + try { + // Build a JS object of arguments from the field.arguments AST, using the + // variables scope to fulfill any variable references. + // TODO: find a way to memoize, in case this field is within a List type. + const args = getArgumentValues(fieldDef, fieldNodes[0], exeContext.variableValues); + + // The resolve function's optional third argument is a context value that + // is provided to every resolve function within an execution. It is commonly + // used to represent an authenticated user, or request-specific caches. + const contextValue = exeContext.contextValue; + + const result = resolveFn(source, args, contextValue, info); + + let completed; + if (isPromise(result)) { + completed = result.then(resolved => completeValue(exeContext, returnType, fieldNodes, info, path, resolved)); + } else { + completed = completeValue(exeContext, returnType, fieldNodes, info, path, result); + } + + if (isPromise(completed)) { + // Note: we don't rely on a `catch` method, but we do expect "thenable" + // to take a second callback for the error case. + return completed.then(undefined, rawError => { + const error = locatedError(rawError, fieldNodes, pathToArray(path)); + return handleFieldError(error, returnType, exeContext); + }); + } + return completed; + } catch (rawError) { + const error = locatedError(rawError, fieldNodes, pathToArray(path)); + return handleFieldError(error, returnType, exeContext); + } +} + +/** + * TODO: consider no longer exporting this function + * @internal + */ +export function buildResolveInfo( + exeContext: ExecutionContext, + fieldDef: GraphQLField, + fieldNodes: ReadonlyArray, + parentType: GraphQLObjectType, + path: Path +): GraphQLResolveInfo { + // The resolve function's optional fourth argument is a collection of + // information about the current execution state. + return { + fieldName: fieldDef.name, + fieldNodes, + returnType: fieldDef.type, + parentType, + path, + schema: exeContext.schema, + fragments: exeContext.fragments, + rootValue: exeContext.rootValue, + operation: exeContext.operation, + variableValues: exeContext.variableValues, + }; +} + +function handleFieldError(error: GraphQLError, returnType: GraphQLOutputType, exeContext: ExecutionContext): null { + // If the field type is non-nullable, then it is resolved without any + // protection from errors, however it still properly locates the error. + if (isNonNullType(returnType)) { + throw error; + } + + // Otherwise, error protection is applied, logging the error and resolving + // a null value for this field if one is encountered. + exeContext.errors.push(error); + return null; +} + +/** + * Implements the instructions for completeValue as defined in the + * "Value Completion" section of the spec. + * + * If the field type is Non-Null, then this recursively completes the value + * for the inner type. It throws a field error if that completion returns null, + * as per the "Nullability" section of the spec. + * + * If the field type is a List, then this recursively completes the value + * for the inner type on each item in the list. + * + * If the field type is a Scalar or Enum, ensures the completed value is a legal + * value of the type by calling the `serialize` method of GraphQL type + * definition. + * + * If the field is an abstract type, determine the runtime type of the value + * and then complete based on that type + * + * Otherwise, the field type expects a sub-selection set, and will complete the + * value by executing all sub-selections. + */ +function completeValue( + exeContext: ExecutionContext, + returnType: GraphQLOutputType, + fieldNodes: ReadonlyArray, + info: GraphQLResolveInfo, + path: Path, + result: unknown +): PromiseOrValue { + // If result is an Error, throw a located error. + if (result instanceof Error) { + throw result; + } + + // If field type is NonNull, complete for inner type, and throw field error + // if result is null. + if (isNonNullType(returnType)) { + const completed = completeValue(exeContext, returnType.ofType, fieldNodes, info, path, result); + if (completed === null) { + throw new Error(`Cannot return null for non-nullable field ${info.parentType.name}.${info.fieldName}.`); + } + return completed; + } + + // If result value is null or undefined then return null. + if (result == null) { + return null; + } + + // If field type is List, complete each item in the list with the inner type + if (isListType(returnType)) { + return completeListValue(exeContext, returnType, fieldNodes, info, path, result); + } + + // If field type is a leaf type, Scalar or Enum, serialize to a valid value, + // returning null if serialization is not possible. + if (isLeafType(returnType)) { + return completeLeafValue(returnType, result); + } + + // If field type is an abstract type, Interface or Union, determine the + // runtime Object type and complete for that type. + if (isAbstractType(returnType)) { + return completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result); + } + + // If field type is Object, execute and complete all sub-selections. + if (isObjectType(returnType)) { + return completeObjectValue(exeContext, returnType, fieldNodes, info, path, result); + } + /* c8 ignore next 6 */ + // Not reachable, all possible output types have been considered. + invariant(false, 'Cannot complete value of unexpected output type: ' + inspect(returnType)); +} + +/** + * Complete a async iterator value by completing the result and calling + * recursively until all the results are completed. + */ +async function completeAsyncIteratorValue( + exeContext: ExecutionContext, + itemType: GraphQLOutputType, + fieldNodes: ReadonlyArray, + info: GraphQLResolveInfo, + path: Path, + iterator: AsyncIterator +): Promise> { + let containsPromise = false; + const completedResults = []; + let index = 0; + + while (true) { + const fieldPath = addPath(path, index, undefined); + try { + const { value, done } = await iterator.next(); + if (done) { + break; + } + + try { + // TODO can the error checking logic be consolidated with completeListValue? + const completedItem = completeValue(exeContext, itemType, fieldNodes, info, fieldPath, value); + if (isPromise(completedItem)) { + containsPromise = true; + } + completedResults.push(completedItem); + } catch (rawError) { + completedResults.push(null); + const error = locatedError(rawError, fieldNodes, pathToArray(fieldPath)); + handleFieldError(error, itemType, exeContext); + } + } catch (rawError) { + completedResults.push(null); + const error = locatedError(rawError, fieldNodes, pathToArray(fieldPath)); + handleFieldError(error, itemType, exeContext); + break; + } + index += 1; + } + return containsPromise ? Promise.all(completedResults) : completedResults; +} + +/** + * Complete a list value by completing each item in the list with the + * inner type + */ +function completeListValue( + exeContext: ExecutionContext, + returnType: GraphQLList, + fieldNodes: ReadonlyArray, + info: GraphQLResolveInfo, + path: Path, + result: unknown +): PromiseOrValue> { + const itemType = returnType.ofType; + + if (isAsyncIterable(result)) { + const iterator = result[Symbol.asyncIterator](); + + return completeAsyncIteratorValue(exeContext, itemType, fieldNodes, info, path, iterator); + } + + if (!isIterableObject(result)) { + throw new GraphQLError( + `Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".` + ); + } + + // This is specified as a simple map, however we're optimizing the path + // where the list contains no Promises by avoiding creating another Promise. + let containsPromise = false; + const completedResults = Array.from(result, (item, index) => { + // No need to modify the info object containing the path, + // since from here on it is not ever accessed by resolver functions. + const itemPath = addPath(path, index, undefined); + try { + let completedItem; + if (isPromise(item)) { + completedItem = item.then(resolved => + completeValue(exeContext, itemType, fieldNodes, info, itemPath, resolved) + ); + } else { + completedItem = completeValue(exeContext, itemType, fieldNodes, info, itemPath, item); + } + + if (isPromise(completedItem)) { + containsPromise = true; + // Note: we don't rely on a `catch` method, but we do expect "thenable" + // to take a second callback for the error case. + return completedItem.then(undefined, rawError => { + const error = locatedError(rawError, fieldNodes, pathToArray(itemPath)); + return handleFieldError(error, itemType, exeContext); + }); + } + return completedItem; + } catch (rawError) { + const error = locatedError(rawError, fieldNodes, pathToArray(itemPath)); + return handleFieldError(error, itemType, exeContext); + } + }); + + return containsPromise ? Promise.all(completedResults) : completedResults; +} + +/** + * Complete a Scalar or Enum by serializing to a valid value, returning + * null if serialization is not possible. + */ +function completeLeafValue(returnType: GraphQLLeafType, result: unknown): unknown { + const serializedResult = returnType.serialize(result); + if (serializedResult == null) { + throw new Error( + `Expected \`${inspect(returnType)}.serialize(${inspect(result)})\` to ` + + `return non-nullable value, returned: ${inspect(serializedResult)}` + ); + } + return serializedResult; +} + +/** + * Complete a value of an abstract type by determining the runtime object type + * of that value, then complete the value for that type. + */ +function completeAbstractValue( + exeContext: ExecutionContext, + returnType: GraphQLAbstractType, + fieldNodes: ReadonlyArray, + info: GraphQLResolveInfo, + path: Path, + result: unknown +): PromiseOrValue> { + const resolveTypeFn = returnType.resolveType ?? exeContext.typeResolver; + const contextValue = exeContext.contextValue; + const runtimeType = resolveTypeFn(result, contextValue, info, returnType); + + if (isPromise(runtimeType)) { + return runtimeType.then(resolvedRuntimeType => + completeObjectValue( + exeContext, + ensureValidRuntimeType(resolvedRuntimeType, exeContext, returnType, fieldNodes, info, result), + fieldNodes, + info, + path, + result + ) + ); + } + + return completeObjectValue( + exeContext, + ensureValidRuntimeType(runtimeType, exeContext, returnType, fieldNodes, info, result), + fieldNodes, + info, + path, + result + ); +} + +function ensureValidRuntimeType( + runtimeTypeName: unknown, + exeContext: ExecutionContext, + returnType: GraphQLAbstractType, + fieldNodes: ReadonlyArray, + info: GraphQLResolveInfo, + result: unknown +): GraphQLObjectType { + if (runtimeTypeName == null) { + throw new GraphQLError( + `Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}". Either the "${returnType.name}" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.`, + { nodes: fieldNodes } + ); + } + + // releases before 16.0.0 supported returning `GraphQLObjectType` from `resolveType` + // TODO: remove in 17.0.0 release + if (isObjectType(runtimeTypeName)) { + throw new GraphQLError( + 'Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.' + ); + } + + if (typeof runtimeTypeName !== 'string') { + throw new GraphQLError( + `Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}" with ` + + `value ${inspect(result)}, received "${inspect(runtimeTypeName)}".` + ); + } + + const runtimeType = exeContext.schema.getType(runtimeTypeName); + if (runtimeType == null) { + throw new GraphQLError( + `Abstract type "${returnType.name}" was resolved to a type "${runtimeTypeName}" that does not exist inside the schema.`, + { nodes: fieldNodes } + ); + } + + if (!isObjectType(runtimeType)) { + throw new GraphQLError( + `Abstract type "${returnType.name}" was resolved to a non-object type "${runtimeTypeName}".`, + { nodes: fieldNodes } + ); + } + + if (!exeContext.schema.isSubType(returnType, runtimeType)) { + throw new GraphQLError( + `Runtime Object type "${runtimeType.name}" is not a possible type for "${returnType.name}".`, + { nodes: fieldNodes } + ); + } + + return runtimeType; +} + +/** + * Complete an Object value by executing all sub-selections. + */ +function completeObjectValue( + exeContext: ExecutionContext, + returnType: GraphQLObjectType, + fieldNodes: ReadonlyArray, + info: GraphQLResolveInfo, + path: Path, + result: unknown +): PromiseOrValue> { + // Collect sub-fields to execute to complete this value. + const subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes); + + // If there is an isTypeOf predicate function, call it with the + // current result. If isTypeOf returns false, then raise an error rather + // than continuing execution. + if (returnType.isTypeOf) { + const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info); + + if (isPromise(isTypeOf)) { + return isTypeOf.then(resolvedIsTypeOf => { + if (!resolvedIsTypeOf) { + throw invalidReturnTypeError(returnType, result, fieldNodes); + } + return executeFields(exeContext, returnType, result, path, subFieldNodes); + }); + } + + if (!isTypeOf) { + throw invalidReturnTypeError(returnType, result, fieldNodes); + } + } + + return executeFields(exeContext, returnType, result, path, subFieldNodes); +} + +function invalidReturnTypeError( + returnType: GraphQLObjectType, + result: unknown, + fieldNodes: ReadonlyArray +): GraphQLError { + return new GraphQLError(`Expected value of type "${returnType.name}" but got: ${inspect(result)}.`, { + nodes: fieldNodes, + }); +} + +/** + * If a resolveType function is not given, then a default resolve behavior is + * used which attempts two strategies: + * + * First, See if the provided value has a `__typename` field defined, if so, use + * that value as name of the resolved type. + * + * Otherwise, test each possible type for the abstract type by calling + * isTypeOf for the object being coerced, returning the first type that matches. + */ +export const defaultTypeResolver: GraphQLTypeResolver = function ( + value, + contextValue, + info, + abstractType +) { + // First, look for `__typename`. + if (isObjectLike(value) && typeof value['__typename'] === 'string') { + return value['__typename']; + } + + // Otherwise, test each possible type. + const possibleTypes = info.schema.getPossibleTypes(abstractType); + const promisedIsTypeOfResults = []; + + for (let i = 0; i < possibleTypes.length; i++) { + const type = possibleTypes[i]; + + if (type.isTypeOf) { + const isTypeOfResult = type.isTypeOf(value, contextValue, info); + + if (isPromise(isTypeOfResult)) { + promisedIsTypeOfResults[i] = isTypeOfResult; + } else if (isTypeOfResult) { + return type.name; + } + } + } + + if (promisedIsTypeOfResults.length) { + return Promise.all(promisedIsTypeOfResults).then(isTypeOfResults => { + for (let i = 0; i < isTypeOfResults.length; i++) { + if (isTypeOfResults[i]) { + return possibleTypes[i].name; + } + } + }); + } +}; + +/** + * If a resolve function is not given, then a default resolve behavior is used + * which takes the property of the source object of the same name as the field + * and returns it as the result, or if it's a function, returns the result + * of calling that function while passing along args and context value. + */ +export const defaultFieldResolver: GraphQLFieldResolver = function ( + source: any, + args, + contextValue, + info +) { + // ensure source is a value for which property access is acceptable. + if (isObjectLike(source) || typeof source === 'function') { + const property = source[info.fieldName]; + if (typeof property === 'function') { + return source[info.fieldName](args, contextValue, info); + } + return property; + } +}; + +/** + * Implements the "Subscribe" algorithm described in the GraphQL specification. + * + * Returns a Promise which resolves to either an AsyncIterator (if successful) + * or an ExecutionResult (error). The promise will be rejected if the schema or + * other arguments to this function are invalid, or if the resolved event stream + * is not an async iterable. + * + * If the client-provided arguments to this function do not result in a + * compliant subscription, a GraphQL Response (ExecutionResult) with + * descriptive errors and no data will be returned. + * + * If the source stream could not be created due to faulty subscription + * resolver logic or underlying systems, the promise will resolve to a single + * ExecutionResult containing `errors` and no `data`. + * + * If the operation succeeded, the promise resolves to an AsyncIterator, which + * yields a stream of ExecutionResults representing the response stream. + * + * Accepts either an object with named arguments, or individual arguments. + */ +export function subscribe( + args: ExecutionArgs +): PromiseOrValue | ExecutionResult> { + // If a valid execution context cannot be created due to incorrect arguments, + // a "Response" with only errors is returned. + const exeContext = buildExecutionContext(args); + + // Return early errors if execution context failed. + if (!('schema' in exeContext)) { + return { errors: exeContext }; + } + + const resultOrStream = createSourceEventStreamImpl(exeContext); + + if (isPromise(resultOrStream)) { + return resultOrStream.then(resolvedResultOrStream => mapSourceToResponse(exeContext, resolvedResultOrStream)); + } + + return mapSourceToResponse(exeContext, resultOrStream); +} + +function mapSourceToResponse( + exeContext: ExecutionContext, + resultOrStream: ExecutionResult | AsyncIterable +): PromiseOrValue | ExecutionResult> { + if (!isAsyncIterable(resultOrStream)) { + return resultOrStream; + } + + // For each payload yielded from a subscription, map it over the normal + // GraphQL `execute` function, with `payload` as the rootValue. + // This implements the "MapSourceToResponseEvent" algorithm described in + // the GraphQL specification. The `execute` function provides the + // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the + // "ExecuteQuery" algorithm, for which `execute` is also used. + return mapAsyncIterator(resultOrStream, (payload: unknown) => + executeImpl(buildPerEventExecutionContext(exeContext, payload)) + ); +} + +/** + * Implements the "CreateSourceEventStream" algorithm described in the + * GraphQL specification, resolving the subscription source event stream. + * + * Returns a Promise which resolves to either an AsyncIterable (if successful) + * or an ExecutionResult (error). The promise will be rejected if the schema or + * other arguments to this function are invalid, or if the resolved event stream + * is not an async iterable. + * + * If the client-provided arguments to this function do not result in a + * compliant subscription, a GraphQL Response (ExecutionResult) with + * descriptive errors and no data will be returned. + * + * If the the source stream could not be created due to faulty subscription + * resolver logic or underlying systems, the promise will resolve to a single + * ExecutionResult containing `errors` and no `data`. + * + * If the operation succeeded, the promise resolves to the AsyncIterable for the + * event stream returned by the resolver. + * + * A Source Event Stream represents a sequence of events, each of which triggers + * a GraphQL execution for that event. + * + * This may be useful when hosting the stateful subscription service in a + * different process or machine than the stateless GraphQL execution engine, + * or otherwise separating these two steps. For more on this, see the + * "Supporting Subscriptions at Scale" information in the GraphQL specification. + */ +export function createSourceEventStream(args: ExecutionArgs): PromiseOrValue | ExecutionResult> { + // If a valid execution context cannot be created due to incorrect arguments, + // a "Response" with only errors is returned. + const exeContext = buildExecutionContext(args); + + // Return early errors if execution context failed. + if (!('schema' in exeContext)) { + return { errors: exeContext }; + } + + return createSourceEventStreamImpl(exeContext); +} + +function createSourceEventStreamImpl( + exeContext: ExecutionContext +): PromiseOrValue | ExecutionResult> { + try { + const eventStream = executeSubscription(exeContext); + if (isPromise(eventStream)) { + return eventStream.then(undefined, error => ({ errors: [error] })); + } + + return eventStream; + } catch (error) { + return { errors: [error as GraphQLError] }; + } +} + +function executeSubscription(exeContext: ExecutionContext): PromiseOrValue> { + const { schema, fragments, operation, variableValues, rootValue } = exeContext; + + const rootType = schema.getSubscriptionType(); + if (rootType == null) { + throw new GraphQLError('Schema is not configured to execute subscription operation.', { nodes: operation }); + } + + const rootFields = collectFields(schema, fragments, variableValues, rootType, operation.selectionSet); + const [responseName, fieldNodes] = [...rootFields.entries()][0]; + const fieldName = fieldNodes[0].name.value; + const fieldDef = schema.getField(rootType, fieldName); + + if (!fieldDef) { + throw new GraphQLError(`The subscription field "${fieldName}" is not defined.`, { nodes: fieldNodes }); + } + + const path = addPath(undefined, responseName, rootType.name); + const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, rootType, path); + + try { + // Implements the "ResolveFieldEventStream" algorithm from GraphQL specification. + // It differs from "ResolveFieldValue" due to providing a different `resolveFn`. + + // Build a JS object of arguments from the field.arguments AST, using the + // variables scope to fulfill any variable references. + const args = getArgumentValues(fieldDef, fieldNodes[0], variableValues); + + // The resolve function's optional third argument is a context value that + // is provided to every resolve function within an execution. It is commonly + // used to represent an authenticated user, or request-specific caches. + const contextValue = exeContext.contextValue; + + // Call the `subscribe()` resolver or the default resolver to produce an + // AsyncIterable yielding raw payloads. + const resolveFn = fieldDef.subscribe ?? exeContext.subscribeFieldResolver; + const result = resolveFn(rootValue, args, contextValue, info); + + if (isPromise(result)) { + return result.then(assertEventStream).then(undefined, error => { + throw locatedError(error, fieldNodes, pathToArray(path)); + }); + } + + return assertEventStream(result); + } catch (error) { + throw locatedError(error, fieldNodes, pathToArray(path)); + } +} + +function assertEventStream(result: unknown): AsyncIterable { + if (result instanceof Error) { + throw result; + } + + // Assert field returned an event stream, otherwise yield an error. + if (!isAsyncIterable(result)) { + throw new GraphQLError('Subscription field must return Async Iterable. ' + `Received: ${inspect(result)}.`); + } + + return result; +} + +/** + * This method looks up the field on the given type definition. + * It has special casing for the three introspection fields, + * __schema, __type and __typename. __typename is special because + * it can always be queried as a field, even in situations where no + * other fields are allowed, like on a Union. __schema and __type + * could get automatically added to the query type, but that would + * require mutating type definitions, which would cause issues. + * + * @internal + */ +export function getFieldDef( + schema: GraphQLSchema, + parentType: GraphQLObjectType, + fieldNode: FieldNode +): Maybe> { + const fieldName = fieldNode.name.value; + + if (fieldName === SchemaMetaFieldDef.name && schema.getQueryType() === parentType) { + return SchemaMetaFieldDef; + } else if (fieldName === TypeMetaFieldDef.name && schema.getQueryType() === parentType) { + return TypeMetaFieldDef; + } else if (fieldName === TypeNameMetaFieldDef.name) { + return TypeNameMetaFieldDef; + } + return parentType.getFields()[fieldName]; +} diff --git a/packages/graphql/src/execution/index.ts b/packages/graphql/src/execution/index.ts new file mode 100644 index 00000000000..7ea8ee1033c --- /dev/null +++ b/packages/graphql/src/execution/index.ts @@ -0,0 +1,5 @@ +export * from './collectFields.js'; +export * from './execute.js'; +export * from './mapAsyncIterator.js'; +export * from './values.js'; +export { pathToArray as responsePathAsArray } from '../jsutils/Path.js'; diff --git a/packages/graphql/src/execution/mapAsyncIterator.ts b/packages/graphql/src/execution/mapAsyncIterator.ts new file mode 100644 index 00000000000..7e58a34bd88 --- /dev/null +++ b/packages/graphql/src/execution/mapAsyncIterator.ts @@ -0,0 +1,55 @@ +import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js'; + +/** + * Given an AsyncIterable and a callback function, return an AsyncIterator + * which produces values mapped via calling the callback function. + */ +export function mapAsyncIterator( + iterable: AsyncGenerator | AsyncIterable, + callback: (value: T) => PromiseOrValue +): AsyncGenerator { + const iterator = iterable[Symbol.asyncIterator](); + + async function mapResult(result: IteratorResult): Promise> { + if (result.done) { + return result; + } + + try { + return { value: await callback(result.value), done: false }; + } catch (error) { + /* c8 ignore start */ + // FIXME: add test case + if (typeof iterator.return === 'function') { + try { + await iterator.return(); + } catch (_e) { + /* ignore error */ + } + } + throw error; + /* c8 ignore stop */ + } + } + + return { + async next() { + return mapResult(await iterator.next()); + }, + async return(): Promise> { + // If iterator.return() does not exist, then type R must be undefined. + return typeof iterator.return === 'function' + ? mapResult(await iterator.return()) + : { value: undefined as any, done: true }; + }, + async throw(error?: unknown) { + if (typeof iterator.throw === 'function') { + return mapResult(await iterator.throw(error)); + } + throw error; + }, + [Symbol.asyncIterator]() { + return this; + }, + }; +} diff --git a/packages/graphql/src/execution/subscribe.ts b/packages/graphql/src/execution/subscribe.ts new file mode 100644 index 00000000000..f92d2afea7b --- /dev/null +++ b/packages/graphql/src/execution/subscribe.ts @@ -0,0 +1,225 @@ +import { devAssert } from '../jsutils/devAssert.js'; +import { inspect } from '../jsutils/inspect.js'; +import { isAsyncIterable } from '../jsutils/isAsyncIterable.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import { addPath, pathToArray } from '../jsutils/Path.js'; + +import { GraphQLError } from '../error/GraphQLError.js'; +import { locatedError } from '../error/locatedError.js'; + +import type { DocumentNode } from '../language/ast.js'; + +import type { GraphQLFieldResolver } from '../type/definition.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +import { collectFields } from './collectFields.js'; +import type { ExecutionArgs, ExecutionContext, ExecutionResult } from './execute.js'; +import { + assertValidExecutionArguments, + buildExecutionContext, + buildResolveInfo, + execute, + getFieldDef, +} from './execute.js'; +import { mapAsyncIterator } from './mapAsyncIterator.js'; +import { getArgumentValues } from './values.js'; + +/** + * Implements the "Subscribe" algorithm described in the GraphQL specification. + * + * Returns a Promise which resolves to either an AsyncIterator (if successful) + * or an ExecutionResult (error). The promise will be rejected if the schema or + * other arguments to this function are invalid, or if the resolved event stream + * is not an async iterable. + * + * If the client-provided arguments to this function do not result in a + * compliant subscription, a GraphQL Response (ExecutionResult) with + * descriptive errors and no data will be returned. + * + * If the source stream could not be created due to faulty subscription + * resolver logic or underlying systems, the promise will resolve to a single + * ExecutionResult containing `errors` and no `data`. + * + * If the operation succeeded, the promise resolves to an AsyncIterator, which + * yields a stream of ExecutionResults representing the response stream. + * + * Accepts either an object with named arguments, or individual arguments. + */ +export async function subscribe( + args: ExecutionArgs +): Promise | ExecutionResult> { + // Temporary for v15 to v16 migration. Remove in v17 + devAssert( + arguments.length < 2, + 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.' + ); + + const { + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + subscribeFieldResolver, + } = args; + + const resultOrStream = await createSourceEventStream( + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + subscribeFieldResolver + ); + + if (!isAsyncIterable(resultOrStream)) { + return resultOrStream; + } + + // For each payload yielded from a subscription, map it over the normal + // GraphQL `execute` function, with `payload` as the rootValue. + // This implements the "MapSourceToResponseEvent" algorithm described in + // the GraphQL specification. The `execute` function provides the + // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the + // "ExecuteQuery" algorithm, for which `execute` is also used. + const mapSourceToResponse = (payload: unknown) => + execute({ + schema, + document, + rootValue: payload, + contextValue, + variableValues, + operationName, + fieldResolver, + }); + + // Map every source value to a ExecutionResult value as described above. + return mapAsyncIterator(resultOrStream, mapSourceToResponse); +} + +/** + * Implements the "CreateSourceEventStream" algorithm described in the + * GraphQL specification, resolving the subscription source event stream. + * + * Returns a Promise which resolves to either an AsyncIterable (if successful) + * or an ExecutionResult (error). The promise will be rejected if the schema or + * other arguments to this function are invalid, or if the resolved event stream + * is not an async iterable. + * + * If the client-provided arguments to this function do not result in a + * compliant subscription, a GraphQL Response (ExecutionResult) with + * descriptive errors and no data will be returned. + * + * If the the source stream could not be created due to faulty subscription + * resolver logic or underlying systems, the promise will resolve to a single + * ExecutionResult containing `errors` and no `data`. + * + * If the operation succeeded, the promise resolves to the AsyncIterable for the + * event stream returned by the resolver. + * + * A Source Event Stream represents a sequence of events, each of which triggers + * a GraphQL execution for that event. + * + * This may be useful when hosting the stateful subscription service in a + * different process or machine than the stateless GraphQL execution engine, + * or otherwise separating these two steps. For more on this, see the + * "Supporting Subscriptions at Scale" information in the GraphQL specification. + */ +export async function createSourceEventStream( + schema: GraphQLSchema, + document: DocumentNode, + rootValue?: unknown, + contextValue?: unknown, + variableValues?: Maybe<{ readonly [variable: string]: unknown }>, + operationName?: Maybe, + subscribeFieldResolver?: Maybe> +): Promise | ExecutionResult> { + // If arguments are missing or incorrectly typed, this is an internal + // developer mistake which should throw an early error. + assertValidExecutionArguments(schema, document, variableValues); + + // If a valid execution context cannot be created due to incorrect arguments, + // a "Response" with only errors is returned. + const exeContext = buildExecutionContext({ + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + subscribeFieldResolver, + }); + + // Return early errors if execution context failed. + if (!('schema' in exeContext)) { + return { errors: exeContext }; + } + + try { + const eventStream = await executeSubscription(exeContext); + + // Assert field returned an event stream, otherwise yield an error. + if (!isAsyncIterable(eventStream)) { + throw new Error('Subscription field must return Async Iterable. ' + `Received: ${inspect(eventStream)}.`); + } + + return eventStream; + } catch (error) { + // If it GraphQLError, report it as an ExecutionResult, containing only errors and no data. + // Otherwise treat the error as a system-class error and re-throw it. + if (error instanceof GraphQLError) { + return { errors: [error] }; + } + throw error; + } +} + +async function executeSubscription(exeContext: ExecutionContext): Promise { + const { schema, fragments, operation, variableValues, rootValue } = exeContext; + + const rootType = schema.getSubscriptionType(); + if (rootType == null) { + throw new GraphQLError('Schema is not configured to execute subscription operation.', { nodes: operation }); + } + + const rootFields = collectFields(schema, fragments, variableValues, rootType, operation.selectionSet); + const [responseName, fieldNodes] = [...rootFields.entries()][0]; + const fieldDef = getFieldDef(schema, rootType, fieldNodes[0]); + + if (!fieldDef) { + const fieldName = fieldNodes[0].name.value; + throw new GraphQLError(`The subscription field "${fieldName}" is not defined.`, { nodes: fieldNodes }); + } + + const path = addPath(undefined, responseName, rootType.name); + const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, rootType, path); + + try { + // Implements the "ResolveFieldEventStream" algorithm from GraphQL specification. + // It differs from "ResolveFieldValue" due to providing a different `resolveFn`. + + // Build a JS object of arguments from the field.arguments AST, using the + // variables scope to fulfill any variable references. + const args = getArgumentValues(fieldDef, fieldNodes[0], variableValues); + + // The resolve function's optional third argument is a context value that + // is provided to every resolve function within an execution. It is commonly + // used to represent an authenticated user, or request-specific caches. + const contextValue = exeContext.contextValue; + + // Call the `subscribe()` resolver or the default resolver to produce an + // AsyncIterable yielding raw payloads. + const resolveFn = fieldDef.subscribe ?? exeContext.subscribeFieldResolver; + const eventStream = await resolveFn(rootValue, args, contextValue, info); + + if (eventStream instanceof Error) { + throw eventStream; + } + return eventStream; + } catch (error) { + throw locatedError(error, fieldNodes, pathToArray(path)); + } +} diff --git a/packages/graphql/src/execution/values.ts b/packages/graphql/src/execution/values.ts new file mode 100644 index 00000000000..28088cb75a0 --- /dev/null +++ b/packages/graphql/src/execution/values.ts @@ -0,0 +1,227 @@ +import { inspect } from '../jsutils/inspect.js'; +import { keyMap } from '../jsutils/keyMap.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; +import { printPathArray } from '../jsutils/printPathArray.js'; + +import { GraphQLError } from '../error/GraphQLError.js'; + +import type { DirectiveNode, FieldNode, VariableDefinitionNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; +import { print } from '../language/printer.js'; + +import type { GraphQLField } from '../type/definition.js'; +import { isInputType, isNonNullType } from '../type/definition.js'; +import type { GraphQLDirective } from '../type/directives.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +import { coerceInputValue } from '../utilities/coerceInputValue.js'; +import { typeFromAST } from '../utilities/typeFromAST.js'; +import { valueFromAST } from '../utilities/valueFromAST.js'; + +type CoercedVariableValues = + | { errors: ReadonlyArray; coerced?: never } + | { coerced: { [variable: string]: unknown }; errors?: never }; + +/** + * Prepares an object map of variableValues of the correct type based on the + * provided variable definitions and arbitrary input. If the input cannot be + * parsed to match the variable definitions, a GraphQLError will be thrown. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ +export function getVariableValues( + schema: GraphQLSchema, + varDefNodes: ReadonlyArray, + inputs: { readonly [variable: string]: unknown }, + options?: { maxErrors?: number } +): CoercedVariableValues { + const errors = []; + const maxErrors = options?.maxErrors; + try { + const coerced = coerceVariableValues(schema, varDefNodes, inputs, error => { + if (maxErrors != null && errors.length >= maxErrors) { + throw new GraphQLError('Too many errors processing variables, error limit reached. Execution aborted.'); + } + errors.push(error); + }); + + if (errors.length === 0) { + return { coerced }; + } + } catch (error) { + errors.push(error); + } + + // @ts-expect-error + return { errors }; +} + +function coerceVariableValues( + schema: GraphQLSchema, + varDefNodes: ReadonlyArray, + inputs: { readonly [variable: string]: unknown }, + onError: (error: GraphQLError) => void +): { [variable: string]: unknown } { + const coercedValues: { [variable: string]: unknown } = {}; + for (const varDefNode of varDefNodes) { + const varName = varDefNode.variable.name.value; + const varType = typeFromAST(schema, varDefNode.type); + if (!isInputType(varType)) { + // Must use input types for variables. This should be caught during + // validation, however is checked again here for safety. + const varTypeStr = print(varDefNode.type); + onError( + new GraphQLError( + `Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`, + { nodes: varDefNode.type } + ) + ); + continue; + } + + if (!hasOwnProperty(inputs, varName)) { + if (varDefNode.defaultValue) { + coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType); + } else if (isNonNullType(varType)) { + const varTypeStr = inspect(varType); + onError( + new GraphQLError(`Variable "$${varName}" of required type "${varTypeStr}" was not provided.`, { + nodes: varDefNode, + }) + ); + } + continue; + } + + const value = inputs[varName]; + if (value === null && isNonNullType(varType)) { + const varTypeStr = inspect(varType); + onError( + new GraphQLError(`Variable "$${varName}" of non-null type "${varTypeStr}" must not be null.`, { + nodes: varDefNode, + }) + ); + continue; + } + + coercedValues[varName] = coerceInputValue(value, varType, (path, invalidValue, error) => { + let prefix = `Variable "$${varName}" got invalid value ` + inspect(invalidValue); + if (path.length > 0) { + prefix += ` at "${varName}${printPathArray(path)}"`; + } + onError( + new GraphQLError(prefix + '; ' + error.message, { + nodes: varDefNode, + originalError: error.originalError, + }) + ); + }); + } + + return coercedValues; +} + +/** + * Prepares an object map of argument values given a list of argument + * definitions and list of argument AST nodes. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ +export function getArgumentValues( + def: GraphQLField | GraphQLDirective, + node: FieldNode | DirectiveNode, + variableValues?: Maybe> +): { [argument: string]: unknown } { + const coercedValues: { [argument: string]: unknown } = {}; + + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const argumentNodes = node.arguments ?? []; + const argNodeMap = keyMap(argumentNodes, arg => arg.name.value); + + for (const argDef of def.args) { + const name = argDef.name; + const argType = argDef.type; + const argumentNode = argNodeMap[name]; + + if (!argumentNode) { + if (argDef.defaultValue !== undefined) { + coercedValues[name] = argDef.defaultValue; + } else if (isNonNullType(argType)) { + throw new GraphQLError(`Argument "${name}" of required type "${inspect(argType)}" ` + 'was not provided.', { + nodes: node, + }); + } + continue; + } + + const valueNode = argumentNode.value; + let isNull = valueNode.kind === Kind.NULL; + + if (valueNode.kind === Kind.VARIABLE) { + const variableName = valueNode.name.value; + if (variableValues == null || !hasOwnProperty(variableValues, variableName)) { + if (argDef.defaultValue !== undefined) { + coercedValues[name] = argDef.defaultValue; + } else if (isNonNullType(argType)) { + throw new GraphQLError( + `Argument "${name}" of required type "${inspect(argType)}" ` + + `was provided the variable "$${variableName}" which was not provided a runtime value.`, + { nodes: valueNode } + ); + } + continue; + } + isNull = variableValues[variableName] == null; + } + + if (isNull && isNonNullType(argType)) { + throw new GraphQLError(`Argument "${name}" of non-null type "${inspect(argType)}" ` + 'must not be null.', { + nodes: valueNode, + }); + } + + const coercedValue = valueFromAST(valueNode, argType, variableValues); + if (coercedValue === undefined) { + // Note: ValuesOfCorrectTypeRule validation should catch this before + // execution. This is a runtime check to ensure execution does not + // continue with an invalid argument value. + throw new GraphQLError(`Argument "${name}" has invalid value ${print(valueNode)}.`, { nodes: valueNode }); + } + coercedValues[name] = coercedValue; + } + return coercedValues; +} + +/** + * Prepares an object map of argument values given a directive definition + * and a AST node which may contain directives. Optionally also accepts a map + * of variable values. + * + * If the directive does not exist on the node, returns undefined. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ +export function getDirectiveValues( + directiveDef: GraphQLDirective, + node: { readonly directives?: ReadonlyArray }, + variableValues?: Maybe> +): undefined | { [argument: string]: unknown } { + const directiveNode = node.directives?.find(directive => directive.name.value === directiveDef.name); + + if (directiveNode) { + return getArgumentValues(directiveDef, directiveNode, variableValues); + } + return undefined; +} + +function hasOwnProperty(obj: unknown, prop: string): boolean { + return Object.prototype.hasOwnProperty.call(obj, prop); +} diff --git a/packages/graphql/src/graphql.ts b/packages/graphql/src/graphql.ts new file mode 100644 index 00000000000..b7adb1b44f9 --- /dev/null +++ b/packages/graphql/src/graphql.ts @@ -0,0 +1,124 @@ +import { isPromise } from './jsutils/isPromise.js'; +import type { Maybe } from './jsutils/Maybe.js'; +import type { PromiseOrValue } from './jsutils/PromiseOrValue.js'; + +import { parse } from './language/parser.js'; +import type { Source } from './language/source.js'; + +import type { GraphQLFieldResolver, GraphQLTypeResolver } from './type/definition.js'; +import type { GraphQLSchema } from './type/schema.js'; +import { validateSchema } from './type/validate.js'; + +import { validate } from './validation/validate.js'; + +import type { ExecutionResult } from './execution/execute.js'; +import { execute } from './execution/execute.js'; +import { GraphQLError } from './error/index.js'; + +/** + * This is the primary entry point function for fulfilling GraphQL operations + * by parsing, validating, and executing a GraphQL document along side a + * GraphQL schema. + * + * More sophisticated GraphQL servers, such as those which persist queries, + * may wish to separate the validation and execution phases to a static time + * tooling step, and a server runtime step. + * + * Accepts either an object with named arguments, or individual arguments: + * + * schema: + * The GraphQL type system to use when validating and executing a query. + * source: + * A GraphQL language formatted string representing the requested operation. + * rootValue: + * The value provided as the first argument to resolver functions on the top + * level type (e.g. the query object type). + * contextValue: + * The context value is provided as an argument to resolver functions after + * field arguments. It is used to pass shared information useful at any point + * during executing this query, for example the currently logged in user and + * connections to databases or other services. + * variableValues: + * A mapping of variable name to runtime value to use for all variables + * defined in the requestString. + * operationName: + * The name of the operation to use if requestString contains multiple + * possible operations. Can be omitted if requestString contains only + * one operation. + * fieldResolver: + * A resolver function to use when one is not provided by the schema. + * If not provided, the default field resolver is used (which looks for a + * value or method on the source value with the field's name). + * typeResolver: + * A type resolver function to use when none is provided by the schema. + * If not provided, the default type resolver is used (which looks for a + * `__typename` field or alternatively calls the `isTypeOf` method). + */ +export interface GraphQLArgs { + schema: GraphQLSchema; + source: string | Source; + rootValue?: unknown; + contextValue?: unknown; + variableValues?: Maybe<{ readonly [variable: string]: unknown }>; + operationName?: Maybe; + fieldResolver?: Maybe>; + typeResolver?: Maybe>; +} + +export function graphql(args: GraphQLArgs): Promise { + // Always return a Promise for a consistent API. + return new Promise(resolve => resolve(graphqlImpl(args))); +} + +/** + * The graphqlSync function also fulfills GraphQL operations by parsing, + * validating, and executing a GraphQL document along side a GraphQL schema. + * However, it guarantees to complete synchronously (or throw an error) assuming + * that all field resolvers are also synchronous. + */ +export function graphqlSync(args: GraphQLArgs): ExecutionResult { + const result = graphqlImpl(args); + + // Assert that the execution was synchronous. + if (isPromise(result)) { + throw new Error('GraphQL execution failed to complete synchronously.'); + } + + return result; +} + +function graphqlImpl(args: GraphQLArgs): PromiseOrValue { + const { schema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver } = args; + + // Validate Schema + const schemaValidationErrors = validateSchema(schema); + if (schemaValidationErrors.length > 0) { + return { errors: schemaValidationErrors }; + } + + // Parse + let document; + try { + document = parse(source); + } catch (syntaxError) { + return { errors: [syntaxError as GraphQLError] }; + } + + // Validate + const validationErrors = validate(schema, document); + if (validationErrors.length > 0) { + return { errors: validationErrors }; + } + + // Execute + return execute({ + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + typeResolver, + }); +} diff --git a/packages/graphql/src/index.ts b/packages/graphql/src/index.ts new file mode 100644 index 00000000000..de3dcc26276 --- /dev/null +++ b/packages/graphql/src/index.ts @@ -0,0 +1,9 @@ +export * from './error/index.js'; +export * from './execution/index.js'; +export * from './graphql.js'; +export * from './language/index.js'; +export * from './type/index.js'; +export * from './utilities/index.js'; +export * from './validation/index.js'; +export * from './version.js'; +export * from './subscription/index.js'; diff --git a/packages/graphql/src/jsutils/AccumulatorMap.ts b/packages/graphql/src/jsutils/AccumulatorMap.ts new file mode 100644 index 00000000000..156fe71c207 --- /dev/null +++ b/packages/graphql/src/jsutils/AccumulatorMap.ts @@ -0,0 +1,17 @@ +/** + * ES6 Map with additional `add` method to accumulate items. + */ +export class AccumulatorMap extends Map> { + get [Symbol.toStringTag]() { + return 'AccumulatorMap'; + } + + add(key: K, item: T): void { + const group = this.get(key); + if (group === undefined) { + this.set(key, [item]); + } else { + group.push(item); + } + } +} diff --git a/packages/graphql/src/jsutils/Maybe.ts b/packages/graphql/src/jsutils/Maybe.ts new file mode 100644 index 00000000000..0ba64a4b645 --- /dev/null +++ b/packages/graphql/src/jsutils/Maybe.ts @@ -0,0 +1,2 @@ +/** Conveniently represents flow's "Maybe" type https://flow.org/en/docs/types/maybe/ */ +export type Maybe = null | undefined | T; diff --git a/packages/graphql/src/jsutils/ObjMap.ts b/packages/graphql/src/jsutils/ObjMap.ts new file mode 100644 index 00000000000..de8e3021349 --- /dev/null +++ b/packages/graphql/src/jsutils/ObjMap.ts @@ -0,0 +1,11 @@ +export interface ObjMap { + [key: string]: T; +} + +export type ObjMapLike = ObjMap | { [key: string]: T }; + +export interface ReadOnlyObjMap { + readonly [key: string]: T; +} + +export type ReadOnlyObjMapLike = ReadOnlyObjMap | { readonly [key: string]: T }; diff --git a/packages/graphql/src/jsutils/Path.ts b/packages/graphql/src/jsutils/Path.ts new file mode 100644 index 00000000000..869879bb534 --- /dev/null +++ b/packages/graphql/src/jsutils/Path.ts @@ -0,0 +1,27 @@ +import type { Maybe } from './Maybe.js'; + +export interface Path { + readonly prev: Path | undefined; + readonly key: string | number; + readonly typename: string | undefined; +} + +/** + * Given a Path and a key, return a new Path containing the new key. + */ +export function addPath(prev: Readonly | undefined, key: string | number, typename: string | undefined): Path { + return { prev, key, typename }; +} + +/** + * Given a Path, return an Array of the path keys. + */ +export function pathToArray(path: Maybe>): Array { + const flattened = []; + let curr = path; + while (curr) { + flattened.push(curr.key); + curr = curr.prev; + } + return flattened.reverse(); +} diff --git a/packages/graphql/src/jsutils/PromiseOrValue.ts b/packages/graphql/src/jsutils/PromiseOrValue.ts new file mode 100644 index 00000000000..6b2517ee625 --- /dev/null +++ b/packages/graphql/src/jsutils/PromiseOrValue.ts @@ -0,0 +1 @@ +export type PromiseOrValue = Promise | T; diff --git a/packages/graphql/src/jsutils/__tests__/AccumulatorMap-test.ts b/packages/graphql/src/jsutils/__tests__/AccumulatorMap-test.ts new file mode 100644 index 00000000000..5b20503ea2d --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/AccumulatorMap-test.ts @@ -0,0 +1,31 @@ +import { AccumulatorMap } from '../AccumulatorMap.js'; + +function expectMap(map: Map) { + return expect(Object.fromEntries(map.entries())); +} + +describe('AccumulatorMap', () => { + it('can be Object.toStringified', () => { + const accumulatorMap = new AccumulatorMap(); + + expect(Object.prototype.toString.call(accumulatorMap)).toEqual('[object AccumulatorMap]'); + }); + + it('accumulate items', () => { + const accumulatorMap = new AccumulatorMap(); + + expectMap(accumulatorMap).toEqual({}); + + accumulatorMap.add('a', 1); + accumulatorMap.add('b', 2); + accumulatorMap.add('c', 3); + accumulatorMap.add('b', 4); + accumulatorMap.add('c', 5); + accumulatorMap.add('c', 6); + expectMap(accumulatorMap).toEqual({ + a: [1], + b: [2, 4], + c: [3, 5, 6], + }); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/Path-test.ts b/packages/graphql/src/jsutils/__tests__/Path-test.ts new file mode 100644 index 00000000000..f2f3445397d --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/Path-test.ts @@ -0,0 +1,33 @@ +import { addPath, pathToArray } from '../Path.js'; + +describe('Path', () => { + it('can create a Path', () => { + const first = addPath(undefined, 1, 'First'); + + expect(first).toEqual({ + prev: undefined, + key: 1, + typename: 'First', + }); + }); + + it('can add a new key to an existing Path', () => { + const first = addPath(undefined, 1, 'First'); + const second = addPath(first, 'two', 'Second'); + + expect(second).toEqual({ + prev: first, + key: 'two', + typename: 'Second', + }); + }); + + it('can convert a Path to an array of its keys', () => { + const root = addPath(undefined, 0, 'Root'); + const first = addPath(root, 'one', 'First'); + const second = addPath(first, 2, 'Second'); + + const path = pathToArray(second); + expect(path).toEqual([0, 'one', 2]); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/capitalize-test.ts b/packages/graphql/src/jsutils/__tests__/capitalize-test.ts new file mode 100644 index 00000000000..05b910beac7 --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/capitalize-test.ts @@ -0,0 +1,18 @@ +import { capitalize } from '../capitalize.js'; + +describe('capitalize', () => { + it('Converts the first character of string to upper case and the remaining to lower case', () => { + expect(capitalize('')).toEqual(''); + + expect(capitalize('a')).toEqual('A'); + expect(capitalize('A')).toEqual('A'); + + expect(capitalize('ab')).toEqual('Ab'); + expect(capitalize('aB')).toEqual('Ab'); + expect(capitalize('Ab')).toEqual('Ab'); + expect(capitalize('AB')).toEqual('Ab'); + + expect(capitalize('platypus')).toEqual('Platypus'); + expect(capitalize('PLATYPUS')).toEqual('Platypus'); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/didYouMean-test.ts b/packages/graphql/src/jsutils/__tests__/didYouMean-test.ts new file mode 100644 index 00000000000..150b7db088b --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/didYouMean-test.ts @@ -0,0 +1,27 @@ +import { didYouMean } from '../didYouMean.js'; + +describe('didYouMean', () => { + it('Does accept an empty list', () => { + expect(didYouMean([])).toEqual(''); + }); + + it('Handles single suggestion', () => { + expect(didYouMean(['A'])).toEqual(' Did you mean "A"?'); + }); + + it('Handles two suggestions', () => { + expect(didYouMean(['A', 'B'])).toEqual(' Did you mean "A" or "B"?'); + }); + + it('Handles multiple suggestions', () => { + expect(didYouMean(['A', 'B', 'C'])).toEqual(' Did you mean "A", "B", or "C"?'); + }); + + it('Limits to five suggestions', () => { + expect(didYouMean(['A', 'B', 'C', 'D', 'E', 'F'])).toEqual(' Did you mean "A", "B", "C", "D", or "E"?'); + }); + + it('Adds sub-message', () => { + expect(didYouMean('the letter', ['A'])).toEqual(' Did you mean the letter "A"?'); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/identityFunc-test.ts b/packages/graphql/src/jsutils/__tests__/identityFunc-test.ts new file mode 100644 index 00000000000..2b6d722d33a --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/identityFunc-test.ts @@ -0,0 +1,13 @@ +import { identityFunc } from '../identityFunc.js'; + +describe('identityFunc', () => { + it('returns the first argument it receives', () => { + // @ts-expect-error (Expects an argument) + expect(identityFunc()).toEqual(undefined); + expect(identityFunc(undefined)).toEqual(undefined); + expect(identityFunc(null)).toEqual(null); + + const obj = {}; + expect(identityFunc(obj)).toEqual(obj); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/inspect-test.ts b/packages/graphql/src/jsutils/__tests__/inspect-test.ts new file mode 100644 index 00000000000..f9ac549af90 --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/inspect-test.ts @@ -0,0 +1,173 @@ +import { inspect } from '../inspect.js'; + +describe('inspect', () => { + it('undefined', () => { + expect(inspect(undefined)).toEqual('undefined'); + }); + + it('null', () => { + expect(inspect(null)).toEqual('null'); + }); + + it('boolean', () => { + expect(inspect(true)).toEqual('true'); + expect(inspect(false)).toEqual('false'); + }); + + it('string', () => { + expect(inspect('')).toEqual('""'); + expect(inspect('abc')).toEqual('"abc"'); + expect(inspect('"')).toEqual('"\\""'); + }); + + it('number', () => { + expect(inspect(0.0)).toEqual('0'); + expect(inspect(3.14)).toEqual('3.14'); + expect(inspect(NaN)).toEqual('NaN'); + expect(inspect(Infinity)).toEqual('Infinity'); + expect(inspect(-Infinity)).toEqual('-Infinity'); + }); + + it('function', () => { + const unnamedFuncStr = inspect( + // Never called and used as a placeholder + /* c8 ignore next */ + () => {} + ); + expect(unnamedFuncStr).toEqual('[function]'); + + // Never called and used as a placeholder + /* c8 ignore next 3 */ + function namedFunc() {} + expect(inspect(namedFunc)).toEqual('[function namedFunc]'); + }); + + it('array', () => { + expect(inspect([])).toEqual('[]'); + expect(inspect([null])).toEqual('[null]'); + expect(inspect([1, NaN])).toEqual('[1, NaN]'); + expect(inspect([['a', 'b'], 'c'])).toEqual('[["a", "b"], "c"]'); + + expect(inspect([[[]]])).toEqual('[[[]]]'); + expect(inspect([[['a']]])).toEqual('[[[Array]]]'); + + expect(inspect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])).toEqual('[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'); + + expect(inspect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toEqual('[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 1 more item]'); + + expect(inspect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])).toEqual('[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 2 more items]'); + }); + + it('object', () => { + expect(inspect({})).toEqual('{}'); + expect(inspect({ a: 1 })).toEqual('{ a: 1 }'); + expect(inspect({ a: 1, b: 2 })).toEqual('{ a: 1, b: 2 }'); + expect(inspect({ array: [null, 0] })).toEqual('{ array: [null, 0] }'); + + expect(inspect({ a: { b: {} } })).toEqual('{ a: { b: {} } }'); + expect(inspect({ a: { b: { c: 1 } } })).toEqual('{ a: { b: [Object] } }'); + + const map = Object.create(null); + map.a = true; + map.b = null; + expect(inspect(map)).toEqual('{ a: true, b: null }'); + }); + + it('use toJSON if provided', () => { + const object = { + toJSON() { + return ''; + }, + }; + + expect(inspect(object)).toEqual(''); + }); + + it('handles toJSON that return `this` should work', () => { + const object = { + toJSON() { + return this; + }, + }; + + expect(inspect(object)).toEqual('{ toJSON: [function toJSON] }'); + }); + + it('handles toJSON returning object values', () => { + const object = { + toJSON() { + return { json: 'value' }; + }, + }; + + expect(inspect(object)).toEqual('{ json: "value" }'); + }); + + it('handles toJSON function that uses this', () => { + const object = { + str: 'Hello World!', + toJSON() { + return this.str; + }, + }; + + expect(inspect(object)).toEqual('Hello World!'); + }); + + it('detect circular objects', () => { + const obj: { [name: string]: unknown } = {}; + obj['self'] = obj; + obj['deepSelf'] = { self: obj }; + + expect(inspect(obj)).toEqual('{ self: [Circular], deepSelf: { self: [Circular] } }'); + + const array: any = []; + array[0] = array; + array[1] = [array]; + + expect(inspect(array)).toEqual('[[Circular], [[Circular]]]'); + + const mixed: any = { array: [] }; + mixed.array[0] = mixed; + + expect(inspect(mixed)).toEqual('{ array: [[Circular]] }'); + + const customA = { + toJSON: () => customB, + }; + + const customB = { + toJSON: () => customA, + }; + + expect(inspect(customA)).toEqual('[Circular]'); + }); + + it('Use class names for the short form of an object', () => { + class Foo { + foo: string; + + constructor() { + this.foo = 'bar'; + } + } + + expect(inspect([[new Foo()]])).toEqual('[[[Foo]]]'); + + class Foo2 { + foo: string; + + [Symbol.toStringTag] = 'Bar'; + + constructor() { + this.foo = 'bar'; + } + } + expect(inspect([[new Foo2()]])).toEqual('[[[Bar]]]'); + + const objectWithoutClassName = new (function (this: any) { + this.foo = 1; + } as any)(); + expect(inspect([[objectWithoutClassName]])).toEqual('[[[Object]]]'); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/invariant-test.ts b/packages/graphql/src/jsutils/__tests__/invariant-test.ts new file mode 100644 index 00000000000..6bddc4fab65 --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/invariant-test.ts @@ -0,0 +1,11 @@ +import { invariant } from '../invariant.js'; + +describe('invariant', () => { + it('throws on false conditions', () => { + expect(() => invariant(false, 'Oops!')).toThrow('Oops!'); + }); + + it('use default error message', () => { + expect(() => invariant(false)).toThrow('Unexpected invariant triggered.'); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/isAsyncIterable-test.ts b/packages/graphql/src/jsutils/__tests__/isAsyncIterable-test.ts new file mode 100644 index 00000000000..c3ae3b11e62 --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/isAsyncIterable-test.ts @@ -0,0 +1,49 @@ +import { identityFunc } from '../identityFunc.js'; +import { isAsyncIterable } from '../isAsyncIterable.js'; + +describe('isAsyncIterable', () => { + it('should return `true` for AsyncIterable', () => { + const asyncIterable = { [Symbol.asyncIterator]: identityFunc }; + expect(isAsyncIterable(asyncIterable)).toEqual(true); + + async function* asyncGeneratorFunc() { + /* do nothing */ + } + + expect(isAsyncIterable(asyncGeneratorFunc())).toEqual(true); + + // But async generator function itself is not iterable + expect(isAsyncIterable(asyncGeneratorFunc)).toEqual(false); + }); + + it('should return `false` for all other values', () => { + expect(isAsyncIterable(null)).toEqual(false); + expect(isAsyncIterable(undefined)).toEqual(false); + + expect(isAsyncIterable('ABC')).toEqual(false); + expect(isAsyncIterable('0')).toEqual(false); + expect(isAsyncIterable('')).toEqual(false); + + expect(isAsyncIterable([])).toEqual(false); + expect(isAsyncIterable(new Int8Array(1))).toEqual(false); + + expect(isAsyncIterable({})).toEqual(false); + expect(isAsyncIterable({ iterable: true })).toEqual(false); + + const asyncIteratorWithoutSymbol = { next: identityFunc }; + expect(isAsyncIterable(asyncIteratorWithoutSymbol)).toEqual(false); + + const nonAsyncIterable = { [Symbol.iterator]: identityFunc }; + expect(isAsyncIterable(nonAsyncIterable)).toEqual(false); + + function* generatorFunc() { + /* do nothing */ + } + expect(isAsyncIterable(generatorFunc())).toEqual(false); + + const invalidAsyncIterable = { + [Symbol.asyncIterator]: { next: identityFunc }, + }; + expect(isAsyncIterable(invalidAsyncIterable)).toEqual(false); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/isIterableObject-test.ts b/packages/graphql/src/jsutils/__tests__/isIterableObject-test.ts new file mode 100644 index 00000000000..f0395f68408 --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/isIterableObject-test.ts @@ -0,0 +1,67 @@ +import { identityFunc } from '../identityFunc.js'; +import { isIterableObject } from '../isIterableObject.js'; + +describe('isIterableObject', () => { + it('should return `true` for collections', () => { + expect(isIterableObject([])).toEqual(true); + expect(isIterableObject(new Int8Array(1))).toEqual(true); + + // eslint-disable-next-line no-new-wrappers + expect(isIterableObject(new String('ABC'))).toEqual(true); + + function getArguments() { + return arguments; + } + expect(isIterableObject(getArguments())).toEqual(true); + + const iterable = { [Symbol.iterator]: identityFunc }; + expect(isIterableObject(iterable)).toEqual(true); + + function* generatorFunc() { + /* do nothing */ + } + expect(isIterableObject(generatorFunc())).toEqual(true); + + // But generator function itself is not iterable + expect(isIterableObject(generatorFunc)).toEqual(false); + }); + + it('should return `false` for non-collections', () => { + expect(isIterableObject(null)).toEqual(false); + expect(isIterableObject(undefined)).toEqual(false); + + expect(isIterableObject('ABC')).toEqual(false); + expect(isIterableObject('0')).toEqual(false); + expect(isIterableObject('')).toEqual(false); + + expect(isIterableObject(1)).toEqual(false); + expect(isIterableObject(0)).toEqual(false); + expect(isIterableObject(NaN)).toEqual(false); + // eslint-disable-next-line no-new-wrappers + expect(isIterableObject(new Number(123))).toEqual(false); + + expect(isIterableObject(true)).toEqual(false); + expect(isIterableObject(false)).toEqual(false); + // eslint-disable-next-line no-new-wrappers + expect(isIterableObject(new Boolean(true))).toEqual(false); + + expect(isIterableObject({})).toEqual(false); + expect(isIterableObject({ iterable: true })).toEqual(false); + + const iteratorWithoutSymbol = { next: identityFunc }; + expect(isIterableObject(iteratorWithoutSymbol)).toEqual(false); + + const invalidIterable = { + [Symbol.iterator]: { next: identityFunc }, + }; + expect(isIterableObject(invalidIterable)).toEqual(false); + + const arrayLike: { [key: string]: unknown } = {}; + arrayLike[0] = 'Alpha'; + arrayLike[1] = 'Bravo'; + arrayLike[2] = 'Charlie'; + arrayLike['length'] = 3; + + expect(isIterableObject(arrayLike)).toEqual(false); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/isObjectLike-test.ts b/packages/graphql/src/jsutils/__tests__/isObjectLike-test.ts new file mode 100644 index 00000000000..1e2a1bdbb05 --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/isObjectLike-test.ts @@ -0,0 +1,19 @@ +import { identityFunc } from '../identityFunc.js'; +import { isObjectLike } from '../isObjectLike.js'; + +describe('isObjectLike', () => { + it('should return `true` for objects', () => { + expect(isObjectLike({})).toEqual(true); + expect(isObjectLike(Object.create(null))).toEqual(true); + expect(isObjectLike(/a/)).toEqual(true); + expect(isObjectLike([])).toEqual(true); + }); + + it('should return `false` for non-objects', () => { + expect(isObjectLike(undefined)).toEqual(false); + expect(isObjectLike(null)).toEqual(false); + expect(isObjectLike(true)).toEqual(false); + expect(isObjectLike('')).toEqual(false); + expect(isObjectLike(identityFunc)).toEqual(false); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/naturalCompare-test.ts b/packages/graphql/src/jsutils/__tests__/naturalCompare-test.ts new file mode 100644 index 00000000000..f5cd504467d --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/naturalCompare-test.ts @@ -0,0 +1,69 @@ +import { naturalCompare } from '../naturalCompare.js'; + +describe('naturalCompare', () => { + it('Handles empty strings', () => { + expect(naturalCompare('', '')).toEqual(0); + + expect(naturalCompare('', 'a')).toEqual(-1); + expect(naturalCompare('', '1')).toEqual(-1); + + expect(naturalCompare('a', '')).toEqual(1); + expect(naturalCompare('1', '')).toEqual(1); + }); + + it('Handles strings of different length', () => { + expect(naturalCompare('A', 'A')).toEqual(0); + expect(naturalCompare('A1', 'A1')).toEqual(0); + + expect(naturalCompare('A', 'AA')).toEqual(-1); + expect(naturalCompare('A1', 'A1A')).toEqual(-1); + + expect(naturalCompare('AA', 'A')).toEqual(1); + expect(naturalCompare('A1A', 'A1')).toEqual(1); + }); + + it('Handles numbers', () => { + expect(naturalCompare('0', '0')).toEqual(0); + expect(naturalCompare('1', '1')).toEqual(0); + + expect(naturalCompare('1', '2')).toEqual(-1); + expect(naturalCompare('2', '1')).toEqual(1); + + expect(naturalCompare('2', '11')).toEqual(-1); + expect(naturalCompare('11', '2')).toEqual(1); + }); + + it('Handles numbers with leading zeros', () => { + expect(naturalCompare('00', '00')).toEqual(0); + expect(naturalCompare('0', '00')).toEqual(-1); + expect(naturalCompare('00', '0')).toEqual(1); + + expect(naturalCompare('02', '11')).toEqual(-1); + expect(naturalCompare('11', '02')).toEqual(1); + + expect(naturalCompare('011', '200')).toEqual(-1); + expect(naturalCompare('200', '011')).toEqual(1); + }); + + it('Handles numbers embedded into names', () => { + expect(naturalCompare('a0a', 'a0a')).toEqual(0); + expect(naturalCompare('a0a', 'a9a')).toEqual(-1); + expect(naturalCompare('a9a', 'a0a')).toEqual(1); + + expect(naturalCompare('a00a', 'a00a')).toEqual(0); + expect(naturalCompare('a00a', 'a09a')).toEqual(-1); + expect(naturalCompare('a09a', 'a00a')).toEqual(1); + + expect(naturalCompare('a0a1', 'a0a1')).toEqual(0); + expect(naturalCompare('a0a1', 'a0a9')).toEqual(-1); + expect(naturalCompare('a0a9', 'a0a1')).toEqual(1); + + expect(naturalCompare('a10a11a', 'a10a11a')).toEqual(0); + expect(naturalCompare('a10a11a', 'a10a19a')).toEqual(-1); + expect(naturalCompare('a10a19a', 'a10a11a')).toEqual(1); + + expect(naturalCompare('a10a11a', 'a10a11a')).toEqual(0); + expect(naturalCompare('a10a11a', 'a10a11b')).toEqual(-1); + expect(naturalCompare('a10a11b', 'a10a11a')).toEqual(1); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/suggestionList-test.ts b/packages/graphql/src/jsutils/__tests__/suggestionList-test.ts new file mode 100644 index 00000000000..e7bac3239cd --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/suggestionList-test.ts @@ -0,0 +1,52 @@ +import { suggestionList } from '../suggestionList.js'; + +function expectSuggestions(input: string, options: ReadonlyArray) { + return expect(suggestionList(input, options)); +} + +describe('suggestionList', () => { + it('Returns results when input is empty', () => { + expectSuggestions('', ['a']).toEqual(['a']); + }); + + it('Returns empty array when there are no options', () => { + expectSuggestions('input', []).toEqual([]); + }); + + it('Returns options with small lexical distance', () => { + expectSuggestions('greenish', ['green']).toEqual(['green']); + expectSuggestions('green', ['greenish']).toEqual(['greenish']); + }); + + it('Rejects options with distance that exceeds threshold', () => { + // spell-checker:disable + expectSuggestions('aaaa', ['aaab']).toEqual(['aaab']); + expectSuggestions('aaaa', ['aabb']).toEqual(['aabb']); + expectSuggestions('aaaa', ['abbb']).toEqual([]); + // spell-checker:enable + + expectSuggestions('ab', ['ca']).toEqual([]); + }); + + it('Returns options with different case', () => { + // cSpell:ignore verylongstring + expectSuggestions('verylongstring', ['VERYLONGSTRING']).toEqual(['VERYLONGSTRING']); + + expectSuggestions('VERYLONGSTRING', ['verylongstring']).toEqual(['verylongstring']); + + expectSuggestions('VERYLONGSTRING', ['VeryLongString']).toEqual(['VeryLongString']); + }); + + it('Returns options with transpositions', () => { + expectSuggestions('agr', ['arg']).toEqual(['arg']); + expectSuggestions('214365879', ['123456789']).toEqual(['123456789']); + }); + + it('Returns options sorted based on lexical distance', () => { + expectSuggestions('abc', ['a', 'ab', 'abc']).toEqual(['abc', 'ab', 'a']); + }); + + it('Returns options with the same lexical distance sorted lexicographically', () => { + expectSuggestions('a', ['az', 'ax', 'ay']).toEqual(['ax', 'ay', 'az']); + }); +}); diff --git a/packages/graphql/src/jsutils/__tests__/toObjMap-test.ts b/packages/graphql/src/jsutils/__tests__/toObjMap-test.ts new file mode 100644 index 00000000000..4c373af1601 --- /dev/null +++ b/packages/graphql/src/jsutils/__tests__/toObjMap-test.ts @@ -0,0 +1,58 @@ +import type { ObjMapLike } from '../ObjMap.js'; +import { toObjMap } from '../toObjMap.js'; + +// Workaround to make both ESLint happy +const __proto__ = '__proto__'; + +describe('toObjMap', () => { + it('convert undefined to ObjMap', () => { + const result = toObjMap(undefined); + expect(result).toEqual({}); + expect(Object.getPrototypeOf(result)).toEqual(null); + }); + + it('convert null to ObjMap', () => { + const result = toObjMap(null); + expect(result).toEqual({}); + expect(Object.getPrototypeOf(result)).toEqual(null); + }); + + it('convert empty object to ObjMap', () => { + const result = toObjMap({}); + expect(result).toEqual({}); + expect(Object.getPrototypeOf(result)).toEqual(null); + }); + + it('convert object with own properties to ObjMap', () => { + const obj: ObjMapLike = Object.freeze({ foo: 'bar' }); + + const result = toObjMap(obj); + expect(result).toEqual(obj); + expect(Object.getPrototypeOf(result)).toEqual(null); + }); + + it('convert object with __proto__ property to ObjMap', () => { + const protoObj = Object.freeze({ toString: false }); + const obj = Object.create(null); + obj[__proto__] = protoObj; + Object.freeze(obj); + + const result = toObjMap(obj); + expect(Object.keys(result)).toEqual(['__proto__']); + expect(Object.getPrototypeOf(result)).toEqual(null); + expect(result[__proto__]).toEqual(protoObj); + }); + + it('passthrough empty ObjMap', () => { + const objMap = Object.create(null); + expect(toObjMap(objMap)).toEqual(objMap); + }); + + it('passthrough ObjMap with properties', () => { + const objMap = Object.freeze({ + __proto__: null, + foo: 'bar', + }); + expect(toObjMap(objMap)).toEqual(objMap); + }); +}); diff --git a/packages/graphql/src/jsutils/capitalize.ts b/packages/graphql/src/jsutils/capitalize.ts new file mode 100644 index 00000000000..064dddcf0f5 --- /dev/null +++ b/packages/graphql/src/jsutils/capitalize.ts @@ -0,0 +1,6 @@ +/** + * Converts the first character of string to upper case and the remaining to lower case. + */ +export function capitalize(str: string): string { + return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); +} diff --git a/packages/graphql/src/jsutils/devAssert.ts b/packages/graphql/src/jsutils/devAssert.ts new file mode 100644 index 00000000000..ef8ed6f1099 --- /dev/null +++ b/packages/graphql/src/jsutils/devAssert.ts @@ -0,0 +1,5 @@ +export function devAssert(condition: boolean, message: string): void { + if (!condition) { + throw new Error(message); + } +} diff --git a/packages/graphql/src/jsutils/didYouMean.ts b/packages/graphql/src/jsutils/didYouMean.ts new file mode 100644 index 00000000000..4c57d65594e --- /dev/null +++ b/packages/graphql/src/jsutils/didYouMean.ts @@ -0,0 +1,26 @@ +import { orList } from './formatList.js'; + +const MAX_SUGGESTIONS = 5; + +/** + * Given [ A, B, C ] return ' Did you mean A, B, or C?'. + */ +export function didYouMean(suggestions: ReadonlyArray): string; +export function didYouMean(subMessage: string, suggestions: ReadonlyArray): string; +export function didYouMean(firstArg: string | ReadonlyArray, secondArg?: ReadonlyArray) { + const [subMessage, suggestions] = secondArg + ? [firstArg as string, secondArg] + : [undefined, firstArg as ReadonlyArray]; + + if (suggestions.length === 0) { + return ''; + } + + let message = ' Did you mean '; + if (subMessage) { + message += subMessage + ' '; + } + + const suggestionList = orList(suggestions.slice(0, MAX_SUGGESTIONS).map(x => `"${x}"`)); + return message + suggestionList + '?'; +} diff --git a/packages/graphql/src/jsutils/formatList.ts b/packages/graphql/src/jsutils/formatList.ts new file mode 100644 index 00000000000..b00dd6591a2 --- /dev/null +++ b/packages/graphql/src/jsutils/formatList.ts @@ -0,0 +1,30 @@ +import { invariant } from './invariant.js'; + +/** + * Given [ A, B, C ] return 'A, B, or C'. + */ +export function orList(items: ReadonlyArray): string { + return formatList('or', items); +} + +/** + * Given [ A, B, C ] return 'A, B, and C'. + */ +export function andList(items: ReadonlyArray): string { + return formatList('and', items); +} + +function formatList(conjunction: string, items: ReadonlyArray): string { + invariant(items.length !== 0); + + switch (items.length) { + case 1: + return items[0]; + case 2: + return items[0] + ' ' + conjunction + ' ' + items[1]; + } + + const allButLast = items.slice(0, -1); + const lastItem = items[items.length - 1]; + return allButLast.join(', ') + ', ' + conjunction + ' ' + lastItem; +} diff --git a/packages/graphql/src/jsutils/groupBy.ts b/packages/graphql/src/jsutils/groupBy.ts new file mode 100644 index 00000000000..2b3c1e271e9 --- /dev/null +++ b/packages/graphql/src/jsutils/groupBy.ts @@ -0,0 +1,12 @@ +import { AccumulatorMap } from './AccumulatorMap.js'; + +/** + * Groups array items into a Map, given a function to produce grouping key. + */ +export function groupBy(list: ReadonlyArray, keyFn: (item: T) => K): Map> { + const result = new AccumulatorMap(); + for (const item of list) { + result.add(keyFn(item), item); + } + return result; +} diff --git a/packages/graphql/src/jsutils/identityFunc.ts b/packages/graphql/src/jsutils/identityFunc.ts new file mode 100644 index 00000000000..a249b51c34c --- /dev/null +++ b/packages/graphql/src/jsutils/identityFunc.ts @@ -0,0 +1,6 @@ +/** + * Returns the first argument it receives. + */ +export function identityFunc(x: T): T { + return x; +} diff --git a/packages/graphql/src/jsutils/inspect.ts b/packages/graphql/src/jsutils/inspect.ts new file mode 100644 index 00000000000..03222280143 --- /dev/null +++ b/packages/graphql/src/jsutils/inspect.ts @@ -0,0 +1,107 @@ +const MAX_ARRAY_LENGTH = 10; +const MAX_RECURSIVE_DEPTH = 2; + +/** + * Used to print values in error messages. + */ +export function inspect(value: unknown): string { + return formatValue(value, []); +} + +function formatValue(value: unknown, seenValues: ReadonlyArray): string { + switch (typeof value) { + case 'string': + return JSON.stringify(value); + case 'function': + return value.name ? `[function ${value.name}]` : '[function]'; + case 'object': + return formatObjectValue(value, seenValues); + default: + return String(value); + } +} + +function formatObjectValue(value: object | null, previouslySeenValues: ReadonlyArray): string { + if (value === null) { + return 'null'; + } + + if (previouslySeenValues.includes(value)) { + return '[Circular]'; + } + + const seenValues = [...previouslySeenValues, value]; + + if (isJSONable(value)) { + const jsonValue = value.toJSON(); + + // check for infinite recursion + if (jsonValue !== value) { + return typeof jsonValue === 'string' ? jsonValue : formatValue(jsonValue, seenValues); + } + } else if (Array.isArray(value)) { + return formatArray(value, seenValues); + } + + return formatObject(value, seenValues); +} + +function isJSONable(value: any): value is { toJSON: () => unknown } { + return typeof value.toJSON === 'function'; +} + +function formatObject(object: object, seenValues: ReadonlyArray): string { + const entries = Object.entries(object); + if (entries.length === 0) { + return '{}'; + } + + if (seenValues.length > MAX_RECURSIVE_DEPTH) { + return '[' + getObjectTag(object) + ']'; + } + + const properties = entries.map(([key, value]) => key + ': ' + formatValue(value, seenValues)); + return '{ ' + properties.join(', ') + ' }'; +} + +function formatArray(array: ReadonlyArray, seenValues: ReadonlyArray): string { + if (array.length === 0) { + return '[]'; + } + + if (seenValues.length > MAX_RECURSIVE_DEPTH) { + return '[Array]'; + } + + const len = Math.min(MAX_ARRAY_LENGTH, array.length); + const remaining = array.length - len; + const items = []; + + for (let i = 0; i < len; ++i) { + items.push(formatValue(array[i], seenValues)); + } + + if (remaining === 1) { + items.push('... 1 more item'); + } else if (remaining > 1) { + items.push(`... ${remaining} more items`); + } + + return '[' + items.join(', ') + ']'; +} + +function getObjectTag(object: object): string { + const tag = Object.prototype.toString + .call(object) + .replace(/^\[object /, '') + .replace(/]$/, ''); + + if (tag === 'Object' && typeof object.constructor === 'function') { + const name = object.constructor.name; + if (typeof name === 'string' && name !== '') { + return name; + } + } + + return tag; +} diff --git a/packages/graphql/src/jsutils/invariant.ts b/packages/graphql/src/jsutils/invariant.ts new file mode 100644 index 00000000000..0b2f891a2cb --- /dev/null +++ b/packages/graphql/src/jsutils/invariant.ts @@ -0,0 +1,5 @@ +export function invariant(condition: boolean, message?: string): asserts condition { + if (!condition) { + throw new Error(message != null ? message : 'Unexpected invariant triggered.'); + } +} diff --git a/packages/graphql/src/jsutils/isAsyncIterable.ts b/packages/graphql/src/jsutils/isAsyncIterable.ts new file mode 100644 index 00000000000..8a60edd69c6 --- /dev/null +++ b/packages/graphql/src/jsutils/isAsyncIterable.ts @@ -0,0 +1,7 @@ +/** + * Returns true if the provided object implements the AsyncIterator protocol via + * implementing a `Symbol.asyncIterator` method. + */ +export function isAsyncIterable(maybeAsyncIterable: any): maybeAsyncIterable is AsyncIterable { + return typeof maybeAsyncIterable?.[Symbol.asyncIterator] === 'function'; +} diff --git a/packages/graphql/src/jsutils/isIterableObject.ts b/packages/graphql/src/jsutils/isIterableObject.ts new file mode 100644 index 00000000000..7a7942cdaab --- /dev/null +++ b/packages/graphql/src/jsutils/isIterableObject.ts @@ -0,0 +1,20 @@ +/** + * Returns true if the provided object is an Object (i.e. not a string literal) + * and implements the Iterator protocol. + * + * This may be used in place of [Array.isArray()][isArray] to determine if + * an object should be iterated-over e.g. Array, Map, Set, Int8Array, + * TypedArray, etc. but excludes string literals. + * + * @example + * ```ts + * isIterableObject([ 1, 2, 3 ]) // true + * isIterableObject(new Map()) // true + * isIterableObject('ABC') // false + * isIterableObject({ key: 'value' }) // false + * isIterableObject({ length: 1, 0: 'Alpha' }) // false + * ``` + */ +export function isIterableObject(maybeIterable: any): maybeIterable is Iterable { + return typeof maybeIterable === 'object' && typeof maybeIterable?.[Symbol.iterator] === 'function'; +} diff --git a/packages/graphql/src/jsutils/isObjectLike.ts b/packages/graphql/src/jsutils/isObjectLike.ts new file mode 100644 index 00000000000..92941dd923f --- /dev/null +++ b/packages/graphql/src/jsutils/isObjectLike.ts @@ -0,0 +1,7 @@ +/** + * Return true if `value` is object-like. A value is object-like if it's not + * `null` and has a `typeof` result of "object". + */ +export function isObjectLike(value: unknown): value is { [key: string]: unknown } { + return typeof value == 'object' && value !== null; +} diff --git a/packages/graphql/src/jsutils/isPromise.ts b/packages/graphql/src/jsutils/isPromise.ts new file mode 100644 index 00000000000..5fc3c10458a --- /dev/null +++ b/packages/graphql/src/jsutils/isPromise.ts @@ -0,0 +1,7 @@ +/** + * Returns true if the value acts like a Promise, i.e. has a "then" function, + * otherwise returns false. + */ +export function isPromise(value: any): value is Promise { + return typeof value?.then === 'function'; +} diff --git a/packages/graphql/src/jsutils/keyMap.ts b/packages/graphql/src/jsutils/keyMap.ts new file mode 100644 index 00000000000..c78b373439a --- /dev/null +++ b/packages/graphql/src/jsutils/keyMap.ts @@ -0,0 +1,36 @@ +import type { ObjMap } from './ObjMap.js'; + +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * for each value in the array. + * + * This provides a convenient lookup for the array items if the key function + * produces unique results. + * ```ts + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * const entriesByName = keyMap( + * phoneBook, + * entry => entry.name + * ) + * + * // { + * // Jon: { name: 'Jon', num: '555-1234' }, + * // Jenny: { name: 'Jenny', num: '867-5309' } + * // } + * + * const jennyEntry = entriesByName['Jenny'] + * + * // { name: 'Jenny', num: '857-6309' } + * ``` + */ +export function keyMap(list: ReadonlyArray, keyFn: (item: T) => string): ObjMap { + const result = Object.create(null); + for (const item of list) { + result[keyFn(item)] = item; + } + return result; +} diff --git a/packages/graphql/src/jsutils/keyValMap.ts b/packages/graphql/src/jsutils/keyValMap.ts new file mode 100644 index 00000000000..5047d5b2e77 --- /dev/null +++ b/packages/graphql/src/jsutils/keyValMap.ts @@ -0,0 +1,26 @@ +import type { ObjMap } from './ObjMap.js'; + +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * and a function to produce the values from each item in the array. + * ```ts + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * // { Jon: '555-1234', Jenny: '867-5309' } + * const phonesByName = keyValMap( + * phoneBook, + * entry => entry.name, + * entry => entry.num + * ) + * ``` + */ +export function keyValMap(list: ReadonlyArray, keyFn: (item: T) => string, valFn: (item: T) => V): ObjMap { + const result = Object.create(null); + for (const item of list) { + result[keyFn(item)] = valFn(item); + } + return result; +} diff --git a/packages/graphql/src/jsutils/mapValue.ts b/packages/graphql/src/jsutils/mapValue.ts new file mode 100644 index 00000000000..d6da619c61f --- /dev/null +++ b/packages/graphql/src/jsutils/mapValue.ts @@ -0,0 +1,14 @@ +import type { ObjMap, ReadOnlyObjMap } from './ObjMap.js'; + +/** + * Creates an object map with the same keys as `map` and values generated by + * running each value of `map` thru `fn`. + */ +export function mapValue(map: ReadOnlyObjMap, fn: (value: T, key: string) => V): ObjMap { + const result = Object.create(null); + + for (const key of Object.keys(map)) { + result[key] = fn(map[key], key); + } + return result; +} diff --git a/packages/graphql/src/jsutils/memoize1.ts b/packages/graphql/src/jsutils/memoize1.ts new file mode 100644 index 00000000000..12d3c1d8b84 --- /dev/null +++ b/packages/graphql/src/jsutils/memoize1.ts @@ -0,0 +1,16 @@ +/** + * Memoizes the provided one-argument function. + */ +export function memoize1 any>(fn: F): F { + const memoize1cache: WeakMap, WeakMap, any>> = new WeakMap(); + return function memoized(a1: any): any { + const cachedValue = memoize1cache.get(a1); + if (cachedValue === undefined) { + const newValue = fn(a1); + memoize1cache.set(a1, newValue); + return newValue; + } + + return cachedValue; + } as F; +} diff --git a/packages/graphql/src/jsutils/memoize3.ts b/packages/graphql/src/jsutils/memoize3.ts new file mode 100644 index 00000000000..65bb52e0307 --- /dev/null +++ b/packages/graphql/src/jsutils/memoize3.ts @@ -0,0 +1,34 @@ +/** + * Memoizes the provided three-argument function. + */ +export function memoize3( + fn: (a1: A1, a2: A2, a3: A3) => R +): (a1: A1, a2: A2, a3: A3) => R { + let cache0: WeakMap>>; + + return function memoized(a1, a2, a3) { + if (cache0 === undefined) { + cache0 = new WeakMap(); + } + + let cache1 = cache0.get(a1); + if (cache1 === undefined) { + cache1 = new WeakMap(); + cache0.set(a1, cache1); + } + + let cache2 = cache1.get(a2); + if (cache2 === undefined) { + cache2 = new WeakMap(); + cache1.set(a2, cache2); + } + + let fnResult = cache2.get(a3); + if (fnResult === undefined) { + fnResult = fn(a1, a2, a3); + cache2.set(a3, fnResult); + } + + return fnResult; + }; +} diff --git a/packages/graphql/src/jsutils/naturalCompare.ts b/packages/graphql/src/jsutils/naturalCompare.ts new file mode 100644 index 00000000000..7a562863063 --- /dev/null +++ b/packages/graphql/src/jsutils/naturalCompare.ts @@ -0,0 +1,58 @@ +/** + * Returns a number indicating whether a reference string comes before, or after, + * or is the same as the given string in natural sort order. + * + * See: https://en.wikipedia.org/wiki/Natural_sort_order + * + */ +export function naturalCompare(aStr: string, bStr: string): number { + let aIndex = 0; + let bIndex = 0; + + while (aIndex < aStr.length && bIndex < bStr.length) { + let aChar = aStr.charCodeAt(aIndex); + let bChar = bStr.charCodeAt(bIndex); + + if (isDigit(aChar) && isDigit(bChar)) { + let aNum = 0; + do { + ++aIndex; + aNum = aNum * 10 + aChar - DIGIT_0; + aChar = aStr.charCodeAt(aIndex); + } while (isDigit(aChar) && aNum > 0); + + let bNum = 0; + do { + ++bIndex; + bNum = bNum * 10 + bChar - DIGIT_0; + bChar = bStr.charCodeAt(bIndex); + } while (isDigit(bChar) && bNum > 0); + + if (aNum < bNum) { + return -1; + } + + if (aNum > bNum) { + return 1; + } + } else { + if (aChar < bChar) { + return -1; + } + if (aChar > bChar) { + return 1; + } + ++aIndex; + ++bIndex; + } + } + + return aStr.length - bStr.length; +} + +const DIGIT_0 = 48; +const DIGIT_9 = 57; + +function isDigit(code: number): boolean { + return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9; +} diff --git a/packages/graphql/src/jsutils/printPathArray.ts b/packages/graphql/src/jsutils/printPathArray.ts new file mode 100644 index 00000000000..d3521718759 --- /dev/null +++ b/packages/graphql/src/jsutils/printPathArray.ts @@ -0,0 +1,6 @@ +/** + * Build a string describing the path. + */ +export function printPathArray(path: ReadonlyArray): string { + return path.map(key => (typeof key === 'number' ? '[' + key.toString() + ']' : '.' + key)).join(''); +} diff --git a/packages/graphql/src/jsutils/promiseForObject.ts b/packages/graphql/src/jsutils/promiseForObject.ts new file mode 100644 index 00000000000..7bcec7bbca4 --- /dev/null +++ b/packages/graphql/src/jsutils/promiseForObject.ts @@ -0,0 +1,18 @@ +import type { ObjMap } from './ObjMap.js'; + +/** + * This function transforms a JS object `ObjMap>` into + * a `Promise>` + * + * This is akin to bluebird's `Promise.props`, but implemented only using + * `Promise.all` so it will work with any implementation of ES6 promises. + */ +export function promiseForObject(object: ObjMap>): Promise> { + return Promise.all(Object.values(object)).then(resolvedValues => { + const resolvedObject = Object.create(null); + for (const [i, key] of Object.keys(object).entries()) { + resolvedObject[key] = resolvedValues[i]; + } + return resolvedObject; + }); +} diff --git a/packages/graphql/src/jsutils/promiseReduce.ts b/packages/graphql/src/jsutils/promiseReduce.ts new file mode 100644 index 00000000000..0a28cb06edf --- /dev/null +++ b/packages/graphql/src/jsutils/promiseReduce.ts @@ -0,0 +1,23 @@ +import { isPromise } from './isPromise.js'; +import type { PromiseOrValue } from './PromiseOrValue.js'; + +/** + * Similar to Array.prototype.reduce(), however the reducing callback may return + * a Promise, in which case reduction will continue after each promise resolves. + * + * If the callback does not return a Promise, then this function will also not + * return a Promise. + */ +export function promiseReduce( + values: Iterable, + callbackFn: (accumulator: U, currentValue: T) => PromiseOrValue, + initialValue: PromiseOrValue +): PromiseOrValue { + let accumulator = initialValue; + for (const value of values) { + accumulator = isPromise(accumulator) + ? accumulator.then(resolved => callbackFn(resolved, value)) + : callbackFn(accumulator, value); + } + return accumulator; +} diff --git a/packages/graphql/src/jsutils/suggestionList.ts b/packages/graphql/src/jsutils/suggestionList.ts new file mode 100644 index 00000000000..c31f2b0a819 --- /dev/null +++ b/packages/graphql/src/jsutils/suggestionList.ts @@ -0,0 +1,134 @@ +import { naturalCompare } from './naturalCompare.js'; + +/** + * Given an invalid input string and a list of valid options, returns a filtered + * list of valid options sorted based on their similarity with the input. + */ +export function suggestionList(input: string, options: ReadonlyArray): Array { + const optionsByDistance = Object.create(null); + const lexicalDistance = new LexicalDistance(input); + + const threshold = Math.floor(input.length * 0.4) + 1; + for (const option of options) { + const distance = lexicalDistance.measure(option, threshold); + if (distance !== undefined) { + optionsByDistance[option] = distance; + } + } + + return Object.keys(optionsByDistance).sort((a, b) => { + const distanceDiff = optionsByDistance[a] - optionsByDistance[b]; + return distanceDiff !== 0 ? distanceDiff : naturalCompare(a, b); + }); +} + +/** + * Computes the lexical distance between strings A and B. + * + * The "distance" between two strings is given by counting the minimum number + * of edits needed to transform string A into string B. An edit can be an + * insertion, deletion, or substitution of a single character, or a swap of two + * adjacent characters. + * + * Includes a custom alteration from Damerau-Levenshtein to treat case changes + * as a single edit which helps identify mis-cased values with an edit distance + * of 1. + * + * This distance can be useful for detecting typos in input or sorting + */ +class LexicalDistance { + _input: string; + _inputLowerCase: string; + _inputArray: Array; + _rows: [Array, Array, Array]; + + constructor(input: string) { + this._input = input; + this._inputLowerCase = input.toLowerCase(); + this._inputArray = stringToArray(this._inputLowerCase); + + this._rows = [ + new Array(input.length + 1).fill(0), + new Array(input.length + 1).fill(0), + new Array(input.length + 1).fill(0), + ]; + } + + measure(option: string, threshold: number): number | undefined { + if (this._input === option) { + return 0; + } + + const optionLowerCase = option.toLowerCase(); + + // Any case change counts as a single edit + if (this._inputLowerCase === optionLowerCase) { + return 1; + } + + let a = stringToArray(optionLowerCase); + let b = this._inputArray; + + if (a.length < b.length) { + const tmp = a; + a = b; + b = tmp; + } + const aLength = a.length; + const bLength = b.length; + + if (aLength - bLength > threshold) { + return undefined; + } + + const rows = this._rows; + for (let j = 0; j <= bLength; j++) { + rows[0][j] = j; + } + + for (let i = 1; i <= aLength; i++) { + const upRow = rows[(i - 1) % 3]; + const currentRow = rows[i % 3]; + + let smallestCell = (currentRow[0] = i); + for (let j = 1; j <= bLength; j++) { + const cost = a[i - 1] === b[j - 1] ? 0 : 1; + + let currentCell = Math.min( + upRow[j] + 1, // delete + currentRow[j - 1] + 1, // insert + upRow[j - 1] + cost // substitute + ); + + if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) { + // transposition + const doubleDiagonalCell = rows[(i - 2) % 3][j - 2]; + currentCell = Math.min(currentCell, doubleDiagonalCell + 1); + } + + if (currentCell < smallestCell) { + smallestCell = currentCell; + } + + currentRow[j] = currentCell; + } + + // Early exit, since distance can't go smaller than smallest element of the previous row. + if (smallestCell > threshold) { + return undefined; + } + } + + const distance = rows[aLength % 3][bLength]; + return distance <= threshold ? distance : undefined; + } +} + +function stringToArray(str: string): Array { + const strLength = str.length; + const array = new Array(strLength); + for (let i = 0; i < strLength; ++i) { + array[i] = str.charCodeAt(i); + } + return array; +} diff --git a/packages/graphql/src/jsutils/toError.ts b/packages/graphql/src/jsutils/toError.ts new file mode 100644 index 00000000000..81b88296ba4 --- /dev/null +++ b/packages/graphql/src/jsutils/toError.ts @@ -0,0 +1,18 @@ +import { inspect } from './inspect.js'; + +/** + * Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface. + */ +export function toError(thrownValue: unknown): Error { + return thrownValue instanceof Error ? thrownValue : new NonErrorThrown(thrownValue); +} + +class NonErrorThrown extends Error { + thrownValue: unknown; + + constructor(thrownValue: unknown) { + super('Unexpected error value: ' + inspect(thrownValue)); + this.name = 'NonErrorThrown'; + this.thrownValue = thrownValue; + } +} diff --git a/packages/graphql/src/jsutils/toObjMap.ts b/packages/graphql/src/jsutils/toObjMap.ts new file mode 100644 index 00000000000..1fdf9f2bb40 --- /dev/null +++ b/packages/graphql/src/jsutils/toObjMap.ts @@ -0,0 +1,18 @@ +import type { Maybe } from './Maybe.js'; +import type { ReadOnlyObjMap, ReadOnlyObjMapLike } from './ObjMap.js'; + +export function toObjMap(obj: Maybe>): ReadOnlyObjMap { + if (obj == null) { + return Object.create(null); + } + + if (Object.getPrototypeOf(obj) === null) { + return obj; + } + + const map = Object.create(null); + for (const [key, value] of Object.entries(obj)) { + map[key] = value; + } + return map; +} diff --git a/packages/graphql/src/language/__tests__/blockString-fuzz.ts b/packages/graphql/src/language/__tests__/blockString-fuzz.ts new file mode 100644 index 00000000000..5f8622beb62 --- /dev/null +++ b/packages/graphql/src/language/__tests__/blockString-fuzz.ts @@ -0,0 +1,45 @@ +import { genFuzzStrings } from '../../__testUtils__/genFuzzStrings.js'; + +import { isPrintableAsBlockString, printBlockString } from '../blockString.js'; +import { Lexer } from '../lexer.js'; +import { Source } from '../source.js'; + +function lexValue(str: string): string { + const lexer = new Lexer(new Source(str)); + const value = lexer.advance().value; + + expect(typeof value === 'string').toBeTruthy(); + expect(lexer.advance().kind === '').toBeTruthy(); + return value; +} + +function testPrintableBlockString(testValue: string, options?: { minimize: boolean }): void { + const blockString = printBlockString(testValue, options); + const printedValue = lexValue(blockString); + expect(testValue === printedValue).toBeTruthy(); +} + +function testNonPrintableBlockString(testValue: string): void { + const blockString = printBlockString(testValue); + const printedValue = lexValue(blockString); + expect(testValue !== printedValue).toBeTruthy(); +} + +describe('printBlockString', () => { + it('correctly print random strings', () => { + // Testing with length >7 is taking exponentially more time. However it is + // highly recommended to test with increased limit if you make any change. + for (const fuzzStr of genFuzzStrings({ + allowedChars: ['\n', '\t', ' ', '"', 'a', '\\'], + maxLength: 7, + })) { + if (!isPrintableAsBlockString(fuzzStr)) { + testNonPrintableBlockString(fuzzStr); + continue; + } + + testPrintableBlockString(fuzzStr); + testPrintableBlockString(fuzzStr, { minimize: true }); + } + }); +}); diff --git a/packages/graphql/src/language/__tests__/blockString-test.ts b/packages/graphql/src/language/__tests__/blockString-test.ts new file mode 100644 index 00000000000..212154b6233 --- /dev/null +++ b/packages/graphql/src/language/__tests__/blockString-test.ts @@ -0,0 +1,208 @@ +import { dedentBlockStringLines, isPrintableAsBlockString, printBlockString } from '../blockString.js'; + +function joinLines(...args: ReadonlyArray) { + return args.join('\n'); +} + +describe('dedentBlockStringLines', () => { + function expectDedent(lines: ReadonlyArray) { + return expect(dedentBlockStringLines(lines)); + } + + it('handles empty string', () => { + expectDedent(['']).toEqual([]); + }); + + it('does not dedent first line', () => { + expectDedent([' a']).toEqual([' a']); + expectDedent([' a', ' b']).toEqual([' a', 'b']); + }); + + it('removes minimal indentation length', () => { + expectDedent(['', ' a', ' b']).toEqual(['a', ' b']); + expectDedent(['', ' a', ' b']).toEqual([' a', 'b']); + expectDedent(['', ' a', ' b', 'c']).toEqual([' a', ' b', 'c']); + }); + + it('dedent both tab and space as single character', () => { + expectDedent(['', '\ta', ' b']).toEqual(['a', ' b']); + expectDedent(['', '\t a', ' b']).toEqual(['a', ' b']); + expectDedent(['', ' \t a', ' b']).toEqual(['a', ' b']); + }); + + it('dedent do not take empty lines into account', () => { + expectDedent(['a', '', ' b']).toEqual(['a', '', 'b']); + expectDedent(['a', ' ', ' b']).toEqual(['a', '', 'b']); + }); + + it('removes uniform indentation from a string', () => { + const lines = ['', ' Hello,', ' World!', '', ' Yours,', ' GraphQL.']; + expectDedent(lines).toEqual(['Hello,', ' World!', '', 'Yours,', ' GraphQL.']); + }); + + it('removes empty leading and trailing lines', () => { + const lines = ['', '', ' Hello,', ' World!', '', ' Yours,', ' GraphQL.', '', '']; + expectDedent(lines).toEqual(['Hello,', ' World!', '', 'Yours,', ' GraphQL.']); + }); + + it('removes blank leading and trailing lines', () => { + const lines = [ + ' ', + ' ', + ' Hello,', + ' World!', + '', + ' Yours,', + ' GraphQL.', + ' ', + ' ', + ]; + expectDedent(lines).toEqual(['Hello,', ' World!', '', 'Yours,', ' GraphQL.']); + }); + + it('retains indentation from first line', () => { + const lines = [' Hello,', ' World!', '', ' Yours,', ' GraphQL.']; + expectDedent(lines).toEqual([' Hello,', ' World!', '', 'Yours,', ' GraphQL.']); + }); + + it('does not alter trailing spaces', () => { + const lines = [ + ' ', + ' Hello, ', + ' World! ', + ' ', + ' Yours, ', + ' GraphQL. ', + ' ', + ]; + expectDedent(lines).toEqual(['Hello, ', ' World! ', ' ', 'Yours, ', ' GraphQL. ']); + }); +}); + +describe('isPrintableAsBlockString', () => { + function expectPrintable(str: string) { + return expect(isPrintableAsBlockString(str)).toEqual(true); + } + + function expectNonPrintable(str: string) { + return expect(isPrintableAsBlockString(str)).toEqual(false); + } + + it('accepts valid strings', () => { + expectPrintable(''); + expectPrintable(' a'); + expectPrintable('\t"\n"'); + expectNonPrintable('\t"\n \n\t"'); + }); + + it('rejects strings with only whitespace', () => { + expectNonPrintable(' '); + expectNonPrintable('\t'); + expectNonPrintable('\t '); + expectNonPrintable(' \t'); + }); + + it('rejects strings with non-printable characters', () => { + expectNonPrintable('\x00'); + expectNonPrintable('a\x00b'); + }); + + it('rejects strings with only empty lines', () => { + expectNonPrintable('\n'); + expectNonPrintable('\n\n'); + expectNonPrintable('\n\n\n'); + expectNonPrintable(' \n \n'); + expectNonPrintable('\t\n\t\t\n'); + }); + + it('rejects strings with carriage return', () => { + expectNonPrintable('\r'); + expectNonPrintable('\n\r'); + expectNonPrintable('\r\n'); + expectNonPrintable('a\rb'); + }); + + it('rejects strings with leading empty lines', () => { + expectNonPrintable('\na'); + expectNonPrintable(' \na'); + expectNonPrintable('\t\na'); + expectNonPrintable('\n\na'); + }); + + it('rejects strings with trailing empty lines', () => { + expectNonPrintable('a\n'); + expectNonPrintable('a\n '); + expectNonPrintable('a\n\t'); + expectNonPrintable('a\n\n'); + }); +}); + +describe('printBlockString', () => { + function expectBlockString(str: string) { + return { + toEqual(expected: string | { readable: string; minimize: string }) { + const { readable, minimize } = + typeof expected === 'string' ? { readable: expected, minimize: expected } : expected; + + expect(printBlockString(str)).toEqual(readable); + expect(printBlockString(str, { minimize: true })).toEqual(minimize); + }, + }; + } + + it('does not escape characters', () => { + const str = '" \\ / \b \f \n \r \t'; + expectBlockString(str).toEqual({ + readable: '"""\n' + str + '\n"""', + minimize: '"""\n' + str + '"""', + }); + }); + + it('by default print block strings as single line', () => { + const str = 'one liner'; + expectBlockString(str).toEqual('"""one liner"""'); + }); + + it('by default print block strings ending with triple quotation as multi-line', () => { + const str = 'triple quotation """'; + expectBlockString(str).toEqual({ + readable: '"""\ntriple quotation \\"""\n"""', + minimize: '"""triple quotation \\""""""', + }); + }); + + it('correctly prints single-line with leading space', () => { + const str = ' space-led string'; + expectBlockString(str).toEqual('""" space-led string"""'); + }); + + it('correctly prints single-line with leading space and trailing quotation', () => { + const str = ' space-led value "quoted string"'; + expectBlockString(str).toEqual('""" space-led value "quoted string"\n"""'); + }); + + it('correctly prints single-line with trailing backslash', () => { + const str = 'backslash \\'; + expectBlockString(str).toEqual({ + readable: '"""\nbackslash \\\n"""', + minimize: '"""backslash \\\n"""', + }); + }); + + it('correctly prints multi-line with internal indent', () => { + const str = 'no indent\n with indent'; + expectBlockString(str).toEqual({ + readable: '"""\nno indent\n with indent\n"""', + minimize: '"""\nno indent\n with indent"""', + }); + }); + + it('correctly prints string with a first line indentation', () => { + const str = joinLines(' first ', ' line ', 'indentation', ' string'); + + expectBlockString(str).toEqual({ + readable: joinLines('"""', ' first ', ' line ', 'indentation', ' string', '"""'), + minimize: joinLines('""" first ', ' line ', 'indentation', ' string"""'), + }); + }); +}); diff --git a/packages/graphql/src/language/__tests__/lexer-test.ts b/packages/graphql/src/language/__tests__/lexer-test.ts new file mode 100644 index 00000000000..1634fe871fa --- /dev/null +++ b/packages/graphql/src/language/__tests__/lexer-test.ts @@ -0,0 +1,1170 @@ +import { dedent } from '../../__testUtils__/dedent.js'; +import { expectToThrowJSON } from '../../__testUtils__/expectJSON.js'; + +import { inspect } from '../../jsutils/inspect.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { Token } from '../ast.js'; +import { isPunctuatorTokenKind, Lexer } from '../lexer.js'; +import { Source } from '../source.js'; +import { TokenKind } from '../tokenKind.js'; + +function lexOne(str: string) { + const lexer = new Lexer(new Source(str)); + return lexer.advance(); +} + +function lexSecond(str: string) { + const lexer = new Lexer(new Source(str)); + lexer.advance(); + return lexer.advance(); +} + +function expectSyntaxError(text: string) { + return expectToThrowJSON(() => lexSecond(text)); +} + +describe('Lexer', () => { + it('ignores BOM header', () => { + expect(lexOne('\uFEFF foo')).toMatchObject({ + kind: TokenKind.NAME, + start: 2, + end: 5, + value: 'foo', + }); + }); + + it('tracks line breaks', () => { + expect(lexOne('foo')).toMatchObject({ + kind: TokenKind.NAME, + start: 0, + end: 3, + line: 1, + column: 1, + value: 'foo', + }); + expect(lexOne('\nfoo')).toMatchObject({ + kind: TokenKind.NAME, + start: 1, + end: 4, + line: 2, + column: 1, + value: 'foo', + }); + expect(lexOne('\rfoo')).toMatchObject({ + kind: TokenKind.NAME, + start: 1, + end: 4, + line: 2, + column: 1, + value: 'foo', + }); + expect(lexOne('\r\nfoo')).toMatchObject({ + kind: TokenKind.NAME, + start: 2, + end: 5, + line: 2, + column: 1, + value: 'foo', + }); + expect(lexOne('\n\rfoo')).toMatchObject({ + kind: TokenKind.NAME, + start: 2, + end: 5, + line: 3, + column: 1, + value: 'foo', + }); + expect(lexOne('\r\r\n\nfoo')).toMatchObject({ + kind: TokenKind.NAME, + start: 4, + end: 7, + line: 4, + column: 1, + value: 'foo', + }); + expect(lexOne('\n\n\r\rfoo')).toMatchObject({ + kind: TokenKind.NAME, + start: 4, + end: 7, + line: 5, + column: 1, + value: 'foo', + }); + }); + + it('records line and column', () => { + expect(lexOne('\n \r\n \r foo\n')).toMatchObject({ + kind: TokenKind.NAME, + start: 8, + end: 11, + line: 4, + column: 3, + value: 'foo', + }); + }); + + it('can be Object.toStringified, JSON.stringified, or jsutils.inspected', () => { + const lexer = new Lexer(new Source('foo')); + const token = lexer.advance(); + + expect(Object.prototype.toString.call(lexer)).toEqual('[object Lexer]'); + + expect(Object.prototype.toString.call(token)).toEqual('[object Token]'); + expect(JSON.stringify(token)).toEqual('{"kind":"Name","value":"foo","line":1,"column":1}'); + expect(inspect(token)).toEqual('{ kind: "Name", value: "foo", line: 1, column: 1 }'); + }); + + it('skips whitespace and comments', () => { + expect( + lexOne(` + + foo + + +`) + ).toMatchObject({ + kind: TokenKind.NAME, + start: 6, + end: 9, + value: 'foo', + }); + + expect(lexOne('\t\tfoo\t\t')).toMatchObject({ + kind: TokenKind.NAME, + start: 2, + end: 5, + value: 'foo', + }); + + expect( + lexOne(` + #comment + foo#comment +`) + ).toMatchObject({ + kind: TokenKind.NAME, + start: 18, + end: 21, + value: 'foo', + }); + + expect(lexOne(',,,foo,,,')).toMatchObject({ + kind: TokenKind.NAME, + start: 3, + end: 6, + value: 'foo', + }); + }); + + it('errors respect whitespace', () => { + let caughtError; + try { + lexOne(['', '', ' ~', ''].join('\n')); + } catch (error) { + caughtError = error; + } + expect(String(caughtError)).toEqual(dedent` + Syntax Error: Unexpected character: "~". + + GraphQL request:3:2 + 2 | + 3 | ~ + | ^ + 4 | + `); + }); + + it('updates line numbers in error for file context', () => { + let caughtError; + try { + const str = ['', '', ' ~', ''].join('\n'); + const source = new Source(str, 'foo.js', { line: 11, column: 12 }); + new Lexer(source).advance(); + } catch (error) { + caughtError = error; + } + expect(String(caughtError)).toEqual(dedent` + Syntax Error: Unexpected character: "~". + + foo.js:13:6 + 12 | + 13 | ~ + | ^ + 14 | + `); + }); + + it('updates column numbers in error for file context', () => { + let caughtError; + try { + const source = new Source('~', 'foo.js', { line: 1, column: 5 }); + new Lexer(source).advance(); + } catch (error) { + caughtError = error; + } + expect(String(caughtError)).toEqual(dedent` + Syntax Error: Unexpected character: "~". + + foo.js:1:5 + 1 | ~ + | ^ + `); + }); + + it('lexes strings', () => { + expect(lexOne('""')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 2, + value: '', + }); + + expect(lexOne('"simple"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 8, + value: 'simple', + }); + + expect(lexOne('" white space "')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 15, + value: ' white space ', + }); + + expect(lexOne('"quote \\""')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 10, + value: 'quote "', + }); + + expect(lexOne('"escaped \\n\\r\\b\\t\\f"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 20, + value: 'escaped \n\r\b\t\f', + }); + + expect(lexOne('"slashes \\\\ \\/"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 15, + value: 'slashes \\ /', + }); + + expect(lexOne('"unescaped unicode outside BMP \u{1f600}"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 34, + value: 'unescaped unicode outside BMP \u{1f600}', + }); + + expect(lexOne('"unescaped maximal unicode outside BMP \u{10ffff}"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 42, + value: 'unescaped maximal unicode outside BMP \u{10ffff}', + }); + + expect(lexOne('"unicode \\u1234\\u5678\\u90AB\\uCDEF"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 34, + value: 'unicode \u1234\u5678\u90AB\uCDEF', + }); + + expect(lexOne('"unicode \\u{1234}\\u{5678}\\u{90AB}\\u{CDEF}"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 42, + value: 'unicode \u1234\u5678\u90AB\uCDEF', + }); + + expect(lexOne('"string with unicode escape outside BMP \\u{1F600}"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 50, + value: 'string with unicode escape outside BMP \u{1f600}', + }); + + expect(lexOne('"string with minimal unicode escape \\u{0}"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 42, + value: 'string with minimal unicode escape \u{0}', + }); + + expect(lexOne('"string with maximal unicode escape \\u{10FFFF}"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 47, + value: 'string with maximal unicode escape \u{10FFFF}', + }); + + expect(lexOne('"string with maximal minimal unicode escape \\u{00000000}"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 57, + value: 'string with maximal minimal unicode escape \u{0}', + }); + + expect(lexOne('"string with unicode surrogate pair escape \\uD83D\\uDE00"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 56, + value: 'string with unicode surrogate pair escape \u{1f600}', + }); + + expect(lexOne('"string with minimal surrogate pair escape \\uD800\\uDC00"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 56, + value: 'string with minimal surrogate pair escape \u{10000}', + }); + + expect(lexOne('"string with maximal surrogate pair escape \\uDBFF\\uDFFF"')).toMatchObject({ + kind: TokenKind.STRING, + start: 0, + end: 56, + value: 'string with maximal surrogate pair escape \u{10FFFF}', + }); + }); + + it('lex reports useful string errors', () => { + expectSyntaxError('"').toMatchObject({ + message: 'Syntax Error: Unterminated string.', + locations: [{ line: 1, column: 2 }], + }); + + expectSyntaxError('"""').toMatchObject({ + message: 'Syntax Error: Unterminated string.', + locations: [{ line: 1, column: 4 }], + }); + + expectSyntaxError('""""').toMatchObject({ + message: 'Syntax Error: Unterminated string.', + locations: [{ line: 1, column: 5 }], + }); + + expectSyntaxError('"no end quote').toMatchObject({ + message: 'Syntax Error: Unterminated string.', + locations: [{ line: 1, column: 14 }], + }); + + expectSyntaxError("'single quotes'").toMatchObject({ + message: 'Syntax Error: Unexpected single quote character (\'), did you mean to use a double quote (")?', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('"bad surrogate \uDEAD"').toMatchObject({ + message: 'Syntax Error: Invalid character within String: U+DEAD.', + locations: [{ line: 1, column: 16 }], + }); + + expectSyntaxError('"bad high surrogate pair \uDEAD\uDEAD"').toMatchObject({ + message: 'Syntax Error: Invalid character within String: U+DEAD.', + locations: [{ line: 1, column: 26 }], + }); + + expectSyntaxError('"bad low surrogate pair \uD800\uD800"').toMatchObject({ + message: 'Syntax Error: Invalid character within String: U+D800.', + locations: [{ line: 1, column: 25 }], + }); + + expectSyntaxError('"multi\nline"').toMatchObject({ + message: 'Syntax Error: Unterminated string.', + locations: [{ line: 1, column: 7 }], + }); + + expectSyntaxError('"multi\rline"').toMatchObject({ + message: 'Syntax Error: Unterminated string.', + locations: [{ line: 1, column: 7 }], + }); + + expectSyntaxError('"bad \\z esc"').toMatchObject({ + message: 'Syntax Error: Invalid character escape sequence: "\\z".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\x esc"').toMatchObject({ + message: 'Syntax Error: Invalid character escape sequence: "\\x".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\u1 esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u1 es".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\u0XX1 esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u0XX1".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\uXXXX esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\uXXXX".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\uFXXX esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\uFXXX".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\uXXXF esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\uXXXF".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\u{} esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{}".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\u{FXXX} esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{FX".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\u{FFFF esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{FFFF ".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"bad \\u{FFFF"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{FFFF"".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('"too high \\u{110000} esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{110000}".', + locations: [{ line: 1, column: 11 }], + }); + + expectSyntaxError('"way too high \\u{12345678} esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{12345678}".', + locations: [{ line: 1, column: 15 }], + }); + + expectSyntaxError('"too long \\u{000000000} esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{000000000".', + locations: [{ line: 1, column: 11 }], + }); + + expectSyntaxError('"bad surrogate \\uDEAD esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\uDEAD".', + locations: [{ line: 1, column: 16 }], + }); + + expectSyntaxError('"bad surrogate \\u{DEAD} esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{DEAD}".', + locations: [{ line: 1, column: 16 }], + }); + + expectSyntaxError('"cannot use braces for surrogate pair \\u{D83D}\\u{DE00} esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\u{D83D}".', + locations: [{ line: 1, column: 39 }], + }); + + expectSyntaxError('"bad high surrogate pair \\uDEAD\\uDEAD esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\uDEAD".', + locations: [{ line: 1, column: 26 }], + }); + + expectSyntaxError('"bad low surrogate pair \\uD800\\uD800 esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\uD800".', + locations: [{ line: 1, column: 25 }], + }); + + expectSyntaxError('"cannot escape half a pair \uD83D\\uDE00 esc"').toMatchObject({ + message: 'Syntax Error: Invalid character within String: U+D83D.', + locations: [{ line: 1, column: 28 }], + }); + + expectSyntaxError('"cannot escape half a pair \\uD83D\uDE00 esc"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\uD83D".', + locations: [{ line: 1, column: 28 }], + }); + + expectSyntaxError('"bad \\uD83D\\not an escape"').toMatchObject({ + message: 'Syntax Error: Invalid Unicode escape sequence: "\\uD83D".', + locations: [{ line: 1, column: 6 }], + }); + }); + + it('lexes block strings', () => { + expect(lexOne('""""""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 6, + line: 1, + column: 1, + value: '', + }); + + expect(lexOne('"""simple"""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 12, + line: 1, + column: 1, + value: 'simple', + }); + + expect(lexOne('""" white space """')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 19, + line: 1, + column: 1, + value: ' white space ', + }); + + expect(lexOne('"""contains " quote"""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 22, + line: 1, + column: 1, + value: 'contains " quote', + }); + + expect(lexOne('"""contains \\""" triple quote"""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 32, + line: 1, + column: 1, + value: 'contains """ triple quote', + }); + + expect(lexOne('"""multi\nline"""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 16, + line: 1, + column: 1, + value: 'multi\nline', + }); + + expect(lexOne('"""multi\rline\r\nnormalized"""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 28, + line: 1, + column: 1, + value: 'multi\nline\nnormalized', + }); + + expect(lexOne('"""unescaped \\n\\r\\b\\t\\f\\u1234"""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 32, + line: 1, + column: 1, + value: 'unescaped \\n\\r\\b\\t\\f\\u1234', + }); + + expect(lexOne('"""unescaped unicode outside BMP \u{1f600}"""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 38, + line: 1, + column: 1, + value: 'unescaped unicode outside BMP \u{1f600}', + }); + + expect(lexOne('"""slashes \\\\ \\/"""')).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 19, + line: 1, + column: 1, + value: 'slashes \\\\ \\/', + }); + + expect( + lexOne(`""" + + spans + multiple + lines + + """`) + ).toMatchObject({ + kind: TokenKind.BLOCK_STRING, + start: 0, + end: 68, + line: 1, + column: 1, + value: 'spans\n multiple\n lines', + }); + }); + + it('advance line after lexing multiline block string', () => { + expect( + lexSecond(`""" + + spans + multiple + lines + + \n """ second_token`) + ).toMatchObject({ + kind: TokenKind.NAME, + start: 71, + end: 83, + line: 8, + column: 6, + value: 'second_token', + }); + + expect( + lexSecond(['""" \n', 'spans \r\n', 'multiple \n\r', 'lines \n\n', '"""\n second_token'].join('')) + ).toMatchObject({ + kind: TokenKind.NAME, + start: 37, + end: 49, + line: 8, + column: 2, + value: 'second_token', + }); + }); + + it('lex reports useful block string errors', () => { + expectSyntaxError('"""').toMatchObject({ + message: 'Syntax Error: Unterminated string.', + locations: [{ line: 1, column: 4 }], + }); + + expectSyntaxError('"""no end quote').toMatchObject({ + message: 'Syntax Error: Unterminated string.', + locations: [{ line: 1, column: 16 }], + }); + + expectSyntaxError('"""contains invalid surrogate \uDEAD"""').toMatchObject({ + message: 'Syntax Error: Invalid character within String: U+DEAD.', + locations: [{ line: 1, column: 31 }], + }); + }); + + it('lexes numbers', () => { + expect(lexOne('4')).toMatchObject({ + kind: TokenKind.INT, + start: 0, + end: 1, + value: '4', + }); + + expect(lexOne('4.123')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 5, + value: '4.123', + }); + + expect(lexOne('-4')).toMatchObject({ + kind: TokenKind.INT, + start: 0, + end: 2, + value: '-4', + }); + + expect(lexOne('9')).toMatchObject({ + kind: TokenKind.INT, + start: 0, + end: 1, + value: '9', + }); + + expect(lexOne('0')).toMatchObject({ + kind: TokenKind.INT, + start: 0, + end: 1, + value: '0', + }); + + expect(lexOne('-4.123')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 6, + value: '-4.123', + }); + + expect(lexOne('0.123')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 5, + value: '0.123', + }); + + expect(lexOne('123e4')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 5, + value: '123e4', + }); + + expect(lexOne('123E4')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 5, + value: '123E4', + }); + + expect(lexOne('123e-4')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 6, + value: '123e-4', + }); + + expect(lexOne('123e+4')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 6, + value: '123e+4', + }); + + expect(lexOne('-1.123e4')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 8, + value: '-1.123e4', + }); + + expect(lexOne('-1.123E4')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 8, + value: '-1.123E4', + }); + + expect(lexOne('-1.123e-4')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 9, + value: '-1.123e-4', + }); + + expect(lexOne('-1.123e+4')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 9, + value: '-1.123e+4', + }); + + expect(lexOne('-1.123e4567')).toMatchObject({ + kind: TokenKind.FLOAT, + start: 0, + end: 11, + value: '-1.123e4567', + }); + }); + + it('lex reports useful number errors', () => { + expectSyntaxError('00').toMatchObject({ + message: 'Syntax Error: Invalid number, unexpected digit after 0: "0".', + locations: [{ line: 1, column: 2 }], + }); + + expectSyntaxError('01').toMatchObject({ + message: 'Syntax Error: Invalid number, unexpected digit after 0: "1".', + locations: [{ line: 1, column: 2 }], + }); + + expectSyntaxError('01.23').toMatchObject({ + message: 'Syntax Error: Invalid number, unexpected digit after 0: "1".', + locations: [{ line: 1, column: 2 }], + }); + + expectSyntaxError('+1').toMatchObject({ + message: 'Syntax Error: Unexpected character: "+".', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('1.').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: .', + locations: [{ line: 1, column: 3 }], + }); + + expectSyntaxError('1e').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: .', + locations: [{ line: 1, column: 3 }], + }); + + expectSyntaxError('1E').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: .', + locations: [{ line: 1, column: 3 }], + }); + + expectSyntaxError('1.e1').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "e".', + locations: [{ line: 1, column: 3 }], + }); + + expectSyntaxError('.123').toMatchObject({ + message: 'Syntax Error: Unexpected character: ".".', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('1.A').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "A".', + locations: [{ line: 1, column: 3 }], + }); + + expectSyntaxError('-A').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "A".', + locations: [{ line: 1, column: 2 }], + }); + + expectSyntaxError('1.0e').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: .', + locations: [{ line: 1, column: 5 }], + }); + + expectSyntaxError('1.0eA').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "A".', + locations: [{ line: 1, column: 5 }], + }); + + expectSyntaxError('1.0e"').toMatchObject({ + message: "Syntax Error: Invalid number, expected digit but got: '\"'.", + locations: [{ line: 1, column: 5 }], + }); + + expectSyntaxError('1.2e3e').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "e".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('1.2e3.4').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: ".".', + locations: [{ line: 1, column: 6 }], + }); + + expectSyntaxError('1.23.4').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: ".".', + locations: [{ line: 1, column: 5 }], + }); + }); + + it('lex does not allow name-start after a number', () => { + expectSyntaxError('0xF1').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "x".', + locations: [{ line: 1, column: 2 }], + }); + expectSyntaxError('0b10').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "b".', + locations: [{ line: 1, column: 2 }], + }); + expectSyntaxError('123abc').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "a".', + locations: [{ line: 1, column: 4 }], + }); + expectSyntaxError('1_234').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "_".', + locations: [{ line: 1, column: 2 }], + }); + expectSyntaxError('1\u00DF').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+00DF.', + locations: [{ line: 1, column: 2 }], + }); + expectSyntaxError('1.23f').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "f".', + locations: [{ line: 1, column: 5 }], + }); + expectSyntaxError('1.234_5').toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "_".', + locations: [{ line: 1, column: 6 }], + }); + }); + + it('lexes punctuation', () => { + expect(lexOne('!')).toMatchObject({ + kind: TokenKind.BANG, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('?')).toMatchObject({ + kind: TokenKind.QUESTION_MARK, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('$')).toMatchObject({ + kind: TokenKind.DOLLAR, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('(')).toMatchObject({ + kind: TokenKind.PAREN_L, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne(')')).toMatchObject({ + kind: TokenKind.PAREN_R, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('...')).toMatchObject({ + kind: TokenKind.SPREAD, + start: 0, + end: 3, + value: undefined, + }); + + expect(lexOne(':')).toMatchObject({ + kind: TokenKind.COLON, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('=')).toMatchObject({ + kind: TokenKind.EQUALS, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('@')).toMatchObject({ + kind: TokenKind.AT, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('[')).toMatchObject({ + kind: TokenKind.BRACKET_L, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne(']')).toMatchObject({ + kind: TokenKind.BRACKET_R, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('{')).toMatchObject({ + kind: TokenKind.BRACE_L, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('|')).toMatchObject({ + kind: TokenKind.PIPE, + start: 0, + end: 1, + value: undefined, + }); + + expect(lexOne('}')).toMatchObject({ + kind: TokenKind.BRACE_R, + start: 0, + end: 1, + value: undefined, + }); + }); + + it('lex reports useful unknown character error', () => { + expectSyntaxError('..').toMatchObject({ + message: 'Syntax Error: Unexpected character: ".".', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('~').toMatchObject({ + message: 'Syntax Error: Unexpected character: "~".', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\x00').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+0000.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\b').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+0008.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\u00AA').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+00AA.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\u0AAA').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+0AAA.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\u203B').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+203B.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\u{1f600}').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+1F600.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\uD83D\uDE00').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+1F600.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\uD800\uDC00').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+10000.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\uDBFF\uDFFF').toMatchObject({ + message: 'Syntax Error: Unexpected character: U+10FFFF.', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('\uDEAD').toMatchObject({ + message: 'Syntax Error: Invalid character: U+DEAD.', + locations: [{ line: 1, column: 1 }], + }); + }); + + it('lex reports useful information for dashes in names', () => { + const source = new Source('a-b'); + const lexer = new Lexer(source); + const firstToken = lexer.advance(); + expect(firstToken).toMatchObject({ + kind: TokenKind.NAME, + start: 0, + end: 1, + value: 'a', + }); + + expect(() => lexer.advance()).toThrow(GraphQLError); + expectToThrowJSON(() => lexer.advance()).toMatchObject({ + message: 'Syntax Error: Invalid number, expected digit but got: "b".', + locations: [{ line: 1, column: 3 }], + }); + }); + + it('produces double linked list of tokens, including comments', () => { + const source = new Source(` + { + #comment + field + } + `); + + const lexer = new Lexer(source); + const startToken = lexer.token; + let endToken; + do { + endToken = lexer.advance(); + // Lexer advances over ignored comment tokens to make writing parsers + // easier, but will include them in the linked list result. + expect(endToken.kind).not.toEqual(TokenKind.COMMENT); + } while (endToken.kind !== TokenKind.EOF); + + expect(startToken.prev).toBeFalsy(); + expect(endToken.next).toBeFalsy(); + + const tokens = []; + for (let tok: Token | null = startToken; tok; tok = tok.next) { + if (tokens.length) { + // Tokens are double-linked, prev should point to last seen token. + expect(tok.prev).toMatchObject(tokens[tokens.length - 1]); + } + tokens.push(tok); + } + + expect(tokens.map(tok => tok.kind)).toMatchObject([ + TokenKind.SOF, + TokenKind.BRACE_L, + TokenKind.COMMENT, + TokenKind.NAME, + TokenKind.BRACE_R, + TokenKind.EOF, + ]); + }); + + it('lexes comments', () => { + expect(lexOne('# Comment').prev).toMatchObject({ + kind: TokenKind.COMMENT, + start: 0, + end: 9, + value: ' Comment', + }); + expect(lexOne('# Comment\nAnother line').prev).toMatchObject({ + kind: TokenKind.COMMENT, + start: 0, + end: 9, + value: ' Comment', + }); + expect(lexOne('# Comment\r\nAnother line').prev).toMatchObject({ + kind: TokenKind.COMMENT, + start: 0, + end: 9, + value: ' Comment', + }); + expect(lexOne('# Comment \u{1f600}').prev).toMatchObject({ + kind: TokenKind.COMMENT, + start: 0, + end: 12, + value: ' Comment \u{1f600}', + }); + expectSyntaxError('# Invalid surrogate \uDEAD').toMatchObject({ + message: 'Syntax Error: Invalid character: U+DEAD.', + locations: [{ line: 1, column: 21 }], + }); + }); +}); + +describe('isPunctuatorTokenKind', () => { + function isPunctuatorToken(text: string) { + return isPunctuatorTokenKind(lexOne(text).kind); + } + + it('returns true for punctuator tokens', () => { + expect(isPunctuatorToken('!')).toEqual(true); + expect(isPunctuatorToken('?')).toEqual(true); + expect(isPunctuatorToken('$')).toEqual(true); + expect(isPunctuatorToken('&')).toEqual(true); + expect(isPunctuatorToken('(')).toEqual(true); + expect(isPunctuatorToken(')')).toEqual(true); + expect(isPunctuatorToken('...')).toEqual(true); + expect(isPunctuatorToken(':')).toEqual(true); + expect(isPunctuatorToken('=')).toEqual(true); + expect(isPunctuatorToken('@')).toEqual(true); + expect(isPunctuatorToken('[')).toEqual(true); + expect(isPunctuatorToken(']')).toEqual(true); + expect(isPunctuatorToken('{')).toEqual(true); + expect(isPunctuatorToken('|')).toEqual(true); + expect(isPunctuatorToken('}')).toEqual(true); + }); + + it('returns false for non-punctuator tokens', () => { + expect(isPunctuatorToken('')).toEqual(false); + expect(isPunctuatorToken('name')).toEqual(false); + expect(isPunctuatorToken('1')).toEqual(false); + expect(isPunctuatorToken('3.14')).toEqual(false); + expect(isPunctuatorToken('"str"')).toEqual(false); + expect(isPunctuatorToken('"""str"""')).toEqual(false); + }); +}); diff --git a/packages/graphql/src/language/__tests__/parser-test.ts b/packages/graphql/src/language/__tests__/parser-test.ts new file mode 100644 index 00000000000..4788c0ffa7f --- /dev/null +++ b/packages/graphql/src/language/__tests__/parser-test.ts @@ -0,0 +1,781 @@ +import { dedent } from '../../__testUtils__/dedent.js'; +import { expectJSON, expectToThrowJSON } from '../../__testUtils__/expectJSON.js'; +import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js'; + +import { inspect } from '../../jsutils/inspect.js'; + +import { Kind } from '../kinds.js'; +import { parse, parseConstValue, parseType, parseValue } from '../parser.js'; +import { Source } from '../source.js'; +import { TokenKind } from '../tokenKind.js'; + +function parseCCN(source: string) { + return parse(source, { experimentalClientControlledNullability: true }); +} + +function expectSyntaxError(text: string) { + return expectToThrowJSON(() => parse(text)); +} + +describe('Parser', () => { + it('parse provides useful errors', () => { + let caughtError; + try { + parse('{'); + } catch (error) { + caughtError = error; + } + + expect(caughtError).toMatchObject({ + message: 'Syntax Error: Expected Name, found .', + positions: [1], + locations: [{ line: 1, column: 2 }], + }); + + expect(String(caughtError)).toEqual(dedent` + Syntax Error: Expected Name, found . + + GraphQL request:1:2 + 1 | { + | ^ + `); + + expectSyntaxError(` + { ...MissingOn } + fragment MissingOn Type + `).toMatchObject({ + message: 'Syntax Error: Expected "on", found Name "Type".', + locations: [{ line: 3, column: 26 }], + }); + + expectSyntaxError('{ field: {} }').toMatchObject({ + message: 'Syntax Error: Expected Name, found "{".', + locations: [{ line: 1, column: 10 }], + }); + + expectSyntaxError('notAnOperation Foo { field }').toMatchObject({ + message: 'Syntax Error: Unexpected Name "notAnOperation".', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('...').toMatchObject({ + message: 'Syntax Error: Unexpected "...".', + locations: [{ line: 1, column: 1 }], + }); + + expectSyntaxError('{ ""').toMatchObject({ + message: 'Syntax Error: Expected Name, found String "".', + locations: [{ line: 1, column: 3 }], + }); + }); + + it('parse provides useful error when using source', () => { + let caughtError; + try { + parse(new Source('query', 'MyQuery.graphql')); + } catch (error) { + caughtError = error; + } + expect(String(caughtError)).toEqual(dedent` + Syntax Error: Expected "{", found . + + MyQuery.graphql:1:6 + 1 | query + | ^ + `); + }); + + it('parses variable inline values', () => { + expect(() => parse('{ field(complex: { a: { b: [ $var ] } }) }')).not.toThrow(); + }); + + it('parses constant default values', () => { + expectSyntaxError('query Foo($x: Complex = { a: { b: [ $var ] } }) { field }').toMatchObject({ + message: 'Syntax Error: Unexpected variable "$var" in constant value.', + locations: [{ line: 1, column: 37 }], + }); + }); + + it('parses variable definition directives', () => { + expect(() => parse('query Foo($x: Boolean = false @bar) { field }')).not.toThrow(); + }); + + it('does not accept fragments named "on"', () => { + expectSyntaxError('fragment on on on { on }').toMatchObject({ + message: 'Syntax Error: Unexpected Name "on".', + locations: [{ line: 1, column: 10 }], + }); + }); + + it('does not accept fragments spread of "on"', () => { + expectSyntaxError('{ ...on }').toMatchObject({ + message: 'Syntax Error: Expected Name, found "}".', + locations: [{ line: 1, column: 9 }], + }); + }); + + it('does not allow "true", "false", or "null" as enum value', () => { + expectSyntaxError('enum Test { VALID, true }').toMatchObject({ + message: 'Syntax Error: Name "true" is reserved and cannot be used for an enum value.', + locations: [{ line: 1, column: 20 }], + }); + + expectSyntaxError('enum Test { VALID, false }').toMatchObject({ + message: 'Syntax Error: Name "false" is reserved and cannot be used for an enum value.', + locations: [{ line: 1, column: 20 }], + }); + + expectSyntaxError('enum Test { VALID, null }').toMatchObject({ + message: 'Syntax Error: Name "null" is reserved and cannot be used for an enum value.', + locations: [{ line: 1, column: 20 }], + }); + }); + + it('parses multi-byte characters', () => { + // Note: \u0A0A could be naively interpreted as two line-feed chars. + const ast = parse(` + # This comment has a \u0A0A multi-byte character. + { field(arg: "Has a \u0A0A multi-byte character.") } + `); + + expect(ast).toHaveProperty( + 'definitions[0].selectionSet.selections[0].arguments[0].value.value', + 'Has a \u0A0A multi-byte character.' + ); + }); + + it('parses kitchen sink', () => { + expect(() => parseCCN(kitchenSinkQuery)).not.toThrow(); + }); + + it('allows non-keywords anywhere a Name is allowed', () => { + const nonKeywords = ['on', 'fragment', 'query', 'mutation', 'subscription', 'true', 'false']; + for (const keyword of nonKeywords) { + // You can't define or reference a fragment named `on`. + const fragmentName = keyword !== 'on' ? keyword : 'a'; + const document = ` + query ${keyword} { + ... ${fragmentName} + ... on ${keyword} { field } + } + fragment ${fragmentName} on Type { + ${keyword}(${keyword}: $${keyword}) + @${keyword}(${keyword}: ${keyword}) + } + `; + + expect(() => parse(document)).not.toThrow(); + } + }); + + it('parses anonymous mutation operations', () => { + expect(() => + parse(` + mutation { + mutationField + } + `) + ).not.toThrow(); + }); + + it('parses anonymous subscription operations', () => { + expect(() => + parse(` + subscription { + subscriptionField + } + `) + ).not.toThrow(); + }); + + it('parses named mutation operations', () => { + expect(() => + parse(` + mutation Foo { + mutationField + } + `) + ).not.toThrow(); + }); + + it('parses named subscription operations', () => { + expect(() => + parse(` + subscription Foo { + subscriptionField + } + `) + ).not.toThrow(); + }); + + it('parses required field', () => { + const result = parseCCN('{ requiredField! }'); + + expectJSON(result).toDeepNestedProperty('definitions[0].selectionSet.selections[0].nullabilityAssertion', { + kind: Kind.NON_NULL_ASSERTION, + loc: { start: 15, end: 16 }, + nullabilityAssertion: undefined, + }); + }); + + it('parses optional field', () => { + expect(() => parseCCN('{ optionalField? }')).not.toThrow(); + }); + + it('does not parse field with multiple designators', () => { + expect(() => parseCCN('{ optionalField?! }')).toThrow('Syntax Error: Expected Name, found "!".'); + + expect(() => parseCCN('{ optionalField!? }')).toThrow('Syntax Error: Expected Name, found "?".'); + }); + + it('parses required with alias', () => { + expect(() => parseCCN('{ requiredField: field! }')).not.toThrow(); + }); + + it('parses optional with alias', () => { + expect(() => parseCCN('{ requiredField: field? }')).not.toThrow(); + }); + + it('does not parse aliased field with bang on left of colon', () => { + expect(() => parseCCN('{ requiredField!: field }')).toThrow(); + }); + + it('does not parse aliased field with question mark on left of colon', () => { + expect(() => parseCCN('{ requiredField?: field }')).toThrow(); + }); + + it('does not parse aliased field with bang on left and right of colon', () => { + expect(() => parseCCN('{ requiredField!: field! }')).toThrow(); + }); + + it('does not parse aliased field with question mark on left and right of colon', () => { + expect(() => parseCCN('{ requiredField?: field? }')).toThrow(); + }); + + it('does not parse designator on query', () => { + expect(() => parseCCN('query? { field }')).toThrow(); + }); + + it('parses required within fragment', () => { + expect(() => parseCCN('fragment MyFragment on Query { field! }')).not.toThrow(); + }); + + it('parses optional within fragment', () => { + expect(() => parseCCN('fragment MyFragment on Query { field? }')).not.toThrow(); + }); + + it('parses field with required list elements', () => { + const result = parseCCN('{ field[!] }'); + + expectJSON(result).toDeepNestedProperty('definitions[0].selectionSet.selections[0].nullabilityAssertion', { + kind: Kind.LIST_NULLABILITY_OPERATOR, + loc: { start: 7, end: 10 }, + nullabilityAssertion: { + kind: Kind.NON_NULL_ASSERTION, + loc: { start: 8, end: 9 }, + nullabilityAssertion: undefined, + }, + }); + }); + + it('parses field with optional list elements', () => { + const result = parseCCN('{ field[?] }'); + + expectJSON(result).toDeepNestedProperty('definitions[0].selectionSet.selections[0].nullabilityAssertion', { + kind: Kind.LIST_NULLABILITY_OPERATOR, + loc: { start: 7, end: 10 }, + nullabilityAssertion: { + kind: Kind.ERROR_BOUNDARY, + loc: { start: 8, end: 9 }, + nullabilityAssertion: undefined, + }, + }); + }); + + it('parses field with required list', () => { + const result = parseCCN('{ field[]! }'); + + expectJSON(result).toDeepNestedProperty('definitions[0].selectionSet.selections[0].nullabilityAssertion', { + kind: Kind.NON_NULL_ASSERTION, + loc: { start: 7, end: 10 }, + nullabilityAssertion: { + kind: Kind.LIST_NULLABILITY_OPERATOR, + loc: { start: 7, end: 9 }, + nullabilityAssertion: undefined, + }, + }); + }); + + it('parses field with optional list', () => { + const result = parseCCN('{ field[]? }'); + + expectJSON(result).toDeepNestedProperty('definitions[0].selectionSet.selections[0].nullabilityAssertion', { + kind: Kind.ERROR_BOUNDARY, + loc: { start: 7, end: 10 }, + nullabilityAssertion: { + kind: Kind.LIST_NULLABILITY_OPERATOR, + loc: { start: 7, end: 9 }, + nullabilityAssertion: undefined, + }, + }); + }); + + it('parses multidimensional field with mixed list elements', () => { + const result = parseCCN('{ field[[[?]!]]! }'); + + expectJSON(result).toDeepNestedProperty('definitions[0].selectionSet.selections[0].nullabilityAssertion', { + kind: Kind.NON_NULL_ASSERTION, + loc: { start: 7, end: 16 }, + nullabilityAssertion: { + kind: Kind.LIST_NULLABILITY_OPERATOR, + loc: { start: 7, end: 15 }, + nullabilityAssertion: { + kind: Kind.LIST_NULLABILITY_OPERATOR, + loc: { start: 8, end: 14 }, + nullabilityAssertion: { + kind: Kind.NON_NULL_ASSERTION, + loc: { start: 9, end: 13 }, + nullabilityAssertion: { + kind: Kind.LIST_NULLABILITY_OPERATOR, + loc: { start: 9, end: 12 }, + nullabilityAssertion: { + kind: Kind.ERROR_BOUNDARY, + loc: { start: 10, end: 11 }, + nullabilityAssertion: undefined, + }, + }, + }, + }, + }, + }); + }); + + it('does not parse field with unbalanced brackets', () => { + expect(() => parseCCN('{ field[[] }')).toThrow('Syntax Error: Expected "]", found "}".'); + + expect(() => parseCCN('{ field[]] }')).toThrow('Syntax Error: Expected Name, found "]".'); + + expect(() => parse('{ field] }')).toThrow('Syntax Error: Expected Name, found "]".'); + + expect(() => parseCCN('{ field[ }')).toThrow('Syntax Error: Expected "]", found "}".'); + }); + + it('does not parse field with assorted invalid nullability designators', () => { + expect(() => parseCCN('{ field[][] }')).toThrow('Syntax Error: Expected Name, found "[".'); + + expect(() => parseCCN('{ field[!!] }')).toThrow('Syntax Error: Expected "]", found "!".'); + + expect(() => parseCCN('{ field[]?! }')).toThrow('Syntax Error: Expected Name, found "!".'); + }); + + it('creates ast', () => { + const result = parse(dedent` + { + node(id: 4) { + id, + name + } + } + `); + + expectJSON(result).toDeepEqual({ + kind: Kind.DOCUMENT, + loc: { start: 0, end: 40 }, + definitions: [ + { + kind: Kind.OPERATION_DEFINITION, + loc: { start: 0, end: 40 }, + operation: 'query', + name: undefined, + variableDefinitions: [], + directives: [], + selectionSet: { + kind: Kind.SELECTION_SET, + loc: { start: 0, end: 40 }, + selections: [ + { + kind: Kind.FIELD, + loc: { start: 4, end: 38 }, + alias: undefined, + name: { + kind: Kind.NAME, + loc: { start: 4, end: 8 }, + value: 'node', + }, + arguments: [ + { + kind: Kind.ARGUMENT, + name: { + kind: Kind.NAME, + loc: { start: 9, end: 11 }, + value: 'id', + }, + value: { + kind: Kind.INT, + loc: { start: 13, end: 14 }, + value: '4', + }, + loc: { start: 9, end: 14 }, + }, + ], + nullabilityAssertion: undefined, + directives: [], + selectionSet: { + kind: Kind.SELECTION_SET, + loc: { start: 16, end: 38 }, + selections: [ + { + kind: Kind.FIELD, + loc: { start: 22, end: 24 }, + alias: undefined, + name: { + kind: Kind.NAME, + loc: { start: 22, end: 24 }, + value: 'id', + }, + arguments: [], + nullabilityAssertion: undefined, + directives: [], + selectionSet: undefined, + }, + { + kind: Kind.FIELD, + loc: { start: 30, end: 34 }, + alias: undefined, + name: { + kind: Kind.NAME, + loc: { start: 30, end: 34 }, + value: 'name', + }, + arguments: [], + nullabilityAssertion: undefined, + directives: [], + selectionSet: undefined, + }, + ], + }, + }, + ], + }, + }, + ], + }); + }); + + it('creates ast from nameless query without variables', () => { + const result = parse(dedent` + query { + node { + id + } + } + `); + + expectJSON(result).toDeepEqual({ + kind: Kind.DOCUMENT, + loc: { start: 0, end: 29 }, + definitions: [ + { + kind: Kind.OPERATION_DEFINITION, + loc: { start: 0, end: 29 }, + operation: 'query', + name: undefined, + variableDefinitions: [], + directives: [], + selectionSet: { + kind: Kind.SELECTION_SET, + loc: { start: 6, end: 29 }, + selections: [ + { + kind: Kind.FIELD, + loc: { start: 10, end: 27 }, + alias: undefined, + name: { + kind: Kind.NAME, + loc: { start: 10, end: 14 }, + value: 'node', + }, + arguments: [], + nullabilityAssertion: undefined, + directives: [], + selectionSet: { + kind: Kind.SELECTION_SET, + loc: { start: 15, end: 27 }, + selections: [ + { + kind: Kind.FIELD, + loc: { start: 21, end: 23 }, + alias: undefined, + name: { + kind: Kind.NAME, + loc: { start: 21, end: 23 }, + value: 'id', + }, + arguments: [], + nullabilityAssertion: undefined, + directives: [], + selectionSet: undefined, + }, + ], + }, + }, + ], + }, + }, + ], + }); + }); + + it('allows parsing without source location information', () => { + const result = parse('{ id }', { noLocation: true }); + expect('loc' in result).toEqual(false); + }); + + it('Legacy: allows parsing fragment defined variables', () => { + const document = 'fragment a($v: Boolean = false) on t { f(v: $v) }'; + + expect(() => parse(document, { allowLegacyFragmentVariables: true })).not.toThrow(); + expect(() => parse(document)).toThrow('Syntax Error'); + }); + + it('contains location that can be Object.toStringified, JSON.stringified, or jsutils.inspected', () => { + const { loc } = parse('{ id }'); + + expect(Object.prototype.toString.call(loc)).toEqual('[object Location]'); + expect(JSON.stringify(loc)).toEqual('{"start":0,"end":6}'); + expect(inspect(loc)).toEqual('{ start: 0, end: 6 }'); + }); + + it('contains references to source', () => { + const source = new Source('{ id }'); + const result = parse(source); + + expect(result).toHaveProperty('loc.source', source); + }); + + it('contains references to start and end tokens', () => { + const result = parse('{ id }'); + + expect(result).toHaveProperty('loc.startToken.kind', TokenKind.SOF); + expect(result).toHaveProperty('loc.endToken.kind', TokenKind.EOF); + }); + + describe('parseValue', () => { + it('parses null value', () => { + const result = parseValue('null'); + expectJSON(result).toDeepEqual({ + kind: Kind.NULL, + loc: { start: 0, end: 4 }, + }); + }); + + it('parses list values', () => { + const result = parseValue('[123 "abc"]'); + expectJSON(result).toDeepEqual({ + kind: Kind.LIST, + loc: { start: 0, end: 11 }, + values: [ + { + kind: Kind.INT, + loc: { start: 1, end: 4 }, + value: '123', + }, + { + kind: Kind.STRING, + loc: { start: 5, end: 10 }, + value: 'abc', + block: false, + }, + ], + }); + }); + + it('parses block strings', () => { + const result = parseValue('["""long""" "short"]'); + expectJSON(result).toDeepEqual({ + kind: Kind.LIST, + loc: { start: 0, end: 20 }, + values: [ + { + kind: Kind.STRING, + loc: { start: 1, end: 11 }, + value: 'long', + block: true, + }, + { + kind: Kind.STRING, + loc: { start: 12, end: 19 }, + value: 'short', + block: false, + }, + ], + }); + }); + + it('allows variables', () => { + const result = parseValue('{ field: $var }'); + expectJSON(result).toDeepEqual({ + kind: Kind.OBJECT, + loc: { start: 0, end: 15 }, + fields: [ + { + kind: Kind.OBJECT_FIELD, + loc: { start: 2, end: 13 }, + name: { + kind: Kind.NAME, + loc: { start: 2, end: 7 }, + value: 'field', + }, + value: { + kind: Kind.VARIABLE, + loc: { start: 9, end: 13 }, + name: { + kind: Kind.NAME, + loc: { start: 10, end: 13 }, + value: 'var', + }, + }, + }, + ], + }); + }); + + it('correct message for incomplete variable', () => { + expect(() => parseValue('$')).toThrow(); + expectToThrowJSON(() => parseValue('$')).toMatchObject({ + message: 'Syntax Error: Expected Name, found .', + locations: [{ line: 1, column: 2 }], + }); + }); + + it('correct message for unexpected token', () => { + expect(() => parseValue(':')).toThrow(); + expectToThrowJSON(() => parseValue(':')).toMatchObject({ + message: 'Syntax Error: Unexpected ":".', + locations: [{ line: 1, column: 1 }], + }); + }); + }); + + describe('parseConstValue', () => { + it('parses values', () => { + const result = parseConstValue('[123 "abc"]'); + expectJSON(result).toDeepEqual({ + kind: Kind.LIST, + loc: { start: 0, end: 11 }, + values: [ + { + kind: Kind.INT, + loc: { start: 1, end: 4 }, + value: '123', + }, + { + kind: Kind.STRING, + loc: { start: 5, end: 10 }, + value: 'abc', + block: false, + }, + ], + }); + }); + + it('does not allow variables', () => { + expect(() => parseConstValue('{ field: $var }')).toThrow(); + expectToThrowJSON(() => parseConstValue('{ field: $var }')).toMatchObject({ + message: 'Syntax Error: Unexpected variable "$var" in constant value.', + locations: [{ line: 1, column: 10 }], + }); + }); + + it('correct message for unexpected token', () => { + expect(() => parseConstValue('$')).toThrow(); + expectToThrowJSON(() => parseConstValue('$')).toMatchObject({ + message: 'Syntax Error: Unexpected "$".', + locations: [{ line: 1, column: 1 }], + }); + }); + }); + + describe('parseType', () => { + it('parses well known types', () => { + const result = parseType('String'); + expectJSON(result).toDeepEqual({ + kind: Kind.NAMED_TYPE, + loc: { start: 0, end: 6 }, + name: { + kind: Kind.NAME, + loc: { start: 0, end: 6 }, + value: 'String', + }, + }); + }); + + it('parses custom types', () => { + const result = parseType('MyType'); + expectJSON(result).toDeepEqual({ + kind: Kind.NAMED_TYPE, + loc: { start: 0, end: 6 }, + name: { + kind: Kind.NAME, + loc: { start: 0, end: 6 }, + value: 'MyType', + }, + }); + }); + + it('parses list types', () => { + const result = parseType('[MyType]'); + expectJSON(result).toDeepEqual({ + kind: Kind.LIST_TYPE, + loc: { start: 0, end: 8 }, + type: { + kind: Kind.NAMED_TYPE, + loc: { start: 1, end: 7 }, + name: { + kind: Kind.NAME, + loc: { start: 1, end: 7 }, + value: 'MyType', + }, + }, + }); + }); + + it('parses non-null types', () => { + const result = parseType('MyType!'); + expectJSON(result).toDeepEqual({ + kind: Kind.NON_NULL_TYPE, + loc: { start: 0, end: 7 }, + type: { + kind: Kind.NAMED_TYPE, + loc: { start: 0, end: 6 }, + name: { + kind: Kind.NAME, + loc: { start: 0, end: 6 }, + value: 'MyType', + }, + }, + }); + }); + + it('parses nested types', () => { + const result = parseType('[MyType!]'); + expectJSON(result).toDeepEqual({ + kind: Kind.LIST_TYPE, + loc: { start: 0, end: 9 }, + type: { + kind: Kind.NON_NULL_TYPE, + loc: { start: 1, end: 8 }, + type: { + kind: Kind.NAMED_TYPE, + loc: { start: 1, end: 7 }, + name: { + kind: Kind.NAME, + loc: { start: 1, end: 7 }, + value: 'MyType', + }, + }, + }, + }); + }); + }); +}); diff --git a/packages/graphql/src/language/__tests__/predicates-test.ts b/packages/graphql/src/language/__tests__/predicates-test.ts new file mode 100644 index 00000000000..de0a121a38c --- /dev/null +++ b/packages/graphql/src/language/__tests__/predicates-test.ts @@ -0,0 +1,139 @@ +import type { ASTNode } from '../ast.js'; +import { Kind } from '../kinds.js'; +import { parseValue } from '../parser.js'; +import { + isConstValueNode, + isDefinitionNode, + isExecutableDefinitionNode, + isNullabilityAssertionNode, + isSelectionNode, + isTypeDefinitionNode, + isTypeExtensionNode, + isTypeNode, + isTypeSystemDefinitionNode, + isTypeSystemExtensionNode, + isValueNode, +} from '../predicates.js'; + +function filterNodes(predicate: (node: ASTNode) => boolean): Array { + return Object.values(Kind).filter( + // @ts-expect-error create node only with kind + kind => predicate({ kind }) + ); +} + +describe('AST node predicates', () => { + it('isDefinitionNode', () => { + expect(filterNodes(isDefinitionNode)).toEqual([ + 'OperationDefinition', + 'FragmentDefinition', + 'SchemaDefinition', + 'ScalarTypeDefinition', + 'ObjectTypeDefinition', + 'InterfaceTypeDefinition', + 'UnionTypeDefinition', + 'EnumTypeDefinition', + 'InputObjectTypeDefinition', + 'DirectiveDefinition', + 'SchemaExtension', + 'ScalarTypeExtension', + 'ObjectTypeExtension', + 'InterfaceTypeExtension', + 'UnionTypeExtension', + 'EnumTypeExtension', + 'InputObjectTypeExtension', + ]); + }); + + it('isExecutableDefinitionNode', () => { + expect(filterNodes(isExecutableDefinitionNode)).toEqual(['OperationDefinition', 'FragmentDefinition']); + }); + + it('isSelectionNode', () => { + expect(filterNodes(isSelectionNode)).toEqual(['Field', 'FragmentSpread', 'InlineFragment']); + }); + + it('isNullabilityAssertionNode', () => { + expect(filterNodes(isNullabilityAssertionNode)).toEqual([ + 'ListNullabilityOperator', + 'NonNullAssertion', + 'ErrorBoundary', + ]); + }); + + it('isValueNode', () => { + expect(filterNodes(isValueNode)).toEqual([ + 'Variable', + 'IntValue', + 'FloatValue', + 'StringValue', + 'BooleanValue', + 'NullValue', + 'EnumValue', + 'ListValue', + 'ObjectValue', + ]); + }); + + it('isConstValueNode', () => { + expect(isConstValueNode(parseValue('"value"'))).toEqual(true); + expect(isConstValueNode(parseValue('$var'))).toEqual(false); + + expect(isConstValueNode(parseValue('{ field: "value" }'))).toEqual(true); + expect(isConstValueNode(parseValue('{ field: $var }'))).toEqual(false); + + expect(isConstValueNode(parseValue('[ "value" ]'))).toEqual(true); + expect(isConstValueNode(parseValue('[ $var ]'))).toEqual(false); + }); + + it('isTypeNode', () => { + expect(filterNodes(isTypeNode)).toEqual(['NamedType', 'ListType', 'NonNullType']); + }); + + it('isTypeSystemDefinitionNode', () => { + expect(filterNodes(isTypeSystemDefinitionNode)).toEqual([ + 'SchemaDefinition', + 'ScalarTypeDefinition', + 'ObjectTypeDefinition', + 'InterfaceTypeDefinition', + 'UnionTypeDefinition', + 'EnumTypeDefinition', + 'InputObjectTypeDefinition', + 'DirectiveDefinition', + ]); + }); + + it('isTypeDefinitionNode', () => { + expect(filterNodes(isTypeDefinitionNode)).toEqual([ + 'ScalarTypeDefinition', + 'ObjectTypeDefinition', + 'InterfaceTypeDefinition', + 'UnionTypeDefinition', + 'EnumTypeDefinition', + 'InputObjectTypeDefinition', + ]); + }); + + it('isTypeSystemExtensionNode', () => { + expect(filterNodes(isTypeSystemExtensionNode)).toEqual([ + 'SchemaExtension', + 'ScalarTypeExtension', + 'ObjectTypeExtension', + 'InterfaceTypeExtension', + 'UnionTypeExtension', + 'EnumTypeExtension', + 'InputObjectTypeExtension', + ]); + }); + + it('isTypeExtensionNode', () => { + expect(filterNodes(isTypeExtensionNode)).toEqual([ + 'ScalarTypeExtension', + 'ObjectTypeExtension', + 'InterfaceTypeExtension', + 'UnionTypeExtension', + 'EnumTypeExtension', + 'InputObjectTypeExtension', + ]); + }); +}); diff --git a/packages/graphql/src/language/__tests__/printLocation-test.ts b/packages/graphql/src/language/__tests__/printLocation-test.ts new file mode 100644 index 00000000000..3188530bc41 --- /dev/null +++ b/packages/graphql/src/language/__tests__/printLocation-test.ts @@ -0,0 +1,68 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import { printSourceLocation } from '../printLocation.js'; +import { Source } from '../source.js'; + +describe('printSourceLocation', () => { + it('prints minified documents', () => { + const minifiedSource = new Source( + 'query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String){someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD...on THIRD_ERROR_HERE}}}' + ); + + const firstLocation = printSourceLocation(minifiedSource, { + line: 1, + column: minifiedSource.body.indexOf('FIRST_ERROR_HERE') + 1, + }); + expect(firstLocation).toEqual(dedent` + GraphQL request:1:53 + 1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String) + | ^ + | {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD. + `); + + const secondLocation = printSourceLocation(minifiedSource, { + line: 1, + column: minifiedSource.body.indexOf('SECOND_ERROR_HERE') + 1, + }); + expect(secondLocation).toEqual(dedent` + GraphQL request:1:114 + 1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String) + | {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD. + | ^ + | ..on THIRD_ERROR_HERE}}} + `); + + const thirdLocation = printSourceLocation(minifiedSource, { + line: 1, + column: minifiedSource.body.indexOf('THIRD_ERROR_HERE') + 1, + }); + expect(thirdLocation).toEqual(dedent` + GraphQL request:1:166 + 1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String) + | {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD. + | ..on THIRD_ERROR_HERE}}} + | ^ + `); + }); + + it('prints single digit line number with no padding', () => { + const result = printSourceLocation(new Source('*', 'Test', { line: 9, column: 1 }), { line: 1, column: 1 }); + + expect(result).toEqual(dedent` + Test:9:1 + 9 | * + | ^ + `); + }); + + it('prints an line numbers with correct padding', () => { + const result = printSourceLocation(new Source('*\n', 'Test', { line: 9, column: 1 }), { line: 1, column: 1 }); + + expect(result).toEqual(dedent` + Test:9:1 + 9 | * + | ^ + 10 | + `); + }); +}); diff --git a/packages/graphql/src/language/__tests__/printString-test.ts b/packages/graphql/src/language/__tests__/printString-test.ts new file mode 100644 index 00000000000..44527572700 --- /dev/null +++ b/packages/graphql/src/language/__tests__/printString-test.ts @@ -0,0 +1,79 @@ +import { printString } from '../printString.js'; + +describe('printString', () => { + it('prints a simple string', () => { + expect(printString('hello world')).toEqual('"hello world"'); + }); + + it('escapes quotes', () => { + expect(printString('"hello world"')).toEqual('"\\"hello world\\""'); + }); + + it('does not escape single quote', () => { + expect(printString("who's test")).toEqual('"who\'s test"'); + }); + + it('escapes backslashes', () => { + expect(printString('escape: \\')).toEqual('"escape: \\\\"'); + }); + + it('escapes well-known control chars', () => { + expect(printString('\b\f\n\r\t')).toEqual('"\\b\\f\\n\\r\\t"'); + }); + + it('escapes zero byte', () => { + expect(printString('\x00')).toEqual('"\\u0000"'); + }); + + it('does not escape space', () => { + expect(printString(' ')).toEqual('" "'); + }); + + it('does not escape non-ascii character', () => { + expect(printString('\u21BB')).toEqual('"\u21BB"'); + }); + + it('does not escape supplementary character', () => { + expect(printString('\u{1f600}')).toEqual('"\u{1f600}"'); + }); + + it('escapes all control chars', () => { + /* spellchecker:ignore abcdefghijklmnopqrstuvwxyz */ + expect( + printString( + '\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007' + + '\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F' + + '\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017' + + '\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F' + + '\u0020\u0021\u0022\u0023\u0024\u0025\u0026\u0027' + + '\u0028\u0029\u002A\u002B\u002C\u002D\u002E\u002F' + + '\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037' + + '\u0038\u0039\u003A\u003B\u003C\u003D\u003E\u003F' + + '\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047' + + '\u0048\u0049\u004A\u004B\u004C\u004D\u004E\u004F' + + '\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057' + + '\u0058\u0059\u005A\u005B\u005C\u005D\u005E\u005F' + + '\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067' + + '\u0068\u0069\u006A\u006B\u006C\u006D\u006E\u006F' + + '\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077' + + '\u0078\u0079\u007A\u007B\u007C\u007D\u007E\u007F' + + '\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087' + + '\u0088\u0089\u008A\u008B\u008C\u008D\u008E\u008F' + + '\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097' + + '\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F' + ) + ).toEqual( + '"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007' + + '\\b\\t\\n\\u000B\\f\\r\\u000E\\u000F' + + '\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017' + + '\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F' + + ' !\\"#$%&\'()*+,-./0123456789:;<=>?' + + '@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_' + + '`abcdefghijklmnopqrstuvwxyz{|}~\\u007F' + + '\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087' + + '\\u0088\\u0089\\u008A\\u008B\\u008C\\u008D\\u008E\\u008F' + + '\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097' + + '\\u0098\\u0099\\u009A\\u009B\\u009C\\u009D\\u009E\\u009F"' + ); + }); +}); diff --git a/packages/graphql/src/language/__tests__/printer-test.ts b/packages/graphql/src/language/__tests__/printer-test.ts new file mode 100644 index 00000000000..822e8cca811 --- /dev/null +++ b/packages/graphql/src/language/__tests__/printer-test.ts @@ -0,0 +1,224 @@ +import { dedent, dedentString } from '../../__testUtils__/dedent.js'; +import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js'; + +import { Kind } from '../kinds.js'; +import { parse } from '../parser.js'; +import { print } from '../printer.js'; + +describe('Printer: Query document', () => { + it('prints minimal ast', () => { + const ast = { + kind: Kind.FIELD, + name: { kind: Kind.NAME, value: 'foo' }, + } as const; + expect(print(ast)).toEqual('foo'); + }); + + it('produces helpful error messages', () => { + const badAST = { random: 'Data' }; + + // @ts-expect-error + expect(() => print(badAST)).toThrow('Invalid AST Node: { random: "Data" }.'); + }); + + it('correctly prints non-query operations without name', () => { + const queryASTShorthanded = parse('query { id, name }'); + expect(print(queryASTShorthanded)).toEqual(dedent` + { + id + name + } + `); + + const mutationAST = parse('mutation { id, name }'); + expect(print(mutationAST)).toEqual(dedent` + mutation { + id + name + } + `); + + const queryASTWithArtifacts = parse('query ($foo: TestType) @testDirective { id, name }'); + expect(print(queryASTWithArtifacts)).toEqual(dedent` + query ($foo: TestType) @testDirective { + id + name + } + `); + + const mutationASTWithArtifacts = parse('mutation ($foo: TestType) @testDirective { id, name }'); + expect(print(mutationASTWithArtifacts)).toEqual(dedent` + mutation ($foo: TestType) @testDirective { + id + name + } + `); + }); + + it('prints query with variable directives', () => { + const queryASTWithVariableDirective = parse( + 'query ($foo: TestType = { a: 123 } @testDirective(if: true) @test) { id }' + ); + expect(print(queryASTWithVariableDirective)).toEqual(dedent` + query ($foo: TestType = { a: 123 } @testDirective(if: true) @test) { + id + } + `); + }); + + it('keeps arguments on one line if line is short (<= 80 chars)', () => { + const printed = print(parse('{trip(wheelchair:false arriveBy:false){dateTime}}')); + + expect(printed).toEqual(dedent` + { + trip(wheelchair: false, arriveBy: false) { + dateTime + } + } + `); + }); + + it('puts arguments on multiple lines if line is long (> 80 chars)', () => { + const printed = print( + parse( + '{trip(wheelchair:false arriveBy:false includePlannedCancellations:true transitDistanceReluctance:2000){dateTime}}' + ) + ); + + expect(printed).toEqual(dedent` + { + trip( + wheelchair: false + arriveBy: false + includePlannedCancellations: true + transitDistanceReluctance: 2000 + ) { + dateTime + } + } + `); + }); + + it('Legacy: prints fragment with variable directives', () => { + const queryASTWithVariableDirective = parse( + 'fragment Foo($foo: TestType @test) on TestType @testDirective { id }', + { allowLegacyFragmentVariables: true } + ); + expect(print(queryASTWithVariableDirective)).toEqual(dedent` + fragment Foo($foo: TestType @test) on TestType @testDirective { + id + } + `); + }); + + it('Legacy: correctly prints fragment defined variables', () => { + const fragmentWithVariable = parse( + ` + fragment Foo($a: ComplexType, $b: Boolean = false) on TestType { + id + } + `, + { allowLegacyFragmentVariables: true } + ); + expect(print(fragmentWithVariable)).toEqual(dedent` + fragment Foo($a: ComplexType, $b: Boolean = false) on TestType { + id + } + `); + }); + + it('prints kitchen sink without altering ast', () => { + const ast = parse(kitchenSinkQuery, { + noLocation: true, + experimentalClientControlledNullability: true, + }); + + const astBeforePrintCall = JSON.stringify(ast); + const printed = print(ast); + const printedAST = parse(printed, { + noLocation: true, + experimentalClientControlledNullability: true, + }); + + expect(printedAST).toEqual(ast); + expect(JSON.stringify(ast)).toEqual(astBeforePrintCall); + + expect(printed).toEqual( + dedentString(String.raw` + query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery { + whoever123is: node(id: [123, 456]) { + id + ... on User @onInlineFragment { + field2 { + id + alias: field1(first: 10, after: $foo) @include(if: $foo) { + id + ...frag @onFragmentSpread + } + } + field3! + field4? + requiredField5: field5! + requiredSelectionSet(first: 10)! @directive { + field + } + unsetListItemsRequiredList: listField[]! + requiredListItemsUnsetList: listField[!] + requiredListItemsRequiredList: listField[!]! + unsetListItemsOptionalList: listField[]? + optionalListItemsUnsetList: listField[?] + optionalListItemsOptionalList: listField[?]? + multidimensionalList: listField[[[!]!]!]! + } + ... @skip(unless: $foo) { + id + } + ... { + id + } + } + } + + mutation likeStory @onMutation { + like(story: 123) @onField { + story { + id @onField + } + } + } + + subscription StoryLikeSubscription($input: StoryLikeSubscribeInput @onVariableDefinition) @onSubscription { + storyLikeSubscribe(input: $input) { + story { + likers { + count + } + likeSentence { + text + } + } + } + } + + fragment frag on Friend @onFragmentDefinition { + foo( + size: $size + bar: $b + obj: { key: "value", block: """ + block string uses \""" + """ } + ) + } + + { + unnamed(truthy: true, falsy: false, nullish: null) + query + } + + { + __typename + } + `) + ); + }); +}); diff --git a/packages/graphql/src/language/__tests__/schema-parser-test.ts b/packages/graphql/src/language/__tests__/schema-parser-test.ts new file mode 100644 index 00000000000..97c4ce84c42 --- /dev/null +++ b/packages/graphql/src/language/__tests__/schema-parser-test.ts @@ -0,0 +1,1057 @@ +import { dedent } from '../../__testUtils__/dedent.js'; +import { expectJSON, expectToThrowJSON } from '../../__testUtils__/expectJSON.js'; +import { kitchenSinkSDL } from '../../__testUtils__/kitchenSinkSDL.js'; + +import { parse } from '../parser.js'; + +function expectSyntaxError(text: string) { + return expectToThrowJSON(() => parse(text)); +} + +function typeNode(name: unknown, loc: unknown) { + return { + kind: 'NamedType', + name: nameNode(name, loc), + loc, + }; +} + +function nameNode(name: unknown, loc: unknown) { + return { + kind: 'Name', + value: name, + loc, + }; +} + +function fieldNode(name: unknown, type: unknown, loc: unknown) { + return fieldNodeWithArgs(name, type, [], loc); +} + +function fieldNodeWithArgs(name: unknown, type: unknown, args: unknown, loc: unknown) { + return { + kind: 'FieldDefinition', + description: undefined, + name, + arguments: args, + type, + directives: [], + loc, + }; +} + +function enumValueNode(name: unknown, loc: unknown) { + return { + kind: 'EnumValueDefinition', + name: nameNode(name, loc), + description: undefined, + directives: [], + loc, + }; +} + +function inputValueNode(name: unknown, type: unknown, defaultValue: unknown, loc: unknown) { + return { + kind: 'InputValueDefinition', + name, + description: undefined, + type, + defaultValue, + directives: [], + loc, + }; +} + +describe('Schema Parser', () => { + it('Simple type', () => { + const doc = parse(dedent` + type Hello { + world: String + } + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [], + directives: [], + fields: [ + fieldNode(nameNode('world', { start: 15, end: 20 }), typeNode('String', { start: 22, end: 28 }), { + start: 15, + end: 28, + }), + ], + loc: { start: 0, end: 30 }, + }, + ], + loc: { start: 0, end: 30 }, + }); + }); + + it('parses type with description string', () => { + const doc = parse(dedent` + "Description" + type Hello { + world: String + } + `); + + expectJSON(doc).toDeepNestedProperty('definitions[0].description', { + kind: 'StringValue', + value: 'Description', + block: false, + loc: { start: 0, end: 13 }, + }); + }); + + it('parses type with description multi-line string', () => { + const doc = parse(dedent` + """ + Description + """ + # Even with comments between them + type Hello { + world: String + } + `); + + expectJSON(doc).toDeepNestedProperty('definitions[0].description', { + kind: 'StringValue', + value: 'Description', + block: true, + loc: { start: 0, end: 19 }, + }); + }); + + it('parses schema with description string', () => { + const doc = parse(dedent` + "Description" + schema { + query: Foo + } + `); + + expectJSON(doc).toDeepNestedProperty('definitions[0].description', { + kind: 'StringValue', + value: 'Description', + block: false, + loc: { start: 0, end: 13 }, + }); + }); + + it('Description followed by something other than type system definition throws', () => { + expectSyntaxError('"Description" 1').toMatchObject({ + message: 'Syntax Error: Unexpected Int "1".', + locations: [{ line: 1, column: 15 }], + }); + }); + + it('Simple extension', () => { + const doc = parse(dedent` + extend type Hello { + world: String + } + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeExtension', + name: nameNode('Hello', { start: 12, end: 17 }), + interfaces: [], + directives: [], + fields: [ + fieldNode(nameNode('world', { start: 22, end: 27 }), typeNode('String', { start: 29, end: 35 }), { + start: 22, + end: 35, + }), + ], + loc: { start: 0, end: 37 }, + }, + ], + loc: { start: 0, end: 37 }, + }); + }); + + it('Object extension without fields', () => { + const doc = parse('extend type Hello implements Greeting'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeExtension', + name: nameNode('Hello', { start: 12, end: 17 }), + interfaces: [typeNode('Greeting', { start: 29, end: 37 })], + directives: [], + fields: [], + loc: { start: 0, end: 37 }, + }, + ], + loc: { start: 0, end: 37 }, + }); + }); + + it('Interface extension without fields', () => { + const doc = parse('extend interface Hello implements Greeting'); + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'InterfaceTypeExtension', + name: nameNode('Hello', { start: 17, end: 22 }), + interfaces: [typeNode('Greeting', { start: 34, end: 42 })], + directives: [], + fields: [], + loc: { start: 0, end: 42 }, + }, + ], + loc: { start: 0, end: 42 }, + }); + }); + + it('Object extension without fields followed by extension', () => { + const doc = parse(` + extend type Hello implements Greeting + + extend type Hello implements SecondGreeting + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeExtension', + name: nameNode('Hello', { start: 19, end: 24 }), + interfaces: [typeNode('Greeting', { start: 36, end: 44 })], + directives: [], + fields: [], + loc: { start: 7, end: 44 }, + }, + { + kind: 'ObjectTypeExtension', + name: nameNode('Hello', { start: 64, end: 69 }), + interfaces: [typeNode('SecondGreeting', { start: 81, end: 95 })], + directives: [], + fields: [], + loc: { start: 52, end: 95 }, + }, + ], + loc: { start: 0, end: 100 }, + }); + }); + + it('Extension without anything throws', () => { + expectSyntaxError('extend scalar Hello').toMatchObject({ + message: 'Syntax Error: Unexpected .', + locations: [{ line: 1, column: 20 }], + }); + + expectSyntaxError('extend type Hello').toMatchObject({ + message: 'Syntax Error: Unexpected .', + locations: [{ line: 1, column: 18 }], + }); + + expectSyntaxError('extend interface Hello').toMatchObject({ + message: 'Syntax Error: Unexpected .', + locations: [{ line: 1, column: 23 }], + }); + + expectSyntaxError('extend union Hello').toMatchObject({ + message: 'Syntax Error: Unexpected .', + locations: [{ line: 1, column: 19 }], + }); + + expectSyntaxError('extend enum Hello').toMatchObject({ + message: 'Syntax Error: Unexpected .', + locations: [{ line: 1, column: 18 }], + }); + + expectSyntaxError('extend input Hello').toMatchObject({ + message: 'Syntax Error: Unexpected .', + locations: [{ line: 1, column: 19 }], + }); + }); + + it('Interface extension without fields followed by extension', () => { + const doc = parse(` + extend interface Hello implements Greeting + + extend interface Hello implements SecondGreeting + `); + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'InterfaceTypeExtension', + name: nameNode('Hello', { start: 24, end: 29 }), + interfaces: [typeNode('Greeting', { start: 41, end: 49 })], + directives: [], + fields: [], + loc: { start: 7, end: 49 }, + }, + { + kind: 'InterfaceTypeExtension', + name: nameNode('Hello', { start: 74, end: 79 }), + interfaces: [typeNode('SecondGreeting', { start: 91, end: 105 })], + directives: [], + fields: [], + loc: { start: 57, end: 105 }, + }, + ], + loc: { start: 0, end: 110 }, + }); + }); + + it('Object extension do not include descriptions', () => { + expectSyntaxError(` + "Description" + extend type Hello { + world: String + } + `).toMatchObject({ + message: 'Syntax Error: Unexpected description, descriptions are supported only on type definitions.', + locations: [{ line: 2, column: 7 }], + }); + + expectSyntaxError(` + extend "Description" type Hello { + world: String + } + `).toMatchObject({ + message: 'Syntax Error: Unexpected String "Description".', + locations: [{ line: 2, column: 14 }], + }); + }); + + it('Interface extension do not include descriptions', () => { + expectSyntaxError(` + "Description" + extend interface Hello { + world: String + } + `).toMatchObject({ + message: 'Syntax Error: Unexpected description, descriptions are supported only on type definitions.', + locations: [{ line: 2, column: 7 }], + }); + + expectSyntaxError(` + extend "Description" interface Hello { + world: String + } + `).toMatchObject({ + message: 'Syntax Error: Unexpected String "Description".', + locations: [{ line: 2, column: 14 }], + }); + }); + + it('Schema extension', () => { + const body = ` + extend schema { + mutation: Mutation + }`; + const doc = parse(body); + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'SchemaExtension', + directives: [], + operationTypes: [ + { + kind: 'OperationTypeDefinition', + operation: 'mutation', + type: typeNode('Mutation', { start: 41, end: 49 }), + loc: { start: 31, end: 49 }, + }, + ], + loc: { start: 7, end: 57 }, + }, + ], + loc: { start: 0, end: 57 }, + }); + }); + + it('Schema extension with only directives', () => { + const body = 'extend schema @directive'; + const doc = parse(body); + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'SchemaExtension', + directives: [ + { + kind: 'Directive', + name: nameNode('directive', { start: 15, end: 24 }), + arguments: [], + loc: { start: 14, end: 24 }, + }, + ], + operationTypes: [], + loc: { start: 0, end: 24 }, + }, + ], + loc: { start: 0, end: 24 }, + }); + }); + + it('Schema extension without anything throws', () => { + expectSyntaxError('extend schema').toMatchObject({ + message: 'Syntax Error: Unexpected .', + locations: [{ line: 1, column: 14 }], + }); + }); + + it('Schema extension with invalid operation type throws', () => { + expectSyntaxError('extend schema { unknown: SomeType }').toMatchObject({ + message: 'Syntax Error: Unexpected Name "unknown".', + locations: [{ line: 1, column: 17 }], + }); + }); + + it('Simple non-null type', () => { + const doc = parse(dedent` + type Hello { + world: String! + } + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [], + directives: [], + fields: [ + fieldNode( + nameNode('world', { start: 15, end: 20 }), + { + kind: 'NonNullType', + type: typeNode('String', { start: 22, end: 28 }), + loc: { start: 22, end: 29 }, + }, + { start: 15, end: 29 } + ), + ], + loc: { start: 0, end: 31 }, + }, + ], + loc: { start: 0, end: 31 }, + }); + }); + + it('Simple interface inheriting interface', () => { + const doc = parse('interface Hello implements World { field: String }'); + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'InterfaceTypeDefinition', + name: nameNode('Hello', { start: 10, end: 15 }), + description: undefined, + interfaces: [typeNode('World', { start: 27, end: 32 })], + directives: [], + fields: [ + fieldNode(nameNode('field', { start: 35, end: 40 }), typeNode('String', { start: 42, end: 48 }), { + start: 35, + end: 48, + }), + ], + loc: { start: 0, end: 50 }, + }, + ], + loc: { start: 0, end: 50 }, + }); + }); + + it('Simple type inheriting interface', () => { + const doc = parse('type Hello implements World { field: String }'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [typeNode('World', { start: 22, end: 27 })], + directives: [], + fields: [ + fieldNode(nameNode('field', { start: 30, end: 35 }), typeNode('String', { start: 37, end: 43 }), { + start: 30, + end: 43, + }), + ], + loc: { start: 0, end: 45 }, + }, + ], + loc: { start: 0, end: 45 }, + }); + }); + + it('Simple type inheriting multiple interfaces', () => { + const doc = parse('type Hello implements Wo & rld { field: String }'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [typeNode('Wo', { start: 22, end: 24 }), typeNode('rld', { start: 27, end: 30 })], + directives: [], + fields: [ + fieldNode(nameNode('field', { start: 33, end: 38 }), typeNode('String', { start: 40, end: 46 }), { + start: 33, + end: 46, + }), + ], + loc: { start: 0, end: 48 }, + }, + ], + loc: { start: 0, end: 48 }, + }); + }); + + it('Simple interface inheriting multiple interfaces', () => { + const doc = parse('interface Hello implements Wo & rld { field: String }'); + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'InterfaceTypeDefinition', + name: nameNode('Hello', { start: 10, end: 15 }), + description: undefined, + interfaces: [typeNode('Wo', { start: 27, end: 29 }), typeNode('rld', { start: 32, end: 35 })], + directives: [], + fields: [ + fieldNode(nameNode('field', { start: 38, end: 43 }), typeNode('String', { start: 45, end: 51 }), { + start: 38, + end: 51, + }), + ], + loc: { start: 0, end: 53 }, + }, + ], + loc: { start: 0, end: 53 }, + }); + }); + + it('Simple type inheriting multiple interfaces with leading ampersand', () => { + const doc = parse('type Hello implements & Wo & rld { field: String }'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [typeNode('Wo', { start: 24, end: 26 }), typeNode('rld', { start: 29, end: 32 })], + directives: [], + fields: [ + fieldNode(nameNode('field', { start: 35, end: 40 }), typeNode('String', { start: 42, end: 48 }), { + start: 35, + end: 48, + }), + ], + loc: { start: 0, end: 50 }, + }, + ], + loc: { start: 0, end: 50 }, + }); + }); + + it('Simple interface inheriting multiple interfaces with leading ampersand', () => { + const doc = parse('interface Hello implements & Wo & rld { field: String }'); + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'InterfaceTypeDefinition', + name: nameNode('Hello', { start: 10, end: 15 }), + description: undefined, + interfaces: [typeNode('Wo', { start: 29, end: 31 }), typeNode('rld', { start: 34, end: 37 })], + directives: [], + fields: [ + fieldNode(nameNode('field', { start: 40, end: 45 }), typeNode('String', { start: 47, end: 53 }), { + start: 40, + end: 53, + }), + ], + loc: { start: 0, end: 55 }, + }, + ], + loc: { start: 0, end: 55 }, + }); + }); + + it('Single value enum', () => { + const doc = parse('enum Hello { WORLD }'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'EnumTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + directives: [], + values: [enumValueNode('WORLD', { start: 13, end: 18 })], + loc: { start: 0, end: 20 }, + }, + ], + loc: { start: 0, end: 20 }, + }); + }); + + it('Double value enum', () => { + const doc = parse('enum Hello { WO, RLD }'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'EnumTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + directives: [], + values: [enumValueNode('WO', { start: 13, end: 15 }), enumValueNode('RLD', { start: 17, end: 20 })], + loc: { start: 0, end: 22 }, + }, + ], + loc: { start: 0, end: 22 }, + }); + }); + + it('Simple interface', () => { + const doc = parse(dedent` + interface Hello { + world: String + } + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'InterfaceTypeDefinition', + name: nameNode('Hello', { start: 10, end: 15 }), + description: undefined, + interfaces: [], + directives: [], + fields: [ + fieldNode(nameNode('world', { start: 20, end: 25 }), typeNode('String', { start: 27, end: 33 }), { + start: 20, + end: 33, + }), + ], + loc: { start: 0, end: 35 }, + }, + ], + loc: { start: 0, end: 35 }, + }); + }); + + it('Simple field with arg', () => { + const doc = parse(dedent` + type Hello { + world(flag: Boolean): String + } + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [], + directives: [], + fields: [ + fieldNodeWithArgs( + nameNode('world', { start: 15, end: 20 }), + typeNode('String', { start: 37, end: 43 }), + [ + inputValueNode( + nameNode('flag', { start: 21, end: 25 }), + typeNode('Boolean', { start: 27, end: 34 }), + undefined, + { start: 21, end: 34 } + ), + ], + { start: 15, end: 43 } + ), + ], + loc: { start: 0, end: 45 }, + }, + ], + loc: { start: 0, end: 45 }, + }); + }); + + it('Simple field with arg with default value', () => { + const doc = parse(dedent` + type Hello { + world(flag: Boolean = true): String + } + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [], + directives: [], + fields: [ + fieldNodeWithArgs( + nameNode('world', { start: 15, end: 20 }), + typeNode('String', { start: 44, end: 50 }), + [ + inputValueNode( + nameNode('flag', { start: 21, end: 25 }), + typeNode('Boolean', { start: 27, end: 34 }), + { + kind: 'BooleanValue', + value: true, + loc: { start: 37, end: 41 }, + }, + { start: 21, end: 41 } + ), + ], + { start: 15, end: 50 } + ), + ], + loc: { start: 0, end: 52 }, + }, + ], + loc: { start: 0, end: 52 }, + }); + }); + + it('Simple field with list arg', () => { + const doc = parse(dedent` + type Hello { + world(things: [String]): String + } + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [], + directives: [], + fields: [ + fieldNodeWithArgs( + nameNode('world', { start: 15, end: 20 }), + typeNode('String', { start: 40, end: 46 }), + [ + inputValueNode( + nameNode('things', { start: 21, end: 27 }), + { + kind: 'ListType', + type: typeNode('String', { start: 30, end: 36 }), + loc: { start: 29, end: 37 }, + }, + undefined, + { start: 21, end: 37 } + ), + ], + { start: 15, end: 46 } + ), + ], + loc: { start: 0, end: 48 }, + }, + ], + loc: { start: 0, end: 48 }, + }); + }); + + it('Simple field with two args', () => { + const doc = parse(dedent` + type Hello { + world(argOne: Boolean, argTwo: Int): String + } + `); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ObjectTypeDefinition', + name: nameNode('Hello', { start: 5, end: 10 }), + description: undefined, + interfaces: [], + directives: [], + fields: [ + fieldNodeWithArgs( + nameNode('world', { start: 15, end: 20 }), + typeNode('String', { start: 52, end: 58 }), + [ + inputValueNode( + nameNode('argOne', { start: 21, end: 27 }), + typeNode('Boolean', { start: 29, end: 36 }), + undefined, + { start: 21, end: 36 } + ), + inputValueNode( + nameNode('argTwo', { start: 38, end: 44 }), + typeNode('Int', { start: 46, end: 49 }), + undefined, + { start: 38, end: 49 } + ), + ], + { start: 15, end: 58 } + ), + ], + loc: { start: 0, end: 60 }, + }, + ], + loc: { start: 0, end: 60 }, + }); + }); + + it('Simple union', () => { + const doc = parse('union Hello = World'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'UnionTypeDefinition', + name: nameNode('Hello', { start: 6, end: 11 }), + description: undefined, + directives: [], + types: [typeNode('World', { start: 14, end: 19 })], + loc: { start: 0, end: 19 }, + }, + ], + loc: { start: 0, end: 19 }, + }); + }); + + it('Union with two types', () => { + const doc = parse('union Hello = Wo | Rld'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'UnionTypeDefinition', + name: nameNode('Hello', { start: 6, end: 11 }), + description: undefined, + directives: [], + types: [typeNode('Wo', { start: 14, end: 16 }), typeNode('Rld', { start: 19, end: 22 })], + loc: { start: 0, end: 22 }, + }, + ], + loc: { start: 0, end: 22 }, + }); + }); + + it('Union with two types and leading pipe', () => { + const doc = parse('union Hello = | Wo | Rld'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'UnionTypeDefinition', + name: nameNode('Hello', { start: 6, end: 11 }), + description: undefined, + directives: [], + types: [typeNode('Wo', { start: 16, end: 18 }), typeNode('Rld', { start: 21, end: 24 })], + loc: { start: 0, end: 24 }, + }, + ], + loc: { start: 0, end: 24 }, + }); + }); + + it('Union fails with no types', () => { + expectSyntaxError('union Hello = |').toMatchObject({ + message: 'Syntax Error: Expected Name, found .', + locations: [{ line: 1, column: 16 }], + }); + }); + + it('Union fails with leading double pipe', () => { + expectSyntaxError('union Hello = || Wo | Rld').toMatchObject({ + message: 'Syntax Error: Expected Name, found "|".', + locations: [{ line: 1, column: 16 }], + }); + }); + + it('Union fails with double pipe', () => { + expectSyntaxError('union Hello = Wo || Rld').toMatchObject({ + message: 'Syntax Error: Expected Name, found "|".', + locations: [{ line: 1, column: 19 }], + }); + }); + + it('Union fails with trailing pipe', () => { + expectSyntaxError('union Hello = | Wo | Rld |').toMatchObject({ + message: 'Syntax Error: Expected Name, found .', + locations: [{ line: 1, column: 27 }], + }); + }); + + it('Scalar', () => { + const doc = parse('scalar Hello'); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'ScalarTypeDefinition', + name: nameNode('Hello', { start: 7, end: 12 }), + description: undefined, + directives: [], + loc: { start: 0, end: 12 }, + }, + ], + loc: { start: 0, end: 12 }, + }); + }); + + it('Simple input object', () => { + const doc = parse(` +input Hello { + world: String +}`); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'InputObjectTypeDefinition', + name: nameNode('Hello', { start: 7, end: 12 }), + description: undefined, + directives: [], + fields: [ + inputValueNode( + nameNode('world', { start: 17, end: 22 }), + typeNode('String', { start: 24, end: 30 }), + undefined, + { start: 17, end: 30 } + ), + ], + loc: { start: 1, end: 32 }, + }, + ], + loc: { start: 0, end: 32 }, + }); + }); + + it('Simple input object with args should fail', () => { + expectSyntaxError(` + input Hello { + world(foo: Int): String + } + `).toMatchObject({ + message: 'Syntax Error: Expected ":", found "(".', + locations: [{ line: 3, column: 14 }], + }); + }); + + it('Directive definition', () => { + const body = 'directive @foo on OBJECT | INTERFACE'; + const doc = parse(body); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'DirectiveDefinition', + description: undefined, + name: { + kind: 'Name', + value: 'foo', + loc: { start: 11, end: 14 }, + }, + arguments: [], + repeatable: false, + locations: [ + { + kind: 'Name', + value: 'OBJECT', + loc: { start: 18, end: 24 }, + }, + { + kind: 'Name', + value: 'INTERFACE', + loc: { start: 27, end: 36 }, + }, + ], + loc: { start: 0, end: 36 }, + }, + ], + loc: { start: 0, end: 36 }, + }); + }); + + it('Repeatable directive definition', () => { + const body = 'directive @foo repeatable on OBJECT | INTERFACE'; + const doc = parse(body); + + expectJSON(doc).toDeepEqual({ + kind: 'Document', + definitions: [ + { + kind: 'DirectiveDefinition', + description: undefined, + name: { + kind: 'Name', + value: 'foo', + loc: { start: 11, end: 14 }, + }, + arguments: [], + repeatable: true, + locations: [ + { + kind: 'Name', + value: 'OBJECT', + loc: { start: 29, end: 35 }, + }, + { + kind: 'Name', + value: 'INTERFACE', + loc: { start: 38, end: 47 }, + }, + ], + loc: { start: 0, end: 47 }, + }, + ], + loc: { start: 0, end: 47 }, + }); + }); + + it('Directive with incorrect locations', () => { + expectSyntaxError('directive @foo on FIELD | INCORRECT_LOCATION').toMatchObject({ + message: 'Syntax Error: Unexpected Name "INCORRECT_LOCATION".', + locations: [{ line: 1, column: 27 }], + }); + }); + + it('parses kitchen sink schema', () => { + expect(() => parse(kitchenSinkSDL)).not.toThrow(); + }); +}); diff --git a/packages/graphql/src/language/__tests__/schema-printer-test.ts b/packages/graphql/src/language/__tests__/schema-printer-test.ts new file mode 100644 index 00000000000..5ab9ce06f7e --- /dev/null +++ b/packages/graphql/src/language/__tests__/schema-printer-test.ts @@ -0,0 +1,172 @@ +import { dedent } from '../../__testUtils__/dedent.js'; +import { kitchenSinkSDL } from '../../__testUtils__/kitchenSinkSDL.js'; + +import { Kind } from '../kinds.js'; +import { parse } from '../parser.js'; +import { print } from '../printer.js'; + +describe('Printer: SDL document', () => { + it('prints minimal ast', () => { + const ast = { + kind: Kind.SCALAR_TYPE_DEFINITION, + name: { kind: Kind.NAME, value: 'foo' }, + } as const; + expect(print(ast)).toEqual('scalar foo'); + }); + + it('produces helpful error messages', () => { + const badAST = { random: 'Data' }; + + // @ts-expect-error + expect(() => print(badAST)).toThrow('Invalid AST Node: { random: "Data" }.'); + }); + + it('prints kitchen sink without altering ast', () => { + const ast = parse(kitchenSinkSDL, { noLocation: true }); + + const astBeforePrintCall = JSON.stringify(ast); + const printed = print(ast); + const printedAST = parse(printed, { noLocation: true }); + + expect(printedAST).toEqual(ast); + expect(JSON.stringify(ast)).toEqual(astBeforePrintCall); + + expect(printed).toEqual(dedent` + """This is a description of the schema as a whole.""" + schema { + query: QueryType + mutation: MutationType + } + + """ + This is a description + of the \`Foo\` type. + """ + type Foo implements Bar & Baz & Two { + "Description of the \`one\` field." + one: Type + """This is a description of the \`two\` field.""" + two( + """This is a description of the \`argument\` argument.""" + argument: InputType! + ): Type + """This is a description of the \`three\` field.""" + three(argument: InputType, other: String): Int + four(argument: String = "string"): String + five(argument: [String] = ["string", "string"]): String + six(argument: InputType = { key: "value" }): Type + seven(argument: Int = null): Type + } + + type AnnotatedObject @onObject(arg: "value") { + annotatedField(arg: Type = "default" @onArgumentDefinition): Type @onField + } + + type UndefinedType + + extend type Foo { + seven(argument: [String]): Type + } + + extend type Foo @onType + + interface Bar { + one: Type + four(argument: String = "string"): String + } + + interface AnnotatedInterface @onInterface { + annotatedField(arg: Type @onArgumentDefinition): Type @onField + } + + interface UndefinedInterface + + extend interface Bar implements Two { + two(argument: InputType!): Type + } + + extend interface Bar @onInterface + + interface Baz implements Bar & Two { + one: Type + two(argument: InputType!): Type + four(argument: String = "string"): String + } + + union Feed = Story | Article | Advert + + union AnnotatedUnion @onUnion = A | B + + union AnnotatedUnionTwo @onUnion = A | B + + union UndefinedUnion + + extend union Feed = Photo | Video + + extend union Feed @onUnion + + scalar CustomScalar + + scalar AnnotatedScalar @onScalar + + extend scalar CustomScalar @onScalar + + enum Site { + """This is a description of the \`DESKTOP\` value""" + DESKTOP + """This is a description of the \`MOBILE\` value""" + MOBILE + "This is a description of the \`WEB\` value" + WEB + } + + enum AnnotatedEnum @onEnum { + ANNOTATED_VALUE @onEnumValue + OTHER_VALUE + } + + enum UndefinedEnum + + extend enum Site { + VR + } + + extend enum Site @onEnum + + input InputType { + key: String! + answer: Int = 42 + } + + input AnnotatedInput @onInputObject { + annotatedField: Type @onInputFieldDefinition + } + + input UndefinedInput + + extend input InputType { + other: Float = 1.23e4 @onInputFieldDefinition + } + + extend input InputType @onInputObject + + """This is a description of the \`@skip\` directive""" + directive @skip( + """This is a description of the \`if\` argument""" + if: Boolean! @onArgumentDefinition + ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT + + directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT + + directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT + + directive @myRepeatableDir(name: String!) repeatable on OBJECT | INTERFACE + + extend schema @onSchema + + extend schema @onSchema { + subscription: SubscriptionType + } + `); + }); +}); diff --git a/packages/graphql/src/language/__tests__/source-test.ts b/packages/graphql/src/language/__tests__/source-test.ts new file mode 100644 index 00000000000..c9b7750b526 --- /dev/null +++ b/packages/graphql/src/language/__tests__/source-test.ts @@ -0,0 +1,29 @@ +import { Source } from '../source.js'; + +describe('Source', () => { + it('can be Object.toStringified', () => { + const source = new Source(''); + + expect(Object.prototype.toString.call(source)).toEqual('[object Source]'); + }); + + it('rejects invalid locationOffset', () => { + function createSource(locationOffset: { line: number; column: number }) { + return new Source('', '', locationOffset); + } + + expect(() => createSource({ line: 0, column: 1 })).toThrow( + 'line in locationOffset is 1-indexed and must be positive.' + ); + expect(() => createSource({ line: -1, column: 1 })).toThrow( + 'line in locationOffset is 1-indexed and must be positive.' + ); + + expect(() => createSource({ line: 1, column: 0 })).toThrow( + 'column in locationOffset is 1-indexed and must be positive.' + ); + expect(() => createSource({ line: 1, column: -1 })).toThrow( + 'column in locationOffset is 1-indexed and must be positive.' + ); + }); +}); diff --git a/packages/graphql/src/language/__tests__/visitor-test.ts b/packages/graphql/src/language/__tests__/visitor-test.ts new file mode 100644 index 00000000000..e130e33e689 --- /dev/null +++ b/packages/graphql/src/language/__tests__/visitor-test.ts @@ -0,0 +1,1466 @@ +import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js'; + +import type { ASTNode, SelectionSetNode } from '../ast.js'; +import { isNode } from '../ast.js'; +import { Kind } from '../kinds.js'; +import { parse } from '../parser.js'; +import type { ASTVisitor, ASTVisitorKeyMap } from '../visitor.js'; +import { BREAK, visit, visitInParallel } from '../visitor.js'; + +function checkVisitorFnArgs(ast: any, args: any, isEdited: boolean = false) { + const [node, key, parent, path, ancestors] = args; + + expect(node).toBeInstanceOf(Object); + expect(Object.values(Kind).includes(node.kind)).toBeTruthy(); + + const isRoot = key === undefined; + if (isRoot) { + if (!isEdited) { + expect(node).toEqual(ast); + } + expect(parent).toEqual(undefined); + expect(path).toEqual([]); + expect(ancestors).toEqual([]); + return; + } + + expect(typeof key === 'string' || typeof key === 'number').toBeTruthy(); + + expect(path).toBeInstanceOf(Array); + expect(path[path.length - 1]).toEqual(key); + + expect(ancestors).toBeInstanceOf(Array); + expect(ancestors.length).toEqual(path.length - 1); + + if (!isEdited) { + let currentNode = ast; + for (let i = 0; i < ancestors.length; ++i) { + expect(ancestors[i]).toEqual(currentNode); + + currentNode = currentNode[path[i]]; + expect(currentNode).not.toEqual(undefined); + } + + expect(parent).toEqual(currentNode); + expect(parent[key]).toEqual(node); + } +} + +function getValue(node: ASTNode) { + return 'value' in node ? node.value : undefined; +} + +describe('Visitor', () => { + it('handles empty visitor', () => { + const ast = parse('{ a }', { noLocation: true }); + expect(() => visit(ast, {})).not.toThrow(); + }); + + it('validates path argument', () => { + const visited: Array = []; + + const ast = parse('{ a }', { noLocation: true }); + + visit(ast, { + enter(_node, _key, _parent, path) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', path.slice()]); + }, + leave(_node, _key, _parent, path) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', path.slice()]); + }, + }); + + expect(visited).toEqual([ + ['enter', []], + ['enter', ['definitions', 0]], + ['enter', ['definitions', 0, 'selectionSet']], + ['enter', ['definitions', 0, 'selectionSet', 'selections', 0]], + ['enter', ['definitions', 0, 'selectionSet', 'selections', 0, 'name']], + ['leave', ['definitions', 0, 'selectionSet', 'selections', 0, 'name']], + ['leave', ['definitions', 0, 'selectionSet', 'selections', 0]], + ['leave', ['definitions', 0, 'selectionSet']], + ['leave', ['definitions', 0]], + ['leave', []], + ]); + }); + + it('validates ancestors argument', () => { + const ast = parse('{ a }', { noLocation: true }); + const visitedNodes: Array = []; + + visit(ast, { + enter(node, key, parent, _path, ancestors) { + const inArray = typeof key === 'number'; + if (inArray) { + visitedNodes.push(parent); + } + visitedNodes.push(node); + + const expectedAncestors = visitedNodes.slice(0, -2); + expect(ancestors).toEqual(expectedAncestors); + }, + leave(_node, key, _parent, _path, ancestors) { + const expectedAncestors = visitedNodes.slice(0, -2); + expect(ancestors).toEqual(expectedAncestors); + + const inArray = typeof key === 'number'; + if (inArray) { + visitedNodes.pop(); + } + visitedNodes.pop(); + }, + }); + }); + + it('allows editing a node both on enter and on leave', () => { + const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true }); + + let selectionSet: SelectionSetNode; + + const editedAST = visit(ast, { + OperationDefinition: { + enter(node) { + checkVisitorFnArgs(ast, arguments); + selectionSet = node.selectionSet; + return { + ...node, + selectionSet: { + kind: 'SelectionSet', + selections: [], + }, + didEnter: true, + }; + }, + leave(node) { + checkVisitorFnArgs(ast, arguments, /* isEdited */ true); + return { + ...node, + selectionSet, + didLeave: true, + }; + }, + }, + }); + + expect(editedAST).toEqual({ + ...ast, + definitions: [ + { + ...ast.definitions[0], + didEnter: true, + didLeave: true, + }, + ], + }); + }); + + it('allows editing the root node on enter and on leave', () => { + const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true }); + + const { definitions } = ast; + + const editedAST = visit(ast, { + Document: { + enter(node) { + checkVisitorFnArgs(ast, arguments); + return { + ...node, + definitions: [], + didEnter: true, + }; + }, + leave(node) { + checkVisitorFnArgs(ast, arguments, /* isEdited */ true); + return { + ...node, + definitions, + didLeave: true, + }; + }, + }, + }); + + expect(editedAST).toEqual({ + ...ast, + didEnter: true, + didLeave: true, + }); + }); + + it('allows for editing on enter', () => { + const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true }); + const editedAST = visit(ast, { + enter(node) { + checkVisitorFnArgs(ast, arguments); + if (node.kind === 'Field' && node.name.value === 'b') { + return null; + } + return undefined; + }, + }); + + expect(ast).toEqual(parse('{ a, b, c { a, b, c } }', { noLocation: true })); + + expect(editedAST).toEqual(parse('{ a, c { a, c } }', { noLocation: true })); + }); + + it('allows for editing on leave', () => { + const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true }); + const editedAST = visit(ast, { + leave(node) { + checkVisitorFnArgs(ast, arguments, /* isEdited */ true); + if (node.kind === 'Field' && node.name.value === 'b') { + return null; + } + return undefined; + }, + }); + + expect(ast).toEqual(parse('{ a, b, c { a, b, c } }', { noLocation: true })); + + expect(editedAST).toEqual(parse('{ a, c { a, c } }', { noLocation: true })); + }); + + it('ignores false returned on leave', () => { + const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true }); + const returnedAST = visit(ast, { + leave() { + return false; + }, + }); + + expect(returnedAST).toEqual(parse('{ a, b, c { a, b, c } }', { noLocation: true })); + }); + + it('visits edited node', () => { + const addedField = { + kind: 'Field', + name: { + kind: 'Name', + value: '__typename', + }, + }; + + let didVisitAddedField; + + const ast = parse('{ a { x } }', { noLocation: true }); + visit(ast, { + enter(node) { + checkVisitorFnArgs(ast, arguments, /* isEdited */ true); + if (node.kind === 'Field' && node.name.value === 'a') { + return { + kind: 'Field', + selectionSet: [addedField, node.selectionSet], + }; + } + if (node === addedField) { + didVisitAddedField = true; + } + return undefined; + }, + }); + + expect(didVisitAddedField).toEqual(true); + }); + + it('allows skipping a sub-tree', () => { + const visited: Array = []; + + const ast = parse('{ a, b { x }, c }', { noLocation: true }); + visit(ast, { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + if (node.kind === 'Field' && node.name.value === 'b') { + return false; + } + return undefined; + }, + + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', node.kind, getValue(node)]); + }, + }); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'c'], + ['leave', 'Name', 'c'], + ['leave', 'Field', undefined], + ['leave', 'SelectionSet', undefined], + ['leave', 'OperationDefinition', undefined], + ['leave', 'Document', undefined], + ]); + }); + + it('allows early exit while visiting', () => { + const visited: Array = []; + + const ast = parse('{ a, b { x }, c }', { noLocation: true }); + visit(ast, { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + if (node.kind === 'Name' && node.value === 'x') { + return BREAK; + } + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', node.kind, getValue(node)]); + }, + }); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'b'], + ['leave', 'Name', 'b'], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'x'], + ]); + }); + + it('allows early exit while leaving', () => { + const visited: Array = []; + + const ast = parse('{ a, b { x }, c }', { noLocation: true }); + visit(ast, { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + }, + + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', node.kind, getValue(node)]); + if (node.kind === 'Name' && node.value === 'x') { + return BREAK; + } + }, + }); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'b'], + ['leave', 'Name', 'b'], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'x'], + ['leave', 'Name', 'x'], + ]); + }); + + it('allows a named functions visitor API', () => { + const visited: Array = []; + + const ast = parse('{ a, b { x }, c }', { noLocation: true }); + visit(ast, { + Name(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + }, + SelectionSet: { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', node.kind, getValue(node)]); + }, + }, + }); + + expect(visited).toEqual([ + ['enter', 'SelectionSet', undefined], + ['enter', 'Name', 'a'], + ['enter', 'Name', 'b'], + ['enter', 'SelectionSet', undefined], + ['enter', 'Name', 'x'], + ['leave', 'SelectionSet', undefined], + ['enter', 'Name', 'c'], + ['leave', 'SelectionSet', undefined], + ]); + }); + + it('visits only the specified `Kind` in visitorKeyMap', () => { + const visited: Array = []; + + const visitorKeyMap: ASTVisitorKeyMap = { + Document: ['definitions'], + OperationDefinition: ['name'], + }; + + const visitor: ASTVisitor = { + enter(node) { + visited.push(['enter', node.kind, getValue(node)]); + }, + leave(node) { + visited.push(['leave', node.kind, getValue(node)]); + }, + }; + + const exampleDocumentAST = parse(` + query ExampleOperation { + someField + } + `); + + visit(exampleDocumentAST, visitor, visitorKeyMap); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'Name', 'ExampleOperation'], + ['leave', 'Name', 'ExampleOperation'], + ['leave', 'OperationDefinition', undefined], + ['leave', 'Document', undefined], + ]); + }); + + it('Legacy: visits variables defined in fragments', () => { + const ast = parse('fragment a($v: Boolean = false) on t { f }', { + noLocation: true, + allowLegacyFragmentVariables: true, + }); + const visited: Array = []; + + visit(ast, { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', node.kind, getValue(node)]); + }, + }); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'FragmentDefinition', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['enter', 'VariableDefinition', undefined], + ['enter', 'Variable', undefined], + ['enter', 'Name', 'v'], + ['leave', 'Name', 'v'], + ['leave', 'Variable', undefined], + ['enter', 'NamedType', undefined], + ['enter', 'Name', 'Boolean'], + ['leave', 'Name', 'Boolean'], + ['leave', 'NamedType', undefined], + ['enter', 'BooleanValue', false], + ['leave', 'BooleanValue', false], + ['leave', 'VariableDefinition', undefined], + ['enter', 'NamedType', undefined], + ['enter', 'Name', 't'], + ['leave', 'Name', 't'], + ['leave', 'NamedType', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'f'], + ['leave', 'Name', 'f'], + ['leave', 'Field', undefined], + ['leave', 'SelectionSet', undefined], + ['leave', 'FragmentDefinition', undefined], + ['leave', 'Document', undefined], + ]); + }); + + it('n', () => { + const ast = parse(kitchenSinkQuery, { + experimentalClientControlledNullability: true, + }); + const visited: Array = []; + const argsStack: Array = []; + + visit(ast, { + enter(node, key, parent) { + visited.push(['enter', node.kind, key, isNode(parent) ? parent.kind : undefined]); + + checkVisitorFnArgs(ast, arguments); + argsStack.push([...arguments]); + }, + + leave(node, key, parent) { + visited.push(['leave', node.kind, key, isNode(parent) ? parent.kind : undefined]); + + expect(argsStack.pop()).toEqual([...arguments]); + }, + }); + + expect(argsStack).toEqual([]); + expect(visited).toEqual([ + ['enter', 'Document', undefined, undefined], + ['enter', 'OperationDefinition', 0, undefined], + ['enter', 'Name', 'name', 'OperationDefinition'], + ['leave', 'Name', 'name', 'OperationDefinition'], + ['enter', 'VariableDefinition', 0, undefined], + ['enter', 'Variable', 'variable', 'VariableDefinition'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'variable', 'VariableDefinition'], + ['enter', 'NamedType', 'type', 'VariableDefinition'], + ['enter', 'Name', 'name', 'NamedType'], + ['leave', 'Name', 'name', 'NamedType'], + ['leave', 'NamedType', 'type', 'VariableDefinition'], + ['leave', 'VariableDefinition', 0, undefined], + ['enter', 'VariableDefinition', 1, undefined], + ['enter', 'Variable', 'variable', 'VariableDefinition'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'variable', 'VariableDefinition'], + ['enter', 'NamedType', 'type', 'VariableDefinition'], + ['enter', 'Name', 'name', 'NamedType'], + ['leave', 'Name', 'name', 'NamedType'], + ['leave', 'NamedType', 'type', 'VariableDefinition'], + ['enter', 'EnumValue', 'defaultValue', 'VariableDefinition'], + ['leave', 'EnumValue', 'defaultValue', 'VariableDefinition'], + ['leave', 'VariableDefinition', 1, undefined], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'ListValue', 'value', 'Argument'], + ['enter', 'IntValue', 0, undefined], + ['leave', 'IntValue', 0, undefined], + ['enter', 'IntValue', 1, undefined], + ['leave', 'IntValue', 1, undefined], + ['leave', 'ListValue', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['enter', 'InlineFragment', 1, undefined], + ['enter', 'NamedType', 'typeCondition', 'InlineFragment'], + ['enter', 'Name', 'name', 'NamedType'], + ['leave', 'Name', 'name', 'NamedType'], + ['leave', 'NamedType', 'typeCondition', 'InlineFragment'], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'InlineFragment'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['enter', 'Field', 1, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'IntValue', 'value', 'Argument'], + ['leave', 'IntValue', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['enter', 'Argument', 1, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'Variable', 'value', 'Argument'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'value', 'Argument'], + ['leave', 'Argument', 1, undefined], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'Variable', 'value', 'Argument'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['enter', 'FragmentSpread', 1, undefined], + ['enter', 'Name', 'name', 'FragmentSpread'], + ['leave', 'Name', 'name', 'FragmentSpread'], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['leave', 'FragmentSpread', 1, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 1, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 0, undefined], + ['enter', 'Field', 1, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 1, undefined], + ['enter', 'Field', 2, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'ErrorBoundary', 'nullabilityAssertion', 'Field'], + ['leave', 'ErrorBoundary', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 2, undefined], + ['enter', 'Field', 3, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 3, undefined], + ['enter', 'Field', 4, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'IntValue', 'value', 'Argument'], + ['leave', 'IntValue', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 4, undefined], + ['enter', 'Field', 5, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 5, undefined], + ['enter', 'Field', 6, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'Field'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 6, undefined], + ['enter', 'Field', 7, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 7, undefined], + ['enter', 'Field', 8, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'ErrorBoundary', 'nullabilityAssertion', 'Field'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'ErrorBoundary'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'ErrorBoundary'], + ['leave', 'ErrorBoundary', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 8, undefined], + ['enter', 'Field', 9, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'Field'], + ['enter', 'ErrorBoundary', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ErrorBoundary', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 9, undefined], + ['enter', 'Field', 10, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'ErrorBoundary', 'nullabilityAssertion', 'Field'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'ErrorBoundary'], + ['enter', 'ErrorBoundary', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ErrorBoundary', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'ErrorBoundary'], + ['leave', 'ErrorBoundary', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 10, undefined], + ['enter', 'Field', 11, undefined], + ['enter', 'Name', 'alias', 'Field'], + ['leave', 'Name', 'alias', 'Field'], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['enter', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['enter', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'ListNullabilityOperator'], + ['leave', 'ListNullabilityOperator', 'nullabilityAssertion', 'NonNullAssertion'], + ['leave', 'NonNullAssertion', 'nullabilityAssertion', 'Field'], + ['leave', 'Field', 11, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'InlineFragment'], + ['leave', 'InlineFragment', 1, undefined], + ['enter', 'InlineFragment', 2, undefined], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'Variable', 'value', 'Argument'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'InlineFragment'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'InlineFragment'], + ['leave', 'InlineFragment', 2, undefined], + ['enter', 'InlineFragment', 3, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'InlineFragment'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'InlineFragment'], + ['leave', 'InlineFragment', 3, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['leave', 'OperationDefinition', 0, undefined], + ['enter', 'OperationDefinition', 1, undefined], + ['enter', 'Name', 'name', 'OperationDefinition'], + ['leave', 'Name', 'name', 'OperationDefinition'], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'IntValue', 'value', 'Argument'], + ['leave', 'IntValue', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['leave', 'OperationDefinition', 1, undefined], + ['enter', 'OperationDefinition', 2, undefined], + ['enter', 'Name', 'name', 'OperationDefinition'], + ['leave', 'Name', 'name', 'OperationDefinition'], + ['enter', 'VariableDefinition', 0, undefined], + ['enter', 'Variable', 'variable', 'VariableDefinition'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'variable', 'VariableDefinition'], + ['enter', 'NamedType', 'type', 'VariableDefinition'], + ['enter', 'Name', 'name', 'NamedType'], + ['leave', 'Name', 'name', 'NamedType'], + ['leave', 'NamedType', 'type', 'VariableDefinition'], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['leave', 'VariableDefinition', 0, undefined], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'Variable', 'value', 'Argument'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 0, undefined], + ['enter', 'Field', 1, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'SelectionSet', 'selectionSet', 'Field'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 1, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['leave', 'OperationDefinition', 2, undefined], + ['enter', 'FragmentDefinition', 3, undefined], + ['enter', 'Name', 'name', 'FragmentDefinition'], + ['leave', 'Name', 'name', 'FragmentDefinition'], + ['enter', 'NamedType', 'typeCondition', 'FragmentDefinition'], + ['enter', 'Name', 'name', 'NamedType'], + ['leave', 'Name', 'name', 'NamedType'], + ['leave', 'NamedType', 'typeCondition', 'FragmentDefinition'], + ['enter', 'Directive', 0, undefined], + ['enter', 'Name', 'name', 'Directive'], + ['leave', 'Name', 'name', 'Directive'], + ['leave', 'Directive', 0, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'FragmentDefinition'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'Variable', 'value', 'Argument'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['enter', 'Argument', 1, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'Variable', 'value', 'Argument'], + ['enter', 'Name', 'name', 'Variable'], + ['leave', 'Name', 'name', 'Variable'], + ['leave', 'Variable', 'value', 'Argument'], + ['leave', 'Argument', 1, undefined], + ['enter', 'Argument', 2, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'ObjectValue', 'value', 'Argument'], + ['enter', 'ObjectField', 0, undefined], + ['enter', 'Name', 'name', 'ObjectField'], + ['leave', 'Name', 'name', 'ObjectField'], + ['enter', 'StringValue', 'value', 'ObjectField'], + ['leave', 'StringValue', 'value', 'ObjectField'], + ['leave', 'ObjectField', 0, undefined], + ['enter', 'ObjectField', 1, undefined], + ['enter', 'Name', 'name', 'ObjectField'], + ['leave', 'Name', 'name', 'ObjectField'], + ['enter', 'StringValue', 'value', 'ObjectField'], + ['leave', 'StringValue', 'value', 'ObjectField'], + ['leave', 'ObjectField', 1, undefined], + ['leave', 'ObjectValue', 'value', 'Argument'], + ['leave', 'Argument', 2, undefined], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'FragmentDefinition'], + ['leave', 'FragmentDefinition', 3, undefined], + ['enter', 'OperationDefinition', 4, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['enter', 'Argument', 0, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'BooleanValue', 'value', 'Argument'], + ['leave', 'BooleanValue', 'value', 'Argument'], + ['leave', 'Argument', 0, undefined], + ['enter', 'Argument', 1, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'BooleanValue', 'value', 'Argument'], + ['leave', 'BooleanValue', 'value', 'Argument'], + ['leave', 'Argument', 1, undefined], + ['enter', 'Argument', 2, undefined], + ['enter', 'Name', 'name', 'Argument'], + ['leave', 'Name', 'name', 'Argument'], + ['enter', 'NullValue', 'value', 'Argument'], + ['leave', 'NullValue', 'value', 'Argument'], + ['leave', 'Argument', 2, undefined], + ['leave', 'Field', 0, undefined], + ['enter', 'Field', 1, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 1, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['leave', 'OperationDefinition', 4, undefined], + ['enter', 'OperationDefinition', 5, undefined], + ['enter', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['enter', 'Field', 0, undefined], + ['enter', 'Name', 'name', 'Field'], + ['leave', 'Name', 'name', 'Field'], + ['leave', 'Field', 0, undefined], + ['leave', 'SelectionSet', 'selectionSet', 'OperationDefinition'], + ['leave', 'OperationDefinition', 5, undefined], + ['leave', 'Document', undefined, undefined], + ]); + }); + + describe('visitInParallel', () => { + // Note: nearly identical to the above test of the same test but + // using visitInParallel. + it('allows skipping a sub-tree', () => { + const visited: Array = []; + + const ast = parse('{ a, b { x }, c }'); + visit( + ast, + visitInParallel([ + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + if (node.kind === 'Field' && node.name.value === 'b') { + return false; + } + return undefined; + }, + + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', node.kind, getValue(node)]); + }, + }, + ]) + ); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'c'], + ['leave', 'Name', 'c'], + ['leave', 'Field', undefined], + ['leave', 'SelectionSet', undefined], + ['leave', 'OperationDefinition', undefined], + ['leave', 'Document', undefined], + ]); + }); + + it('allows skipping different sub-trees', () => { + const visited: Array = []; + + const ast = parse('{ a { x }, b { y} }'); + visit( + ast, + visitInParallel([ + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['no-a', 'enter', node.kind, getValue(node)]); + if (node.kind === 'Field' && node.name.value === 'a') { + return false; + } + return undefined; + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['no-a', 'leave', node.kind, getValue(node)]); + }, + }, + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['no-b', 'enter', node.kind, getValue(node)]); + if (node.kind === 'Field' && node.name.value === 'b') { + return false; + } + return undefined; + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['no-b', 'leave', node.kind, getValue(node)]); + }, + }, + ]) + ); + + expect(visited).toEqual([ + ['no-a', 'enter', 'Document', undefined], + ['no-b', 'enter', 'Document', undefined], + ['no-a', 'enter', 'OperationDefinition', undefined], + ['no-b', 'enter', 'OperationDefinition', undefined], + ['no-a', 'enter', 'SelectionSet', undefined], + ['no-b', 'enter', 'SelectionSet', undefined], + ['no-a', 'enter', 'Field', undefined], + ['no-b', 'enter', 'Field', undefined], + ['no-b', 'enter', 'Name', 'a'], + ['no-b', 'leave', 'Name', 'a'], + ['no-b', 'enter', 'SelectionSet', undefined], + ['no-b', 'enter', 'Field', undefined], + ['no-b', 'enter', 'Name', 'x'], + ['no-b', 'leave', 'Name', 'x'], + ['no-b', 'leave', 'Field', undefined], + ['no-b', 'leave', 'SelectionSet', undefined], + ['no-b', 'leave', 'Field', undefined], + ['no-a', 'enter', 'Field', undefined], + ['no-b', 'enter', 'Field', undefined], + ['no-a', 'enter', 'Name', 'b'], + ['no-a', 'leave', 'Name', 'b'], + ['no-a', 'enter', 'SelectionSet', undefined], + ['no-a', 'enter', 'Field', undefined], + ['no-a', 'enter', 'Name', 'y'], + ['no-a', 'leave', 'Name', 'y'], + ['no-a', 'leave', 'Field', undefined], + ['no-a', 'leave', 'SelectionSet', undefined], + ['no-a', 'leave', 'Field', undefined], + ['no-a', 'leave', 'SelectionSet', undefined], + ['no-b', 'leave', 'SelectionSet', undefined], + ['no-a', 'leave', 'OperationDefinition', undefined], + ['no-b', 'leave', 'OperationDefinition', undefined], + ['no-a', 'leave', 'Document', undefined], + ['no-b', 'leave', 'Document', undefined], + ]); + }); + + // Note: nearly identical to the above test of the same test but + // using visitInParallel. + it('allows early exit while visiting', () => { + const visited: Array = []; + + const ast = parse('{ a, b { x }, c }'); + visit( + ast, + visitInParallel([ + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + if (node.kind === 'Name' && node.value === 'x') { + return BREAK; + } + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', node.kind, getValue(node)]); + }, + }, + ]) + ); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'b'], + ['leave', 'Name', 'b'], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'x'], + ]); + }); + + it('allows early exit from different points', () => { + const visited: Array = []; + + const ast = parse('{ a { y }, b { x } }'); + visit( + ast, + visitInParallel([ + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['break-a', 'enter', node.kind, getValue(node)]); + if (node.kind === 'Name' && node.value === 'a') { + return BREAK; + } + }, + /* c8 ignore next 3 */ + leave() {}, + }, + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['break-b', 'enter', node.kind, getValue(node)]); + if (node.kind === 'Name' && node.value === 'b') { + return BREAK; + } + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['break-b', 'leave', node.kind, getValue(node)]); + }, + }, + ]) + ); + + expect(visited).toEqual([ + ['break-a', 'enter', 'Document', undefined], + ['break-b', 'enter', 'Document', undefined], + ['break-a', 'enter', 'OperationDefinition', undefined], + ['break-b', 'enter', 'OperationDefinition', undefined], + ['break-a', 'enter', 'SelectionSet', undefined], + ['break-b', 'enter', 'SelectionSet', undefined], + ['break-a', 'enter', 'Field', undefined], + ['break-b', 'enter', 'Field', undefined], + ['break-a', 'enter', 'Name', 'a'], + ['break-b', 'enter', 'Name', 'a'], + ['break-b', 'leave', 'Name', 'a'], + ['break-b', 'enter', 'SelectionSet', undefined], + ['break-b', 'enter', 'Field', undefined], + ['break-b', 'enter', 'Name', 'y'], + ['break-b', 'leave', 'Name', 'y'], + ['break-b', 'leave', 'Field', undefined], + ['break-b', 'leave', 'SelectionSet', undefined], + ['break-b', 'leave', 'Field', undefined], + ['break-b', 'enter', 'Field', undefined], + ['break-b', 'enter', 'Name', 'b'], + ]); + }); + + // Note: nearly identical to the above test of the same test but + // using visitInParallel. + it('allows early exit while leaving', () => { + const visited: Array = []; + + const ast = parse('{ a, b { x }, c }'); + visit( + ast, + visitInParallel([ + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['leave', node.kind, getValue(node)]); + if (node.kind === 'Name' && node.value === 'x') { + return BREAK; + } + }, + }, + ]) + ); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'b'], + ['leave', 'Name', 'b'], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'x'], + ['leave', 'Name', 'x'], + ]); + }); + + it('allows early exit from leaving different points', () => { + const visited: Array = []; + + const ast = parse('{ a { y }, b { x } }'); + visit( + ast, + visitInParallel([ + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['break-a', 'enter', node.kind, getValue(node)]); + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['break-a', 'leave', node.kind, getValue(node)]); + if (node.kind === 'Field' && node.name.value === 'a') { + return BREAK; + } + }, + }, + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['break-b', 'enter', node.kind, getValue(node)]); + }, + leave(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['break-b', 'leave', node.kind, getValue(node)]); + if (node.kind === 'Field' && node.name.value === 'b') { + return BREAK; + } + }, + }, + ]) + ); + + expect(visited).toEqual([ + ['break-a', 'enter', 'Document', undefined], + ['break-b', 'enter', 'Document', undefined], + ['break-a', 'enter', 'OperationDefinition', undefined], + ['break-b', 'enter', 'OperationDefinition', undefined], + ['break-a', 'enter', 'SelectionSet', undefined], + ['break-b', 'enter', 'SelectionSet', undefined], + ['break-a', 'enter', 'Field', undefined], + ['break-b', 'enter', 'Field', undefined], + ['break-a', 'enter', 'Name', 'a'], + ['break-b', 'enter', 'Name', 'a'], + ['break-a', 'leave', 'Name', 'a'], + ['break-b', 'leave', 'Name', 'a'], + ['break-a', 'enter', 'SelectionSet', undefined], + ['break-b', 'enter', 'SelectionSet', undefined], + ['break-a', 'enter', 'Field', undefined], + ['break-b', 'enter', 'Field', undefined], + ['break-a', 'enter', 'Name', 'y'], + ['break-b', 'enter', 'Name', 'y'], + ['break-a', 'leave', 'Name', 'y'], + ['break-b', 'leave', 'Name', 'y'], + ['break-a', 'leave', 'Field', undefined], + ['break-b', 'leave', 'Field', undefined], + ['break-a', 'leave', 'SelectionSet', undefined], + ['break-b', 'leave', 'SelectionSet', undefined], + ['break-a', 'leave', 'Field', undefined], + ['break-b', 'leave', 'Field', undefined], + ['break-b', 'enter', 'Field', undefined], + ['break-b', 'enter', 'Name', 'b'], + ['break-b', 'leave', 'Name', 'b'], + ['break-b', 'enter', 'SelectionSet', undefined], + ['break-b', 'enter', 'Field', undefined], + ['break-b', 'enter', 'Name', 'x'], + ['break-b', 'leave', 'Name', 'x'], + ['break-b', 'leave', 'Field', undefined], + ['break-b', 'leave', 'SelectionSet', undefined], + ['break-b', 'leave', 'Field', undefined], + ]); + }); + + it('allows for editing on enter', () => { + const visited: Array = []; + + const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true }); + const editedAST = visit( + ast, + visitInParallel([ + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + if (node.kind === 'Field' && node.name.value === 'b') { + return null; + } + return undefined; + }, + }, + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + }, + leave(node) { + checkVisitorFnArgs(ast, arguments, /* isEdited */ true); + visited.push(['leave', node.kind, getValue(node)]); + }, + }, + ]) + ); + + expect(ast).toEqual(parse('{ a, b, c { a, b, c } }', { noLocation: true })); + + expect(editedAST).toEqual(parse('{ a, c { a, c } }', { noLocation: true })); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'c'], + ['leave', 'Name', 'c'], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'c'], + ['leave', 'Name', 'c'], + ['leave', 'Field', undefined], + ['leave', 'SelectionSet', undefined], + ['leave', 'Field', undefined], + ['leave', 'SelectionSet', undefined], + ['leave', 'OperationDefinition', undefined], + ['leave', 'Document', undefined], + ]); + }); + + it('allows for editing on leave', () => { + const visited: Array = []; + + const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true }); + const editedAST = visit( + ast, + visitInParallel([ + { + leave(node) { + checkVisitorFnArgs(ast, arguments, /* isEdited */ true); + if (node.kind === 'Field' && node.name.value === 'b') { + return null; + } + return undefined; + }, + }, + { + enter(node) { + checkVisitorFnArgs(ast, arguments); + visited.push(['enter', node.kind, getValue(node)]); + }, + leave(node) { + checkVisitorFnArgs(ast, arguments, /* isEdited */ true); + visited.push(['leave', node.kind, getValue(node)]); + }, + }, + ]) + ); + + expect(ast).toEqual(parse('{ a, b, c { a, b, c } }', { noLocation: true })); + + expect(editedAST).toEqual(parse('{ a, c { a, c } }', { noLocation: true })); + + expect(visited).toEqual([ + ['enter', 'Document', undefined], + ['enter', 'OperationDefinition', undefined], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'b'], + ['leave', 'Name', 'b'], + ['enter', 'Field', undefined], + ['enter', 'Name', 'c'], + ['leave', 'Name', 'c'], + ['enter', 'SelectionSet', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'a'], + ['leave', 'Name', 'a'], + ['leave', 'Field', undefined], + ['enter', 'Field', undefined], + ['enter', 'Name', 'b'], + ['leave', 'Name', 'b'], + ['enter', 'Field', undefined], + ['enter', 'Name', 'c'], + ['leave', 'Name', 'c'], + ['leave', 'Field', undefined], + ['leave', 'SelectionSet', undefined], + ['leave', 'Field', undefined], + ['leave', 'SelectionSet', undefined], + ['leave', 'OperationDefinition', undefined], + ['leave', 'Document', undefined], + ]); + }); + }); +}); diff --git a/packages/graphql/src/language/ast.ts b/packages/graphql/src/language/ast.ts new file mode 100644 index 00000000000..e67fc827215 --- /dev/null +++ b/packages/graphql/src/language/ast.ts @@ -0,0 +1,739 @@ +import type { Kind } from './kinds.js'; +import type { Source } from './source.js'; +import type { TokenKind } from './tokenKind.js'; + +/** + * Represents a range of characters represented by a lexical token + * within a Source. + */ +export class Token { + /** + * The kind of Token. + */ + readonly kind: TokenKind; + + /** + * The character offset at which this Node begins. + */ + readonly start: number; + + /** + * The character offset at which this Node ends. + */ + readonly end: number; + + /** + * The 1-indexed line number on which this Token appears. + */ + readonly line: number; + + /** + * The 1-indexed column number at which this Token begins. + */ + readonly column: number; + + /** + * For non-punctuation tokens, represents the interpreted value of the token. + * + * Note: is undefined for punctuation tokens, but typed as string for + * convenience in the parser. + */ + readonly value: string; + + /** + * Tokens exist as nodes in a double-linked-list amongst all tokens + * including ignored tokens. is always the first node and + * the last. + */ + readonly prev: Token | null; + readonly next: Token | null; + + constructor(kind: TokenKind, start: number, end: number, line: number, column: number, value?: string) { + this.kind = kind; + this.start = start; + this.end = end; + this.line = line; + this.column = column; + + this.value = value!; + this.prev = null; + this.next = null; + } + + get [Symbol.toStringTag]() { + return 'Token'; + } + + toJSON(): { + kind: TokenKind; + value?: string; + line: number; + column: number; + } { + return { + kind: this.kind, + value: this.value, + line: this.line, + column: this.column, + }; + } +} + +/** + * Contains a range of UTF-8 character offsets and token references that + * identify the region of the source from which the AST derived. + */ +export class Location { + /** + * The character offset at which this Node begins. + */ + readonly start: number; + + /** + * The character offset at which this Node ends. + */ + readonly end: number; + + /** + * The Token at which this Node begins. + */ + readonly startToken: Token; + + /** + * The Token at which this Node ends. + */ + readonly endToken: Token; + + /** + * The Source document the AST represents. + */ + readonly source: Source; + + constructor(startToken: Token, endToken: Token, source: Source) { + this.start = startToken.start; + this.end = endToken.end; + this.startToken = startToken; + this.endToken = endToken; + this.source = source; + } + + get [Symbol.toStringTag]() { + return 'Location'; + } + + toJSON(): { start: number; end: number } { + return { start: this.start, end: this.end }; + } +} + +/** + * The list of all possible AST node types. + */ +export type ASTNode = + | NameNode + | DocumentNode + | OperationDefinitionNode + | VariableDefinitionNode + | VariableNode + | SelectionSetNode + | FieldNode + | ArgumentNode + | FragmentSpreadNode + | InlineFragmentNode + | FragmentDefinitionNode + | IntValueNode + | FloatValueNode + | StringValueNode + | BooleanValueNode + | NullValueNode + | EnumValueNode + | ListValueNode + | ObjectValueNode + | ObjectFieldNode + | DirectiveNode + | NamedTypeNode + | ListTypeNode + | NonNullTypeNode + | SchemaDefinitionNode + | OperationTypeDefinitionNode + | ScalarTypeDefinitionNode + | ObjectTypeDefinitionNode + | FieldDefinitionNode + | InputValueDefinitionNode + | InterfaceTypeDefinitionNode + | UnionTypeDefinitionNode + | EnumTypeDefinitionNode + | EnumValueDefinitionNode + | InputObjectTypeDefinitionNode + | DirectiveDefinitionNode + | SchemaExtensionNode + | ScalarTypeExtensionNode + | ObjectTypeExtensionNode + | InterfaceTypeExtensionNode + | UnionTypeExtensionNode + | EnumTypeExtensionNode + | InputObjectTypeExtensionNode + | NonNullAssertionNode + | ErrorBoundaryNode + | ListNullabilityOperatorNode; + +/** + * Utility type listing all nodes indexed by their kind. + */ +export type ASTKindToNode = { + [NodeT in ASTNode as NodeT['kind']]: NodeT; +}; + +/** + * @internal + */ +export const QueryDocumentKeys: { + [NodeT in ASTNode as NodeT['kind']]: ReadonlyArray; +} = { + Name: [], + + Document: ['definitions'], + OperationDefinition: ['name', 'variableDefinitions', 'directives', 'selectionSet'], + VariableDefinition: ['variable', 'type', 'defaultValue', 'directives'], + Variable: ['name'], + SelectionSet: ['selections'], + Field: [ + 'alias', + 'name', + 'arguments', + 'directives', + 'selectionSet', + // Note: Client Controlled Nullability is experimental and may be changed + // or removed in the future. + 'nullabilityAssertion', + ], + Argument: ['name', 'value'], + // Note: Client Controlled Nullability is experimental and may be changed + // or removed in the future. + ListNullabilityOperator: ['nullabilityAssertion'], + NonNullAssertion: ['nullabilityAssertion'], + ErrorBoundary: ['nullabilityAssertion'], + + FragmentSpread: ['name', 'directives'], + InlineFragment: ['typeCondition', 'directives', 'selectionSet'], + FragmentDefinition: [ + 'name', + // Note: fragment variable definitions are deprecated and will removed in v17.0.0 + 'variableDefinitions', + 'typeCondition', + 'directives', + 'selectionSet', + ], + + IntValue: [], + FloatValue: [], + StringValue: [], + BooleanValue: [], + NullValue: [], + EnumValue: [], + ListValue: ['values'], + ObjectValue: ['fields'], + ObjectField: ['name', 'value'], + + Directive: ['name', 'arguments'], + + NamedType: ['name'], + ListType: ['type'], + NonNullType: ['type'], + + SchemaDefinition: ['description', 'directives', 'operationTypes'], + OperationTypeDefinition: ['type'], + + ScalarTypeDefinition: ['description', 'name', 'directives'], + ObjectTypeDefinition: ['description', 'name', 'interfaces', 'directives', 'fields'], + FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'], + InputValueDefinition: ['description', 'name', 'type', 'defaultValue', 'directives'], + InterfaceTypeDefinition: ['description', 'name', 'interfaces', 'directives', 'fields'], + UnionTypeDefinition: ['description', 'name', 'directives', 'types'], + EnumTypeDefinition: ['description', 'name', 'directives', 'values'], + EnumValueDefinition: ['description', 'name', 'directives'], + InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'], + + DirectiveDefinition: ['description', 'name', 'arguments', 'locations'], + + SchemaExtension: ['directives', 'operationTypes'], + + ScalarTypeExtension: ['name', 'directives'], + ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'], + InterfaceTypeExtension: ['name', 'interfaces', 'directives', 'fields'], + UnionTypeExtension: ['name', 'directives', 'types'], + EnumTypeExtension: ['name', 'directives', 'values'], + InputObjectTypeExtension: ['name', 'directives', 'fields'], +}; + +const kindValues = new Set(Object.keys(QueryDocumentKeys)); +/** + * @internal + */ +export function isNode(maybeNode: any): maybeNode is ASTNode { + const maybeKind = maybeNode?.kind; + return typeof maybeKind === 'string' && kindValues.has(maybeKind); +} + +/** Name */ + +export interface NameNode { + readonly kind: Kind.NAME; + readonly loc?: Location; + readonly value: string; +} + +/** Document */ + +export interface DocumentNode { + readonly kind: Kind.DOCUMENT; + readonly loc?: Location; + readonly definitions: ReadonlyArray; +} + +export type DefinitionNode = ExecutableDefinitionNode | TypeSystemDefinitionNode | TypeSystemExtensionNode; + +export type ExecutableDefinitionNode = OperationDefinitionNode | FragmentDefinitionNode; + +export interface OperationDefinitionNode { + readonly kind: Kind.OPERATION_DEFINITION; + readonly loc?: Location; + readonly operation: OperationTypeNode; + readonly name?: NameNode; + readonly variableDefinitions?: ReadonlyArray; + readonly directives?: ReadonlyArray; + readonly selectionSet: SelectionSetNode; +} + +export enum OperationTypeNode { + QUERY = 'query', + MUTATION = 'mutation', + SUBSCRIPTION = 'subscription', +} + +export interface VariableDefinitionNode { + readonly kind: Kind.VARIABLE_DEFINITION; + readonly loc?: Location; + readonly variable: VariableNode; + readonly type: TypeNode; + readonly defaultValue?: ConstValueNode; + readonly directives?: ReadonlyArray; +} + +export interface VariableNode { + readonly kind: Kind.VARIABLE; + readonly loc?: Location; + readonly name: NameNode; +} + +export interface SelectionSetNode { + kind: Kind.SELECTION_SET; + loc?: Location; + selections: ReadonlyArray; +} + +export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode; + +export interface FieldNode { + readonly kind: Kind.FIELD; + readonly loc?: Location; + readonly alias?: NameNode; + readonly name: NameNode; + readonly arguments?: ReadonlyArray; + // Note: Client Controlled Nullability is experimental + // and may be changed or removed in the future. + readonly nullabilityAssertion?: NullabilityAssertionNode; + readonly directives?: ReadonlyArray; + readonly selectionSet?: SelectionSetNode; +} + +export type NullabilityAssertionNode = NonNullAssertionNode | ErrorBoundaryNode | ListNullabilityOperatorNode; + +export interface ListNullabilityOperatorNode { + readonly kind: Kind.LIST_NULLABILITY_OPERATOR; + readonly loc?: Location; + readonly nullabilityAssertion?: NullabilityAssertionNode; +} + +export interface NonNullAssertionNode { + readonly kind: Kind.NON_NULL_ASSERTION; + readonly loc?: Location; + readonly nullabilityAssertion?: ListNullabilityOperatorNode; +} + +export interface ErrorBoundaryNode { + readonly kind: Kind.ERROR_BOUNDARY; + readonly loc?: Location; + readonly nullabilityAssertion?: ListNullabilityOperatorNode; +} + +export interface ArgumentNode { + readonly kind: Kind.ARGUMENT; + readonly loc?: Location; + readonly name: NameNode; + readonly value: ValueNode; +} + +export interface ConstArgumentNode { + readonly kind: Kind.ARGUMENT; + readonly loc?: Location; + readonly name: NameNode; + readonly value: ConstValueNode; +} + +/** Fragments */ + +export interface FragmentSpreadNode { + readonly kind: Kind.FRAGMENT_SPREAD; + readonly loc?: Location; + readonly name: NameNode; + readonly directives?: ReadonlyArray; +} + +export interface InlineFragmentNode { + readonly kind: Kind.INLINE_FRAGMENT; + readonly loc?: Location; + readonly typeCondition?: NamedTypeNode; + readonly directives?: ReadonlyArray; + readonly selectionSet: SelectionSetNode; +} + +export interface FragmentDefinitionNode { + readonly kind: Kind.FRAGMENT_DEFINITION; + readonly loc?: Location; + readonly name: NameNode; + /** @deprecated variableDefinitions will be removed in v17.0.0 */ + readonly variableDefinitions?: ReadonlyArray; + readonly typeCondition: NamedTypeNode; + readonly directives?: ReadonlyArray; + readonly selectionSet: SelectionSetNode; +} + +/** Values */ + +export type ValueNode = + | VariableNode + | IntValueNode + | FloatValueNode + | StringValueNode + | BooleanValueNode + | NullValueNode + | EnumValueNode + | ListValueNode + | ObjectValueNode; + +export type ConstValueNode = + | IntValueNode + | FloatValueNode + | StringValueNode + | BooleanValueNode + | NullValueNode + | EnumValueNode + | ConstListValueNode + | ConstObjectValueNode; + +export interface IntValueNode { + readonly kind: Kind.INT; + readonly loc?: Location; + readonly value: string; +} + +export interface FloatValueNode { + readonly kind: Kind.FLOAT; + readonly loc?: Location; + readonly value: string; +} + +export interface StringValueNode { + readonly kind: Kind.STRING; + readonly loc?: Location; + readonly value: string; + readonly block?: boolean; +} + +export interface BooleanValueNode { + readonly kind: Kind.BOOLEAN; + readonly loc?: Location; + readonly value: boolean; +} + +export interface NullValueNode { + readonly kind: Kind.NULL; + readonly loc?: Location; +} + +export interface EnumValueNode { + readonly kind: Kind.ENUM; + readonly loc?: Location; + readonly value: string; +} + +export interface ListValueNode { + readonly kind: Kind.LIST; + readonly loc?: Location; + readonly values: ReadonlyArray; +} + +export interface ConstListValueNode { + readonly kind: Kind.LIST; + readonly loc?: Location; + readonly values: ReadonlyArray; +} + +export interface ObjectValueNode { + readonly kind: Kind.OBJECT; + readonly loc?: Location; + readonly fields: ReadonlyArray; +} + +export interface ConstObjectValueNode { + readonly kind: Kind.OBJECT; + readonly loc?: Location; + readonly fields: ReadonlyArray; +} + +export interface ObjectFieldNode { + readonly kind: Kind.OBJECT_FIELD; + readonly loc?: Location; + readonly name: NameNode; + readonly value: ValueNode; +} + +export interface ConstObjectFieldNode { + readonly kind: Kind.OBJECT_FIELD; + readonly loc?: Location; + readonly name: NameNode; + readonly value: ConstValueNode; +} + +/** Directives */ + +export interface DirectiveNode { + readonly kind: Kind.DIRECTIVE; + readonly loc?: Location; + readonly name: NameNode; + readonly arguments?: ReadonlyArray; +} + +export interface ConstDirectiveNode { + readonly kind: Kind.DIRECTIVE; + readonly loc?: Location; + readonly name: NameNode; + readonly arguments?: ReadonlyArray; +} + +/** Type Reference */ + +export type TypeNode = NamedTypeNode | ListTypeNode | NonNullTypeNode; + +export interface NamedTypeNode { + readonly kind: Kind.NAMED_TYPE; + readonly loc?: Location; + readonly name: NameNode; +} + +export interface ListTypeNode { + readonly kind: Kind.LIST_TYPE; + readonly loc?: Location; + readonly type: TypeNode; +} + +export interface NonNullTypeNode { + readonly kind: Kind.NON_NULL_TYPE; + readonly loc?: Location; + readonly type: NamedTypeNode | ListTypeNode; +} + +/** Type System Definition */ + +export type TypeSystemDefinitionNode = SchemaDefinitionNode | TypeDefinitionNode | DirectiveDefinitionNode; + +export interface SchemaDefinitionNode { + readonly kind: Kind.SCHEMA_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly directives?: ReadonlyArray; + readonly operationTypes: ReadonlyArray; +} + +export interface OperationTypeDefinitionNode { + readonly kind: Kind.OPERATION_TYPE_DEFINITION; + readonly loc?: Location; + readonly operation: OperationTypeNode; + readonly type: NamedTypeNode; +} + +/** Type Definition */ + +export type TypeDefinitionNode = + | ScalarTypeDefinitionNode + | ObjectTypeDefinitionNode + | InterfaceTypeDefinitionNode + | UnionTypeDefinitionNode + | EnumTypeDefinitionNode + | InputObjectTypeDefinitionNode; + +export interface ScalarTypeDefinitionNode { + readonly kind: Kind.SCALAR_TYPE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly directives?: ReadonlyArray; +} + +export interface ObjectTypeDefinitionNode { + readonly kind: Kind.OBJECT_TYPE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly interfaces?: ReadonlyArray; + readonly directives?: ReadonlyArray; + readonly fields?: ReadonlyArray; +} + +export interface FieldDefinitionNode { + readonly kind: Kind.FIELD_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly arguments?: ReadonlyArray; + readonly type: TypeNode; + readonly directives?: ReadonlyArray; +} + +export interface InputValueDefinitionNode { + readonly kind: Kind.INPUT_VALUE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly type: TypeNode; + readonly defaultValue?: ConstValueNode; + readonly directives?: ReadonlyArray; +} + +export interface InterfaceTypeDefinitionNode { + readonly kind: Kind.INTERFACE_TYPE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly interfaces?: ReadonlyArray; + readonly directives?: ReadonlyArray; + readonly fields?: ReadonlyArray; +} + +export interface UnionTypeDefinitionNode { + readonly kind: Kind.UNION_TYPE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly directives?: ReadonlyArray; + readonly types?: ReadonlyArray; +} + +export interface EnumTypeDefinitionNode { + readonly kind: Kind.ENUM_TYPE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly directives?: ReadonlyArray; + readonly values?: ReadonlyArray; +} + +export interface EnumValueDefinitionNode { + readonly kind: Kind.ENUM_VALUE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly directives?: ReadonlyArray; +} + +export interface InputObjectTypeDefinitionNode { + readonly kind: Kind.INPUT_OBJECT_TYPE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly directives?: ReadonlyArray; + readonly fields?: ReadonlyArray; +} + +/** Directive Definitions */ + +export interface DirectiveDefinitionNode { + readonly kind: Kind.DIRECTIVE_DEFINITION; + readonly loc?: Location; + readonly description?: StringValueNode; + readonly name: NameNode; + readonly arguments?: ReadonlyArray; + readonly repeatable: boolean; + readonly locations: ReadonlyArray; +} + +/** Type System Extensions */ + +export type TypeSystemExtensionNode = SchemaExtensionNode | TypeExtensionNode; + +export interface SchemaExtensionNode { + readonly kind: Kind.SCHEMA_EXTENSION; + readonly loc?: Location; + readonly directives?: ReadonlyArray; + readonly operationTypes?: ReadonlyArray; +} + +/** Type Extensions */ + +export type TypeExtensionNode = + | ScalarTypeExtensionNode + | ObjectTypeExtensionNode + | InterfaceTypeExtensionNode + | UnionTypeExtensionNode + | EnumTypeExtensionNode + | InputObjectTypeExtensionNode; + +export interface ScalarTypeExtensionNode { + readonly kind: Kind.SCALAR_TYPE_EXTENSION; + readonly loc?: Location; + readonly name: NameNode; + readonly directives?: ReadonlyArray; +} + +export interface ObjectTypeExtensionNode { + readonly kind: Kind.OBJECT_TYPE_EXTENSION; + readonly loc?: Location; + readonly name: NameNode; + readonly interfaces?: ReadonlyArray; + readonly directives?: ReadonlyArray; + readonly fields?: ReadonlyArray; +} + +export interface InterfaceTypeExtensionNode { + readonly kind: Kind.INTERFACE_TYPE_EXTENSION; + readonly loc?: Location; + readonly name: NameNode; + readonly interfaces?: ReadonlyArray; + readonly directives?: ReadonlyArray; + readonly fields?: ReadonlyArray; +} + +export interface UnionTypeExtensionNode { + readonly kind: Kind.UNION_TYPE_EXTENSION; + readonly loc?: Location; + readonly name: NameNode; + readonly directives?: ReadonlyArray; + readonly types?: ReadonlyArray; +} + +export interface EnumTypeExtensionNode { + readonly kind: Kind.ENUM_TYPE_EXTENSION; + readonly loc?: Location; + readonly name: NameNode; + readonly directives?: ReadonlyArray; + readonly values?: ReadonlyArray; +} + +export interface InputObjectTypeExtensionNode { + readonly kind: Kind.INPUT_OBJECT_TYPE_EXTENSION; + readonly loc?: Location; + readonly name: NameNode; + readonly directives?: ReadonlyArray; + readonly fields?: ReadonlyArray; +} diff --git a/packages/graphql/src/language/blockString.ts b/packages/graphql/src/language/blockString.ts new file mode 100644 index 00000000000..0ad5cacd37a --- /dev/null +++ b/packages/graphql/src/language/blockString.ts @@ -0,0 +1,157 @@ +import { isWhiteSpace } from './characterClasses.js'; + +/** + * Produces the value of a block string from its parsed raw value, similar to + * CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc. + * + * This implements the GraphQL spec's BlockStringValue() static algorithm. + * + * @internal + */ +export function dedentBlockStringLines(lines: ReadonlyArray): Array { + let commonIndent = Number.MAX_SAFE_INTEGER; + let firstNonEmptyLine = null; + let lastNonEmptyLine = -1; + + for (let i = 0; i < lines.length; ++i) { + const line = lines[i]; + const indent = leadingWhitespace(line); + + if (indent === line.length) { + continue; // skip empty lines + } + + firstNonEmptyLine = firstNonEmptyLine ?? i; + lastNonEmptyLine = i; + + if (i !== 0 && indent < commonIndent) { + commonIndent = indent; + } + } + + return ( + lines + // Remove common indentation from all lines but first. + .map((line, i) => (i === 0 ? line : line.slice(commonIndent))) + // Remove leading and trailing blank lines. + .slice(firstNonEmptyLine ?? 0, lastNonEmptyLine + 1) + ); +} + +function leadingWhitespace(str: string): number { + let i = 0; + while (i < str.length && isWhiteSpace(str.charCodeAt(i))) { + ++i; + } + return i; +} + +/** + * @internal + */ +export function isPrintableAsBlockString(value: string): boolean { + if (value === '') { + return true; // empty string is printable + } + + let isEmptyLine = true; + let hasIndent = false; + let hasCommonIndent = true; + let seenNonEmptyLine = false; + + for (let i = 0; i < value.length; ++i) { + switch (value.codePointAt(i)) { + case 0x0000: + case 0x0001: + case 0x0002: + case 0x0003: + case 0x0004: + case 0x0005: + case 0x0006: + case 0x0007: + case 0x0008: + case 0x000b: + case 0x000c: + case 0x000e: + case 0x000f: + return false; // Has non-printable characters + + case 0x000d: // \r + return false; // Has \r or \r\n which will be replaced as \n + + case 10: // \n + if (isEmptyLine && !seenNonEmptyLine) { + return false; // Has leading new line + } + seenNonEmptyLine = true; + + isEmptyLine = true; + hasIndent = false; + break; + case 9: // \t + case 32: // + hasIndent ||= isEmptyLine; + break; + default: + hasCommonIndent &&= hasIndent; + isEmptyLine = false; + } + } + + if (isEmptyLine) { + return false; // Has trailing empty lines + } + + if (hasCommonIndent && seenNonEmptyLine) { + return false; // Has internal indent + } + + return true; +} + +/** + * Print a block string in the indented block form by adding a leading and + * trailing blank line. However, if a block string starts with whitespace and is + * a single-line, adding a leading blank line would strip that whitespace. + * + * @internal + */ +export function printBlockString(value: string, options?: { minimize?: boolean }): string { + const escapedValue = value.replace(/"""/g, '\\"""'); + + // Expand a block string's raw value into independent lines. + const lines = escapedValue.split(/\r\n|[\n\r]/g); + const isSingleLine = lines.length === 1; + + // If common indentation is found we can fix some of those cases by adding leading new line + const forceLeadingNewLine = + lines.length > 1 && lines.slice(1).every(line => line.length === 0 || isWhiteSpace(line.charCodeAt(0))); + + // Trailing triple quotes just looks confusing but doesn't force trailing new line + const hasTrailingTripleQuotes = escapedValue.endsWith('\\"""'); + + // Trailing quote (single or double) or slash forces trailing new line + const hasTrailingQuote = value.endsWith('"') && !hasTrailingTripleQuotes; + const hasTrailingSlash = value.endsWith('\\'); + const forceTrailingNewline = hasTrailingQuote || hasTrailingSlash; + + const printAsMultipleLines = + !options?.minimize && + // add leading and trailing new lines only if it improves readability + (!isSingleLine || value.length > 70 || forceTrailingNewline || forceLeadingNewLine || hasTrailingTripleQuotes); + + let result = ''; + + // Format a multi-line block quote to account for leading space. + const skipLeadingNewLine = isSingleLine && isWhiteSpace(value.charCodeAt(0)); + if ((printAsMultipleLines && !skipLeadingNewLine) || forceLeadingNewLine) { + result += '\n'; + } + + result += escapedValue; + if (printAsMultipleLines || forceTrailingNewline) { + result += '\n'; + } + + return '"""' + result + '"""'; +} diff --git a/packages/graphql/src/language/characterClasses.ts b/packages/graphql/src/language/characterClasses.ts new file mode 100644 index 00000000000..c1182d10da6 --- /dev/null +++ b/packages/graphql/src/language/characterClasses.ts @@ -0,0 +1,64 @@ +/** + * ``` + * WhiteSpace :: + * - "Horizontal Tab (U+0009)" + * - "Space (U+0020)" + * ``` + * @internal + */ +export function isWhiteSpace(code: number): boolean { + return code === 0x0009 || code === 0x0020; +} + +/** + * ``` + * Digit :: one of + * - `0` `1` `2` `3` `4` `5` `6` `7` `8` `9` + * ``` + * @internal + */ +export function isDigit(code: number): boolean { + return code >= 0x0030 && code <= 0x0039; +} + +/** + * ``` + * Letter :: one of + * - `A` `B` `C` `D` `E` `F` `G` `H` `I` `J` `K` `L` `M` + * - `N` `O` `P` `Q` `R` `S` `T` `U` `V` `W` `X` `Y` `Z` + * - `a` `b` `c` `d` `e` `f` `g` `h` `i` `j` `k` `l` `m` + * - `n` `o` `p` `q` `r` `s` `t` `u` `v` `w` `x` `y` `z` + * ``` + * @internal + */ +export function isLetter(code: number): boolean { + return ( + (code >= 0x0061 && code <= 0x007a) || // A-Z + (code >= 0x0041 && code <= 0x005a) // a-z + ); +} + +/** + * ``` + * NameStart :: + * - Letter + * - `_` + * ``` + * @internal + */ +export function isNameStart(code: number): boolean { + return isLetter(code) || code === 0x005f; +} + +/** + * ``` + * NameContinue :: + * - Letter + * - Digit + * - `_` + * ``` + * @internal + */ +export function isNameContinue(code: number): boolean { + return isLetter(code) || isDigit(code) || code === 0x005f; +} diff --git a/packages/graphql/src/language/directiveLocation.ts b/packages/graphql/src/language/directiveLocation.ts new file mode 100644 index 00000000000..e98ddf6d751 --- /dev/null +++ b/packages/graphql/src/language/directiveLocation.ts @@ -0,0 +1,33 @@ +/** + * The set of allowed directive location values. + */ +export enum DirectiveLocation { + /** Request Definitions */ + QUERY = 'QUERY', + MUTATION = 'MUTATION', + SUBSCRIPTION = 'SUBSCRIPTION', + FIELD = 'FIELD', + FRAGMENT_DEFINITION = 'FRAGMENT_DEFINITION', + FRAGMENT_SPREAD = 'FRAGMENT_SPREAD', + INLINE_FRAGMENT = 'INLINE_FRAGMENT', + VARIABLE_DEFINITION = 'VARIABLE_DEFINITION', + /** Type System Definitions */ + SCHEMA = 'SCHEMA', + SCALAR = 'SCALAR', + OBJECT = 'OBJECT', + FIELD_DEFINITION = 'FIELD_DEFINITION', + ARGUMENT_DEFINITION = 'ARGUMENT_DEFINITION', + INTERFACE = 'INTERFACE', + UNION = 'UNION', + ENUM = 'ENUM', + ENUM_VALUE = 'ENUM_VALUE', + INPUT_OBJECT = 'INPUT_OBJECT', + INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION', +} + +/** + * The enum type representing the directive location values. + * + * @deprecated Please use `DirectiveLocation`. Will be remove in v17. + */ +export type DirectiveLocationEnum = typeof DirectiveLocation; diff --git a/packages/graphql/src/language/index.ts b/packages/graphql/src/language/index.ts new file mode 100644 index 00000000000..ac8118892bd --- /dev/null +++ b/packages/graphql/src/language/index.ts @@ -0,0 +1,15 @@ +export * from './ast.js'; +export * from './blockString.js'; +export * from './characterClasses.js'; +export * from './directiveLocation.js'; +export * from './kinds.js'; +export * from './lexer.js'; +export * from './location.js'; +export * from './parser.js'; +export * from './predicates.js'; +export * from './printLocation.js'; +export * from './printString.js'; +export * from './printer.js'; +export * from './source.js'; +export * from './tokenKind.js'; +export * from './visitor.js'; diff --git a/packages/graphql/src/language/kinds.ts b/packages/graphql/src/language/kinds.ts new file mode 100644 index 00000000000..c80351c0dc7 --- /dev/null +++ b/packages/graphql/src/language/kinds.ts @@ -0,0 +1,81 @@ +/** + * The set of allowed kind values for AST nodes. + */ +export enum Kind { + /** Name */ + NAME = 'Name', + + /** Document */ + DOCUMENT = 'Document', + OPERATION_DEFINITION = 'OperationDefinition', + VARIABLE_DEFINITION = 'VariableDefinition', + SELECTION_SET = 'SelectionSet', + FIELD = 'Field', + ARGUMENT = 'Argument', + + /** Nullability Modifiers */ + LIST_NULLABILITY_OPERATOR = 'ListNullabilityOperator', + NON_NULL_ASSERTION = 'NonNullAssertion', + ERROR_BOUNDARY = 'ErrorBoundary', + + /** Fragments */ + FRAGMENT_SPREAD = 'FragmentSpread', + INLINE_FRAGMENT = 'InlineFragment', + FRAGMENT_DEFINITION = 'FragmentDefinition', + + /** Values */ + VARIABLE = 'Variable', + INT = 'IntValue', + FLOAT = 'FloatValue', + STRING = 'StringValue', + BOOLEAN = 'BooleanValue', + NULL = 'NullValue', + ENUM = 'EnumValue', + LIST = 'ListValue', + OBJECT = 'ObjectValue', + OBJECT_FIELD = 'ObjectField', + + /** Directives */ + DIRECTIVE = 'Directive', + + /** Types */ + NAMED_TYPE = 'NamedType', + LIST_TYPE = 'ListType', + NON_NULL_TYPE = 'NonNullType', + + /** Type System Definitions */ + SCHEMA_DEFINITION = 'SchemaDefinition', + OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition', + + /** Type Definitions */ + SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition', + OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition', + FIELD_DEFINITION = 'FieldDefinition', + INPUT_VALUE_DEFINITION = 'InputValueDefinition', + INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition', + UNION_TYPE_DEFINITION = 'UnionTypeDefinition', + ENUM_TYPE_DEFINITION = 'EnumTypeDefinition', + ENUM_VALUE_DEFINITION = 'EnumValueDefinition', + INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition', + + /** Directive Definitions */ + DIRECTIVE_DEFINITION = 'DirectiveDefinition', + + /** Type System Extensions */ + SCHEMA_EXTENSION = 'SchemaExtension', + + /** Type Extensions */ + SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension', + OBJECT_TYPE_EXTENSION = 'ObjectTypeExtension', + INTERFACE_TYPE_EXTENSION = 'InterfaceTypeExtension', + UNION_TYPE_EXTENSION = 'UnionTypeExtension', + ENUM_TYPE_EXTENSION = 'EnumTypeExtension', + INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension', +} + +/** + * The enum type representing the possible kind values of AST nodes. + * + * @deprecated Please use `Kind`. Will be remove in v17. + */ +export type KindEnum = typeof Kind; diff --git a/packages/graphql/src/language/lexer.ts b/packages/graphql/src/language/lexer.ts new file mode 100644 index 00000000000..85022e565a7 --- /dev/null +++ b/packages/graphql/src/language/lexer.ts @@ -0,0 +1,788 @@ +import { syntaxError } from '../error/syntaxError.js'; + +import { Token } from './ast.js'; +import { dedentBlockStringLines } from './blockString.js'; +import { isDigit, isNameContinue, isNameStart } from './characterClasses.js'; +import type { Source } from './source.js'; +import { TokenKind } from './tokenKind.js'; + +/** + * Given a Source object, creates a Lexer for that source. + * A Lexer is a stateful stream generator in that every time + * it is advanced, it returns the next token in the Source. Assuming the + * source lexes, the final Token emitted by the lexer will be of kind + * EOF, after which the lexer will repeatedly return the same EOF token + * whenever called. + */ +export class Lexer { + source: Source; + + /** + * The previously focused non-ignored token. + */ + lastToken: Token; + + /** + * The currently focused non-ignored token. + */ + token: Token; + + /** + * The (1-indexed) line containing the current token. + */ + line: number; + + /** + * The character offset at which the current line begins. + */ + lineStart: number; + + constructor(source: Source) { + const startOfFileToken = new Token(TokenKind.SOF, 0, 0, 0, 0); + + this.source = source; + this.lastToken = startOfFileToken; + this.token = startOfFileToken; + this.line = 1; + this.lineStart = 0; + } + + get [Symbol.toStringTag]() { + return 'Lexer'; + } + + /** + * Advances the token stream to the next non-ignored token. + */ + advance(): Token { + this.lastToken = this.token; + const token = (this.token = this.lookahead()); + return token; + } + + /** + * Looks ahead and returns the next non-ignored token, but does not change + * the state of Lexer. + */ + lookahead(): Token { + let token = this.token; + if (token.kind !== TokenKind.EOF) { + do { + if (token.next) { + token = token.next; + } else { + // Read the next token and form a link in the token linked-list. + const nextToken = readNextToken(this, token.end); + // @ts-expect-error next is only mutable during parsing. + token.next = nextToken; + // @ts-expect-error prev is only mutable during parsing. + nextToken.prev = token; + token = nextToken; + } + } while (token.kind === TokenKind.COMMENT); + } + return token; + } +} + +/** + * @internal + */ +export function isPunctuatorTokenKind(kind: TokenKind): boolean { + return ( + kind === TokenKind.BANG || + kind === TokenKind.QUESTION_MARK || + kind === TokenKind.DOLLAR || + kind === TokenKind.AMP || + kind === TokenKind.PAREN_L || + kind === TokenKind.PAREN_R || + kind === TokenKind.SPREAD || + kind === TokenKind.COLON || + kind === TokenKind.EQUALS || + kind === TokenKind.AT || + kind === TokenKind.BRACKET_L || + kind === TokenKind.BRACKET_R || + kind === TokenKind.BRACE_L || + kind === TokenKind.PIPE || + kind === TokenKind.BRACE_R + ); +} + +/** + * A Unicode scalar value is any Unicode code point except surrogate code + * points. In other words, the inclusive ranges of values 0x0000 to 0xD7FF and + * 0xE000 to 0x10FFFF. + * + * SourceCharacter :: + * - "Any Unicode scalar value" + */ +function isUnicodeScalarValue(code: number): boolean { + return (code >= 0x0000 && code <= 0xd7ff) || (code >= 0xe000 && code <= 0x10ffff); +} + +/** + * The GraphQL specification defines source text as a sequence of unicode scalar + * values (which Unicode defines to exclude surrogate code points). However + * JavaScript defines strings as a sequence of UTF-16 code units which may + * include surrogates. A surrogate pair is a valid source character as it + * encodes a supplementary code point (above U+FFFF), but unpaired surrogate + * code points are not valid source characters. + */ +function isSupplementaryCodePoint(body: string, location: number): boolean { + return isLeadingSurrogate(body.charCodeAt(location)) && isTrailingSurrogate(body.charCodeAt(location + 1)); +} + +function isLeadingSurrogate(code: number): boolean { + return code >= 0xd800 && code <= 0xdbff; +} + +function isTrailingSurrogate(code: number): boolean { + return code >= 0xdc00 && code <= 0xdfff; +} + +/** + * Prints the code point (or end of file reference) at a given location in a + * source for use in error messages. + * + * Printable ASCII is printed quoted, while other points are printed in Unicode + * code point form (ie. U+1234). + */ +function printCodePointAt(lexer: Lexer, location: number): string { + const code = lexer.source.body.codePointAt(location); + + if (code === undefined) { + return TokenKind.EOF; + } else if (code >= 0x0020 && code <= 0x007e) { + // Printable ASCII + const char = String.fromCodePoint(code); + return char === '"' ? "'\"'" : `"${char}"`; + } + + // Unicode code point + return 'U+' + code.toString(16).toUpperCase().padStart(4, '0'); +} + +/** + * Create a token with line and column location information. + */ +function createToken(lexer: Lexer, kind: TokenKind, start: number, end: number, value?: string): Token { + const line = lexer.line; + const col = 1 + start - lexer.lineStart; + return new Token(kind, start, end, line, col, value); +} + +/** + * Gets the next token from the source starting at the given position. + * + * This skips over whitespace until it finds the next lexable token, then lexes + * punctuators immediately or calls the appropriate helper function for more + * complicated tokens. + */ +function readNextToken(lexer: Lexer, start: number): Token { + const body = lexer.source.body; + const bodyLength = body.length; + let position = start; + + while (position < bodyLength) { + const code = body.charCodeAt(position); + + // SourceCharacter + switch (code) { + // Ignored :: + // - UnicodeBOM + // - WhiteSpace + // - LineTerminator + // - Comment + // - Comma + // + // UnicodeBOM :: "Byte Order Mark (U+FEFF)" + // + // WhiteSpace :: + // - "Horizontal Tab (U+0009)" + // - "Space (U+0020)" + // + // Comma :: , + case 0xfeff: // + case 0x0009: // \t + case 0x0020: // + case 0x002c: // , + ++position; + continue; + // LineTerminator :: + // - "New Line (U+000A)" + // - "Carriage Return (U+000D)" [lookahead != "New Line (U+000A)"] + // - "Carriage Return (U+000D)" "New Line (U+000A)" + case 0x000a: // \n + ++position; + ++lexer.line; + lexer.lineStart = position; + continue; + case 0x000d: // \r + if (body.charCodeAt(position + 1) === 0x000a) { + position += 2; + } else { + ++position; + } + ++lexer.line; + lexer.lineStart = position; + continue; + // Comment + case 0x0023: // # + return readComment(lexer, position); + // Token :: + // - Punctuator + // - Name + // - IntValue + // - FloatValue + // - StringValue + // + // Punctuator :: one of ! $ & ( ) ... : = @ [ ] { | } + case 0x0021: // ! + return createToken(lexer, TokenKind.BANG, position, position + 1); + case 0x0024: // $ + return createToken(lexer, TokenKind.DOLLAR, position, position + 1); + case 0x0026: // & + return createToken(lexer, TokenKind.AMP, position, position + 1); + case 0x0028: // ( + return createToken(lexer, TokenKind.PAREN_L, position, position + 1); + case 0x0029: // ) + return createToken(lexer, TokenKind.PAREN_R, position, position + 1); + case 0x002e: // . + if (body.charCodeAt(position + 1) === 0x002e && body.charCodeAt(position + 2) === 0x002e) { + return createToken(lexer, TokenKind.SPREAD, position, position + 3); + } + break; + case 0x003a: // : + return createToken(lexer, TokenKind.COLON, position, position + 1); + case 0x003d: // = + return createToken(lexer, TokenKind.EQUALS, position, position + 1); + case 0x0040: // @ + return createToken(lexer, TokenKind.AT, position, position + 1); + case 0x005b: // [ + return createToken(lexer, TokenKind.BRACKET_L, position, position + 1); + case 0x005d: // ] + return createToken(lexer, TokenKind.BRACKET_R, position, position + 1); + case 0x007b: // { + return createToken(lexer, TokenKind.BRACE_L, position, position + 1); + case 0x007c: // | + return createToken(lexer, TokenKind.PIPE, position, position + 1); + case 0x007d: // } + return createToken(lexer, TokenKind.BRACE_R, position, position + 1); + case 0x003f: // ? + return createToken(lexer, TokenKind.QUESTION_MARK, position, position + 1); + // StringValue + case 0x0022: // " + if (body.charCodeAt(position + 1) === 0x0022 && body.charCodeAt(position + 2) === 0x0022) { + return readBlockString(lexer, position); + } + return readString(lexer, position); + } + + // IntValue | FloatValue (Digit | -) + if (isDigit(code) || code === 0x002d) { + return readNumber(lexer, position, code); + } + + // Name + if (isNameStart(code)) { + return readName(lexer, position); + } + + throw syntaxError( + lexer.source, + position, + code === 0x0027 + ? 'Unexpected single quote character (\'), did you mean to use a double quote (")?' + : isUnicodeScalarValue(code) || isSupplementaryCodePoint(body, position) + ? `Unexpected character: ${printCodePointAt(lexer, position)}.` + : `Invalid character: ${printCodePointAt(lexer, position)}.` + ); + } + + return createToken(lexer, TokenKind.EOF, bodyLength, bodyLength); +} + +/** + * Reads a comment token from the source file. + * + * ``` + * Comment :: # CommentChar* [lookahead != CommentChar] + * + * CommentChar :: SourceCharacter but not LineTerminator + * ``` + */ +function readComment(lexer: Lexer, start: number): Token { + const body = lexer.source.body; + const bodyLength = body.length; + let position = start + 1; + + while (position < bodyLength) { + const code = body.charCodeAt(position); + + // LineTerminator (\n | \r) + if (code === 0x000a || code === 0x000d) { + break; + } + + // SourceCharacter + if (isUnicodeScalarValue(code)) { + ++position; + } else if (isSupplementaryCodePoint(body, position)) { + position += 2; + } else { + break; + } + } + + return createToken(lexer, TokenKind.COMMENT, start, position, body.slice(start + 1, position)); +} + +/** + * Reads a number token from the source file, either a FloatValue or an IntValue + * depending on whether a FractionalPart or ExponentPart is encountered. + * + * ``` + * IntValue :: IntegerPart [lookahead != {Digit, `.`, NameStart}] + * + * IntegerPart :: + * - NegativeSign? 0 + * - NegativeSign? NonZeroDigit Digit* + * + * NegativeSign :: - + * + * NonZeroDigit :: Digit but not `0` + * + * FloatValue :: + * - IntegerPart FractionalPart ExponentPart [lookahead != {Digit, `.`, NameStart}] + * - IntegerPart FractionalPart [lookahead != {Digit, `.`, NameStart}] + * - IntegerPart ExponentPart [lookahead != {Digit, `.`, NameStart}] + * + * FractionalPart :: . Digit+ + * + * ExponentPart :: ExponentIndicator Sign? Digit+ + * + * ExponentIndicator :: one of `e` `E` + * + * Sign :: one of + - + * ``` + */ +function readNumber(lexer: Lexer, start: number, firstCode: number): Token { + const body = lexer.source.body; + let position = start; + let code = firstCode; + let isFloat = false; + + // NegativeSign (-) + if (code === 0x002d) { + code = body.charCodeAt(++position); + } + + // Zero (0) + if (code === 0x0030) { + code = body.charCodeAt(++position); + if (isDigit(code)) { + throw syntaxError( + lexer.source, + position, + `Invalid number, unexpected digit after 0: ${printCodePointAt(lexer, position)}.` + ); + } + } else { + position = readDigits(lexer, position, code); + code = body.charCodeAt(position); + } + + // Full stop (.) + if (code === 0x002e) { + isFloat = true; + + code = body.charCodeAt(++position); + position = readDigits(lexer, position, code); + code = body.charCodeAt(position); + } + + // E e + if (code === 0x0045 || code === 0x0065) { + isFloat = true; + + code = body.charCodeAt(++position); + // + - + if (code === 0x002b || code === 0x002d) { + code = body.charCodeAt(++position); + } + position = readDigits(lexer, position, code); + code = body.charCodeAt(position); + } + + // Numbers cannot be followed by . or NameStart + if (code === 0x002e || isNameStart(code)) { + throw syntaxError( + lexer.source, + position, + `Invalid number, expected digit but got: ${printCodePointAt(lexer, position)}.` + ); + } + + return createToken(lexer, isFloat ? TokenKind.FLOAT : TokenKind.INT, start, position, body.slice(start, position)); +} + +/** + * Returns the new position in the source after reading one or more digits. + */ +function readDigits(lexer: Lexer, start: number, firstCode: number): number { + if (!isDigit(firstCode)) { + throw syntaxError( + lexer.source, + start, + `Invalid number, expected digit but got: ${printCodePointAt(lexer, start)}.` + ); + } + + const body = lexer.source.body; + let position = start + 1; // +1 to skip first firstCode + + while (isDigit(body.charCodeAt(position))) { + ++position; + } + + return position; +} + +/** + * Reads a single-quote string token from the source file. + * + * ``` + * StringValue :: + * - `""` [lookahead != `"`] + * - `"` StringCharacter+ `"` + * + * StringCharacter :: + * - SourceCharacter but not `"` or `\` or LineTerminator + * - `\u` EscapedUnicode + * - `\` EscapedCharacter + * + * EscapedUnicode :: + * - `{` HexDigit+ `}` + * - HexDigit HexDigit HexDigit HexDigit + * + * EscapedCharacter :: one of `"` `\` `/` `b` `f` `n` `r` `t` + * ``` + */ +function readString(lexer: Lexer, start: number): Token { + const body = lexer.source.body; + const bodyLength = body.length; + let position = start + 1; + let chunkStart = position; + let value = ''; + + while (position < bodyLength) { + const code = body.charCodeAt(position); + + // Closing Quote (") + if (code === 0x0022) { + value += body.slice(chunkStart, position); + return createToken(lexer, TokenKind.STRING, start, position + 1, value); + } + + // Escape Sequence (\) + if (code === 0x005c) { + value += body.slice(chunkStart, position); + const escape = + body.charCodeAt(position + 1) === 0x0075 // u + ? body.charCodeAt(position + 2) === 0x007b // { + ? readEscapedUnicodeVariableWidth(lexer, position) + : readEscapedUnicodeFixedWidth(lexer, position) + : readEscapedCharacter(lexer, position); + value += escape.value; + position += escape.size; + chunkStart = position; + continue; + } + + // LineTerminator (\n | \r) + if (code === 0x000a || code === 0x000d) { + break; + } + + // SourceCharacter + if (isUnicodeScalarValue(code)) { + ++position; + } else if (isSupplementaryCodePoint(body, position)) { + position += 2; + } else { + throw syntaxError( + lexer.source, + position, + `Invalid character within String: ${printCodePointAt(lexer, position)}.` + ); + } + } + + throw syntaxError(lexer.source, position, 'Unterminated string.'); +} + +// The string value and lexed size of an escape sequence. +interface EscapeSequence { + value: string; + size: number; +} + +function readEscapedUnicodeVariableWidth(lexer: Lexer, position: number): EscapeSequence { + const body = lexer.source.body; + let point = 0; + let size = 3; + // Cannot be larger than 12 chars (\u{00000000}). + while (size < 12) { + const code = body.charCodeAt(position + size++); + // Closing Brace (}) + if (code === 0x007d) { + // Must be at least 5 chars (\u{0}) and encode a Unicode scalar value. + if (size < 5 || !isUnicodeScalarValue(point)) { + break; + } + return { value: String.fromCodePoint(point), size }; + } + // Append this hex digit to the code point. + point = (point << 4) | readHexDigit(code); + if (point < 0) { + break; + } + } + + throw syntaxError( + lexer.source, + position, + `Invalid Unicode escape sequence: "${body.slice(position, position + size)}".` + ); +} + +function readEscapedUnicodeFixedWidth(lexer: Lexer, position: number): EscapeSequence { + const body = lexer.source.body; + const code = read16BitHexCode(body, position + 2); + + if (isUnicodeScalarValue(code)) { + return { value: String.fromCodePoint(code), size: 6 }; + } + + // GraphQL allows JSON-style surrogate pair escape sequences, but only when + // a valid pair is formed. + if (isLeadingSurrogate(code)) { + // \u + if (body.charCodeAt(position + 6) === 0x005c && body.charCodeAt(position + 7) === 0x0075) { + const trailingCode = read16BitHexCode(body, position + 8); + if (isTrailingSurrogate(trailingCode)) { + // JavaScript defines strings as a sequence of UTF-16 code units and + // encodes Unicode code points above U+FFFF using a surrogate pair of + // code units. Since this is a surrogate pair escape sequence, just + // include both codes into the JavaScript string value. Had JavaScript + // not been internally based on UTF-16, then this surrogate pair would + // be decoded to retrieve the supplementary code point. + return { value: String.fromCodePoint(code, trailingCode), size: 12 }; + } + } + } + + throw syntaxError( + lexer.source, + position, + `Invalid Unicode escape sequence: "${body.slice(position, position + 6)}".` + ); +} + +/** + * Reads four hexadecimal characters and returns the positive integer that 16bit + * hexadecimal string represents. For example, "000f" will return 15, and "dead" + * will return 57005. + * + * Returns a negative number if any char was not a valid hexadecimal digit. + */ +function read16BitHexCode(body: string, position: number): number { + // readHexDigit() returns -1 on error. ORing a negative value with any other + // value always produces a negative value. + return ( + (readHexDigit(body.charCodeAt(position)) << 12) | + (readHexDigit(body.charCodeAt(position + 1)) << 8) | + (readHexDigit(body.charCodeAt(position + 2)) << 4) | + readHexDigit(body.charCodeAt(position + 3)) + ); +} + +/** + * Reads a hexadecimal character and returns its positive integer value (0-15). + * + * '0' becomes 0, '9' becomes 9 + * 'A' becomes 10, 'F' becomes 15 + * 'a' becomes 10, 'f' becomes 15 + * + * Returns -1 if the provided character code was not a valid hexadecimal digit. + * + * HexDigit :: one of + * - `0` `1` `2` `3` `4` `5` `6` `7` `8` `9` + * - `A` `B` `C` `D` `E` `F` + * - `a` `b` `c` `d` `e` `f` + */ +function readHexDigit(code: number): number { + return code >= 0x0030 && code <= 0x0039 // 0-9 + ? code - 0x0030 + : code >= 0x0041 && code <= 0x0046 // A-F + ? code - 0x0037 + : code >= 0x0061 && code <= 0x0066 // a-f + ? code - 0x0057 + : -1; +} + +/** + * | Escaped Character | Code Point | Character Name | + * | ----------------- | ---------- | ---------------------------- | + * | `"` | U+0022 | double quote | + * | `\` | U+005C | reverse solidus (back slash) | + * | `/` | U+002F | solidus (forward slash) | + * | `b` | U+0008 | backspace | + * | `f` | U+000C | form feed | + * | `n` | U+000A | line feed (new line) | + * | `r` | U+000D | carriage return | + * | `t` | U+0009 | horizontal tab | + */ +function readEscapedCharacter(lexer: Lexer, position: number): EscapeSequence { + const body = lexer.source.body; + const code = body.charCodeAt(position + 1); + switch (code) { + case 0x0022: // " + return { value: '\u0022', size: 2 }; + case 0x005c: // \ + return { value: '\u005c', size: 2 }; + case 0x002f: // / + return { value: '\u002f', size: 2 }; + case 0x0062: // b + return { value: '\u0008', size: 2 }; + case 0x0066: // f + return { value: '\u000c', size: 2 }; + case 0x006e: // n + return { value: '\u000a', size: 2 }; + case 0x0072: // r + return { value: '\u000d', size: 2 }; + case 0x0074: // t + return { value: '\u0009', size: 2 }; + } + throw syntaxError( + lexer.source, + position, + `Invalid character escape sequence: "${body.slice(position, position + 2)}".` + ); +} + +/** + * Reads a block string token from the source file. + * + * ``` + * StringValue :: + * - `"""` BlockStringCharacter* `"""` + * + * BlockStringCharacter :: + * - SourceCharacter but not `"""` or `\"""` + * - `\"""` + * ``` + */ +function readBlockString(lexer: Lexer, start: number): Token { + const body = lexer.source.body; + const bodyLength = body.length; + let lineStart = lexer.lineStart; + + let position = start + 3; + let chunkStart = position; + let currentLine = ''; + + const blockLines = []; + while (position < bodyLength) { + const code = body.charCodeAt(position); + + // Closing Triple-Quote (""") + if (code === 0x0022 && body.charCodeAt(position + 1) === 0x0022 && body.charCodeAt(position + 2) === 0x0022) { + currentLine += body.slice(chunkStart, position); + blockLines.push(currentLine); + + const token = createToken( + lexer, + TokenKind.BLOCK_STRING, + start, + position + 3, + // Return a string of the lines joined with U+000A. + dedentBlockStringLines(blockLines).join('\n') + ); + + lexer.line += blockLines.length - 1; + lexer.lineStart = lineStart; + return token; + } + + // Escaped Triple-Quote (\""") + if ( + code === 0x005c && + body.charCodeAt(position + 1) === 0x0022 && + body.charCodeAt(position + 2) === 0x0022 && + body.charCodeAt(position + 3) === 0x0022 + ) { + currentLine += body.slice(chunkStart, position); + chunkStart = position + 1; // skip only slash + position += 4; + continue; + } + + // LineTerminator + if (code === 0x000a || code === 0x000d) { + currentLine += body.slice(chunkStart, position); + blockLines.push(currentLine); + + if (code === 0x000d && body.charCodeAt(position + 1) === 0x000a) { + position += 2; + } else { + ++position; + } + + currentLine = ''; + chunkStart = position; + lineStart = position; + continue; + } + + // SourceCharacter + if (isUnicodeScalarValue(code)) { + ++position; + } else if (isSupplementaryCodePoint(body, position)) { + position += 2; + } else { + throw syntaxError( + lexer.source, + position, + `Invalid character within String: ${printCodePointAt(lexer, position)}.` + ); + } + } + + throw syntaxError(lexer.source, position, 'Unterminated string.'); +} + +/** + * Reads an alphanumeric + underscore name from the source. + * + * ``` + * Name :: + * - NameStart NameContinue* [lookahead != NameContinue] + * ``` + */ +function readName(lexer: Lexer, start: number): Token { + const body = lexer.source.body; + const bodyLength = body.length; + let position = start + 1; + + while (position < bodyLength) { + const code = body.charCodeAt(position); + if (isNameContinue(code)) { + ++position; + } else { + break; + } + } + + return createToken(lexer, TokenKind.NAME, start, position, body.slice(start, position)); +} diff --git a/packages/graphql/src/language/location.ts b/packages/graphql/src/language/location.ts new file mode 100644 index 00000000000..105137f8a32 --- /dev/null +++ b/packages/graphql/src/language/location.ts @@ -0,0 +1,33 @@ +import { invariant } from '../jsutils/invariant.js'; + +import type { Source } from './source.js'; + +const LineRegExp = /\r\n|[\n\r]/g; + +/** + * Represents a location in a Source. + */ +export interface SourceLocation { + readonly line: number; + readonly column: number; +} + +/** + * Takes a Source and a UTF-8 character offset, and returns the corresponding + * line and column as a SourceLocation. + */ +export function getLocation(source: Source, position: number): SourceLocation { + let lastLineStart = 0; + let line = 1; + + for (const match of source.body.matchAll(LineRegExp)) { + invariant(typeof match.index === 'number'); + if (match.index >= position) { + break; + } + lastLineStart = match.index + match[0].length; + line += 1; + } + + return { line, column: position + 1 - lastLineStart }; +} diff --git a/packages/graphql/src/language/parser.ts b/packages/graphql/src/language/parser.ts new file mode 100644 index 00000000000..f006ef2df9a --- /dev/null +++ b/packages/graphql/src/language/parser.ts @@ -0,0 +1,1534 @@ +import type { Maybe } from '../jsutils/Maybe.js'; + +import type { GraphQLError } from '../error/GraphQLError.js'; +import { syntaxError } from '../error/syntaxError.js'; + +import type { + ArgumentNode, + BooleanValueNode, + ConstArgumentNode, + ConstDirectiveNode, + ConstListValueNode, + ConstObjectFieldNode, + ConstObjectValueNode, + ConstValueNode, + DefinitionNode, + DirectiveDefinitionNode, + DirectiveNode, + DocumentNode, + EnumTypeDefinitionNode, + EnumTypeExtensionNode, + EnumValueDefinitionNode, + EnumValueNode, + ErrorBoundaryNode, + FieldDefinitionNode, + FieldNode, + FloatValueNode, + FragmentDefinitionNode, + FragmentSpreadNode, + InlineFragmentNode, + InputObjectTypeDefinitionNode, + InputObjectTypeExtensionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + InterfaceTypeExtensionNode, + IntValueNode, + ListNullabilityOperatorNode, + ListTypeNode, + ListValueNode, + NamedTypeNode, + NameNode, + NonNullAssertionNode, + NonNullTypeNode, + NullabilityAssertionNode, + NullValueNode, + ObjectFieldNode, + ObjectTypeDefinitionNode, + ObjectTypeExtensionNode, + ObjectValueNode, + OperationDefinitionNode, + OperationTypeDefinitionNode, + ScalarTypeDefinitionNode, + ScalarTypeExtensionNode, + SchemaDefinitionNode, + SchemaExtensionNode, + SelectionNode, + SelectionSetNode, + StringValueNode, + Token, + TypeNode, + TypeSystemExtensionNode, + UnionTypeDefinitionNode, + UnionTypeExtensionNode, + ValueNode, + VariableDefinitionNode, + VariableNode, +} from './ast.js'; +import { Location, OperationTypeNode } from './ast.js'; +import { DirectiveLocation } from './directiveLocation.js'; +import { Kind } from './kinds.js'; +import { isPunctuatorTokenKind, Lexer } from './lexer.js'; +import { isSource, Source } from './source.js'; +import { TokenKind } from './tokenKind.js'; + +/** + * Configuration options to control parser behavior + */ +export interface ParseOptions { + /** + * By default, the parser creates AST nodes that know the location + * in the source that they correspond to. This configuration flag + * disables that behavior for performance or testing. + */ + noLocation?: boolean; + + /** + * @deprecated will be removed in the v17.0.0 + * + * If enabled, the parser will understand and parse variable definitions + * contained in a fragment definition. They'll be represented in the + * `variableDefinitions` field of the FragmentDefinitionNode. + * + * The syntax is identical to normal, query-defined variables. For example: + * + * ```graphql + * fragment A($var: Boolean = false) on T { + * ... + * } + * ``` + */ + allowLegacyFragmentVariables?: boolean; + + /** + * EXPERIMENTAL: + * + * If enabled, the parser will understand and parse Client Controlled Nullability + * Designators contained in Fields. They'll be represented in the + * `nullabilityAssertion` field of the FieldNode. + * + * The syntax looks like the following: + * + * ```graphql + * { + * nullableField! + * nonNullableField? + * nonNullableSelectionSet? { + * childField! + * } + * } + * ``` + * Note: this feature is experimental and may change or be removed in the + * future. + */ + experimentalClientControlledNullability?: boolean; +} + +/** + * Given a GraphQL source, parses it into a Document. + * Throws GraphQLError if a syntax error is encountered. + */ +export function parse(source: string | Source, options?: ParseOptions): DocumentNode { + const parser = new Parser(source, options); + return parser.parseDocument(); +} + +/** + * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for + * that value. + * Throws GraphQLError if a syntax error is encountered. + * + * This is useful within tools that operate upon GraphQL Values directly and + * in isolation of complete GraphQL documents. + * + * Consider providing the results to the utility function: valueFromAST(). + */ +export function parseValue(source: string | Source, options?: ParseOptions): ValueNode { + const parser = new Parser(source, options); + parser.expectToken(TokenKind.SOF); + const value = parser.parseValueLiteral(false); + parser.expectToken(TokenKind.EOF); + return value; +} + +/** + * Similar to parseValue(), but raises a parse error if it encounters a + * variable. The return type will be a constant value. + */ +export function parseConstValue(source: string | Source, options?: ParseOptions): ConstValueNode { + const parser = new Parser(source, options); + parser.expectToken(TokenKind.SOF); + const value = parser.parseConstValueLiteral(); + parser.expectToken(TokenKind.EOF); + return value; +} + +/** + * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for + * that type. + * Throws GraphQLError if a syntax error is encountered. + * + * This is useful within tools that operate upon GraphQL Types directly and + * in isolation of complete GraphQL documents. + * + * Consider providing the results to the utility function: typeFromAST(). + */ +export function parseType(source: string | Source, options?: ParseOptions): TypeNode { + const parser = new Parser(source, options); + parser.expectToken(TokenKind.SOF); + const type = parser.parseTypeReference(); + parser.expectToken(TokenKind.EOF); + return type; +} + +/** + * This class is exported only to assist people in implementing their own parsers + * without duplicating too much code and should be used only as last resort for cases + * such as experimental syntax or if certain features could not be contributed upstream. + * + * It is still part of the internal API and is versioned, so any changes to it are never + * considered breaking changes. If you still need to support multiple versions of the + * library, please use the `versionInfo` variable for version detection. + * + * @internal + */ +export class Parser { + protected _options: Maybe; + protected _lexer: Lexer; + + constructor(source: string | Source, options?: ParseOptions) { + const sourceObj = isSource(source) ? source : new Source(source); + + this._lexer = new Lexer(sourceObj); + this._options = options; + } + + /** + * Converts a name lex token into a name parse node. + */ + parseName(): NameNode { + const token = this.expectToken(TokenKind.NAME); + return this.node(token, { + kind: Kind.NAME, + value: token.value, + }); + } + + // Implements the parsing rules in the Document section. + + /** + * Document : Definition+ + */ + parseDocument(): DocumentNode { + return this.node(this._lexer.token, { + kind: Kind.DOCUMENT, + definitions: this.many(TokenKind.SOF, this.parseDefinition, TokenKind.EOF), + }); + } + + /** + * Definition : + * - ExecutableDefinition + * - TypeSystemDefinition + * - TypeSystemExtension + * + * ExecutableDefinition : + * - OperationDefinition + * - FragmentDefinition + * + * TypeSystemDefinition : + * - SchemaDefinition + * - TypeDefinition + * - DirectiveDefinition + * + * TypeDefinition : + * - ScalarTypeDefinition + * - ObjectTypeDefinition + * - InterfaceTypeDefinition + * - UnionTypeDefinition + * - EnumTypeDefinition + * - InputObjectTypeDefinition + */ + parseDefinition(): DefinitionNode { + if (this.peek(TokenKind.BRACE_L)) { + return this.parseOperationDefinition(); + } + + // Many definitions begin with a description and require a lookahead. + const hasDescription = this.peekDescription(); + const keywordToken = hasDescription ? this._lexer.lookahead() : this._lexer.token; + + if (keywordToken.kind === TokenKind.NAME) { + switch (keywordToken.value) { + case 'schema': + return this.parseSchemaDefinition(); + case 'scalar': + return this.parseScalarTypeDefinition(); + case 'type': + return this.parseObjectTypeDefinition(); + case 'interface': + return this.parseInterfaceTypeDefinition(); + case 'union': + return this.parseUnionTypeDefinition(); + case 'enum': + return this.parseEnumTypeDefinition(); + case 'input': + return this.parseInputObjectTypeDefinition(); + case 'directive': + return this.parseDirectiveDefinition(); + } + + if (hasDescription) { + throw syntaxError( + this._lexer.source, + this._lexer.token.start, + 'Unexpected description, descriptions are supported only on type definitions.' + ); + } + + switch (keywordToken.value) { + case 'query': + case 'mutation': + case 'subscription': + return this.parseOperationDefinition(); + case 'fragment': + return this.parseFragmentDefinition(); + case 'extend': + return this.parseTypeSystemExtension(); + } + } + + throw this.unexpected(keywordToken); + } + + // Implements the parsing rules in the Operations section. + + /** + * OperationDefinition : + * - SelectionSet + * - OperationType Name? VariableDefinitions? Directives? SelectionSet + */ + parseOperationDefinition(): OperationDefinitionNode { + const start = this._lexer.token; + if (this.peek(TokenKind.BRACE_L)) { + return this.node(start, { + kind: Kind.OPERATION_DEFINITION, + operation: OperationTypeNode.QUERY, + name: undefined, + variableDefinitions: [], + directives: [], + selectionSet: this.parseSelectionSet(), + }); + } + const operation = this.parseOperationType(); + let name; + if (this.peek(TokenKind.NAME)) { + name = this.parseName(); + } + return this.node(start, { + kind: Kind.OPERATION_DEFINITION, + operation, + name, + variableDefinitions: this.parseVariableDefinitions(), + directives: this.parseDirectives(false), + selectionSet: this.parseSelectionSet(), + }); + } + + /** + * OperationType : one of query mutation subscription + */ + parseOperationType(): OperationTypeNode { + const operationToken = this.expectToken(TokenKind.NAME); + switch (operationToken.value) { + case 'query': + return OperationTypeNode.QUERY; + case 'mutation': + return OperationTypeNode.MUTATION; + case 'subscription': + return OperationTypeNode.SUBSCRIPTION; + } + + throw this.unexpected(operationToken); + } + + /** + * VariableDefinitions : ( VariableDefinition+ ) + */ + parseVariableDefinitions(): Array { + return this.optionalMany(TokenKind.PAREN_L, this.parseVariableDefinition, TokenKind.PAREN_R); + } + + /** + * VariableDefinition : Variable : Type DefaultValue? Directives[Const]? + */ + parseVariableDefinition(): VariableDefinitionNode { + return this.node(this._lexer.token, { + kind: Kind.VARIABLE_DEFINITION, + variable: this.parseVariable(), + type: (this.expectToken(TokenKind.COLON), this.parseTypeReference()), + defaultValue: this.expectOptionalToken(TokenKind.EQUALS) ? this.parseConstValueLiteral() : undefined, + directives: this.parseConstDirectives(), + }); + } + + /** + * Variable : $ Name + */ + parseVariable(): VariableNode { + const start = this._lexer.token; + this.expectToken(TokenKind.DOLLAR); + return this.node(start, { + kind: Kind.VARIABLE, + name: this.parseName(), + }); + } + + /** + * ``` + * SelectionSet : { Selection+ } + * ``` + */ + parseSelectionSet(): SelectionSetNode { + return this.node(this._lexer.token, { + kind: Kind.SELECTION_SET, + selections: this.many(TokenKind.BRACE_L, this.parseSelection, TokenKind.BRACE_R), + }); + } + + /** + * Selection : + * - Field + * - FragmentSpread + * - InlineFragment + */ + parseSelection(): SelectionNode { + return this.peek(TokenKind.SPREAD) ? this.parseFragment() : this.parseField(); + } + + /** + * Field : Alias? Name Arguments? Directives? SelectionSet? + * + * Alias : Name : + */ + parseField(): FieldNode { + const start = this._lexer.token; + + const nameOrAlias = this.parseName(); + let alias; + let name; + if (this.expectOptionalToken(TokenKind.COLON)) { + alias = nameOrAlias; + name = this.parseName(); + } else { + name = nameOrAlias; + } + + return this.node(start, { + kind: Kind.FIELD, + alias, + name, + arguments: this.parseArguments(false), + // Experimental support for Client Controlled Nullability changes + // the grammar of Field: + nullabilityAssertion: this.parseNullabilityAssertion(), + directives: this.parseDirectives(false), + selectionSet: this.peek(TokenKind.BRACE_L) ? this.parseSelectionSet() : undefined, + }); + } + + // TODO: add grammar comment after it finalizes + parseNullabilityAssertion(): NullabilityAssertionNode | undefined { + // Note: Client Controlled Nullability is experimental and may be changed or + // removed in the future. + if (this._options?.experimentalClientControlledNullability !== true) { + return undefined; + } + + const start = this._lexer.token; + let nullabilityAssertion; + + if (this.expectOptionalToken(TokenKind.BRACKET_L)) { + const innerModifier = this.parseNullabilityAssertion(); + this.expectToken(TokenKind.BRACKET_R); + nullabilityAssertion = this.node(start, { + kind: Kind.LIST_NULLABILITY_OPERATOR, + nullabilityAssertion: innerModifier, + }); + } + + if (this.expectOptionalToken(TokenKind.BANG)) { + nullabilityAssertion = this.node(start, { + kind: Kind.NON_NULL_ASSERTION, + nullabilityAssertion, + }); + } else if (this.expectOptionalToken(TokenKind.QUESTION_MARK)) { + nullabilityAssertion = this.node(start, { + kind: Kind.ERROR_BOUNDARY, + nullabilityAssertion, + }); + } + + return nullabilityAssertion; + } + + /** + * Arguments[Const] : ( Argument[?Const]+ ) + */ + parseArguments(isConst: true): Array; + parseArguments(isConst: boolean): Array; + parseArguments(isConst: boolean): Array { + const item = isConst ? this.parseConstArgument : this.parseArgument; + return this.optionalMany(TokenKind.PAREN_L, item, TokenKind.PAREN_R); + } + + /** + * Argument[Const] : Name : Value[?Const] + */ + parseArgument(isConst: true): ConstArgumentNode; + parseArgument(isConst?: boolean): ArgumentNode; + parseArgument(isConst: boolean = false): ArgumentNode { + const start = this._lexer.token; + const name = this.parseName(); + + this.expectToken(TokenKind.COLON); + return this.node(start, { + kind: Kind.ARGUMENT, + name, + value: this.parseValueLiteral(isConst), + }); + } + + parseConstArgument(): ConstArgumentNode { + return this.parseArgument(true); + } + + // Implements the parsing rules in the Fragments section. + + /** + * Corresponds to both FragmentSpread and InlineFragment in the spec. + * + * FragmentSpread : ... FragmentName Directives? + * + * InlineFragment : ... TypeCondition? Directives? SelectionSet + */ + parseFragment(): FragmentSpreadNode | InlineFragmentNode { + const start = this._lexer.token; + this.expectToken(TokenKind.SPREAD); + + const hasTypeCondition = this.expectOptionalKeyword('on'); + if (!hasTypeCondition && this.peek(TokenKind.NAME)) { + return this.node(start, { + kind: Kind.FRAGMENT_SPREAD, + name: this.parseFragmentName(), + directives: this.parseDirectives(false), + }); + } + return this.node(start, { + kind: Kind.INLINE_FRAGMENT, + typeCondition: hasTypeCondition ? this.parseNamedType() : undefined, + directives: this.parseDirectives(false), + selectionSet: this.parseSelectionSet(), + }); + } + + /** + * FragmentDefinition : + * - fragment FragmentName on TypeCondition Directives? SelectionSet + * + * TypeCondition : NamedType + */ + parseFragmentDefinition(): FragmentDefinitionNode { + const start = this._lexer.token; + this.expectKeyword('fragment'); + // Legacy support for defining variables within fragments changes + // the grammar of FragmentDefinition: + // - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet + if (this._options?.allowLegacyFragmentVariables === true) { + return this.node(start, { + kind: Kind.FRAGMENT_DEFINITION, + name: this.parseFragmentName(), + variableDefinitions: this.parseVariableDefinitions(), + typeCondition: (this.expectKeyword('on'), this.parseNamedType()), + directives: this.parseDirectives(false), + selectionSet: this.parseSelectionSet(), + }); + } + return this.node(start, { + kind: Kind.FRAGMENT_DEFINITION, + name: this.parseFragmentName(), + typeCondition: (this.expectKeyword('on'), this.parseNamedType()), + directives: this.parseDirectives(false), + selectionSet: this.parseSelectionSet(), + }); + } + + /** + * FragmentName : Name but not `on` + */ + parseFragmentName(): NameNode { + if (this._lexer.token.value === 'on') { + throw this.unexpected(); + } + return this.parseName(); + } + + // Implements the parsing rules in the Values section. + + /** + * Value[Const] : + * - [~Const] Variable + * - IntValue + * - FloatValue + * - StringValue + * - BooleanValue + * - NullValue + * - EnumValue + * - ListValue[?Const] + * - ObjectValue[?Const] + * + * BooleanValue : one of `true` `false` + * + * NullValue : `null` + * + * EnumValue : Name but not `true`, `false` or `null` + */ + parseValueLiteral(isConst: true): ConstValueNode; + parseValueLiteral(isConst: boolean): ValueNode; + parseValueLiteral(isConst: boolean): ValueNode { + const token = this._lexer.token; + switch (token.kind) { + case TokenKind.BRACKET_L: + return this.parseList(isConst); + case TokenKind.BRACE_L: + return this.parseObject(isConst); + case TokenKind.INT: + this._lexer.advance(); + return this.node(token, { + kind: Kind.INT, + value: token.value, + }); + case TokenKind.FLOAT: + this._lexer.advance(); + return this.node(token, { + kind: Kind.FLOAT, + value: token.value, + }); + case TokenKind.STRING: + case TokenKind.BLOCK_STRING: + return this.parseStringLiteral(); + case TokenKind.NAME: + this._lexer.advance(); + switch (token.value) { + case 'true': + return this.node(token, { + kind: Kind.BOOLEAN, + value: true, + }); + case 'false': + return this.node(token, { + kind: Kind.BOOLEAN, + value: false, + }); + case 'null': + return this.node(token, { kind: Kind.NULL }); + default: + return this.node(token, { + kind: Kind.ENUM, + value: token.value, + }); + } + case TokenKind.DOLLAR: + if (isConst) { + this.expectToken(TokenKind.DOLLAR); + if (this._lexer.token.kind === TokenKind.NAME) { + const varName = this._lexer.token.value; + throw syntaxError(this._lexer.source, token.start, `Unexpected variable "$${varName}" in constant value.`); + } else { + throw this.unexpected(token); + } + } + return this.parseVariable(); + default: + throw this.unexpected(); + } + } + + parseConstValueLiteral(): ConstValueNode { + return this.parseValueLiteral(true); + } + + parseStringLiteral(): StringValueNode { + const token = this._lexer.token; + this._lexer.advance(); + return this.node(token, { + kind: Kind.STRING, + value: token.value, + block: token.kind === TokenKind.BLOCK_STRING, + }); + } + + /** + * ListValue[Const] : + * - [ ] + * - [ Value[?Const]+ ] + */ + parseList(isConst: true): ConstListValueNode; + parseList(isConst: boolean): ListValueNode; + parseList(isConst: boolean): ListValueNode { + const item = () => this.parseValueLiteral(isConst); + return this.node(this._lexer.token, { + kind: Kind.LIST, + values: this.any(TokenKind.BRACKET_L, item, TokenKind.BRACKET_R), + }); + } + + /** + * ``` + * ObjectValue[Const] : + * - { } + * - { ObjectField[?Const]+ } + * ``` + */ + parseObject(isConst: true): ConstObjectValueNode; + parseObject(isConst: boolean): ObjectValueNode; + parseObject(isConst: boolean): ObjectValueNode { + const item = () => this.parseObjectField(isConst); + return this.node(this._lexer.token, { + kind: Kind.OBJECT, + fields: this.any(TokenKind.BRACE_L, item, TokenKind.BRACE_R), + }); + } + + /** + * ObjectField[Const] : Name : Value[?Const] + */ + parseObjectField(isConst: true): ConstObjectFieldNode; + parseObjectField(isConst: boolean): ObjectFieldNode; + parseObjectField(isConst: boolean): ObjectFieldNode { + const start = this._lexer.token; + const name = this.parseName(); + this.expectToken(TokenKind.COLON); + return this.node(start, { + kind: Kind.OBJECT_FIELD, + name, + value: this.parseValueLiteral(isConst), + }); + } + + // Implements the parsing rules in the Directives section. + + /** + * Directives[Const] : Directive[?Const]+ + */ + parseDirectives(isConst: true): Array; + parseDirectives(isConst: boolean): Array; + parseDirectives(isConst: boolean): Array { + const directives = []; + while (this.peek(TokenKind.AT)) { + directives.push(this.parseDirective(isConst)); + } + return directives; + } + + parseConstDirectives(): Array { + return this.parseDirectives(true); + } + + /** + * ``` + * Directive[Const] : @ Name Arguments[?Const]? + * ``` + */ + parseDirective(isConst: true): ConstDirectiveNode; + parseDirective(isConst: boolean): DirectiveNode; + parseDirective(isConst: boolean): DirectiveNode { + const start = this._lexer.token; + this.expectToken(TokenKind.AT); + return this.node(start, { + kind: Kind.DIRECTIVE, + name: this.parseName(), + arguments: this.parseArguments(isConst), + }); + } + + // Implements the parsing rules in the Types section. + + /** + * Type : + * - NamedType + * - ListType + * - NonNullType + */ + parseTypeReference(): TypeNode { + const start = this._lexer.token; + let type; + if (this.expectOptionalToken(TokenKind.BRACKET_L)) { + const innerType = this.parseTypeReference(); + this.expectToken(TokenKind.BRACKET_R); + type = this.node(start, { + kind: Kind.LIST_TYPE, + type: innerType, + }); + } else { + type = this.parseNamedType(); + } + + if (this.expectOptionalToken(TokenKind.BANG)) { + return this.node(start, { + kind: Kind.NON_NULL_TYPE, + type, + }); + } + + return type; + } + + /** + * NamedType : Name + */ + parseNamedType(): NamedTypeNode { + return this.node(this._lexer.token, { + kind: Kind.NAMED_TYPE, + name: this.parseName(), + }); + } + + // Implements the parsing rules in the Type Definition section. + + peekDescription(): boolean { + return this.peek(TokenKind.STRING) || this.peek(TokenKind.BLOCK_STRING); + } + + /** + * Description : StringValue + */ + parseDescription(): undefined | StringValueNode { + if (this.peekDescription()) { + return this.parseStringLiteral(); + } + return undefined; + } + + /** + * ``` + * SchemaDefinition : Description? schema Directives[Const]? { OperationTypeDefinition+ } + * ``` + */ + parseSchemaDefinition(): SchemaDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + this.expectKeyword('schema'); + const directives = this.parseConstDirectives(); + const operationTypes = this.many(TokenKind.BRACE_L, this.parseOperationTypeDefinition, TokenKind.BRACE_R); + return this.node(start, { + kind: Kind.SCHEMA_DEFINITION, + description, + directives, + operationTypes, + }); + } + + /** + * OperationTypeDefinition : OperationType : NamedType + */ + parseOperationTypeDefinition(): OperationTypeDefinitionNode { + const start = this._lexer.token; + const operation = this.parseOperationType(); + this.expectToken(TokenKind.COLON); + const type = this.parseNamedType(); + return this.node(start, { + kind: Kind.OPERATION_TYPE_DEFINITION, + operation, + type, + }); + } + + /** + * ScalarTypeDefinition : Description? scalar Name Directives[Const]? + */ + parseScalarTypeDefinition(): ScalarTypeDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + this.expectKeyword('scalar'); + const name = this.parseName(); + const directives = this.parseConstDirectives(); + return this.node(start, { + kind: Kind.SCALAR_TYPE_DEFINITION, + description, + name, + directives, + }); + } + + /** + * ObjectTypeDefinition : + * Description? + * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition? + */ + parseObjectTypeDefinition(): ObjectTypeDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + this.expectKeyword('type'); + const name = this.parseName(); + const interfaces = this.parseImplementsInterfaces(); + const directives = this.parseConstDirectives(); + const fields = this.parseFieldsDefinition(); + return this.node(start, { + kind: Kind.OBJECT_TYPE_DEFINITION, + description, + name, + interfaces, + directives, + fields, + }); + } + + /** + * ImplementsInterfaces : + * - implements `&`? NamedType + * - ImplementsInterfaces & NamedType + */ + parseImplementsInterfaces(): Array { + return this.expectOptionalKeyword('implements') ? this.delimitedMany(TokenKind.AMP, this.parseNamedType) : []; + } + + /** + * ``` + * FieldsDefinition : { FieldDefinition+ } + * ``` + */ + parseFieldsDefinition(): Array { + return this.optionalMany(TokenKind.BRACE_L, this.parseFieldDefinition, TokenKind.BRACE_R); + } + + /** + * FieldDefinition : + * - Description? Name ArgumentsDefinition? : Type Directives[Const]? + */ + parseFieldDefinition(): FieldDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + const name = this.parseName(); + const args = this.parseArgumentDefs(); + this.expectToken(TokenKind.COLON); + const type = this.parseTypeReference(); + const directives = this.parseConstDirectives(); + return this.node(start, { + kind: Kind.FIELD_DEFINITION, + description, + name, + arguments: args, + type, + directives, + }); + } + + /** + * ArgumentsDefinition : ( InputValueDefinition+ ) + */ + parseArgumentDefs(): Array { + return this.optionalMany(TokenKind.PAREN_L, this.parseInputValueDef, TokenKind.PAREN_R); + } + + /** + * InputValueDefinition : + * - Description? Name : Type DefaultValue? Directives[Const]? + */ + parseInputValueDef(): InputValueDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + const name = this.parseName(); + this.expectToken(TokenKind.COLON); + const type = this.parseTypeReference(); + let defaultValue; + if (this.expectOptionalToken(TokenKind.EQUALS)) { + defaultValue = this.parseConstValueLiteral(); + } + const directives = this.parseConstDirectives(); + return this.node(start, { + kind: Kind.INPUT_VALUE_DEFINITION, + description, + name, + type, + defaultValue, + directives, + }); + } + + /** + * InterfaceTypeDefinition : + * - Description? interface Name Directives[Const]? FieldsDefinition? + */ + parseInterfaceTypeDefinition(): InterfaceTypeDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + this.expectKeyword('interface'); + const name = this.parseName(); + const interfaces = this.parseImplementsInterfaces(); + const directives = this.parseConstDirectives(); + const fields = this.parseFieldsDefinition(); + return this.node(start, { + kind: Kind.INTERFACE_TYPE_DEFINITION, + description, + name, + interfaces, + directives, + fields, + }); + } + + /** + * UnionTypeDefinition : + * - Description? union Name Directives[Const]? UnionMemberTypes? + */ + parseUnionTypeDefinition(): UnionTypeDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + this.expectKeyword('union'); + const name = this.parseName(); + const directives = this.parseConstDirectives(); + const types = this.parseUnionMemberTypes(); + return this.node(start, { + kind: Kind.UNION_TYPE_DEFINITION, + description, + name, + directives, + types, + }); + } + + /** + * UnionMemberTypes : + * - = `|`? NamedType + * - UnionMemberTypes | NamedType + */ + parseUnionMemberTypes(): Array { + return this.expectOptionalToken(TokenKind.EQUALS) ? this.delimitedMany(TokenKind.PIPE, this.parseNamedType) : []; + } + + /** + * EnumTypeDefinition : + * - Description? enum Name Directives[Const]? EnumValuesDefinition? + */ + parseEnumTypeDefinition(): EnumTypeDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + this.expectKeyword('enum'); + const name = this.parseName(); + const directives = this.parseConstDirectives(); + const values = this.parseEnumValuesDefinition(); + return this.node(start, { + kind: Kind.ENUM_TYPE_DEFINITION, + description, + name, + directives, + values, + }); + } + + /** + * ``` + * EnumValuesDefinition : { EnumValueDefinition+ } + * ``` + */ + parseEnumValuesDefinition(): Array { + return this.optionalMany(TokenKind.BRACE_L, this.parseEnumValueDefinition, TokenKind.BRACE_R); + } + + /** + * EnumValueDefinition : Description? EnumValue Directives[Const]? + */ + parseEnumValueDefinition(): EnumValueDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + const name = this.parseEnumValueName(); + const directives = this.parseConstDirectives(); + return this.node(start, { + kind: Kind.ENUM_VALUE_DEFINITION, + description, + name, + directives, + }); + } + + /** + * EnumValue : Name but not `true`, `false` or `null` + */ + parseEnumValueName(): NameNode { + if ( + this._lexer.token.value === 'true' || + this._lexer.token.value === 'false' || + this._lexer.token.value === 'null' + ) { + throw syntaxError( + this._lexer.source, + this._lexer.token.start, + `${getTokenDesc(this._lexer.token)} is reserved and cannot be used for an enum value.` + ); + } + return this.parseName(); + } + + /** + * InputObjectTypeDefinition : + * - Description? input Name Directives[Const]? InputFieldsDefinition? + */ + parseInputObjectTypeDefinition(): InputObjectTypeDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + this.expectKeyword('input'); + const name = this.parseName(); + const directives = this.parseConstDirectives(); + const fields = this.parseInputFieldsDefinition(); + return this.node(start, { + kind: Kind.INPUT_OBJECT_TYPE_DEFINITION, + description, + name, + directives, + fields, + }); + } + + /** + * ``` + * InputFieldsDefinition : { InputValueDefinition+ } + * ``` + */ + parseInputFieldsDefinition(): Array { + return this.optionalMany(TokenKind.BRACE_L, this.parseInputValueDef, TokenKind.BRACE_R); + } + + /** + * TypeSystemExtension : + * - SchemaExtension + * - TypeExtension + * + * TypeExtension : + * - ScalarTypeExtension + * - ObjectTypeExtension + * - InterfaceTypeExtension + * - UnionTypeExtension + * - EnumTypeExtension + * - InputObjectTypeDefinition + */ + parseTypeSystemExtension(): TypeSystemExtensionNode { + const keywordToken = this._lexer.lookahead(); + + if (keywordToken.kind === TokenKind.NAME) { + switch (keywordToken.value) { + case 'schema': + return this.parseSchemaExtension(); + case 'scalar': + return this.parseScalarTypeExtension(); + case 'type': + return this.parseObjectTypeExtension(); + case 'interface': + return this.parseInterfaceTypeExtension(); + case 'union': + return this.parseUnionTypeExtension(); + case 'enum': + return this.parseEnumTypeExtension(); + case 'input': + return this.parseInputObjectTypeExtension(); + } + } + + throw this.unexpected(keywordToken); + } + + /** + * ``` + * SchemaExtension : + * - extend schema Directives[Const]? { OperationTypeDefinition+ } + * - extend schema Directives[Const] + * ``` + */ + parseSchemaExtension(): SchemaExtensionNode { + const start = this._lexer.token; + this.expectKeyword('extend'); + this.expectKeyword('schema'); + const directives = this.parseConstDirectives(); + const operationTypes = this.optionalMany(TokenKind.BRACE_L, this.parseOperationTypeDefinition, TokenKind.BRACE_R); + if (directives.length === 0 && operationTypes.length === 0) { + throw this.unexpected(); + } + return this.node(start, { + kind: Kind.SCHEMA_EXTENSION, + directives, + operationTypes, + }); + } + + /** + * ScalarTypeExtension : + * - extend scalar Name Directives[Const] + */ + parseScalarTypeExtension(): ScalarTypeExtensionNode { + const start = this._lexer.token; + this.expectKeyword('extend'); + this.expectKeyword('scalar'); + const name = this.parseName(); + const directives = this.parseConstDirectives(); + if (directives.length === 0) { + throw this.unexpected(); + } + return this.node(start, { + kind: Kind.SCALAR_TYPE_EXTENSION, + name, + directives, + }); + } + + /** + * ObjectTypeExtension : + * - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition + * - extend type Name ImplementsInterfaces? Directives[Const] + * - extend type Name ImplementsInterfaces + */ + parseObjectTypeExtension(): ObjectTypeExtensionNode { + const start = this._lexer.token; + this.expectKeyword('extend'); + this.expectKeyword('type'); + const name = this.parseName(); + const interfaces = this.parseImplementsInterfaces(); + const directives = this.parseConstDirectives(); + const fields = this.parseFieldsDefinition(); + if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) { + throw this.unexpected(); + } + return this.node(start, { + kind: Kind.OBJECT_TYPE_EXTENSION, + name, + interfaces, + directives, + fields, + }); + } + + /** + * InterfaceTypeExtension : + * - extend interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition + * - extend interface Name ImplementsInterfaces? Directives[Const] + * - extend interface Name ImplementsInterfaces + */ + parseInterfaceTypeExtension(): InterfaceTypeExtensionNode { + const start = this._lexer.token; + this.expectKeyword('extend'); + this.expectKeyword('interface'); + const name = this.parseName(); + const interfaces = this.parseImplementsInterfaces(); + const directives = this.parseConstDirectives(); + const fields = this.parseFieldsDefinition(); + if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) { + throw this.unexpected(); + } + return this.node(start, { + kind: Kind.INTERFACE_TYPE_EXTENSION, + name, + interfaces, + directives, + fields, + }); + } + + /** + * UnionTypeExtension : + * - extend union Name Directives[Const]? UnionMemberTypes + * - extend union Name Directives[Const] + */ + parseUnionTypeExtension(): UnionTypeExtensionNode { + const start = this._lexer.token; + this.expectKeyword('extend'); + this.expectKeyword('union'); + const name = this.parseName(); + const directives = this.parseConstDirectives(); + const types = this.parseUnionMemberTypes(); + if (directives.length === 0 && types.length === 0) { + throw this.unexpected(); + } + return this.node(start, { + kind: Kind.UNION_TYPE_EXTENSION, + name, + directives, + types, + }); + } + + /** + * EnumTypeExtension : + * - extend enum Name Directives[Const]? EnumValuesDefinition + * - extend enum Name Directives[Const] + */ + parseEnumTypeExtension(): EnumTypeExtensionNode { + const start = this._lexer.token; + this.expectKeyword('extend'); + this.expectKeyword('enum'); + const name = this.parseName(); + const directives = this.parseConstDirectives(); + const values = this.parseEnumValuesDefinition(); + if (directives.length === 0 && values.length === 0) { + throw this.unexpected(); + } + return this.node(start, { + kind: Kind.ENUM_TYPE_EXTENSION, + name, + directives, + values, + }); + } + + /** + * InputObjectTypeExtension : + * - extend input Name Directives[Const]? InputFieldsDefinition + * - extend input Name Directives[Const] + */ + parseInputObjectTypeExtension(): InputObjectTypeExtensionNode { + const start = this._lexer.token; + this.expectKeyword('extend'); + this.expectKeyword('input'); + const name = this.parseName(); + const directives = this.parseConstDirectives(); + const fields = this.parseInputFieldsDefinition(); + if (directives.length === 0 && fields.length === 0) { + throw this.unexpected(); + } + return this.node(start, { + kind: Kind.INPUT_OBJECT_TYPE_EXTENSION, + name, + directives, + fields, + }); + } + + /** + * ``` + * DirectiveDefinition : + * - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations + * ``` + */ + parseDirectiveDefinition(): DirectiveDefinitionNode { + const start = this._lexer.token; + const description = this.parseDescription(); + this.expectKeyword('directive'); + this.expectToken(TokenKind.AT); + const name = this.parseName(); + const args = this.parseArgumentDefs(); + const repeatable = this.expectOptionalKeyword('repeatable'); + this.expectKeyword('on'); + const locations = this.parseDirectiveLocations(); + return this.node(start, { + kind: Kind.DIRECTIVE_DEFINITION, + description, + name, + arguments: args, + repeatable, + locations, + }); + } + + /** + * DirectiveLocations : + * - `|`? DirectiveLocation + * - DirectiveLocations | DirectiveLocation + */ + parseDirectiveLocations(): Array { + return this.delimitedMany(TokenKind.PIPE, this.parseDirectiveLocation); + } + + /* + * DirectiveLocation : + * - ExecutableDirectiveLocation + * - TypeSystemDirectiveLocation + * + * ExecutableDirectiveLocation : one of + * `QUERY` + * `MUTATION` + * `SUBSCRIPTION` + * `FIELD` + * `FRAGMENT_DEFINITION` + * `FRAGMENT_SPREAD` + * `INLINE_FRAGMENT` + * + * TypeSystemDirectiveLocation : one of + * `SCHEMA` + * `SCALAR` + * `OBJECT` + * `FIELD_DEFINITION` + * `ARGUMENT_DEFINITION` + * `INTERFACE` + * `UNION` + * `ENUM` + * `ENUM_VALUE` + * `INPUT_OBJECT` + * `INPUT_FIELD_DEFINITION` + */ + parseDirectiveLocation(): NameNode { + const start = this._lexer.token; + const name = this.parseName(); + if (Object.prototype.hasOwnProperty.call(DirectiveLocation, name.value)) { + return name; + } + throw this.unexpected(start); + } + + // Core parsing utility functions + + /** + * Returns a node that, if configured to do so, sets a "loc" field as a + * location object, used to identify the place in the source that created a + * given parsed object. + */ + node(startToken: Token, node: T): T { + if (this._options?.noLocation !== true) { + node.loc = new Location(startToken, this._lexer.lastToken, this._lexer.source); + } + return node; + } + + /** + * Determines if the next token is of a given kind + */ + peek(kind: TokenKind): boolean { + return this._lexer.token.kind === kind; + } + + /** + * If the next token is of the given kind, return that token after advancing the lexer. + * Otherwise, do not change the parser state and throw an error. + */ + expectToken(kind: TokenKind): Token { + const token = this._lexer.token; + if (token.kind === kind) { + this._lexer.advance(); + return token; + } + + throw syntaxError( + this._lexer.source, + token.start, + `Expected ${getTokenKindDesc(kind)}, found ${getTokenDesc(token)}.` + ); + } + + /** + * If the next token is of the given kind, return "true" after advancing the lexer. + * Otherwise, do not change the parser state and return "false". + */ + expectOptionalToken(kind: TokenKind): boolean { + const token = this._lexer.token; + if (token.kind === kind) { + this._lexer.advance(); + return true; + } + return false; + } + + /** + * If the next token is a given keyword, advance the lexer. + * Otherwise, do not change the parser state and throw an error. + */ + expectKeyword(value: string): void { + const token = this._lexer.token; + if (token.kind === TokenKind.NAME && token.value === value) { + this._lexer.advance(); + } else { + throw syntaxError(this._lexer.source, token.start, `Expected "${value}", found ${getTokenDesc(token)}.`); + } + } + + /** + * If the next token is a given keyword, return "true" after advancing the lexer. + * Otherwise, do not change the parser state and return "false". + */ + expectOptionalKeyword(value: string): boolean { + const token = this._lexer.token; + if (token.kind === TokenKind.NAME && token.value === value) { + this._lexer.advance(); + return true; + } + return false; + } + + /** + * Helper function for creating an error when an unexpected lexed token is encountered. + */ + unexpected(atToken?: Maybe): GraphQLError { + const token = atToken ?? this._lexer.token; + return syntaxError(this._lexer.source, token.start, `Unexpected ${getTokenDesc(token)}.`); + } + + /** + * Returns a possibly empty list of parse nodes, determined by the parseFn. + * This list begins with a lex token of openKind and ends with a lex token of closeKind. + * Advances the parser to the next lex token after the closing token. + */ + any(openKind: TokenKind, parseFn: () => T, closeKind: TokenKind): Array { + this.expectToken(openKind); + const nodes = []; + while (!this.expectOptionalToken(closeKind)) { + nodes.push(parseFn.call(this)); + } + return nodes; + } + + /** + * Returns a list of parse nodes, determined by the parseFn. + * It can be empty only if open token is missing otherwise it will always return non-empty list + * that begins with a lex token of openKind and ends with a lex token of closeKind. + * Advances the parser to the next lex token after the closing token. + */ + optionalMany(openKind: TokenKind, parseFn: () => T, closeKind: TokenKind): Array { + if (this.expectOptionalToken(openKind)) { + const nodes = []; + do { + nodes.push(parseFn.call(this)); + } while (!this.expectOptionalToken(closeKind)); + return nodes; + } + return []; + } + + /** + * Returns a non-empty list of parse nodes, determined by the parseFn. + * This list begins with a lex token of openKind and ends with a lex token of closeKind. + * Advances the parser to the next lex token after the closing token. + */ + many(openKind: TokenKind, parseFn: () => T, closeKind: TokenKind): Array { + this.expectToken(openKind); + const nodes = []; + do { + nodes.push(parseFn.call(this)); + } while (!this.expectOptionalToken(closeKind)); + return nodes; + } + + /** + * Returns a non-empty list of parse nodes, determined by the parseFn. + * This list may begin with a lex token of delimiterKind followed by items separated by lex tokens of tokenKind. + * Advances the parser to the next lex token after last item in the list. + */ + delimitedMany(delimiterKind: TokenKind, parseFn: () => T): Array { + this.expectOptionalToken(delimiterKind); + + const nodes = []; + do { + nodes.push(parseFn.call(this)); + } while (this.expectOptionalToken(delimiterKind)); + return nodes; + } +} + +/** + * A helper function to describe a token as a string for debugging. + */ +function getTokenDesc(token: Token): string { + const value = token.value; + return getTokenKindDesc(token.kind) + (value != null ? ` "${value}"` : ''); +} + +/** + * A helper function to describe a token kind as a string for debugging. + */ +function getTokenKindDesc(kind: TokenKind): string { + return isPunctuatorTokenKind(kind) ? `"${kind}"` : kind; +} diff --git a/packages/graphql/src/language/predicates.ts b/packages/graphql/src/language/predicates.ts new file mode 100644 index 00000000000..79bd871f684 --- /dev/null +++ b/packages/graphql/src/language/predicates.ts @@ -0,0 +1,94 @@ +import type { + ASTNode, + ConstValueNode, + DefinitionNode, + ExecutableDefinitionNode, + NullabilityAssertionNode, + SelectionNode, + TypeDefinitionNode, + TypeExtensionNode, + TypeNode, + TypeSystemDefinitionNode, + TypeSystemExtensionNode, + ValueNode, +} from './ast.js'; +import { Kind } from './kinds.js'; + +export function isDefinitionNode(node: ASTNode): node is DefinitionNode { + return isExecutableDefinitionNode(node) || isTypeSystemDefinitionNode(node) || isTypeSystemExtensionNode(node); +} + +export function isExecutableDefinitionNode(node: ASTNode): node is ExecutableDefinitionNode { + return node.kind === Kind.OPERATION_DEFINITION || node.kind === Kind.FRAGMENT_DEFINITION; +} + +export function isSelectionNode(node: ASTNode): node is SelectionNode { + return node.kind === Kind.FIELD || node.kind === Kind.FRAGMENT_SPREAD || node.kind === Kind.INLINE_FRAGMENT; +} + +export function isNullabilityAssertionNode(node: ASTNode): node is NullabilityAssertionNode { + return ( + node.kind === Kind.LIST_NULLABILITY_OPERATOR || + node.kind === Kind.NON_NULL_ASSERTION || + node.kind === Kind.ERROR_BOUNDARY + ); +} + +export function isValueNode(node: ASTNode): node is ValueNode { + return ( + node.kind === Kind.VARIABLE || + node.kind === Kind.INT || + node.kind === Kind.FLOAT || + node.kind === Kind.STRING || + node.kind === Kind.BOOLEAN || + node.kind === Kind.NULL || + node.kind === Kind.ENUM || + node.kind === Kind.LIST || + node.kind === Kind.OBJECT + ); +} + +export function isConstValueNode(node: ASTNode): node is ConstValueNode { + return ( + isValueNode(node) && + (node.kind === Kind.LIST + ? node.values.some(isConstValueNode) + : node.kind === Kind.OBJECT + ? node.fields.some(field => isConstValueNode(field.value)) + : node.kind !== Kind.VARIABLE) + ); +} + +export function isTypeNode(node: ASTNode): node is TypeNode { + return node.kind === Kind.NAMED_TYPE || node.kind === Kind.LIST_TYPE || node.kind === Kind.NON_NULL_TYPE; +} + +export function isTypeSystemDefinitionNode(node: ASTNode): node is TypeSystemDefinitionNode { + return node.kind === Kind.SCHEMA_DEFINITION || isTypeDefinitionNode(node) || node.kind === Kind.DIRECTIVE_DEFINITION; +} + +export function isTypeDefinitionNode(node: ASTNode): node is TypeDefinitionNode { + return ( + node.kind === Kind.SCALAR_TYPE_DEFINITION || + node.kind === Kind.OBJECT_TYPE_DEFINITION || + node.kind === Kind.INTERFACE_TYPE_DEFINITION || + node.kind === Kind.UNION_TYPE_DEFINITION || + node.kind === Kind.ENUM_TYPE_DEFINITION || + node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION + ); +} + +export function isTypeSystemExtensionNode(node: ASTNode): node is TypeSystemExtensionNode { + return node.kind === Kind.SCHEMA_EXTENSION || isTypeExtensionNode(node); +} + +export function isTypeExtensionNode(node: ASTNode): node is TypeExtensionNode { + return ( + node.kind === Kind.SCALAR_TYPE_EXTENSION || + node.kind === Kind.OBJECT_TYPE_EXTENSION || + node.kind === Kind.INTERFACE_TYPE_EXTENSION || + node.kind === Kind.UNION_TYPE_EXTENSION || + node.kind === Kind.ENUM_TYPE_EXTENSION || + node.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION + ); +} diff --git a/packages/graphql/src/language/printLocation.ts b/packages/graphql/src/language/printLocation.ts new file mode 100644 index 00000000000..21c0eb06c25 --- /dev/null +++ b/packages/graphql/src/language/printLocation.ts @@ -0,0 +1,68 @@ +import type { Location } from './ast.js'; +import type { SourceLocation } from './location.js'; +import { getLocation } from './location.js'; +import type { Source } from './source.js'; + +/** + * Render a helpful description of the location in the GraphQL Source document. + */ +export function printLocation(location: Location): string { + return printSourceLocation(location.source, getLocation(location.source, location.start)); +} + +/** + * Render a helpful description of the location in the GraphQL Source document. + */ +export function printSourceLocation(source: Source, sourceLocation: SourceLocation): string { + const firstLineColumnOffset = source.locationOffset.column - 1; + const body = ''.padStart(firstLineColumnOffset) + source.body; + + const lineIndex = sourceLocation.line - 1; + const lineOffset = source.locationOffset.line - 1; + const lineNum = sourceLocation.line + lineOffset; + + const columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0; + const columnNum = sourceLocation.column + columnOffset; + const locationStr = `${source.name}:${lineNum}:${columnNum}\n`; + + const lines = body.split(/\r\n|[\n\r]/g); + const locationLine = lines[lineIndex]; + + // Special case for minified documents + if (locationLine.length > 120) { + const subLineIndex = Math.floor(columnNum / 80); + const subLineColumnNum = columnNum % 80; + const subLines: Array = []; + for (let i = 0; i < locationLine.length; i += 80) { + subLines.push(locationLine.slice(i, i + 80)); + } + + return ( + locationStr + + printPrefixedLines([ + [`${lineNum} |`, subLines[0]], + ...subLines.slice(1, subLineIndex + 1).map(subLine => ['|', subLine] as const), + ['|', '^'.padStart(subLineColumnNum)], + ['|', subLines[subLineIndex + 1]], + ]) + ); + } + + return ( + locationStr + + printPrefixedLines([ + // Lines specified like this: ["prefix", "string"], + [`${lineNum - 1} |`, lines[lineIndex - 1]], + [`${lineNum} |`, locationLine], + ['|', '^'.padStart(columnNum)], + [`${lineNum + 1} |`, lines[lineIndex + 1]], + ]) + ); +} + +function printPrefixedLines(lines: ReadonlyArray): string { + const existingLines = lines.filter(([_, line]) => line !== undefined); + + const padLen = Math.max(...existingLines.map(([prefix]) => prefix.length)); + return existingLines.map(([prefix, line]) => prefix.padStart(padLen) + (line ? ' ' + line : '')).join('\n'); +} diff --git a/packages/graphql/src/language/printString.ts b/packages/graphql/src/language/printString.ts new file mode 100644 index 00000000000..b091bcc2c13 --- /dev/null +++ b/packages/graphql/src/language/printString.ts @@ -0,0 +1,38 @@ +/** + * Prints a string as a GraphQL StringValue literal. Replaces control characters + * and excluded characters (" U+0022 and \\ U+005C) with escape sequences. + */ +export function printString(str: string): string { + return `"${str.replace(escapedRegExp, escapedReplacer)}"`; +} + +// eslint-disable-next-line no-control-regex +const escapedRegExp = /[\x00-\x1f\x22\x5c\x7f-\x9f]/g; + +function escapedReplacer(str: string): string { + return escapeSequences[str.charCodeAt(0)]; +} + +// prettier-ignore +const escapeSequences = [ + '\\u0000', '\\u0001', '\\u0002', '\\u0003', '\\u0004', '\\u0005', '\\u0006', '\\u0007', + '\\b', '\\t', '\\n', '\\u000B', '\\f', '\\r', '\\u000E', '\\u000F', + '\\u0010', '\\u0011', '\\u0012', '\\u0013', '\\u0014', '\\u0015', '\\u0016', '\\u0017', + '\\u0018', '\\u0019', '\\u001A', '\\u001B', '\\u001C', '\\u001D', '\\u001E', '\\u001F', + '', '', '\\"', '', '', '', '', '', + '', '', '', '', '', '', '', '', // 2F + '', '', '', '', '', '', '', '', + '', '', '', '', '', '', '', '', // 3F + '', '', '', '', '', '', '', '', + '', '', '', '', '', '', '', '', // 4F + '', '', '', '', '', '', '', '', + '', '', '', '', '\\\\', '', '', '', // 5F + '', '', '', '', '', '', '', '', + '', '', '', '', '', '', '', '', // 6F + '', '', '', '', '', '', '', '', + '', '', '', '', '', '', '', '\\u007F', + '\\u0080', '\\u0081', '\\u0082', '\\u0083', '\\u0084', '\\u0085', '\\u0086', '\\u0087', + '\\u0088', '\\u0089', '\\u008A', '\\u008B', '\\u008C', '\\u008D', '\\u008E', '\\u008F', + '\\u0090', '\\u0091', '\\u0092', '\\u0093', '\\u0094', '\\u0095', '\\u0096', '\\u0097', + '\\u0098', '\\u0099', '\\u009A', '\\u009B', '\\u009C', '\\u009D', '\\u009E', '\\u009F', +]; diff --git a/packages/graphql/src/language/printer.ts b/packages/graphql/src/language/printer.ts new file mode 100644 index 00000000000..79641f7265c --- /dev/null +++ b/packages/graphql/src/language/printer.ts @@ -0,0 +1,279 @@ +import type { Maybe } from '../jsutils/Maybe.js'; + +import type { ASTNode } from './ast.js'; +import { printBlockString } from './blockString.js'; +import { printString } from './printString.js'; +import type { ASTReducer } from './visitor.js'; +import { visit } from './visitor.js'; + +/** + * Converts an AST into a string, using one set of reasonable + * formatting rules. + */ +export function print(ast: ASTNode): string { + return visit(ast, printDocASTReducer); +} + +const MAX_LINE_LENGTH = 80; + +const printDocASTReducer: ASTReducer = { + Name: { leave: node => node.value }, + Variable: { leave: node => '$' + node.name }, + + // Document + + Document: { + leave: node => join(node.definitions, '\n\n'), + }, + + OperationDefinition: { + leave(node) { + const varDefs = wrap('(', join(node.variableDefinitions, ', '), ')'); + const prefix = join([node.operation, join([node.name, varDefs]), join(node.directives, ' ')], ' '); + + // Anonymous queries with no directives or variable definitions can use + // the query short form. + return (prefix === 'query' ? '' : prefix + ' ') + node.selectionSet; + }, + }, + + VariableDefinition: { + leave: ({ variable, type, defaultValue, directives }) => + variable + ': ' + type + wrap(' = ', defaultValue) + wrap(' ', join(directives, ' ')), + }, + SelectionSet: { leave: ({ selections }) => block(selections) }, + + Field: { + leave({ alias, name, arguments: args, nullabilityAssertion, directives, selectionSet }) { + const prefix = join([wrap('', alias, ': '), name], ''); + let argsLine = prefix + wrap('(', join(args, ', '), ')'); + + if (argsLine.length > MAX_LINE_LENGTH) { + argsLine = prefix + wrap('(\n', indent(join(args, '\n')), '\n)'); + } + + return join([ + argsLine, + // Note: Client Controlled Nullability is experimental and may be + // changed or removed in the future. + nullabilityAssertion, + wrap(' ', join(directives, ' ')), + wrap(' ', selectionSet), + ]); + }, + }, + Argument: { leave: ({ name, value }) => name + ': ' + value }, + + // Nullability Modifiers + + ListNullabilityOperator: { + leave({ nullabilityAssertion }) { + return join(['[', nullabilityAssertion, ']']); + }, + }, + + NonNullAssertion: { + leave({ nullabilityAssertion }) { + return join([nullabilityAssertion, '!']); + }, + }, + + ErrorBoundary: { + leave({ nullabilityAssertion }) { + return join([nullabilityAssertion, '?']); + }, + }, + + // Fragments + + FragmentSpread: { + leave: ({ name, directives }) => '...' + name + wrap(' ', join(directives, ' ')), + }, + + InlineFragment: { + leave: ({ typeCondition, directives, selectionSet }) => + join(['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], ' '), + }, + + FragmentDefinition: { + leave: ({ name, typeCondition, variableDefinitions, directives, selectionSet }) => + // Note: fragment variable definitions are experimental and may be changed + // or removed in the future. + `fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` + + `on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}` + + selectionSet, + }, + + // Value + + IntValue: { leave: ({ value }) => value }, + FloatValue: { leave: ({ value }) => value }, + StringValue: { + leave: ({ value, block: isBlockString }) => (isBlockString ? printBlockString(value) : printString(value)), + }, + BooleanValue: { leave: ({ value }) => (value ? 'true' : 'false') }, + NullValue: { leave: () => 'null' }, + EnumValue: { leave: ({ value }) => value }, + ListValue: { leave: ({ values }) => '[' + join(values, ', ') + ']' }, + ObjectValue: { leave: ({ fields }) => '{ ' + join(fields, ', ') + ' }' }, + ObjectField: { leave: ({ name, value }) => name + ': ' + value }, + + // Directive + + Directive: { + leave: ({ name, arguments: args }) => '@' + name + wrap('(', join(args, ', '), ')'), + }, + + // Type + + NamedType: { leave: ({ name }) => name }, + ListType: { leave: ({ type }) => '[' + type + ']' }, + NonNullType: { leave: ({ type }) => type + '!' }, + + // Type System Definitions + + SchemaDefinition: { + leave: ({ description, directives, operationTypes }) => + wrap('', description, '\n') + join(['schema', join(directives, ' '), block(operationTypes)], ' '), + }, + + OperationTypeDefinition: { + leave: ({ operation, type }) => operation + ': ' + type, + }, + + ScalarTypeDefinition: { + leave: ({ description, name, directives }) => + wrap('', description, '\n') + join(['scalar', name, join(directives, ' ')], ' '), + }, + + ObjectTypeDefinition: { + leave: ({ description, name, interfaces, directives, fields }) => + wrap('', description, '\n') + + join(['type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '), + }, + + FieldDefinition: { + leave: ({ description, name, arguments: args, type, directives }) => + wrap('', description, '\n') + + name + + (hasMultilineItems(args) ? wrap('(\n', indent(join(args, '\n')), '\n)') : wrap('(', join(args, ', '), ')')) + + ': ' + + type + + wrap(' ', join(directives, ' ')), + }, + + InputValueDefinition: { + leave: ({ description, name, type, defaultValue, directives }) => + wrap('', description, '\n') + join([name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], ' '), + }, + + InterfaceTypeDefinition: { + leave: ({ description, name, interfaces, directives, fields }) => + wrap('', description, '\n') + + join( + ['interface', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], + ' ' + ), + }, + + UnionTypeDefinition: { + leave: ({ description, name, directives, types }) => + wrap('', description, '\n') + join(['union', name, join(directives, ' '), wrap('= ', join(types, ' | '))], ' '), + }, + + EnumTypeDefinition: { + leave: ({ description, name, directives, values }) => + wrap('', description, '\n') + join(['enum', name, join(directives, ' '), block(values)], ' '), + }, + + EnumValueDefinition: { + leave: ({ description, name, directives }) => + wrap('', description, '\n') + join([name, join(directives, ' ')], ' '), + }, + + InputObjectTypeDefinition: { + leave: ({ description, name, directives, fields }) => + wrap('', description, '\n') + join(['input', name, join(directives, ' '), block(fields)], ' '), + }, + + DirectiveDefinition: { + leave: ({ description, name, arguments: args, repeatable, locations }) => + wrap('', description, '\n') + + 'directive @' + + name + + (hasMultilineItems(args) ? wrap('(\n', indent(join(args, '\n')), '\n)') : wrap('(', join(args, ', '), ')')) + + (repeatable ? ' repeatable' : '') + + ' on ' + + join(locations, ' | '), + }, + + SchemaExtension: { + leave: ({ directives, operationTypes }) => + join(['extend schema', join(directives, ' '), block(operationTypes)], ' '), + }, + + ScalarTypeExtension: { + leave: ({ name, directives }) => join(['extend scalar', name, join(directives, ' ')], ' '), + }, + + ObjectTypeExtension: { + leave: ({ name, interfaces, directives, fields }) => + join( + ['extend type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], + ' ' + ), + }, + + InterfaceTypeExtension: { + leave: ({ name, interfaces, directives, fields }) => + join( + ['extend interface', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], + ' ' + ), + }, + + UnionTypeExtension: { + leave: ({ name, directives, types }) => + join(['extend union', name, join(directives, ' '), wrap('= ', join(types, ' | '))], ' '), + }, + + EnumTypeExtension: { + leave: ({ name, directives, values }) => join(['extend enum', name, join(directives, ' '), block(values)], ' '), + }, + + InputObjectTypeExtension: { + leave: ({ name, directives, fields }) => join(['extend input', name, join(directives, ' '), block(fields)], ' '), + }, +}; + +/** + * Given maybeArray, print an empty string if it is null or empty, otherwise + * print all items together separated by separator if provided + */ +function join(maybeArray: Maybe>, separator = ''): string { + return maybeArray?.filter(x => x).join(separator) ?? ''; +} + +/** + * Given array, print each item on its own line, wrapped in an indented `{ }` block. + */ +function block(array: Maybe>): string { + return wrap('{\n', indent(join(array, '\n')), '\n}'); +} + +/** + * If maybeString is not null or empty, then wrap with start and end, otherwise print an empty string. + */ +function wrap(start: string, maybeString: Maybe, end: string = ''): string { + return maybeString != null && maybeString !== '' ? start + maybeString + end : ''; +} + +function indent(str: string): string { + return wrap(' ', str.replace(/\n/g, '\n ')); +} + +function hasMultilineItems(maybeArray: Maybe>): boolean { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + return maybeArray?.some(str => str.includes('\n')) ?? false; +} diff --git a/packages/graphql/src/language/source.ts b/packages/graphql/src/language/source.ts new file mode 100644 index 00000000000..41a4e52ebc5 --- /dev/null +++ b/packages/graphql/src/language/source.ts @@ -0,0 +1,43 @@ +import { devAssert } from '../jsutils/devAssert.js'; + +interface Location { + line: number; + column: number; +} + +const isSourceSymbol = Symbol.for('Source'); + +/** + * A representation of source input to GraphQL. The `name` and `locationOffset` parameters are + * optional, but they are useful for clients who store GraphQL documents in source files. + * For example, if the GraphQL input starts at line 40 in a file named `Foo.graphql`, it might + * be useful for `name` to be `"Foo.graphql"` and location to be `{ line: 40, column: 1 }`. + * The `line` and `column` properties in `locationOffset` are 1-indexed. + */ +export class Source { + readonly [isSourceSymbol]: true = true; + body: string; + name: string; + locationOffset: Location; + + constructor(body: string, name: string = 'GraphQL request', locationOffset: Location = { line: 1, column: 1 }) { + this.body = body; + this.name = name; + this.locationOffset = locationOffset; + devAssert(this.locationOffset.line > 0, 'line in locationOffset is 1-indexed and must be positive.'); + devAssert(this.locationOffset.column > 0, 'column in locationOffset is 1-indexed and must be positive.'); + } + + get [Symbol.toStringTag]() { + return 'Source'; + } +} + +/** + * Test if the given value is a Source object. + * + * @internal + */ +export function isSource(source: unknown): source is Source { + return typeof source === 'object' && source != null && isSourceSymbol in source; +} diff --git a/packages/graphql/src/language/tokenKind.ts b/packages/graphql/src/language/tokenKind.ts new file mode 100644 index 00000000000..540ea9f95d4 --- /dev/null +++ b/packages/graphql/src/language/tokenKind.ts @@ -0,0 +1,36 @@ +/** + * An exported enum describing the different kinds of tokens that the + * lexer emits. + */ +export enum TokenKind { + SOF = '', + EOF = '', + BANG = '!', + QUESTION_MARK = '?', + DOLLAR = '$', + AMP = '&', + PAREN_L = '(', + PAREN_R = ')', + SPREAD = '...', + COLON = ':', + EQUALS = '=', + AT = '@', + BRACKET_L = '[', + BRACKET_R = ']', + BRACE_L = '{', + PIPE = '|', + BRACE_R = '}', + NAME = 'Name', + INT = 'Int', + FLOAT = 'Float', + STRING = 'String', + BLOCK_STRING = 'BlockString', + COMMENT = 'Comment', +} + +/** + * The enum type representing the token kinds values. + * + * @deprecated Please use `TokenKind`. Will be remove in v17. + */ +export type TokenKindEnum = typeof TokenKind; diff --git a/packages/graphql/src/language/visitor.ts b/packages/graphql/src/language/visitor.ts new file mode 100644 index 00000000000..5b64b0444e9 --- /dev/null +++ b/packages/graphql/src/language/visitor.ts @@ -0,0 +1,379 @@ +import { devAssert } from '../jsutils/devAssert.js'; +import { inspect } from '../jsutils/inspect.js'; + +import type { ASTNode } from './ast.js'; +import { isNode, QueryDocumentKeys } from './ast.js'; +import { Kind } from './kinds.js'; + +/** + * A visitor is provided to visit, it contains the collection of + * relevant functions to be called during the visitor's traversal. + */ +export type ASTVisitor = EnterLeaveVisitor | KindVisitor; + +type KindVisitor = { + readonly [NodeT in ASTNode as NodeT['kind']]?: ASTVisitFn | EnterLeaveVisitor; +}; + +interface EnterLeaveVisitor { + readonly enter?: ASTVisitFn; + readonly leave?: ASTVisitFn; +} + +/** + * A visitor is comprised of visit functions, which are called on each node + * during the visitor's traversal. + */ +export type ASTVisitFn = ( + /** The current node being visiting. */ + node: TVisitedNode, + /** The index or key to this node from the parent node or Array. */ + key: string | number | undefined, + /** The parent immediately above this node, which may be an Array. */ + parent: ASTNode | ReadonlyArray | undefined, + /** The key path to get to this node from the root node. */ + path: ReadonlyArray, + /** + * All nodes and Arrays visited before reaching parent of this node. + * These correspond to array indices in `path`. + * Note: ancestors includes arrays which contain the parent of visited node. + */ + ancestors: ReadonlyArray> +) => any; + +/** + * A reducer is comprised of reducer functions which convert AST nodes into + * another form. + */ +export type ASTReducer = { + readonly [NodeT in ASTNode as NodeT['kind']]?: { + readonly enter?: ASTVisitFn; + readonly leave: ASTReducerFn; + }; +}; + +type ASTReducerFn = ( + /** The current node being visiting. */ + node: { [K in keyof TReducedNode]: ReducedField }, + /** The index or key to this node from the parent node or Array. */ + key: string | number | undefined, + /** The parent immediately above this node, which may be an Array. */ + parent: ASTNode | ReadonlyArray | undefined, + /** The key path to get to this node from the root node. */ + path: ReadonlyArray, + /** + * All nodes and Arrays visited before reaching parent of this node. + * These correspond to array indices in `path`. + * Note: ancestors includes arrays which contain the parent of visited node. + */ + ancestors: ReadonlyArray> +) => R; + +type ReducedField = T extends null | undefined ? T : T extends ReadonlyArray ? ReadonlyArray : R; + +/** + * A KeyMap describes each the traversable properties of each kind of node. + */ +export type ASTVisitorKeyMap = { + [NodeT in ASTNode as NodeT['kind']]?: ReadonlyArray; +}; + +export const BREAK: unknown = Object.freeze({}); + +/** + * visit() will walk through an AST using a depth-first traversal, calling + * the visitor's enter function at each node in the traversal, and calling the + * leave function after visiting that node and all of its child nodes. + * + * By returning different values from the enter and leave functions, the + * behavior of the visitor can be altered, including skipping over a sub-tree of + * the AST (by returning false), editing the AST by returning a value or null + * to remove the value, or to stop the whole traversal by returning BREAK. + * + * When using visit() to edit an AST, the original AST will not be modified, and + * a new version of the AST with the changes applied will be returned from the + * visit function. + * + * ```ts + * const editedAST = visit(ast, { + * enter(node, key, parent, path, ancestors) { + * // @return + * // undefined: no action + * // false: skip visiting this node + * // visitor.BREAK: stop visiting altogether + * // null: delete this node + * // any value: replace this node with the returned value + * }, + * leave(node, key, parent, path, ancestors) { + * // @return + * // undefined: no action + * // false: no action + * // visitor.BREAK: stop visiting altogether + * // null: delete this node + * // any value: replace this node with the returned value + * } + * }); + * ``` + * + * Alternatively to providing enter() and leave() functions, a visitor can + * instead provide functions named the same as the kinds of AST nodes, or + * enter/leave visitors at a named key, leading to three permutations of the + * visitor API: + * + * 1) Named visitors triggered when entering a node of a specific kind. + * + * ```ts + * visit(ast, { + * Kind(node) { + * // enter the "Kind" node + * } + * }) + * ``` + * + * 2) Named visitors that trigger upon entering and leaving a node of a specific kind. + * + * ```ts + * visit(ast, { + * Kind: { + * enter(node) { + * // enter the "Kind" node + * } + * leave(node) { + * // leave the "Kind" node + * } + * } + * }) + * ``` + * + * 3) Generic visitors that trigger upon entering and leaving any node. + * + * ```ts + * visit(ast, { + * enter(node) { + * // enter any node + * }, + * leave(node) { + * // leave any node + * } + * }) + * ``` + */ +export function visit(root: N, visitor: ASTVisitor, visitorKeys?: ASTVisitorKeyMap): N; +export function visit(root: ASTNode, visitor: ASTReducer, visitorKeys?: ASTVisitorKeyMap): R; +export function visit( + root: ASTNode, + visitor: ASTVisitor | ASTReducer, + visitorKeys: ASTVisitorKeyMap = QueryDocumentKeys +): any { + const enterLeaveMap = new Map>(); + for (const kind of Object.values(Kind)) { + enterLeaveMap.set(kind, getEnterLeaveForKind(visitor, kind)); + } + + /* eslint-disable no-undef-init */ + let stack: any = undefined; + let inArray = Array.isArray(root); + let keys: any = [root]; + let index = -1; + let edits = []; + let node: any = root; + let key: any = undefined; + let parent: any = undefined; + const path: any = []; + const ancestors = []; + /* eslint-enable no-undef-init */ + + do { + index++; + const isLeaving = index === keys.length; + const isEdited = isLeaving && edits.length !== 0; + if (isLeaving) { + key = ancestors.length === 0 ? undefined : path[path.length - 1]; + node = parent; + parent = ancestors.pop(); + if (isEdited) { + if (inArray) { + node = node.slice(); + + let editOffset = 0; + for (const [editKey, editValue] of edits) { + const arrayKey = editKey - editOffset; + if (editValue === null) { + node.splice(arrayKey, 1); + editOffset++; + } else { + node[arrayKey] = editValue; + } + } + } else { + node = Object.defineProperties({}, Object.getOwnPropertyDescriptors(node)); + for (const [editKey, editValue] of edits) { + node[editKey] = editValue; + } + } + } + index = stack.index; + keys = stack.keys; + edits = stack.edits; + inArray = stack.inArray; + stack = stack.prev; + } else if (parent) { + key = inArray ? index : keys[index]; + node = parent[key]; + if (node === null || node === undefined) { + continue; + } + path.push(key); + } + + let result; + if (!Array.isArray(node)) { + devAssert(isNode(node), `Invalid AST Node: ${inspect(node)}.`); + + const visitFn = isLeaving ? enterLeaveMap.get(node.kind)?.leave : enterLeaveMap.get(node.kind)?.enter; + + result = visitFn?.call(visitor, node, key, parent, path, ancestors); + + if (result === BREAK) { + break; + } + + if (result === false) { + if (!isLeaving) { + path.pop(); + continue; + } + } else if (result !== undefined) { + edits.push([key, result]); + if (!isLeaving) { + if (isNode(result)) { + node = result; + } else { + path.pop(); + continue; + } + } + } + } + + if (result === undefined && isEdited) { + edits.push([key, node]); + } + + if (isLeaving) { + path.pop(); + } else { + stack = { inArray, index, keys, edits, prev: stack }; + inArray = Array.isArray(node); + keys = inArray ? node : (visitorKeys as any)[node.kind] ?? []; + index = -1; + edits = []; + if (parent) { + ancestors.push(parent); + } + parent = node; + } + } while (stack !== undefined); + + if (edits.length !== 0) { + // New root + return edits[edits.length - 1][1]; + } + + return root; +} + +/** + * Creates a new visitor instance which delegates to many visitors to run in + * parallel. Each visitor will be visited for each node before moving on. + * + * If a prior visitor edits a node, no following visitors will see that node. + */ +export function visitInParallel(visitors: ReadonlyArray): ASTVisitor { + const skipping = new Array(visitors.length).fill(null); + const mergedVisitor = Object.create(null); + + for (const kind of Object.values(Kind)) { + let hasVisitor = false; + const enterList = new Array(visitors.length).fill(undefined); + const leaveList = new Array(visitors.length).fill(undefined); + + for (let i = 0; i < visitors.length; ++i) { + const { enter, leave } = getEnterLeaveForKind(visitors[i], kind); + hasVisitor ||= enter != null || leave != null; + enterList[i] = enter; + leaveList[i] = leave; + } + + if (!hasVisitor) { + continue; + } + + const mergedEnterLeave: EnterLeaveVisitor = { + enter(...args) { + const node = args[0]; + for (let i = 0; i < visitors.length; i++) { + if (skipping[i] === null) { + const result = enterList[i]?.apply(visitors[i], args); + if (result === false) { + skipping[i] = node; + } else if (result === BREAK) { + skipping[i] = BREAK; + } else if (result !== undefined) { + return result; + } + } + } + }, + leave(...args) { + const node = args[0]; + for (let i = 0; i < visitors.length; i++) { + if (skipping[i] === null) { + const result = leaveList[i]?.apply(visitors[i], args); + if (result === BREAK) { + skipping[i] = BREAK; + } else if (result !== undefined && result !== false) { + return result; + } + } else if (skipping[i] === node) { + skipping[i] = null; + } + } + }, + }; + + mergedVisitor[kind] = mergedEnterLeave; + } + + return mergedVisitor; +} + +/** + * Given a visitor instance and a node kind, return EnterLeaveVisitor for that kind. + */ +export function getEnterLeaveForKind(visitor: ASTVisitor, kind: Kind): EnterLeaveVisitor { + const kindVisitor: ASTVisitFn | EnterLeaveVisitor | undefined = (visitor as any)[kind]; + + if (typeof kindVisitor === 'object') { + // { Kind: { enter() {}, leave() {} } } + return kindVisitor; + } else if (typeof kindVisitor === 'function') { + // { Kind() {} } + return { enter: kindVisitor, leave: undefined }; + } + + // { enter() {}, leave() {} } + return { enter: (visitor as any).enter, leave: (visitor as any).leave }; +} + +/** + * Given a visitor instance, if it is leaving or not, and a node kind, return + * the function the visitor runtime should call. + * + * @deprecated Please use `getEnterLeaveForKind` instead. Will be removed in v17 + */ +/* c8 ignore next 8 */ +export function getVisitFn(visitor: ASTVisitor, kind: Kind, isLeaving: boolean): ASTVisitFn | undefined { + const { enter, leave } = getEnterLeaveForKind(visitor, kind); + return isLeaving ? leave : enter; +} diff --git a/packages/graphql/src/subscription/index.ts b/packages/graphql/src/subscription/index.ts new file mode 100644 index 00000000000..52a7a1a68f6 --- /dev/null +++ b/packages/graphql/src/subscription/index.ts @@ -0,0 +1,21 @@ +/** + * NOTE: the `graphql/subscription` module has been deprecated with its + * exported functions integrated into the `graphql/execution` module, to + * better conform with the terminology of the GraphQL specification. + * + * For backwards compatibility, the `graphql/subscription` module + * currently re-exports the moved functions from the `graphql/execution` + * module. In the next major release, the `graphql/subscription` module + * will be dropped entirely. + */ + +import type { ExecutionArgs } from '../execution/execute.js'; + +/** + * @deprecated use ExecutionArgs instead. Will be removed in v17 + * + * ExecutionArgs has been broadened to include all properties within SubscriptionArgs. + * The SubscriptionArgs type is retained for backwards compatibility. + */ + +export interface SubscriptionArgs extends ExecutionArgs {} diff --git a/packages/graphql/src/type/__tests__/assertName-test.ts b/packages/graphql/src/type/__tests__/assertName-test.ts new file mode 100644 index 00000000000..6ba22b2e16d --- /dev/null +++ b/packages/graphql/src/type/__tests__/assertName-test.ts @@ -0,0 +1,49 @@ +import { assertEnumValueName, assertName } from '../assertName.js'; + +describe('assertName', () => { + it('passthrough valid name', () => { + expect(assertName('_ValidName123')).toEqual('_ValidName123'); + }); + + it('throws on empty strings', () => { + expect(() => assertName('')).toThrow('Expected name to be a non-empty string.'); + }); + + it('throws for names with invalid characters', () => { + expect(() => assertName('>--()-->')).toThrow('Names must only contain [_a-zA-Z0-9] but ">--()-->" does not.'); + }); + + it('throws for names starting with invalid characters', () => { + expect(() => assertName('42MeaningsOfLife')).toThrow( + 'Names must start with [_a-zA-Z] but "42MeaningsOfLife" does not.' + ); + }); +}); + +describe('assertEnumValueName', () => { + it('passthrough valid name', () => { + expect(assertEnumValueName('_ValidName123')).toEqual('_ValidName123'); + }); + + it('throws on empty strings', () => { + expect(() => assertEnumValueName('')).toThrow('Expected name to be a non-empty string.'); + }); + + it('throws for names with invalid characters', () => { + expect(() => assertEnumValueName('>--()-->')).toThrow( + 'Names must only contain [_a-zA-Z0-9] but ">--()-->" does not.' + ); + }); + + it('throws for names starting with invalid characters', () => { + expect(() => assertEnumValueName('42MeaningsOfLife')).toThrow( + 'Names must start with [_a-zA-Z] but "42MeaningsOfLife" does not.' + ); + }); + + it('throws for restricted names', () => { + expect(() => assertEnumValueName('true')).toThrow('Enum values cannot be named: true'); + expect(() => assertEnumValueName('false')).toThrow('Enum values cannot be named: false'); + expect(() => assertEnumValueName('null')).toThrow('Enum values cannot be named: null'); + }); +}); diff --git a/packages/graphql/src/type/__tests__/definition-test.ts b/packages/graphql/src/type/__tests__/definition-test.ts new file mode 100644 index 00000000000..bc6170714aa --- /dev/null +++ b/packages/graphql/src/type/__tests__/definition-test.ts @@ -0,0 +1,672 @@ +import { identityFunc } from '../../jsutils/identityFunc.js'; +import { inspect } from '../../jsutils/inspect.js'; + +import { parseValue } from '../../language/parser.js'; + +import type { GraphQLNullableType, GraphQLType } from '../definition.js'; +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, +} from '../definition.js'; + +const ScalarType = new GraphQLScalarType({ name: 'Scalar' }); +const ObjectType = new GraphQLObjectType({ name: 'Object', fields: {} }); +const InterfaceType = new GraphQLInterfaceType({ + name: 'Interface', + fields: {}, +}); +const UnionType = new GraphQLUnionType({ name: 'Union', types: [ObjectType] }); +const EnumType = new GraphQLEnumType({ name: 'Enum', values: { foo: {} } }); +const InputObjectType = new GraphQLInputObjectType({ + name: 'InputObject', + fields: {}, +}); + +const ListOfScalarsType = new GraphQLList(ScalarType); +const NonNullScalarType = new GraphQLNonNull(ScalarType); +const ListOfNonNullScalarsType = new GraphQLList(NonNullScalarType); +const NonNullListOfScalars = new GraphQLNonNull(ListOfScalarsType); + +/* c8 ignore next */ +// @ts-expect-error +const dummyFunc = () => expect.fail('Never called and used as a placeholder'); + +describe('Type System: Scalars', () => { + it('accepts a Scalar type defining serialize', () => { + expect(() => new GraphQLScalarType({ name: 'SomeScalar' })).not.toThrow(); + }); + + it('accepts a Scalar type defining specifiedByURL', () => { + expect( + () => + new GraphQLScalarType({ + name: 'SomeScalar', + specifiedByURL: 'https://example.com/foo_spec', + }) + ).not.toThrow(); + }); + + it('accepts a Scalar type defining parseValue and parseLiteral', () => { + expect( + () => + new GraphQLScalarType({ + name: 'SomeScalar', + parseValue: dummyFunc, + parseLiteral: dummyFunc, + }) + ).not.toThrow(); + }); + + it('provides default methods if omitted', () => { + const scalar = new GraphQLScalarType({ name: 'Foo' }); + + expect(scalar.serialize).toEqual(identityFunc); + expect(scalar.parseValue).toEqual(identityFunc); + expect(typeof scalar.parseLiteral === 'function').toBeTruthy(); + }); + + it('use parseValue for parsing literals if parseLiteral omitted', () => { + const scalar = new GraphQLScalarType({ + name: 'Foo', + parseValue(value) { + return 'parseValue: ' + inspect(value); + }, + }); + + expect(scalar.parseLiteral(parseValue('null'))).toEqual('parseValue: null'); + expect(scalar.parseLiteral(parseValue('{ foo: "bar" }'))).toEqual('parseValue: { foo: "bar" }'); + expect(scalar.parseLiteral(parseValue('{ foo: { bar: $var } }'), { var: 'baz' })).toEqual( + 'parseValue: { foo: { bar: "baz" } }' + ); + }); + + it('rejects a Scalar type defining parseLiteral but not parseValue', () => { + expect( + () => + new GraphQLScalarType({ + name: 'SomeScalar', + parseLiteral: dummyFunc, + }) + ).toThrow('SomeScalar must provide both "parseValue" and "parseLiteral" functions.'); + }); +}); + +describe('Type System: Objects', () => { + it('does not mutate passed field definitions', () => { + const outputFields = { + field1: { type: ScalarType }, + field2: { + type: ScalarType, + args: { + id: { type: ScalarType }, + }, + }, + }; + const testObject1 = new GraphQLObjectType({ + name: 'Test1', + fields: outputFields, + }); + const testObject2 = new GraphQLObjectType({ + name: 'Test2', + fields: outputFields, + }); + + expect(testObject1.getFields()).toEqual(testObject2.getFields()); + expect(outputFields).toEqual({ + field1: { + type: ScalarType, + }, + field2: { + type: ScalarType, + args: { + id: { type: ScalarType }, + }, + }, + }); + + const inputFields = { + field1: { type: ScalarType }, + field2: { type: ScalarType }, + }; + const testInputObject1 = new GraphQLInputObjectType({ + name: 'Test1', + fields: inputFields, + }); + const testInputObject2 = new GraphQLInputObjectType({ + name: 'Test2', + fields: inputFields, + }); + + expect(testInputObject1.getFields()).toEqual(testInputObject2.getFields()); + expect(inputFields).toEqual({ + field1: { type: ScalarType }, + field2: { type: ScalarType }, + }); + }); + + it('defines an object type with deprecated field', () => { + const TypeWithDeprecatedField = new GraphQLObjectType({ + name: 'foo', + fields: { + bar: { + type: ScalarType, + deprecationReason: 'A terrible reason', + }, + baz: { + type: ScalarType, + deprecationReason: '', + }, + }, + }); + + expect(TypeWithDeprecatedField.getFields()['bar']).toMatchObject({ + name: 'bar', + deprecationReason: 'A terrible reason', + }); + + expect(TypeWithDeprecatedField.getFields()['baz']).toMatchObject({ + name: 'baz', + deprecationReason: '', + }); + }); + + it('accepts an Object type with a field function', () => { + const objType = new GraphQLObjectType({ + name: 'SomeObject', + fields: () => ({ + f: { type: ScalarType }, + }), + }); + expect(objType.getFields()).toEqual({ + f: { + name: 'f', + description: undefined, + type: ScalarType, + args: [], + resolve: undefined, + subscribe: undefined, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + }); + }); + + it('accepts an Object type with field args', () => { + const objType = new GraphQLObjectType({ + name: 'SomeObject', + fields: { + f: { + type: ScalarType, + args: { + arg: { type: ScalarType }, + }, + }, + }, + }); + expect(objType.getFields()).toEqual({ + f: { + name: 'f', + description: undefined, + type: ScalarType, + args: [ + { + name: 'arg', + description: undefined, + type: ScalarType, + defaultValue: undefined, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + ], + resolve: undefined, + subscribe: undefined, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + }); + }); + + it('accepts an Object type with array interfaces', () => { + const objType = new GraphQLObjectType({ + name: 'SomeObject', + fields: {}, + interfaces: [InterfaceType], + }); + expect(objType.getInterfaces()).toEqual([InterfaceType]); + }); + + it('accepts an Object type with interfaces as a function returning an array', () => { + const objType = new GraphQLObjectType({ + name: 'SomeObject', + fields: {}, + interfaces: () => [InterfaceType], + }); + expect(objType.getInterfaces()).toEqual([InterfaceType]); + }); + + it('accepts a lambda as an Object field resolver', () => { + const objType = new GraphQLObjectType({ + name: 'SomeObject', + fields: { + f: { + type: ScalarType, + resolve: dummyFunc, + }, + }, + }); + expect(() => objType.getFields()).not.toThrow(); + }); + + it('rejects an Object type with invalid name', () => { + expect(() => new GraphQLObjectType({ name: 'bad-name', fields: {} })).toThrow( + 'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.' + ); + }); + + it('rejects an Object type with incorrectly named fields', () => { + const objType = new GraphQLObjectType({ + name: 'SomeObject', + fields: { + 'bad-name': { type: ScalarType }, + }, + }); + expect(() => objType.getFields()).toThrow('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.'); + }); + + it('rejects an Object type with a field function that returns incorrect type', () => { + const objType = new GraphQLObjectType({ + name: 'SomeObject', + // @ts-expect-error (Wrong type of return) + fields() { + return [{ field: ScalarType }]; + }, + }); + expect(() => objType.getFields()).toThrow(); + }); + + it('rejects an Object type with incorrectly named field args', () => { + const objType = new GraphQLObjectType({ + name: 'SomeObject', + fields: { + badField: { + type: ScalarType, + args: { + 'bad-name': { type: ScalarType }, + }, + }, + }, + }); + expect(() => objType.getFields()).toThrow('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.'); + }); +}); + +describe('Type System: Interfaces', () => { + it('accepts an Interface type defining resolveType', () => { + expect( + () => + new GraphQLInterfaceType({ + name: 'AnotherInterface', + fields: { f: { type: ScalarType } }, + }) + ).not.toThrow(); + }); + + it('accepts an Interface type with an array of interfaces', () => { + const implementing = new GraphQLInterfaceType({ + name: 'AnotherInterface', + fields: {}, + interfaces: [InterfaceType], + }); + expect(implementing.getInterfaces()).toEqual([InterfaceType]); + }); + + it('accepts an Interface type with interfaces as a function returning an array', () => { + const implementing = new GraphQLInterfaceType({ + name: 'AnotherInterface', + fields: {}, + interfaces: () => [InterfaceType], + }); + expect(implementing.getInterfaces()).toEqual([InterfaceType]); + }); + + it('rejects an Interface type with invalid name', () => { + expect(() => new GraphQLInterfaceType({ name: 'bad-name', fields: {} })).toThrow( + 'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.' + ); + }); +}); + +describe('Type System: Unions', () => { + it('accepts a Union type defining resolveType', () => { + expect( + () => + new GraphQLUnionType({ + name: 'SomeUnion', + types: [ObjectType], + }) + ).not.toThrow(); + }); + + it('accepts a Union type with array types', () => { + const unionType = new GraphQLUnionType({ + name: 'SomeUnion', + types: [ObjectType], + }); + expect(unionType.getTypes()).toEqual([ObjectType]); + }); + + it('accepts a Union type with function returning an array of types', () => { + const unionType = new GraphQLUnionType({ + name: 'SomeUnion', + types: () => [ObjectType], + }); + expect(unionType.getTypes()).toEqual([ObjectType]); + }); + + it('accepts a Union type without types', () => { + const unionType = new GraphQLUnionType({ + name: 'SomeUnion', + types: [], + }); + expect(unionType.getTypes()).toEqual([]); + }); + + it('rejects an Union type with invalid name', () => { + expect(() => new GraphQLUnionType({ name: 'bad-name', types: [] })).toThrow( + 'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.' + ); + }); +}); + +describe('Type System: Enums', () => { + it('defines an enum type with deprecated value', () => { + const EnumTypeWithDeprecatedValue = new GraphQLEnumType({ + name: 'EnumWithDeprecatedValue', + values: { + foo: { deprecationReason: 'Just because' }, + bar: { deprecationReason: '' }, + }, + }); + + expect(EnumTypeWithDeprecatedValue.getValues()[0]).toMatchObject({ + name: 'foo', + deprecationReason: 'Just because', + }); + + expect(EnumTypeWithDeprecatedValue.getValues()[1]).toMatchObject({ + name: 'bar', + deprecationReason: '', + }); + }); + + it('defines an enum type with a value of `null` and `undefined`', () => { + const EnumTypeWithNullishValue = new GraphQLEnumType({ + name: 'EnumWithNullishValue', + values: { + NULL: { value: null }, + NAN: { value: NaN }, + NO_CUSTOM_VALUE: { value: undefined }, + }, + }); + + expect(EnumTypeWithNullishValue.getValues()).toEqual([ + { + name: 'NULL', + description: undefined, + value: null, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + { + name: 'NAN', + description: undefined, + value: NaN, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + { + name: 'NO_CUSTOM_VALUE', + description: undefined, + value: 'NO_CUSTOM_VALUE', + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + ]); + }); + + it('accepts a well defined Enum type with empty value definition', () => { + const enumType = new GraphQLEnumType({ + name: 'SomeEnum', + values: { + FOO: {}, + BAR: {}, + }, + }); + expect(enumType.getValue('FOO')).toHaveProperty('value', 'FOO'); + expect(enumType.getValue('BAR')).toHaveProperty('value', 'BAR'); + }); + + it('accepts a well defined Enum type with internal value definition', () => { + const enumType = new GraphQLEnumType({ + name: 'SomeEnum', + values: { + FOO: { value: 10 }, + BAR: { value: 20 }, + }, + }); + expect(enumType.getValue('FOO')).toHaveProperty('value', 10); + expect(enumType.getValue('BAR')).toHaveProperty('value', 20); + }); + + it('rejects an Enum type with invalid name', () => { + expect(() => new GraphQLEnumType({ name: 'bad-name', values: {} })).toThrow( + 'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.' + ); + }); + + it('rejects an Enum type with incorrectly named values', () => { + expect( + () => + new GraphQLEnumType({ + name: 'SomeEnum', + values: { + 'bad-name': {}, + }, + }) + ).toThrow('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.'); + }); +}); + +describe('Type System: Input Objects', () => { + describe('Input Objects must have fields', () => { + it('accepts an Input Object type with fields', () => { + const inputObjType = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + f: { type: ScalarType }, + }, + }); + expect(inputObjType.getFields()).toEqual({ + f: { + name: 'f', + description: undefined, + type: ScalarType, + defaultValue: undefined, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + }); + }); + + it('accepts an Input Object type with a field function', () => { + const inputObjType = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: () => ({ + f: { type: ScalarType }, + }), + }); + expect(inputObjType.getFields()).toEqual({ + f: { + name: 'f', + description: undefined, + type: ScalarType, + defaultValue: undefined, + extensions: {}, + deprecationReason: undefined, + astNode: undefined, + }, + }); + }); + + it('rejects an Input Object type with invalid name', () => { + expect(() => new GraphQLInputObjectType({ name: 'bad-name', fields: {} })).toThrow( + 'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.' + ); + }); + + it('rejects an Input Object type with incorrectly named fields', () => { + const inputObjType = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + 'bad-name': { type: ScalarType }, + }, + }); + expect(() => inputObjType.getFields()).toThrow('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.'); + }); + }); + + describe('Input Object fields must not have resolvers', () => { + it('rejects an Input Object type with resolvers', () => { + const inputObjType = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + // @ts-expect-error (Input fields cannot have resolvers) + f: { type: ScalarType, resolve: dummyFunc }, + }, + }); + expect(() => inputObjType.getFields()).toThrow( + 'SomeInputObject.f field has a resolve property, but Input Types cannot define resolvers.' + ); + }); + + it('rejects an Input Object type with resolver constant', () => { + const inputObjType = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + // @ts-expect-error (Input fields cannot have resolvers) + f: { type: ScalarType, resolve: {} }, + }, + }); + expect(() => inputObjType.getFields()).toThrow( + 'SomeInputObject.f field has a resolve property, but Input Types cannot define resolvers.' + ); + }); + }); + + it('Deprecation reason is preserved on fields', () => { + const inputObjType = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + deprecatedField: { + type: ScalarType, + deprecationReason: 'not used anymore', + }, + }, + }); + expect(inputObjType.toConfig()).toHaveProperty('fields.deprecatedField.deprecationReason', 'not used anymore'); + }); +}); + +describe('Type System: List', () => { + function expectList(type: GraphQLType) { + return expect(() => new GraphQLList(type)); + } + + it('accepts an type as item type of list', () => { + expectList(ScalarType).not.toThrow(); + expectList(ObjectType).not.toThrow(); + expectList(UnionType).not.toThrow(); + expectList(InterfaceType).not.toThrow(); + expectList(EnumType).not.toThrow(); + expectList(InputObjectType).not.toThrow(); + expectList(ListOfScalarsType).not.toThrow(); + expectList(NonNullScalarType).not.toThrow(); + }); +}); + +describe('Type System: Non-Null', () => { + function expectNonNull(type: GraphQLNullableType) { + return expect(() => new GraphQLNonNull(type)); + } + + it('accepts an type as nullable type of non-null', () => { + expectNonNull(ScalarType).not.toThrow(); + expectNonNull(ObjectType).not.toThrow(); + expectNonNull(UnionType).not.toThrow(); + expectNonNull(InterfaceType).not.toThrow(); + expectNonNull(EnumType).not.toThrow(); + expectNonNull(InputObjectType).not.toThrow(); + expectNonNull(ListOfScalarsType).not.toThrow(); + expectNonNull(ListOfNonNullScalarsType).not.toThrow(); + }); +}); + +describe('Type System: test utility methods', () => { + it('stringifies types', () => { + expect(String(ScalarType)).toEqual('Scalar'); + expect(String(ObjectType)).toEqual('Object'); + expect(String(InterfaceType)).toEqual('Interface'); + expect(String(UnionType)).toEqual('Union'); + expect(String(EnumType)).toEqual('Enum'); + expect(String(InputObjectType)).toEqual('InputObject'); + + expect(String(NonNullScalarType)).toEqual('Scalar!'); + expect(String(ListOfScalarsType)).toEqual('[Scalar]'); + expect(String(NonNullListOfScalars)).toEqual('[Scalar]!'); + expect(String(ListOfNonNullScalarsType)).toEqual('[Scalar!]'); + expect(String(new GraphQLList(ListOfScalarsType))).toEqual('[[Scalar]]'); + }); + + it('JSON.stringifies types', () => { + expect(JSON.stringify(ScalarType)).toEqual('"Scalar"'); + expect(JSON.stringify(ObjectType)).toEqual('"Object"'); + expect(JSON.stringify(InterfaceType)).toEqual('"Interface"'); + expect(JSON.stringify(UnionType)).toEqual('"Union"'); + expect(JSON.stringify(EnumType)).toEqual('"Enum"'); + expect(JSON.stringify(InputObjectType)).toEqual('"InputObject"'); + + expect(JSON.stringify(NonNullScalarType)).toEqual('"Scalar!"'); + expect(JSON.stringify(ListOfScalarsType)).toEqual('"[Scalar]"'); + expect(JSON.stringify(NonNullListOfScalars)).toEqual('"[Scalar]!"'); + expect(JSON.stringify(ListOfNonNullScalarsType)).toEqual('"[Scalar!]"'); + expect(JSON.stringify(new GraphQLList(ListOfScalarsType))).toEqual('"[[Scalar]]"'); + }); + + it('Object.toStringifies types', () => { + function toString(obj: unknown): string { + return Object.prototype.toString.call(obj); + } + + expect(toString(ScalarType)).toEqual('[object GraphQLScalarType]'); + expect(toString(ObjectType)).toEqual('[object GraphQLObjectType]'); + expect(toString(InterfaceType)).toEqual('[object GraphQLInterfaceType]'); + expect(toString(UnionType)).toEqual('[object GraphQLUnionType]'); + expect(toString(EnumType)).toEqual('[object GraphQLEnumType]'); + expect(toString(InputObjectType)).toEqual('[object GraphQLInputObjectType]'); + expect(toString(NonNullScalarType)).toEqual('[object GraphQLNonNull]'); + expect(toString(ListOfScalarsType)).toEqual('[object GraphQLList]'); + }); +}); diff --git a/packages/graphql/src/type/__tests__/directive-test.ts b/packages/graphql/src/type/__tests__/directive-test.ts new file mode 100644 index 00000000000..0accbe572a3 --- /dev/null +++ b/packages/graphql/src/type/__tests__/directive-test.ts @@ -0,0 +1,106 @@ +import { DirectiveLocation } from '../../language/directiveLocation.js'; + +import { GraphQLDirective } from '../directives.js'; +import { GraphQLInt, GraphQLString } from '../scalars.js'; + +describe('Type System: Directive', () => { + it('defines a directive with no args', () => { + const directive = new GraphQLDirective({ + name: 'Foo', + locations: [DirectiveLocation.QUERY], + }); + + expect(directive).toMatchObject({ + name: 'Foo', + args: [], + isRepeatable: false, + locations: ['QUERY'], + }); + }); + + it('defines a directive with multiple args', () => { + const directive = new GraphQLDirective({ + name: 'Foo', + args: { + foo: { type: GraphQLString }, + bar: { type: GraphQLInt }, + }, + locations: [DirectiveLocation.QUERY], + }); + + expect(directive).toMatchObject({ + name: 'Foo', + args: [ + { + name: 'foo', + description: undefined, + type: GraphQLString, + defaultValue: undefined, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + { + name: 'bar', + description: undefined, + type: GraphQLInt, + defaultValue: undefined, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + ], + isRepeatable: false, + locations: ['QUERY'], + }); + }); + + it('defines a repeatable directive', () => { + const directive = new GraphQLDirective({ + name: 'Foo', + isRepeatable: true, + locations: [DirectiveLocation.QUERY], + }); + + expect(directive).toMatchObject({ + name: 'Foo', + args: [], + isRepeatable: true, + locations: ['QUERY'], + }); + }); + + it('can be stringified, JSON.stringified and Object.toStringified', () => { + const directive = new GraphQLDirective({ + name: 'Foo', + locations: [DirectiveLocation.QUERY], + }); + + expect(String(directive)).toEqual('@Foo'); + expect(JSON.stringify(directive)).toEqual('"@Foo"'); + expect(Object.prototype.toString.call(directive)).toEqual('[object GraphQLDirective]'); + }); + + it('rejects a directive with invalid name', () => { + expect( + () => + new GraphQLDirective({ + name: 'bad-name', + locations: [DirectiveLocation.QUERY], + }) + ).toThrow('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.'); + }); + + it('rejects a directive with incorrectly named arg', () => { + expect( + () => + new GraphQLDirective({ + name: 'Foo', + locations: [DirectiveLocation.QUERY], + args: { + 'bad-name': { type: GraphQLString }, + }, + }) + ).toThrow('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.'); + }); +}); diff --git a/packages/graphql/src/type/__tests__/enumType-test.ts b/packages/graphql/src/type/__tests__/enumType-test.ts new file mode 100644 index 00000000000..d4403ed63d0 --- /dev/null +++ b/packages/graphql/src/type/__tests__/enumType-test.ts @@ -0,0 +1,388 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { introspectionFromSchema } from '../../utilities/introspectionFromSchema.js'; + +import { graphqlSync } from '../../graphql.js'; + +import { GraphQLEnumType, GraphQLObjectType } from '../definition.js'; +import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../scalars.js'; +import { GraphQLSchema } from '../schema.js'; + +const ColorType = new GraphQLEnumType({ + name: 'Color', + values: { + RED: { value: 0 }, + GREEN: { value: 1 }, + BLUE: { value: 2 }, + }, +}); + +const Complex1 = { someRandomObject: new Date() }; +const Complex2 = { someRandomValue: 123 }; + +const ComplexEnum = new GraphQLEnumType({ + name: 'Complex', + values: { + ONE: { value: Complex1 }, + TWO: { value: Complex2 }, + }, +}); + +const QueryType = new GraphQLObjectType({ + name: 'Query', + fields: { + colorEnum: { + type: ColorType, + args: { + fromEnum: { type: ColorType }, + fromInt: { type: GraphQLInt }, + fromString: { type: GraphQLString }, + }, + resolve(_source, { fromEnum, fromInt, fromString }) { + return fromInt !== undefined ? fromInt : fromString !== undefined ? fromString : fromEnum; + }, + }, + colorInt: { + type: GraphQLInt, + args: { + fromEnum: { type: ColorType }, + }, + resolve(_source, { fromEnum }) { + return fromEnum; + }, + }, + complexEnum: { + type: ComplexEnum, + args: { + fromEnum: { + type: ComplexEnum, + // Note: defaultValue is provided an *internal* representation for + // Enums, rather than the string name. + defaultValue: Complex1, + }, + provideGoodValue: { type: GraphQLBoolean }, + provideBadValue: { type: GraphQLBoolean }, + }, + resolve(_source, { fromEnum, provideGoodValue, provideBadValue }) { + if (provideGoodValue) { + // Note: this is one of the references of the internal values which + // ComplexEnum allows. + return Complex2; + } + if (provideBadValue) { + // Note: similar shape, but not the same *reference* + // as Complex2 above. Enum internal values require === equality. + return { someRandomValue: 123 }; + } + return fromEnum; + }, + }, + }, +}); + +const MutationType = new GraphQLObjectType({ + name: 'Mutation', + fields: { + favoriteEnum: { + type: ColorType, + args: { color: { type: ColorType } }, + resolve: (_source, { color }) => color, + }, + }, +}); + +const SubscriptionType = new GraphQLObjectType({ + name: 'Subscription', + fields: { + subscribeToEnum: { + type: ColorType, + args: { color: { type: ColorType } }, + resolve: (_source, { color }) => color, + }, + }, +}); + +const schema = new GraphQLSchema({ + query: QueryType, + mutation: MutationType, + subscription: SubscriptionType, +}); + +function executeQuery(source: string, variableValues?: { readonly [variable: string]: unknown }) { + return graphqlSync({ schema, source, variableValues }); +} + +describe('Type System: Enum Values', () => { + it('accepts enum literals as input', () => { + const result = executeQuery('{ colorInt(fromEnum: GREEN) }'); + + expect(result).toEqual({ + data: { colorInt: 1 }, + }); + }); + + it('enum may be output type', () => { + const result = executeQuery('{ colorEnum(fromInt: 1) }'); + + expect(result).toEqual({ + data: { colorEnum: 'GREEN' }, + }); + }); + + it('enum may be both input and output type', () => { + const result = executeQuery('{ colorEnum(fromEnum: GREEN) }'); + + expect(result).toEqual({ + data: { colorEnum: 'GREEN' }, + }); + }); + + it('does not accept string literals', () => { + const result = executeQuery('{ colorEnum(fromEnum: "GREEN") }'); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Enum "Color" cannot represent non-enum value: "GREEN". Did you mean the enum value "GREEN"?', + locations: [{ line: 1, column: 23 }], + }, + ], + }); + }); + + it('does not accept values not in the enum', () => { + const result = executeQuery('{ colorEnum(fromEnum: GREENISH) }'); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Value "GREENISH" does not exist in "Color" enum. Did you mean the enum value "GREEN"?', + locations: [{ line: 1, column: 23 }], + }, + ], + }); + }); + + it('does not accept values with incorrect casing', () => { + const result = executeQuery('{ colorEnum(fromEnum: green) }'); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Value "green" does not exist in "Color" enum. Did you mean the enum value "GREEN" or "RED"?', + locations: [{ line: 1, column: 23 }], + }, + ], + }); + }); + + it('does not accept incorrect internal value', () => { + const result = executeQuery('{ colorEnum(fromString: "GREEN") }'); + + expectJSON(result).toDeepEqual({ + data: { colorEnum: null }, + errors: [ + { + message: 'Enum "Color" cannot represent value: "GREEN"', + locations: [{ line: 1, column: 3 }], + path: ['colorEnum'], + }, + ], + }); + }); + + it('does not accept internal value in place of enum literal', () => { + const result = executeQuery('{ colorEnum(fromEnum: 1) }'); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Enum "Color" cannot represent non-enum value: 1.', + locations: [{ line: 1, column: 23 }], + }, + ], + }); + }); + + it('does not accept enum literal in place of int', () => { + const result = executeQuery('{ colorEnum(fromInt: GREEN) }'); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Int cannot represent non-integer value: GREEN', + locations: [{ line: 1, column: 22 }], + }, + ], + }); + }); + + it('accepts JSON string as enum variable', () => { + const doc = 'query ($color: Color!) { colorEnum(fromEnum: $color) }'; + const result = executeQuery(doc, { color: 'BLUE' }); + + expect(result).toEqual({ + data: { colorEnum: 'BLUE' }, + }); + }); + + it('accepts enum literals as input arguments to mutations', () => { + const doc = 'mutation ($color: Color!) { favoriteEnum(color: $color) }'; + const result = executeQuery(doc, { color: 'GREEN' }); + + expect(result).toEqual({ + data: { favoriteEnum: 'GREEN' }, + }); + }); + + it('accepts enum literals as input arguments to subscriptions', () => { + const doc = 'subscription ($color: Color!) { subscribeToEnum(color: $color) }'; + const result = executeQuery(doc, { color: 'GREEN' }); + + expect(result).toEqual({ + data: { subscribeToEnum: 'GREEN' }, + }); + }); + + it('does not accept internal value as enum variable', () => { + const doc = 'query ($color: Color!) { colorEnum(fromEnum: $color) }'; + const result = executeQuery(doc, { color: 2 }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$color" got invalid value 2; Enum "Color" cannot represent non-string value: 2.', + locations: [{ line: 1, column: 8 }], + }, + ], + }); + }); + + it('does not accept string variables as enum input', () => { + const doc = 'query ($color: String!) { colorEnum(fromEnum: $color) }'; + const result = executeQuery(doc, { color: 'BLUE' }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$color" of type "String!" used in position expecting type "Color".', + locations: [ + { line: 1, column: 8 }, + { line: 1, column: 47 }, + ], + }, + ], + }); + }); + + it('does not accept internal value variable as enum input', () => { + const doc = 'query ($color: Int!) { colorEnum(fromEnum: $color) }'; + const result = executeQuery(doc, { color: 2 }); + + expectJSON(result).toDeepEqual({ + errors: [ + { + message: 'Variable "$color" of type "Int!" used in position expecting type "Color".', + locations: [ + { line: 1, column: 8 }, + { line: 1, column: 44 }, + ], + }, + ], + }); + }); + + it('enum value may have an internal value of 0', () => { + const result = executeQuery(` + { + colorEnum(fromEnum: RED) + colorInt(fromEnum: RED) + } + `); + + expect(result).toEqual({ + data: { + colorEnum: 'RED', + colorInt: 0, + }, + }); + }); + + it('enum inputs may be nullable', () => { + const result = executeQuery(` + { + colorEnum + colorInt + } + `); + + expect(result).toEqual({ + data: { + colorEnum: null, + colorInt: null, + }, + }); + }); + + it('presents a getValues() API for complex enums', () => { + const values = ComplexEnum.getValues(); + expect(values).toMatchObject([ + { + name: 'ONE', + description: undefined, + value: Complex1, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + { + name: 'TWO', + description: undefined, + value: Complex2, + deprecationReason: undefined, + extensions: {}, + astNode: undefined, + }, + ]); + }); + + it('presents a getValue() API for complex enums', () => { + const oneValue = ComplexEnum.getValue('ONE'); + expect(oneValue).toMatchObject({ name: 'ONE', value: Complex1 }); + + // @ts-expect-error + const badUsage = ComplexEnum.getValue(Complex1); + expect(badUsage).toEqual(undefined); + }); + + it('may be internally represented with complex values', () => { + const result = executeQuery(` + { + first: complexEnum + second: complexEnum(fromEnum: TWO) + good: complexEnum(provideGoodValue: true) + bad: complexEnum(provideBadValue: true) + } + `); + + expectJSON(result).toDeepEqual({ + data: { + first: 'ONE', + second: 'TWO', + good: 'TWO', + bad: null, + }, + errors: [ + { + message: 'Enum "Complex" cannot represent value: { someRandomValue: 123 }', + locations: [{ line: 6, column: 9 }], + path: ['bad'], + }, + ], + }); + }); + + it('can be introspected without error', () => { + expect(() => introspectionFromSchema(schema)).not.toThrow(); + }); +}); diff --git a/packages/graphql/src/type/__tests__/extensions-test.ts b/packages/graphql/src/type/__tests__/extensions-test.ts new file mode 100644 index 00000000000..5f228e62773 --- /dev/null +++ b/packages/graphql/src/type/__tests__/extensions-test.ts @@ -0,0 +1,386 @@ +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, +} from '../definition.js'; +import { GraphQLDirective } from '../directives.js'; +import { GraphQLSchema } from '../schema.js'; + +const dummyType = new GraphQLScalarType({ name: 'DummyScalar' }); + +function expectObjMap(value: unknown) { + expect(value != null && typeof value === 'object').toBeTruthy(); + expect(Object.getPrototypeOf(value)).toEqual(null); + return expect(value); +} + +describe('Type System: Extensions', () => { + describe('GraphQLScalarType', () => { + it('without extensions', () => { + const someScalar = new GraphQLScalarType({ name: 'SomeScalar' }); + expect(someScalar.extensions).toEqual({}); + + const config = someScalar.toConfig(); + expect(config.extensions).toEqual({}); + }); + + it('with extensions', () => { + const scalarExtensions = Object.freeze({ SomeScalarExt: 'scalar' }); + const someScalar = new GraphQLScalarType({ + name: 'SomeScalar', + extensions: scalarExtensions, + }); + + expectObjMap(someScalar.extensions).toEqual(scalarExtensions); + + const config = someScalar.toConfig(); + expectObjMap(config.extensions).toEqual(scalarExtensions); + }); + }); + + describe('GraphQLObjectType', () => { + it('without extensions', () => { + const someObject = new GraphQLObjectType({ + name: 'SomeObject', + fields: { + someField: { + type: dummyType, + args: { + someArg: { + type: dummyType, + }, + }, + }, + }, + }); + + expect(someObject.extensions).toEqual({}); + const someField = someObject.getFields()['someField']; + expect(someField.extensions).toEqual({}); + const someArg = someField.args[0]; + expect(someArg.extensions).toEqual({}); + + const config = someObject.toConfig(); + expect(config.extensions).toEqual({}); + const someFieldConfig = config.fields['someField']; + expect(someFieldConfig.extensions).toEqual({}); + expect(someFieldConfig.args != null).toBeTruthy(); + // @ts-expect-error + const someArgConfig = someFieldConfig.args.someArg; + expect(someArgConfig.extensions).toEqual({}); + }); + + it('with extensions', () => { + const objectExtensions = Object.freeze({ SomeObjectExt: 'object' }); + const fieldExtensions = Object.freeze({ SomeFieldExt: 'field' }); + const argExtensions = Object.freeze({ SomeArgExt: 'arg' }); + + const someObject = new GraphQLObjectType({ + name: 'SomeObject', + fields: { + someField: { + type: dummyType, + args: { + someArg: { + type: dummyType, + extensions: argExtensions, + }, + }, + extensions: fieldExtensions, + }, + }, + extensions: objectExtensions, + }); + + expectObjMap(someObject.extensions).toEqual(objectExtensions); + const someField = someObject.getFields()['someField']; + expectObjMap(someField.extensions).toEqual(fieldExtensions); + const someArg = someField.args[0]; + expectObjMap(someArg.extensions).toEqual(argExtensions); + + const config = someObject.toConfig(); + expectObjMap(config.extensions).toEqual(objectExtensions); + const someFieldConfig = config.fields['someField']; + expectObjMap(someFieldConfig.extensions).toEqual(fieldExtensions); + expect(someFieldConfig.args != null).toBeTruthy(); + // @ts-expect-error + const someArgConfig = someFieldConfig.args.someArg; + expectObjMap(someArgConfig.extensions).toEqual(argExtensions); + }); + }); + + describe('GraphQLInterfaceType', () => { + it('without extensions', () => { + const someInterface = new GraphQLInterfaceType({ + name: 'SomeInterface', + fields: { + someField: { + type: dummyType, + args: { + someArg: { + type: dummyType, + }, + }, + }, + }, + }); + + expect(someInterface.extensions).toEqual({}); + const someField = someInterface.getFields()['someField']; + expect(someField.extensions).toEqual({}); + const someArg = someField.args[0]; + expect(someArg.extensions).toEqual({}); + + const config = someInterface.toConfig(); + expect(config.extensions).toEqual({}); + const someFieldConfig = config.fields['someField']; + expect(someFieldConfig.extensions).toEqual({}); + expect(someFieldConfig.args != null).toBeTruthy(); + // @ts-expect-error + const someArgConfig = someFieldConfig.args.someArg; + expect(someArgConfig.extensions).toEqual({}); + }); + + it('with extensions', () => { + const interfaceExtensions = Object.freeze({ + SomeInterfaceExt: 'interface', + }); + const fieldExtensions = Object.freeze({ SomeFieldExt: 'field' }); + const argExtensions = Object.freeze({ SomeArgExt: 'arg' }); + + const someInterface = new GraphQLInterfaceType({ + name: 'SomeInterface', + fields: { + someField: { + type: dummyType, + args: { + someArg: { + type: dummyType, + extensions: argExtensions, + }, + }, + extensions: fieldExtensions, + }, + }, + extensions: interfaceExtensions, + }); + + expectObjMap(someInterface.extensions).toEqual(interfaceExtensions); + const someField = someInterface.getFields()['someField']; + expectObjMap(someField.extensions).toEqual(fieldExtensions); + const someArg = someField.args[0]; + expectObjMap(someArg.extensions).toEqual(argExtensions); + + const config = someInterface.toConfig(); + expectObjMap(config.extensions).toEqual(interfaceExtensions); + const someFieldConfig = config.fields['someField']; + expectObjMap(someFieldConfig.extensions).toEqual(fieldExtensions); + expect(someFieldConfig.args != null).toBeTruthy(); + // @ts-expect-error + const someArgConfig = someFieldConfig.args.someArg; + expectObjMap(someArgConfig.extensions).toEqual(argExtensions); + }); + }); + + describe('GraphQLUnionType', () => { + it('without extensions', () => { + const someUnion = new GraphQLUnionType({ + name: 'SomeUnion', + types: [], + }); + + expect(someUnion.extensions).toEqual({}); + + const config = someUnion.toConfig(); + expect(config.extensions).toEqual({}); + }); + + it('with extensions', () => { + const unionExtensions = Object.freeze({ SomeUnionExt: 'union' }); + + const someUnion = new GraphQLUnionType({ + name: 'SomeUnion', + types: [], + extensions: unionExtensions, + }); + + expectObjMap(someUnion.extensions).toEqual(unionExtensions); + + const config = someUnion.toConfig(); + expectObjMap(config.extensions).toEqual(unionExtensions); + }); + }); + + describe('GraphQLEnumType', () => { + it('without extensions', () => { + const someEnum = new GraphQLEnumType({ + name: 'SomeEnum', + values: { + SOME_VALUE: {}, + }, + }); + + expect(someEnum.extensions).toEqual({}); + const someValue = someEnum.getValues()[0]; + expect(someValue.extensions).toEqual({}); + + const config = someEnum.toConfig(); + expect(config.extensions).toEqual({}); + const someValueConfig = config.values['SOME_VALUE']; + expect(someValueConfig.extensions).toEqual({}); + }); + + it('with extensions', () => { + const enumExtensions = Object.freeze({ SomeEnumExt: 'enum' }); + const valueExtensions = Object.freeze({ SomeValueExt: 'value' }); + + const someEnum = new GraphQLEnumType({ + name: 'SomeEnum', + values: { + SOME_VALUE: { + extensions: valueExtensions, + }, + }, + extensions: enumExtensions, + }); + + expectObjMap(someEnum.extensions).toEqual(enumExtensions); + const someValue = someEnum.getValues()[0]; + expectObjMap(someValue.extensions).toEqual(valueExtensions); + + const config = someEnum.toConfig(); + expectObjMap(config.extensions).toEqual(enumExtensions); + const someValueConfig = config.values['SOME_VALUE']; + expectObjMap(someValueConfig.extensions).toEqual(valueExtensions); + }); + }); + + describe('GraphQLInputObjectType', () => { + it('without extensions', () => { + const someInputObject = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + someInputField: { + type: dummyType, + }, + }, + }); + + expect(someInputObject.extensions).toEqual({}); + const someInputField = someInputObject.getFields()['someInputField']; + expect(someInputField.extensions).toEqual({}); + + const config = someInputObject.toConfig(); + expect(config.extensions).toEqual({}); + const someInputFieldConfig = config.fields['someInputField']; + expect(someInputFieldConfig.extensions).toEqual({}); + }); + + it('with extensions', () => { + const inputObjectExtensions = Object.freeze({ + SomeInputObjectExt: 'inputObject', + }); + const inputFieldExtensions = Object.freeze({ + SomeInputFieldExt: 'inputField', + }); + + const someInputObject = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + someInputField: { + type: dummyType, + extensions: inputFieldExtensions, + }, + }, + extensions: inputObjectExtensions, + }); + + expectObjMap(someInputObject.extensions).toEqual(inputObjectExtensions); + const someInputField = someInputObject.getFields()['someInputField']; + expectObjMap(someInputField.extensions).toEqual(inputFieldExtensions); + + const config = someInputObject.toConfig(); + expectObjMap(config.extensions).toEqual(inputObjectExtensions); + const someInputFieldConfig = config.fields['someInputField']; + expectObjMap(someInputFieldConfig.extensions).toEqual(inputFieldExtensions); + }); + }); + + describe('GraphQLDirective', () => { + it('without extensions', () => { + const someDirective = new GraphQLDirective({ + name: 'SomeDirective', + args: { + someArg: { + type: dummyType, + }, + }, + locations: [], + }); + + expect(someDirective.extensions).toEqual({}); + const someArg = someDirective.args[0]; + expect(someArg.extensions).toEqual({}); + + const config = someDirective.toConfig(); + expect(config.extensions).toEqual({}); + const someArgConfig = config.args['someArg']; + expect(someArgConfig.extensions).toEqual({}); + }); + + it('with extensions', () => { + const directiveExtensions = Object.freeze({ + SomeDirectiveExt: 'directive', + }); + const argExtensions = Object.freeze({ SomeArgExt: 'arg' }); + + const someDirective = new GraphQLDirective({ + name: 'SomeDirective', + args: { + someArg: { + type: dummyType, + extensions: argExtensions, + }, + }, + locations: [], + extensions: directiveExtensions, + }); + + expectObjMap(someDirective.extensions).toEqual(directiveExtensions); + const someArg = someDirective.args[0]; + expectObjMap(someArg.extensions).toEqual(argExtensions); + + const config = someDirective.toConfig(); + expectObjMap(config.extensions).toEqual(directiveExtensions); + const someArgConfig = config.args['someArg']; + expectObjMap(someArgConfig.extensions).toEqual(argExtensions); + }); + }); + + describe('GraphQLSchema', () => { + it('without extensions', () => { + const schema = new GraphQLSchema({}); + + expect(schema.extensions).toEqual({}); + + const config = schema.toConfig(); + expect(config.extensions).toEqual({}); + }); + + it('with extensions', () => { + const schemaExtensions = Object.freeze({ + schemaExtension: 'schema', + }); + + const schema = new GraphQLSchema({ extensions: schemaExtensions }); + + expectObjMap(schema.extensions).toEqual(schemaExtensions); + + const config = schema.toConfig(); + expectObjMap(config.extensions).toEqual(schemaExtensions); + }); + }); +}); diff --git a/packages/graphql/src/type/__tests__/introspection-test.ts b/packages/graphql/src/type/__tests__/introspection-test.ts new file mode 100644 index 00000000000..294dbd770ec --- /dev/null +++ b/packages/graphql/src/type/__tests__/introspection-test.ts @@ -0,0 +1,1629 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; +import { getIntrospectionQuery } from '../../utilities/getIntrospectionQuery.js'; + +import { graphqlSync } from '../../graphql.js'; + +import type { GraphQLResolveInfo } from '../definition.js'; + +describe('Introspection', () => { + it('executes an introspection query', () => { + const schema = buildSchema(` + type SomeObject { + someField: String + } + + schema { + query: SomeObject + } + `); + + const source = getIntrospectionQuery({ + descriptions: false, + specifiedByUrl: true, + directiveIsRepeatable: true, + }); + + const result = graphqlSync({ schema, source }); + expect(result).toEqual({ + data: { + __schema: { + queryType: { name: 'SomeObject' }, + mutationType: null, + subscriptionType: null, + types: [ + { + kind: 'OBJECT', + name: 'SomeObject', + specifiedByURL: null, + fields: [ + { + name: 'someField', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + ], + inputFields: null, + interfaces: [], + enumValues: null, + possibleTypes: null, + }, + { + kind: 'SCALAR', + name: 'String', + specifiedByURL: null, + fields: null, + inputFields: null, + interfaces: null, + enumValues: null, + possibleTypes: null, + }, + { + kind: 'SCALAR', + name: 'Boolean', + specifiedByURL: null, + fields: null, + inputFields: null, + interfaces: null, + enumValues: null, + possibleTypes: null, + }, + { + kind: 'OBJECT', + name: '__Schema', + specifiedByURL: null, + fields: [ + { + name: 'description', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'types', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'queryType', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'mutationType', + args: [], + type: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'subscriptionType', + args: [], + type: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'directives', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__Directive', + ofType: null, + }, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + ], + inputFields: null, + interfaces: [], + enumValues: null, + possibleTypes: null, + }, + { + kind: 'OBJECT', + name: '__Type', + specifiedByURL: null, + fields: [ + { + name: 'kind', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'ENUM', + name: '__TypeKind', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'name', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'description', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'specifiedByURL', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'fields', + args: [ + { + name: 'includeDeprecated', + type: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + defaultValue: 'false', + }, + ], + type: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__Field', + ofType: null, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'interfaces', + args: [], + type: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'possibleTypes', + args: [], + type: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'enumValues', + args: [ + { + name: 'includeDeprecated', + type: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + defaultValue: 'false', + }, + ], + type: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__EnumValue', + ofType: null, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'inputFields', + args: [ + { + name: 'includeDeprecated', + type: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + defaultValue: 'false', + }, + ], + type: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__InputValue', + ofType: null, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'ofType', + args: [], + type: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + ], + inputFields: null, + interfaces: [], + enumValues: null, + possibleTypes: null, + }, + { + kind: 'ENUM', + name: '__TypeKind', + specifiedByURL: null, + fields: null, + inputFields: null, + interfaces: null, + enumValues: [ + { + name: 'SCALAR', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'OBJECT', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'INTERFACE', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'UNION', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'ENUM', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'INPUT_OBJECT', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'LIST', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'NON_NULL', + isDeprecated: false, + deprecationReason: null, + }, + ], + possibleTypes: null, + }, + { + kind: 'OBJECT', + name: '__Field', + specifiedByURL: null, + fields: [ + { + name: 'name', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'description', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'args', + args: [ + { + name: 'includeDeprecated', + type: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + defaultValue: 'false', + }, + ], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__InputValue', + ofType: null, + }, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'type', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'isDeprecated', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'deprecationReason', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + ], + inputFields: null, + interfaces: [], + enumValues: null, + possibleTypes: null, + }, + { + kind: 'OBJECT', + name: '__InputValue', + specifiedByURL: null, + fields: [ + { + name: 'name', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'description', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'type', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__Type', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'defaultValue', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'isDeprecated', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'deprecationReason', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + ], + inputFields: null, + interfaces: [], + enumValues: null, + possibleTypes: null, + }, + { + kind: 'OBJECT', + name: '__EnumValue', + specifiedByURL: null, + fields: [ + { + name: 'name', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'description', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'isDeprecated', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'deprecationReason', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + ], + inputFields: null, + interfaces: [], + enumValues: null, + possibleTypes: null, + }, + { + kind: 'OBJECT', + name: '__Directive', + specifiedByURL: null, + fields: [ + { + name: 'name', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'description', + args: [], + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'isRepeatable', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'locations', + args: [], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'ENUM', + name: '__DirectiveLocation', + ofType: null, + }, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'args', + args: [ + { + name: 'includeDeprecated', + type: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + defaultValue: 'false', + }, + ], + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'LIST', + name: null, + ofType: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'OBJECT', + name: '__InputValue', + ofType: null, + }, + }, + }, + }, + isDeprecated: false, + deprecationReason: null, + }, + ], + inputFields: null, + interfaces: [], + enumValues: null, + possibleTypes: null, + }, + { + kind: 'ENUM', + name: '__DirectiveLocation', + specifiedByURL: null, + fields: null, + inputFields: null, + interfaces: null, + enumValues: [ + { + name: 'QUERY', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'MUTATION', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'SUBSCRIPTION', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'FIELD', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'FRAGMENT_DEFINITION', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'FRAGMENT_SPREAD', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'INLINE_FRAGMENT', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'VARIABLE_DEFINITION', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'SCHEMA', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'SCALAR', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'OBJECT', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'FIELD_DEFINITION', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'ARGUMENT_DEFINITION', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'INTERFACE', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'UNION', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'ENUM', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'ENUM_VALUE', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'INPUT_OBJECT', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'INPUT_FIELD_DEFINITION', + isDeprecated: false, + deprecationReason: null, + }, + ], + possibleTypes: null, + }, + ], + directives: [ + { + name: 'include', + isRepeatable: false, + locations: ['FIELD', 'FRAGMENT_SPREAD', 'INLINE_FRAGMENT'], + args: [ + { + defaultValue: null, + name: 'if', + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + }, + }, + ], + }, + { + name: 'skip', + isRepeatable: false, + locations: ['FIELD', 'FRAGMENT_SPREAD', 'INLINE_FRAGMENT'], + args: [ + { + defaultValue: null, + name: 'if', + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'Boolean', + ofType: null, + }, + }, + }, + ], + }, + { + name: 'deprecated', + isRepeatable: false, + locations: ['FIELD_DEFINITION', 'ARGUMENT_DEFINITION', 'INPUT_FIELD_DEFINITION', 'ENUM_VALUE'], + args: [ + { + defaultValue: '"No longer supported"', + name: 'reason', + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + ], + }, + { + name: 'specifiedBy', + isRepeatable: false, + locations: ['SCALAR'], + args: [ + { + defaultValue: null, + name: 'url', + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + }, + ], + }, + ], + }, + }, + }); + }); + + it('introspects on input object', () => { + const schema = buildSchema(` + input SomeInputObject { + a: String = "tes\\t de\\fault" + b: [String] + c: String = null + } + + type Query { + someField(someArg: SomeInputObject): String + } + `); + + const source = ` + { + __type(name: "SomeInputObject") { + kind + name + inputFields { + name + type { ...TypeRef } + defaultValue + } + } + } + + fragment TypeRef on __Type { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + } + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + kind: 'INPUT_OBJECT', + name: 'SomeInputObject', + inputFields: [ + { + name: 'a', + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + defaultValue: '"tes\\t de\\fault"', + }, + { + name: 'b', + type: { + kind: 'LIST', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + defaultValue: null, + }, + { + name: 'c', + type: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + defaultValue: 'null', + }, + ], + }, + }, + }); + }); + + it('introspects any default value', () => { + const schema = buildSchema(` + input InputObjectWithDefaultValues { + a: String = "Emoji: \\u{1F600}" + b: Complex = { x: ["abc"], y: 123 } + } + + input Complex { + x: [String] + y: Int + } + + type Query { + someField(someArg: InputObjectWithDefaultValues): String + } + `); + + const source = ` + { + __type(name: "InputObjectWithDefaultValues") { + inputFields { + name + defaultValue + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + inputFields: [ + { + name: 'a', + defaultValue: '"Emoji: \u{1F600}"', + }, + { + name: 'b', + defaultValue: '{ x: ["abc"], y: 123 }', + }, + ], + }, + }, + }); + }); + + it('supports the __type root field', () => { + const schema = buildSchema(` + type Query { + someField: String + } + `); + + const source = ` + { + __type(name: "Query") { + name + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { name: 'Query' }, + }, + }); + }); + + it('identifies deprecated fields', () => { + const schema = buildSchema(` + type Query { + nonDeprecated: String + deprecated: String @deprecated(reason: "Removed in 1.0") + deprecatedWithEmptyReason: String @deprecated(reason: "") + } + `); + + const source = ` + { + __type(name: "Query") { + fields(includeDeprecated: true) { + name + isDeprecated, + deprecationReason + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + fields: [ + { + name: 'nonDeprecated', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'deprecated', + isDeprecated: true, + deprecationReason: 'Removed in 1.0', + }, + { + name: 'deprecatedWithEmptyReason', + isDeprecated: true, + deprecationReason: '', + }, + ], + }, + }, + }); + }); + + it('respects the includeDeprecated parameter for fields', () => { + const schema = buildSchema(` + type Query { + nonDeprecated: String + deprecated: String @deprecated(reason: "Removed in 1.0") + } + `); + + const source = ` + { + __type(name: "Query") { + trueFields: fields(includeDeprecated: true) { + name + } + falseFields: fields(includeDeprecated: false) { + name + } + omittedFields: fields { + name + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + trueFields: [{ name: 'nonDeprecated' }, { name: 'deprecated' }], + falseFields: [{ name: 'nonDeprecated' }], + omittedFields: [{ name: 'nonDeprecated' }], + }, + }, + }); + }); + + it('identifies deprecated args', () => { + const schema = buildSchema(` + type Query { + someField( + nonDeprecated: String + deprecated: String @deprecated(reason: "Removed in 1.0") + deprecatedWithEmptyReason: String @deprecated(reason: "") + ): String + } + `); + + const source = ` + { + __type(name: "Query") { + fields { + args(includeDeprecated: true) { + name + isDeprecated, + deprecationReason + } + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + fields: [ + { + args: [ + { + name: 'nonDeprecated', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'deprecated', + isDeprecated: true, + deprecationReason: 'Removed in 1.0', + }, + { + name: 'deprecatedWithEmptyReason', + isDeprecated: true, + deprecationReason: '', + }, + ], + }, + ], + }, + }, + }); + }); + + it('respects the includeDeprecated parameter for args', () => { + const schema = buildSchema(` + type Query { + someField( + nonDeprecated: String + deprecated: String @deprecated(reason: "Removed in 1.0") + ): String + } + `); + + const source = ` + { + __type(name: "Query") { + fields { + trueArgs: args(includeDeprecated: true) { + name + } + falseArgs: args(includeDeprecated: false) { + name + } + omittedArgs: args { + name + } + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + fields: [ + { + trueArgs: [{ name: 'nonDeprecated' }, { name: 'deprecated' }], + falseArgs: [{ name: 'nonDeprecated' }], + omittedArgs: [{ name: 'nonDeprecated' }], + }, + ], + }, + }, + }); + }); + + it('identifies deprecated enum values', () => { + const schema = buildSchema(` + enum SomeEnum { + NON_DEPRECATED + DEPRECATED @deprecated(reason: "Removed in 1.0") + ALSO_NON_DEPRECATED + } + + type Query { + someField(someArg: SomeEnum): String + } + `); + + const source = ` + { + __type(name: "SomeEnum") { + enumValues(includeDeprecated: true) { + name + isDeprecated, + deprecationReason + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + enumValues: [ + { + name: 'NON_DEPRECATED', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'DEPRECATED', + isDeprecated: true, + deprecationReason: 'Removed in 1.0', + }, + { + name: 'ALSO_NON_DEPRECATED', + isDeprecated: false, + deprecationReason: null, + }, + ], + }, + }, + }); + }); + + it('respects the includeDeprecated parameter for enum values', () => { + const schema = buildSchema(` + enum SomeEnum { + NON_DEPRECATED + DEPRECATED @deprecated(reason: "Removed in 1.0") + DEPRECATED_WITH_EMPTY_REASON @deprecated(reason: "") + ALSO_NON_DEPRECATED + } + + type Query { + someField(someArg: SomeEnum): String + } + `); + + const source = ` + { + __type(name: "SomeEnum") { + trueValues: enumValues(includeDeprecated: true) { + name + } + falseValues: enumValues(includeDeprecated: false) { + name + } + omittedValues: enumValues { + name + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + trueValues: [ + { name: 'NON_DEPRECATED' }, + { name: 'DEPRECATED' }, + { name: 'DEPRECATED_WITH_EMPTY_REASON' }, + { name: 'ALSO_NON_DEPRECATED' }, + ], + falseValues: [{ name: 'NON_DEPRECATED' }, { name: 'ALSO_NON_DEPRECATED' }], + omittedValues: [{ name: 'NON_DEPRECATED' }, { name: 'ALSO_NON_DEPRECATED' }], + }, + }, + }); + }); + + it('identifies deprecated for input fields', () => { + const schema = buildSchema(` + input SomeInputObject { + nonDeprecated: String + deprecated: String @deprecated(reason: "Removed in 1.0") + deprecatedWithEmptyReason: String @deprecated(reason: "") + } + + type Query { + someField(someArg: SomeInputObject): String + } + `); + + const source = ` + { + __type(name: "SomeInputObject") { + inputFields(includeDeprecated: true) { + name + isDeprecated, + deprecationReason + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + inputFields: [ + { + name: 'nonDeprecated', + isDeprecated: false, + deprecationReason: null, + }, + { + name: 'deprecated', + isDeprecated: true, + deprecationReason: 'Removed in 1.0', + }, + { + name: 'deprecatedWithEmptyReason', + isDeprecated: true, + deprecationReason: '', + }, + ], + }, + }, + }); + }); + + it('respects the includeDeprecated parameter for input fields', () => { + const schema = buildSchema(` + input SomeInputObject { + nonDeprecated: String + deprecated: String @deprecated(reason: "Removed in 1.0") + } + + type Query { + someField(someArg: SomeInputObject): String + } + `); + + const source = ` + { + __type(name: "SomeInputObject") { + trueFields: inputFields(includeDeprecated: true) { + name + } + falseFields: inputFields(includeDeprecated: false) { + name + } + omittedFields: inputFields { + name + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + __type: { + trueFields: [{ name: 'nonDeprecated' }, { name: 'deprecated' }], + falseFields: [{ name: 'nonDeprecated' }], + omittedFields: [{ name: 'nonDeprecated' }], + }, + }, + }); + }); + + it('fails as expected on the __type root field without an arg', () => { + const schema = buildSchema(` + type Query { + someField: String + } + `); + + const source = ` + { + __type { + name + } + } + `; + + expectJSON(graphqlSync({ schema, source })).toDeepEqual({ + errors: [ + { + message: 'Field "__type" argument "name" of type "String!" is required, but it was not provided.', + locations: [{ line: 3, column: 9 }], + }, + ], + }); + }); + + it('exposes descriptions', () => { + const schema = buildSchema(` + """Enum description""" + enum SomeEnum { + """Value description""" + VALUE + } + + """Object description""" + type SomeObject { + """Field description""" + someField(arg: SomeEnum): String + } + + """Schema description""" + schema { + query: SomeObject + } + `); + + const source = ` + { + Schema: __schema { description } + SomeObject: __type(name: "SomeObject") { + description, + fields { + name + description + } + } + SomeEnum: __type(name: "SomeEnum") { + description + enumValues { + name + description + } + } + } + `; + + expect(graphqlSync({ schema, source })).toEqual({ + data: { + Schema: { + description: 'Schema description', + }, + SomeEnum: { + description: 'Enum description', + enumValues: [ + { + name: 'VALUE', + description: 'Value description', + }, + ], + }, + SomeObject: { + description: 'Object description', + fields: [ + { + name: 'someField', + description: 'Field description', + }, + ], + }, + }, + }); + }); + + it('executes an introspection query without calling global resolvers', () => { + const schema = buildSchema(` + type Query { + someField: String + } + `); + + const source = getIntrospectionQuery({ + specifiedByUrl: true, + directiveIsRepeatable: true, + schemaDescription: true, + }); + + /* c8 ignore start */ + // @ts-expect-error + function fieldResolver(_1: any, _2: any, _3: any, info: GraphQLResolveInfo): never {} + + // @ts-expect-error + function typeResolver(_1: any, _2: any, info: GraphQLResolveInfo): never {} + /* c8 ignore stop */ + + const result = graphqlSync({ + schema, + source, + fieldResolver, + typeResolver, + }); + expect(result).not.toHaveProperty('errors'); + }); +}); diff --git a/packages/graphql/src/type/__tests__/predicate-test.ts b/packages/graphql/src/type/__tests__/predicate-test.ts new file mode 100644 index 00000000000..3ff1559597d --- /dev/null +++ b/packages/graphql/src/type/__tests__/predicate-test.ts @@ -0,0 +1,682 @@ +import { DirectiveLocation } from '../../language/directiveLocation.js'; + +import type { GraphQLArgument, GraphQLInputField, GraphQLInputType } from '../definition.js'; +import { + assertAbstractType, + assertCompositeType, + assertEnumType, + assertInputObjectType, + assertInputType, + assertInterfaceType, + assertLeafType, + assertListType, + assertNamedType, + assertNonNullType, + assertNullableType, + assertObjectType, + assertOutputType, + assertScalarType, + assertType, + assertUnionType, + assertWrappingType, + getNamedType, + getNullableType, + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, + isAbstractType, + isCompositeType, + isEnumType, + isInputObjectType, + isInputType, + isInterfaceType, + isLeafType, + isListType, + isNamedType, + isNonNullType, + isNullableType, + isObjectType, + isOutputType, + isRequiredArgument, + isRequiredInputField, + isScalarType, + isType, + isUnionType, + isWrappingType, +} from '../definition.js'; +import { + assertDirective, + GraphQLDeprecatedDirective, + GraphQLDirective, + GraphQLIncludeDirective, + GraphQLSkipDirective, + isDirective, + isSpecifiedDirective, +} from '../directives.js'; +import { + GraphQLBoolean, + GraphQLFloat, + GraphQLID, + GraphQLInt, + GraphQLString, + isSpecifiedScalarType, +} from '../scalars.js'; +import { assertSchema, GraphQLSchema, isSchema } from '../schema.js'; + +const ObjectType = new GraphQLObjectType({ name: 'Object', fields: {} }); +const InterfaceType = new GraphQLInterfaceType({ + name: 'Interface', + fields: {}, +}); +const UnionType = new GraphQLUnionType({ name: 'Union', types: [ObjectType] }); +const EnumType = new GraphQLEnumType({ name: 'Enum', values: { foo: {} } }); +const InputObjectType = new GraphQLInputObjectType({ + name: 'InputObject', + fields: {}, +}); +const ScalarType = new GraphQLScalarType({ name: 'Scalar' }); +const Directive = new GraphQLDirective({ + name: 'Directive', + locations: [DirectiveLocation.QUERY], +}); + +describe('Type predicates', () => { + describe('isType', () => { + it('returns true for unwrapped types', () => { + expect(isType(GraphQLString)).toEqual(true); + expect(() => assertType(GraphQLString)).not.toThrow(); + expect(isType(ObjectType)).toEqual(true); + expect(() => assertType(ObjectType)).not.toThrow(); + }); + + it('returns true for wrapped types', () => { + expect(isType(new GraphQLNonNull(GraphQLString))).toEqual(true); + expect(() => assertType(new GraphQLNonNull(GraphQLString))).not.toThrow(); + }); + + it('returns false for type classes (rather than instances)', () => { + expect(isType(GraphQLObjectType)).toEqual(false); + expect(() => assertType(GraphQLObjectType)).toThrow(); + }); + + it('returns false for random garbage', () => { + expect(isType({ what: 'is this' })).toEqual(false); + expect(() => assertType({ what: 'is this' })).toThrow(); + }); + }); + + describe('isScalarType', () => { + it('returns true for spec defined scalar', () => { + expect(isScalarType(GraphQLString)).toEqual(true); + expect(() => assertScalarType(GraphQLString)).not.toThrow(); + }); + + it('returns true for custom scalar', () => { + expect(isScalarType(ScalarType)).toEqual(true); + expect(() => assertScalarType(ScalarType)).not.toThrow(); + }); + + it('returns false for scalar class (rather than instance)', () => { + expect(isScalarType(GraphQLScalarType)).toEqual(false); + expect(() => assertScalarType(GraphQLScalarType)).toThrow(); + }); + + it('returns false for wrapped scalar', () => { + expect(isScalarType(new GraphQLList(ScalarType))).toEqual(false); + expect(() => assertScalarType(new GraphQLList(ScalarType))).toThrow(); + }); + + it('returns false for non-scalar', () => { + expect(isScalarType(EnumType)).toEqual(false); + expect(() => assertScalarType(EnumType)).toThrow(); + expect(isScalarType(Directive)).toEqual(false); + expect(() => assertScalarType(Directive)).toThrow(); + }); + + it('returns false for random garbage', () => { + expect(isScalarType({ what: 'is this' })).toEqual(false); + expect(() => assertScalarType({ what: 'is this' })).toThrow(); + }); + }); + + describe('isSpecifiedScalarType', () => { + it('returns true for specified scalars', () => { + expect(isSpecifiedScalarType(GraphQLString)).toEqual(true); + expect(isSpecifiedScalarType(GraphQLInt)).toEqual(true); + expect(isSpecifiedScalarType(GraphQLFloat)).toEqual(true); + expect(isSpecifiedScalarType(GraphQLBoolean)).toEqual(true); + expect(isSpecifiedScalarType(GraphQLID)).toEqual(true); + }); + + it('returns false for custom scalar', () => { + expect(isSpecifiedScalarType(ScalarType)).toEqual(false); + }); + }); + + describe('isObjectType', () => { + it('returns true for object type', () => { + expect(isObjectType(ObjectType)).toEqual(true); + expect(() => assertObjectType(ObjectType)).not.toThrow(); + }); + + it('returns false for wrapped object type', () => { + expect(isObjectType(new GraphQLList(ObjectType))).toEqual(false); + expect(() => assertObjectType(new GraphQLList(ObjectType))).toThrow(); + }); + + it('returns false for non-object type', () => { + expect(isObjectType(InterfaceType)).toEqual(false); + expect(() => assertObjectType(InterfaceType)).toThrow(); + }); + }); + + describe('isInterfaceType', () => { + it('returns true for interface type', () => { + expect(isInterfaceType(InterfaceType)).toEqual(true); + expect(() => assertInterfaceType(InterfaceType)).not.toThrow(); + }); + + it('returns false for wrapped interface type', () => { + expect(isInterfaceType(new GraphQLList(InterfaceType))).toEqual(false); + expect(() => assertInterfaceType(new GraphQLList(InterfaceType))).toThrow(); + }); + + it('returns false for non-interface type', () => { + expect(isInterfaceType(ObjectType)).toEqual(false); + expect(() => assertInterfaceType(ObjectType)).toThrow(); + }); + }); + + describe('isUnionType', () => { + it('returns true for union type', () => { + expect(isUnionType(UnionType)).toEqual(true); + expect(() => assertUnionType(UnionType)).not.toThrow(); + }); + + it('returns false for wrapped union type', () => { + expect(isUnionType(new GraphQLList(UnionType))).toEqual(false); + expect(() => assertUnionType(new GraphQLList(UnionType))).toThrow(); + }); + + it('returns false for non-union type', () => { + expect(isUnionType(ObjectType)).toEqual(false); + expect(() => assertUnionType(ObjectType)).toThrow(); + }); + }); + + describe('isEnumType', () => { + it('returns true for enum type', () => { + expect(isEnumType(EnumType)).toEqual(true); + expect(() => assertEnumType(EnumType)).not.toThrow(); + }); + + it('returns false for wrapped enum type', () => { + expect(isEnumType(new GraphQLList(EnumType))).toEqual(false); + expect(() => assertEnumType(new GraphQLList(EnumType))).toThrow(); + }); + + it('returns false for non-enum type', () => { + expect(isEnumType(ScalarType)).toEqual(false); + expect(() => assertEnumType(ScalarType)).toThrow(); + }); + }); + + describe('isInputObjectType', () => { + it('returns true for input object type', () => { + expect(isInputObjectType(InputObjectType)).toEqual(true); + expect(() => assertInputObjectType(InputObjectType)).not.toThrow(); + }); + + it('returns false for wrapped input object type', () => { + expect(isInputObjectType(new GraphQLList(InputObjectType))).toEqual(false); + expect(() => assertInputObjectType(new GraphQLList(InputObjectType))).toThrow(); + }); + + it('returns false for non-input-object type', () => { + expect(isInputObjectType(ObjectType)).toEqual(false); + expect(() => assertInputObjectType(ObjectType)).toThrow(); + }); + }); + + describe('isListType', () => { + it('returns true for a list wrapped type', () => { + expect(isListType(new GraphQLList(ObjectType))).toEqual(true); + expect(() => assertListType(new GraphQLList(ObjectType))).not.toThrow(); + }); + + it('returns false for an unwrapped type', () => { + expect(isListType(ObjectType)).toEqual(false); + expect(() => assertListType(ObjectType)).toThrow(); + }); + + it('returns false for a non-list wrapped type', () => { + expect(isListType(new GraphQLNonNull(new GraphQLList(ObjectType)))).toEqual(false); + expect(() => assertListType(new GraphQLNonNull(new GraphQLList(ObjectType)))).toThrow(); + }); + }); + + describe('isNonNullType', () => { + it('returns true for a non-null wrapped type', () => { + expect(isNonNullType(new GraphQLNonNull(ObjectType))).toEqual(true); + expect(() => assertNonNullType(new GraphQLNonNull(ObjectType))).not.toThrow(); + }); + + it('returns false for an unwrapped type', () => { + expect(isNonNullType(ObjectType)).toEqual(false); + expect(() => assertNonNullType(ObjectType)).toThrow(); + }); + + it('returns false for a not non-null wrapped type', () => { + expect(isNonNullType(new GraphQLList(new GraphQLNonNull(ObjectType)))).toEqual(false); + expect(() => assertNonNullType(new GraphQLList(new GraphQLNonNull(ObjectType)))).toThrow(); + }); + }); + + describe('isInputType', () => { + function expectInputType(type: unknown) { + expect(isInputType(type)).toEqual(true); + expect(() => assertInputType(type)).not.toThrow(); + } + + it('returns true for an input type', () => { + expectInputType(GraphQLString); + expectInputType(EnumType); + expectInputType(InputObjectType); + }); + + it('returns true for a wrapped input type', () => { + expectInputType(new GraphQLList(GraphQLString)); + expectInputType(new GraphQLList(EnumType)); + expectInputType(new GraphQLList(InputObjectType)); + + expectInputType(new GraphQLNonNull(GraphQLString)); + expectInputType(new GraphQLNonNull(EnumType)); + expectInputType(new GraphQLNonNull(InputObjectType)); + }); + + function expectNonInputType(type: unknown) { + expect(isInputType(type)).toEqual(false); + expect(() => assertInputType(type)).toThrow(); + } + + it('returns false for an output type', () => { + expectNonInputType(ObjectType); + expectNonInputType(InterfaceType); + expectNonInputType(UnionType); + }); + + it('returns false for a wrapped output type', () => { + expectNonInputType(new GraphQLList(ObjectType)); + expectNonInputType(new GraphQLList(InterfaceType)); + expectNonInputType(new GraphQLList(UnionType)); + + expectNonInputType(new GraphQLNonNull(ObjectType)); + expectNonInputType(new GraphQLNonNull(InterfaceType)); + expectNonInputType(new GraphQLNonNull(UnionType)); + }); + }); + + describe('isOutputType', () => { + function expectOutputType(type: unknown) { + expect(isOutputType(type)).toEqual(true); + expect(() => assertOutputType(type)).not.toThrow(); + } + + it('returns true for an output type', () => { + expectOutputType(GraphQLString); + expectOutputType(ObjectType); + expectOutputType(InterfaceType); + expectOutputType(UnionType); + expectOutputType(EnumType); + }); + + it('returns true for a wrapped output type', () => { + expectOutputType(new GraphQLList(GraphQLString)); + expectOutputType(new GraphQLList(ObjectType)); + expectOutputType(new GraphQLList(InterfaceType)); + expectOutputType(new GraphQLList(UnionType)); + expectOutputType(new GraphQLList(EnumType)); + + expectOutputType(new GraphQLNonNull(GraphQLString)); + expectOutputType(new GraphQLNonNull(ObjectType)); + expectOutputType(new GraphQLNonNull(InterfaceType)); + expectOutputType(new GraphQLNonNull(UnionType)); + expectOutputType(new GraphQLNonNull(EnumType)); + }); + + function expectNonOutputType(type: unknown) { + expect(isOutputType(type)).toEqual(false); + expect(() => assertOutputType(type)).toThrow(); + } + + it('returns false for an input type', () => { + expectNonOutputType(InputObjectType); + }); + + it('returns false for a wrapped input type', () => { + expectNonOutputType(new GraphQLList(InputObjectType)); + expectNonOutputType(new GraphQLNonNull(InputObjectType)); + }); + }); + + describe('isLeafType', () => { + it('returns true for scalar and enum types', () => { + expect(isLeafType(ScalarType)).toEqual(true); + expect(() => assertLeafType(ScalarType)).not.toThrow(); + expect(isLeafType(EnumType)).toEqual(true); + expect(() => assertLeafType(EnumType)).not.toThrow(); + }); + + it('returns false for wrapped leaf type', () => { + expect(isLeafType(new GraphQLList(ScalarType))).toEqual(false); + expect(() => assertLeafType(new GraphQLList(ScalarType))).toThrow(); + }); + + it('returns false for non-leaf type', () => { + expect(isLeafType(ObjectType)).toEqual(false); + expect(() => assertLeafType(ObjectType)).toThrow(); + }); + + it('returns false for wrapped non-leaf type', () => { + expect(isLeafType(new GraphQLList(ObjectType))).toEqual(false); + expect(() => assertLeafType(new GraphQLList(ObjectType))).toThrow(); + }); + }); + + describe('isCompositeType', () => { + it('returns true for object, interface, and union types', () => { + expect(isCompositeType(ObjectType)).toEqual(true); + expect(() => assertCompositeType(ObjectType)).not.toThrow(); + expect(isCompositeType(InterfaceType)).toEqual(true); + expect(() => assertCompositeType(InterfaceType)).not.toThrow(); + expect(isCompositeType(UnionType)).toEqual(true); + expect(() => assertCompositeType(UnionType)).not.toThrow(); + }); + + it('returns false for wrapped composite type', () => { + expect(isCompositeType(new GraphQLList(ObjectType))).toEqual(false); + expect(() => assertCompositeType(new GraphQLList(ObjectType))).toThrow(); + }); + + it('returns false for non-composite type', () => { + expect(isCompositeType(InputObjectType)).toEqual(false); + expect(() => assertCompositeType(InputObjectType)).toThrow(); + }); + + it('returns false for wrapped non-composite type', () => { + expect(isCompositeType(new GraphQLList(InputObjectType))).toEqual(false); + expect(() => assertCompositeType(new GraphQLList(InputObjectType))).toThrow(); + }); + }); + + describe('isAbstractType', () => { + it('returns true for interface and union types', () => { + expect(isAbstractType(InterfaceType)).toEqual(true); + expect(() => assertAbstractType(InterfaceType)).not.toThrow(); + expect(isAbstractType(UnionType)).toEqual(true); + expect(() => assertAbstractType(UnionType)).not.toThrow(); + }); + + it('returns false for wrapped abstract type', () => { + expect(isAbstractType(new GraphQLList(InterfaceType))).toEqual(false); + expect(() => assertAbstractType(new GraphQLList(InterfaceType))).toThrow(); + }); + + it('returns false for non-abstract type', () => { + expect(isAbstractType(ObjectType)).toEqual(false); + expect(() => assertAbstractType(ObjectType)).toThrow(); + }); + + it('returns false for wrapped non-abstract type', () => { + expect(isAbstractType(new GraphQLList(ObjectType))).toEqual(false); + expect(() => assertAbstractType(new GraphQLList(ObjectType))).toThrow(); + }); + }); + + describe('isWrappingType', () => { + it('returns true for list and non-null types', () => { + expect(isWrappingType(new GraphQLList(ObjectType))).toEqual(true); + expect(() => assertWrappingType(new GraphQLList(ObjectType))).not.toThrow(); + expect(isWrappingType(new GraphQLNonNull(ObjectType))).toEqual(true); + expect(() => assertWrappingType(new GraphQLNonNull(ObjectType))).not.toThrow(); + }); + + it('returns false for unwrapped types', () => { + expect(isWrappingType(ObjectType)).toEqual(false); + expect(() => assertWrappingType(ObjectType)).toThrow(); + }); + }); + + describe('isNullableType', () => { + it('returns true for unwrapped types', () => { + expect(isNullableType(ObjectType)).toEqual(true); + expect(() => assertNullableType(ObjectType)).not.toThrow(); + }); + + it('returns true for list of non-null types', () => { + expect(isNullableType(new GraphQLList(new GraphQLNonNull(ObjectType)))).toEqual(true); + expect(() => assertNullableType(new GraphQLList(new GraphQLNonNull(ObjectType)))).not.toThrow(); + }); + + it('returns false for non-null types', () => { + expect(isNullableType(new GraphQLNonNull(ObjectType))).toEqual(false); + expect(() => assertNullableType(new GraphQLNonNull(ObjectType))).toThrow(); + }); + }); + + describe('getNullableType', () => { + it('returns undefined for no type', () => { + expect(getNullableType(undefined)).toEqual(undefined); + expect(getNullableType(null)).toEqual(undefined); + }); + + it('returns self for a nullable type', () => { + expect(getNullableType(ObjectType)).toEqual(ObjectType); + const listOfObj = new GraphQLList(ObjectType); + expect(getNullableType(listOfObj)).toEqual(listOfObj); + }); + + it('unwraps non-null type', () => { + expect(getNullableType(new GraphQLNonNull(ObjectType))).toEqual(ObjectType); + }); + }); + + describe('isNamedType', () => { + it('returns true for unwrapped types', () => { + expect(isNamedType(ObjectType)).toEqual(true); + expect(() => assertNamedType(ObjectType)).not.toThrow(); + }); + + it('returns false for list and non-null types', () => { + expect(isNamedType(new GraphQLList(ObjectType))).toEqual(false); + expect(() => assertNamedType(new GraphQLList(ObjectType))).toThrow(); + expect(isNamedType(new GraphQLNonNull(ObjectType))).toEqual(false); + expect(() => assertNamedType(new GraphQLNonNull(ObjectType))).toThrow(); + }); + }); + + describe('getNamedType', () => { + it('returns undefined for no type', () => { + expect(getNamedType(undefined)).toEqual(undefined); + expect(getNamedType(null)).toEqual(undefined); + }); + + it('returns self for a unwrapped type', () => { + expect(getNamedType(ObjectType)).toEqual(ObjectType); + }); + + it('unwraps wrapper types', () => { + expect(getNamedType(new GraphQLNonNull(ObjectType))).toEqual(ObjectType); + expect(getNamedType(new GraphQLList(ObjectType))).toEqual(ObjectType); + }); + + it('unwraps deeply wrapper types', () => { + expect(getNamedType(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(ObjectType))))).toEqual(ObjectType); + }); + }); + + describe('isRequiredArgument', () => { + function buildArg(config: { type: GraphQLInputType; defaultValue?: unknown }): GraphQLArgument { + return { + name: 'someArg', + type: config.type, + description: undefined, + defaultValue: config.defaultValue, + deprecationReason: null, + extensions: Object.create(null), + astNode: undefined, + }; + } + + it('returns true for required arguments', () => { + const requiredArg = buildArg({ + type: new GraphQLNonNull(GraphQLString), + }); + expect(isRequiredArgument(requiredArg)).toEqual(true); + }); + + it('returns false for optional arguments', () => { + const optArg1 = buildArg({ + type: GraphQLString, + }); + expect(isRequiredArgument(optArg1)).toEqual(false); + + const optArg2 = buildArg({ + type: GraphQLString, + defaultValue: null, + }); + expect(isRequiredArgument(optArg2)).toEqual(false); + + const optArg3 = buildArg({ + type: new GraphQLList(new GraphQLNonNull(GraphQLString)), + }); + expect(isRequiredArgument(optArg3)).toEqual(false); + + const optArg4 = buildArg({ + type: new GraphQLNonNull(GraphQLString), + defaultValue: 'default', + }); + expect(isRequiredArgument(optArg4)).toEqual(false); + }); + }); + + describe('isRequiredInputField', () => { + function buildInputField(config: { type: GraphQLInputType; defaultValue?: unknown }): GraphQLInputField { + return { + name: 'someInputField', + type: config.type, + description: undefined, + defaultValue: config.defaultValue, + deprecationReason: null, + extensions: Object.create(null), + astNode: undefined, + }; + } + + it('returns true for required input field', () => { + const requiredField = buildInputField({ + type: new GraphQLNonNull(GraphQLString), + }); + expect(isRequiredInputField(requiredField)).toEqual(true); + }); + + it('returns false for optional input field', () => { + const optField1 = buildInputField({ + type: GraphQLString, + }); + expect(isRequiredInputField(optField1)).toEqual(false); + + const optField2 = buildInputField({ + type: GraphQLString, + defaultValue: null, + }); + expect(isRequiredInputField(optField2)).toEqual(false); + + const optField3 = buildInputField({ + type: new GraphQLList(new GraphQLNonNull(GraphQLString)), + }); + expect(isRequiredInputField(optField3)).toEqual(false); + + const optField4 = buildInputField({ + type: new GraphQLNonNull(GraphQLString), + defaultValue: 'default', + }); + expect(isRequiredInputField(optField4)).toEqual(false); + }); + }); +}); + +describe('Directive predicates', () => { + describe('isDirective', () => { + it('returns true for spec defined directive', () => { + expect(isDirective(GraphQLSkipDirective)).toEqual(true); + expect(() => assertDirective(GraphQLSkipDirective)).not.toThrow(); + }); + + it('returns true for custom directive', () => { + expect(isDirective(Directive)).toEqual(true); + expect(() => assertDirective(Directive)).not.toThrow(); + }); + + it('returns false for directive class (rather than instance)', () => { + expect(isDirective(GraphQLDirective)).toEqual(false); + expect(() => assertDirective(GraphQLDirective)).toThrow(); + }); + + it('returns false for non-directive', () => { + expect(isDirective(EnumType)).toEqual(false); + expect(() => assertDirective(EnumType)).toThrow(); + expect(isDirective(ScalarType)).toEqual(false); + expect(() => assertDirective(ScalarType)).toThrow(); + }); + + it('returns false for random garbage', () => { + expect(isDirective({ what: 'is this' })).toEqual(false); + expect(() => assertDirective({ what: 'is this' })).toThrow(); + }); + }); + describe('isSpecifiedDirective', () => { + it('returns true for specified directives', () => { + expect(isSpecifiedDirective(GraphQLIncludeDirective)).toEqual(true); + expect(isSpecifiedDirective(GraphQLSkipDirective)).toEqual(true); + expect(isSpecifiedDirective(GraphQLDeprecatedDirective)).toEqual(true); + }); + + it('returns false for custom directive', () => { + expect(isSpecifiedDirective(Directive)).toEqual(false); + }); + }); +}); + +describe('Schema predicates', () => { + const schema = new GraphQLSchema({}); + + describe('isSchema/assertSchema', () => { + it('returns true for schema', () => { + expect(isSchema(schema)).toEqual(true); + expect(() => assertSchema(schema)).not.toThrow(); + }); + + it('returns false for schema class (rather than instance)', () => { + expect(isSchema(GraphQLSchema)).toEqual(false); + expect(() => assertSchema(GraphQLSchema)).toThrow(); + }); + + it('returns false for non-schema', () => { + expect(isSchema(EnumType)).toEqual(false); + expect(() => assertSchema(EnumType)).toThrow(); + expect(isSchema(ScalarType)).toEqual(false); + expect(() => assertSchema(ScalarType)).toThrow(); + }); + + it('returns false for random garbage', () => { + expect(isSchema({ what: 'is this' })).toEqual(false); + expect(() => assertSchema({ what: 'is this' })).toThrow(); + }); + }); +}); diff --git a/packages/graphql/src/type/__tests__/scalars-test.ts b/packages/graphql/src/type/__tests__/scalars-test.ts new file mode 100644 index 00000000000..d070e3d630b --- /dev/null +++ b/packages/graphql/src/type/__tests__/scalars-test.ts @@ -0,0 +1,427 @@ +import { parseValue as parseValueToAST } from '../../language/parser.js'; + +import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, GraphQLString } from '../scalars.js'; + +describe('Type System: Specified scalar types', () => { + describe('GraphQLInt', () => { + it('parseValue', () => { + function parseValue(value: unknown) { + return GraphQLInt.parseValue(value); + } + + expect(parseValue(1)).toEqual(1); + expect(parseValue(0)).toEqual(0); + expect(parseValue(-1)).toEqual(-1); + + expect(() => parseValue(9876504321)).toThrow('Int cannot represent non 32-bit signed integer value: 9876504321'); + expect(() => parseValue(-9876504321)).toThrow( + 'Int cannot represent non 32-bit signed integer value: -9876504321' + ); + expect(() => parseValue(0.1)).toThrow('Int cannot represent non-integer value: 0.1'); + expect(() => parseValue(NaN)).toThrow('Int cannot represent non-integer value: NaN'); + expect(() => parseValue(Infinity)).toThrow('Int cannot represent non-integer value: Infinity'); + + expect(() => parseValue(undefined)).toThrow('Int cannot represent non-integer value: undefined'); + expect(() => parseValue(null)).toThrow('Int cannot represent non-integer value: null'); + expect(() => parseValue('')).toThrow('Int cannot represent non-integer value: ""'); + expect(() => parseValue('123')).toThrow('Int cannot represent non-integer value: "123"'); + expect(() => parseValue(false)).toThrow('Int cannot represent non-integer value: false'); + expect(() => parseValue(true)).toThrow('Int cannot represent non-integer value: true'); + expect(() => parseValue([1])).toThrow('Int cannot represent non-integer value: [1]'); + expect(() => parseValue({ value: 1 })).toThrow('Int cannot represent non-integer value: { value: 1 }'); + }); + + it('parseLiteral', () => { + function parseLiteral(str: string) { + return GraphQLInt.parseLiteral(parseValueToAST(str), undefined); + } + + expect(parseLiteral('1')).toEqual(1); + expect(parseLiteral('0')).toEqual(0); + expect(parseLiteral('-1')).toEqual(-1); + + expect(() => parseLiteral('9876504321')).toThrow( + 'Int cannot represent non 32-bit signed integer value: 9876504321' + ); + expect(() => parseLiteral('-9876504321')).toThrow( + 'Int cannot represent non 32-bit signed integer value: -9876504321' + ); + + expect(() => parseLiteral('1.0')).toThrow('Int cannot represent non-integer value: 1.0'); + expect(() => parseLiteral('null')).toThrow('Int cannot represent non-integer value: null'); + expect(() => parseLiteral('""')).toThrow('Int cannot represent non-integer value: ""'); + expect(() => parseLiteral('"123"')).toThrow('Int cannot represent non-integer value: "123"'); + expect(() => parseLiteral('false')).toThrow('Int cannot represent non-integer value: false'); + expect(() => parseLiteral('[1]')).toThrow('Int cannot represent non-integer value: [1]'); + expect(() => parseLiteral('{ value: 1 }')).toThrow('Int cannot represent non-integer value: { value: 1 }'); + expect(() => parseLiteral('ENUM_VALUE')).toThrow('Int cannot represent non-integer value: ENUM_VALUE'); + expect(() => parseLiteral('$var')).toThrow('Int cannot represent non-integer value: $var'); + }); + + it('serialize', () => { + function serialize(value: unknown) { + return GraphQLInt.serialize(value); + } + + expect(serialize(1)).toEqual(1); + expect(serialize('123')).toEqual(123); + expect(serialize(0)).toEqual(0); + expect(serialize(-1)).toEqual(-1); + expect(serialize(1e5)).toEqual(100000); + expect(serialize(false)).toEqual(0); + expect(serialize(true)).toEqual(1); + + const customValueOfObj = { + value: 5, + valueOf() { + return this.value; + }, + }; + expect(serialize(customValueOfObj)).toEqual(5); + + // The GraphQL specification does not allow serializing non-integer values + // as Int to avoid accidental data loss. + expect(() => serialize(0.1)).toThrow('Int cannot represent non-integer value: 0.1'); + expect(() => serialize(1.1)).toThrow('Int cannot represent non-integer value: 1.1'); + expect(() => serialize(-1.1)).toThrow('Int cannot represent non-integer value: -1.1'); + expect(() => serialize('-1.1')).toThrow('Int cannot represent non-integer value: "-1.1"'); + + // Maybe a safe JavaScript int, but bigger than 2^32, so not + // representable as a GraphQL Int + expect(() => serialize(9876504321)).toThrow('Int cannot represent non 32-bit signed integer value: 9876504321'); + expect(() => serialize(-9876504321)).toThrow('Int cannot represent non 32-bit signed integer value: -9876504321'); + + // Too big to represent as an Int in JavaScript or GraphQL + expect(() => serialize(1e100)).toThrow('Int cannot represent non 32-bit signed integer value: 1e+100'); + expect(() => serialize(-1e100)).toThrow('Int cannot represent non 32-bit signed integer value: -1e+100'); + expect(() => serialize('one')).toThrow('Int cannot represent non-integer value: "one"'); + + // Doesn't represent number + expect(() => serialize('')).toThrow('Int cannot represent non-integer value: ""'); + expect(() => serialize(NaN)).toThrow('Int cannot represent non-integer value: NaN'); + expect(() => serialize(Infinity)).toThrow('Int cannot represent non-integer value: Infinity'); + expect(() => serialize([5])).toThrow('Int cannot represent non-integer value: [5]'); + }); + }); + + describe('GraphQLFloat', () => { + it('parseValue', () => { + function parseValue(value: unknown) { + return GraphQLFloat.parseValue(value); + } + + expect(parseValue(1)).toEqual(1); + expect(parseValue(0)).toEqual(0); + expect(parseValue(-1)).toEqual(-1); + expect(parseValue(0.1)).toEqual(0.1); + expect(parseValue(Math.PI)).toEqual(Math.PI); + + expect(() => parseValue(NaN)).toThrow('Float cannot represent non numeric value: NaN'); + expect(() => parseValue(Infinity)).toThrow('Float cannot represent non numeric value: Infinity'); + + expect(() => parseValue(undefined)).toThrow('Float cannot represent non numeric value: undefined'); + expect(() => parseValue(null)).toThrow('Float cannot represent non numeric value: null'); + expect(() => parseValue('')).toThrow('Float cannot represent non numeric value: ""'); + expect(() => parseValue('123')).toThrow('Float cannot represent non numeric value: "123"'); + expect(() => parseValue('123.5')).toThrow('Float cannot represent non numeric value: "123.5"'); + expect(() => parseValue(false)).toThrow('Float cannot represent non numeric value: false'); + expect(() => parseValue(true)).toThrow('Float cannot represent non numeric value: true'); + expect(() => parseValue([0.1])).toThrow('Float cannot represent non numeric value: [0.1]'); + expect(() => parseValue({ value: 0.1 })).toThrow('Float cannot represent non numeric value: { value: 0.1 }'); + }); + + it('parseLiteral', () => { + function parseLiteral(str: string) { + return GraphQLFloat.parseLiteral(parseValueToAST(str), undefined); + } + + expect(parseLiteral('1')).toEqual(1); + expect(parseLiteral('0')).toEqual(0); + expect(parseLiteral('-1')).toEqual(-1); + expect(parseLiteral('0.1')).toEqual(0.1); + expect(parseLiteral(Math.PI.toString())).toEqual(Math.PI); + + expect(() => parseLiteral('null')).toThrow('Float cannot represent non numeric value: null'); + expect(() => parseLiteral('""')).toThrow('Float cannot represent non numeric value: ""'); + expect(() => parseLiteral('"123"')).toThrow('Float cannot represent non numeric value: "123"'); + expect(() => parseLiteral('"123.5"')).toThrow('Float cannot represent non numeric value: "123.5"'); + expect(() => parseLiteral('false')).toThrow('Float cannot represent non numeric value: false'); + expect(() => parseLiteral('[0.1]')).toThrow('Float cannot represent non numeric value: [0.1]'); + expect(() => parseLiteral('{ value: 0.1 }')).toThrow('Float cannot represent non numeric value: { value: 0.1 }'); + expect(() => parseLiteral('ENUM_VALUE')).toThrow('Float cannot represent non numeric value: ENUM_VALUE'); + expect(() => parseLiteral('$var')).toThrow('Float cannot represent non numeric value: $var'); + }); + + it('serialize', () => { + function serialize(value: unknown) { + return GraphQLFloat.serialize(value); + } + + expect(serialize(1)).toEqual(1.0); + expect(serialize(0)).toEqual(0.0); + expect(serialize('123.5')).toEqual(123.5); + expect(serialize(-1)).toEqual(-1.0); + expect(serialize(0.1)).toEqual(0.1); + expect(serialize(1.1)).toEqual(1.1); + expect(serialize(-1.1)).toEqual(-1.1); + expect(serialize('-1.1')).toEqual(-1.1); + expect(serialize(false)).toEqual(0.0); + expect(serialize(true)).toEqual(1.0); + + const customValueOfObj = { + value: 5.5, + valueOf() { + return this.value; + }, + }; + expect(serialize(customValueOfObj)).toEqual(5.5); + + expect(() => serialize(NaN)).toThrow('Float cannot represent non numeric value: NaN'); + expect(() => serialize(Infinity)).toThrow('Float cannot represent non numeric value: Infinity'); + expect(() => serialize('one')).toThrow('Float cannot represent non numeric value: "one"'); + expect(() => serialize('')).toThrow('Float cannot represent non numeric value: ""'); + expect(() => serialize([5])).toThrow('Float cannot represent non numeric value: [5]'); + }); + }); + + describe('GraphQLString', () => { + it('parseValue', () => { + function parseValue(value: unknown) { + return GraphQLString.parseValue(value); + } + + expect(parseValue('foo')).toEqual('foo'); + + expect(() => parseValue(undefined)).toThrow('String cannot represent a non string value: undefined'); + expect(() => parseValue(null)).toThrow('String cannot represent a non string value: null'); + expect(() => parseValue(1)).toThrow('String cannot represent a non string value: 1'); + expect(() => parseValue(NaN)).toThrow('String cannot represent a non string value: NaN'); + expect(() => parseValue(false)).toThrow('String cannot represent a non string value: false'); + expect(() => parseValue(['foo'])).toThrow('String cannot represent a non string value: ["foo"]'); + expect(() => parseValue({ value: 'foo' })).toThrow( + 'String cannot represent a non string value: { value: "foo" }' + ); + }); + + it('parseLiteral', () => { + function parseLiteral(str: string) { + return GraphQLString.parseLiteral(parseValueToAST(str), undefined); + } + + expect(parseLiteral('"foo"')).toEqual('foo'); + expect(parseLiteral('"""bar"""')).toEqual('bar'); + + expect(() => parseLiteral('null')).toThrow('String cannot represent a non string value: null'); + expect(() => parseLiteral('1')).toThrow('String cannot represent a non string value: 1'); + expect(() => parseLiteral('0.1')).toThrow('String cannot represent a non string value: 0.1'); + expect(() => parseLiteral('false')).toThrow('String cannot represent a non string value: false'); + expect(() => parseLiteral('["foo"]')).toThrow('String cannot represent a non string value: ["foo"]'); + expect(() => parseLiteral('{ value: "foo" }')).toThrow( + 'String cannot represent a non string value: { value: "foo" }' + ); + expect(() => parseLiteral('ENUM_VALUE')).toThrow('String cannot represent a non string value: ENUM_VALUE'); + expect(() => parseLiteral('$var')).toThrow('String cannot represent a non string value: $var'); + }); + + it('serialize', () => { + function serialize(value: unknown) { + return GraphQLString.serialize(value); + } + + expect(serialize('string')).toEqual('string'); + expect(serialize(1)).toEqual('1'); + expect(serialize(-1.1)).toEqual('-1.1'); + expect(serialize(true)).toEqual('true'); + expect(serialize(false)).toEqual('false'); + + const valueOf = () => 'valueOf string'; + const toJSON = () => 'toJSON string'; + + const valueOfAndToJSONValue = { valueOf, toJSON }; + expect(serialize(valueOfAndToJSONValue)).toEqual('valueOf string'); + + const onlyToJSONValue = { toJSON }; + expect(serialize(onlyToJSONValue)).toEqual('toJSON string'); + + expect(() => serialize(NaN)).toThrow('String cannot represent value: NaN'); + + expect(() => serialize([1])).toThrow('String cannot represent value: [1]'); + + const badObjValue = {}; + expect(() => serialize(badObjValue)).toThrow('String cannot represent value: {}'); + + const badValueOfObjValue = { valueOf: 'valueOf string' }; + expect(() => serialize(badValueOfObjValue)).toThrow( + 'String cannot represent value: { valueOf: "valueOf string" }' + ); + }); + }); + + describe('GraphQLBoolean', () => { + it('parseValue', () => { + function parseValue(value: unknown) { + return GraphQLBoolean.parseValue(value); + } + + expect(parseValue(true)).toEqual(true); + expect(parseValue(false)).toEqual(false); + + expect(() => parseValue(undefined)).toThrow('Boolean cannot represent a non boolean value: undefined'); + expect(() => parseValue(null)).toThrow('Boolean cannot represent a non boolean value: null'); + expect(() => parseValue(0)).toThrow('Boolean cannot represent a non boolean value: 0'); + expect(() => parseValue(1)).toThrow('Boolean cannot represent a non boolean value: 1'); + expect(() => parseValue(NaN)).toThrow('Boolean cannot represent a non boolean value: NaN'); + expect(() => parseValue('')).toThrow('Boolean cannot represent a non boolean value: ""'); + expect(() => parseValue('false')).toThrow('Boolean cannot represent a non boolean value: "false"'); + expect(() => parseValue([false])).toThrow('Boolean cannot represent a non boolean value: [false]'); + expect(() => parseValue({ value: false })).toThrow( + 'Boolean cannot represent a non boolean value: { value: false }' + ); + }); + + it('parseLiteral', () => { + function parseLiteral(str: string) { + return GraphQLBoolean.parseLiteral(parseValueToAST(str), undefined); + } + + expect(parseLiteral('true')).toEqual(true); + expect(parseLiteral('false')).toEqual(false); + + expect(() => parseLiteral('null')).toThrow('Boolean cannot represent a non boolean value: null'); + expect(() => parseLiteral('0')).toThrow('Boolean cannot represent a non boolean value: 0'); + expect(() => parseLiteral('1')).toThrow('Boolean cannot represent a non boolean value: 1'); + expect(() => parseLiteral('0.1')).toThrow('Boolean cannot represent a non boolean value: 0.1'); + expect(() => parseLiteral('""')).toThrow('Boolean cannot represent a non boolean value: ""'); + expect(() => parseLiteral('"false"')).toThrow('Boolean cannot represent a non boolean value: "false"'); + expect(() => parseLiteral('[false]')).toThrow('Boolean cannot represent a non boolean value: [false]'); + expect(() => parseLiteral('{ value: false }')).toThrow( + 'Boolean cannot represent a non boolean value: { value: false }' + ); + expect(() => parseLiteral('ENUM_VALUE')).toThrow('Boolean cannot represent a non boolean value: ENUM_VALUE'); + expect(() => parseLiteral('$var')).toThrow('Boolean cannot represent a non boolean value: $var'); + }); + + it('serialize', () => { + function serialize(value: unknown) { + return GraphQLBoolean.serialize(value); + } + + expect(serialize(1)).toEqual(true); + expect(serialize(0)).toEqual(false); + expect(serialize(true)).toEqual(true); + expect(serialize(false)).toEqual(false); + expect( + serialize({ + value: true, + valueOf() { + return (this as { value: boolean }).value; + }, + }) + ).toEqual(true); + + expect(() => serialize(NaN)).toThrow('Boolean cannot represent a non boolean value: NaN'); + expect(() => serialize('')).toThrow('Boolean cannot represent a non boolean value: ""'); + expect(() => serialize('true')).toThrow('Boolean cannot represent a non boolean value: "true"'); + expect(() => serialize([false])).toThrow('Boolean cannot represent a non boolean value: [false]'); + expect(() => serialize({})).toThrow('Boolean cannot represent a non boolean value: {}'); + }); + }); + + describe('GraphQLID', () => { + it('parseValue', () => { + function parseValue(value: unknown) { + return GraphQLID.parseValue(value); + } + + expect(parseValue('')).toEqual(''); + expect(parseValue('1')).toEqual('1'); + expect(parseValue('foo')).toEqual('foo'); + expect(parseValue(1)).toEqual('1'); + expect(parseValue(0)).toEqual('0'); + expect(parseValue(-1)).toEqual('-1'); + + // Maximum and minimum safe numbers in JS + expect(parseValue(9007199254740991)).toEqual('9007199254740991'); + expect(parseValue(-9007199254740991)).toEqual('-9007199254740991'); + + expect(() => parseValue(undefined)).toThrow('ID cannot represent value: undefined'); + expect(() => parseValue(null)).toThrow('ID cannot represent value: null'); + expect(() => parseValue(0.1)).toThrow('ID cannot represent value: 0.1'); + expect(() => parseValue(NaN)).toThrow('ID cannot represent value: NaN'); + expect(() => parseValue(Infinity)).toThrow('ID cannot represent value: Inf'); + expect(() => parseValue(false)).toThrow('ID cannot represent value: false'); + expect(() => GraphQLID.parseValue(['1'])).toThrow('ID cannot represent value: ["1"]'); + expect(() => GraphQLID.parseValue({ value: '1' })).toThrow('ID cannot represent value: { value: "1" }'); + }); + + it('parseLiteral', () => { + function parseLiteral(str: string) { + return GraphQLID.parseLiteral(parseValueToAST(str), undefined); + } + + expect(parseLiteral('""')).toEqual(''); + expect(parseLiteral('"1"')).toEqual('1'); + expect(parseLiteral('"foo"')).toEqual('foo'); + expect(parseLiteral('"""foo"""')).toEqual('foo'); + expect(parseLiteral('1')).toEqual('1'); + expect(parseLiteral('0')).toEqual('0'); + expect(parseLiteral('-1')).toEqual('-1'); + + // Support arbitrary long numbers even if they can't be represented in JS + expect(parseLiteral('90071992547409910')).toEqual('90071992547409910'); + expect(parseLiteral('-90071992547409910')).toEqual('-90071992547409910'); + + expect(() => parseLiteral('null')).toThrow('ID cannot represent a non-string and non-integer value: null'); + expect(() => parseLiteral('0.1')).toThrow('ID cannot represent a non-string and non-integer value: 0.1'); + expect(() => parseLiteral('false')).toThrow('ID cannot represent a non-string and non-integer value: false'); + expect(() => parseLiteral('["1"]')).toThrow('ID cannot represent a non-string and non-integer value: ["1"]'); + expect(() => parseLiteral('{ value: "1" }')).toThrow( + 'ID cannot represent a non-string and non-integer value: { value: "1" }' + ); + expect(() => parseLiteral('ENUM_VALUE')).toThrow( + 'ID cannot represent a non-string and non-integer value: ENUM_VALUE' + ); + expect(() => parseLiteral('$var')).toThrow('ID cannot represent a non-string and non-integer value: $var'); + }); + + it('serialize', () => { + function serialize(value: unknown) { + return GraphQLID.serialize(value); + } + + expect(serialize('string')).toEqual('string'); + expect(serialize('false')).toEqual('false'); + expect(serialize('')).toEqual(''); + expect(serialize(123)).toEqual('123'); + expect(serialize(0)).toEqual('0'); + expect(serialize(-1)).toEqual('-1'); + + const valueOf = () => 'valueOf ID'; + const toJSON = () => 'toJSON ID'; + + const valueOfAndToJSONValue = { valueOf, toJSON }; + expect(serialize(valueOfAndToJSONValue)).toEqual('valueOf ID'); + + const onlyToJSONValue = { toJSON }; + expect(serialize(onlyToJSONValue)).toEqual('toJSON ID'); + + const badObjValue = { + _id: false, + valueOf() { + return this._id; + }, + }; + expect(() => serialize(badObjValue)).toThrow( + 'ID cannot represent value: { _id: false, valueOf: [function valueOf] }' + ); + + expect(() => serialize(true)).toThrow('ID cannot represent value: true'); + + expect(() => serialize(3.14)).toThrow('ID cannot represent value: 3.14'); + + expect(() => serialize({})).toThrow('ID cannot represent value: {}'); + + expect(() => serialize(['abc'])).toThrow('ID cannot represent value: ["abc"]'); + }); + }); +}); diff --git a/packages/graphql/src/type/__tests__/schema-test.ts b/packages/graphql/src/type/__tests__/schema-test.ts new file mode 100644 index 00000000000..6d3de3860f7 --- /dev/null +++ b/packages/graphql/src/type/__tests__/schema-test.ts @@ -0,0 +1,483 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import { DirectiveLocation } from '../../language/directiveLocation.js'; + +import { printSchema } from '../../utilities/printSchema.js'; + +import type { GraphQLCompositeType } from '../definition.js'; +import { + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLList, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, +} from '../definition.js'; +import { GraphQLDirective } from '../directives.js'; +import { SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef } from '../introspection.js'; +import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../scalars.js'; +import { GraphQLSchema } from '../schema.js'; + +describe('Type System: Schema', () => { + it('Define sample schema', () => { + const BlogImage = new GraphQLObjectType({ + name: 'Image', + fields: { + url: { type: GraphQLString }, + width: { type: GraphQLInt }, + height: { type: GraphQLInt }, + }, + }); + + const BlogAuthor: GraphQLObjectType = new GraphQLObjectType({ + name: 'Author', + fields: () => ({ + id: { type: GraphQLString }, + name: { type: GraphQLString }, + pic: { + args: { width: { type: GraphQLInt }, height: { type: GraphQLInt } }, + type: BlogImage, + }, + recentArticle: { type: BlogArticle }, + }), + }); + + const BlogArticle: GraphQLObjectType = new GraphQLObjectType({ + name: 'Article', + fields: { + id: { type: GraphQLString }, + isPublished: { type: GraphQLBoolean }, + author: { type: BlogAuthor }, + title: { type: GraphQLString }, + body: { type: GraphQLString }, + }, + }); + + const BlogQuery = new GraphQLObjectType({ + name: 'Query', + fields: { + article: { + args: { id: { type: GraphQLString } }, + type: BlogArticle, + }, + feed: { + type: new GraphQLList(BlogArticle), + }, + }, + }); + + const BlogMutation = new GraphQLObjectType({ + name: 'Mutation', + fields: { + writeArticle: { + type: BlogArticle, + }, + }, + }); + + const BlogSubscription = new GraphQLObjectType({ + name: 'Subscription', + fields: { + articleSubscribe: { + args: { id: { type: GraphQLString } }, + type: BlogArticle, + }, + }, + }); + + const schema = new GraphQLSchema({ + description: 'Sample schema', + query: BlogQuery, + mutation: BlogMutation, + subscription: BlogSubscription, + }); + + expect(printSchema(schema)).toEqual(dedent` + """Sample schema""" + schema { + query: Query + mutation: Mutation + subscription: Subscription + } + + type Query { + article(id: String): Article + feed: [Article] + } + + type Article { + id: String + isPublished: Boolean + author: Author + title: String + body: String + } + + type Author { + id: String + name: String + pic(width: Int, height: Int): Image + recentArticle: Article + } + + type Image { + url: String + width: Int + height: Int + } + + type Mutation { + writeArticle: Article + } + + type Subscription { + articleSubscribe(id: String): Article + } + `); + }); + + describe('Root types', () => { + const testType = new GraphQLObjectType({ name: 'TestType', fields: {} }); + + it('defines a query root', () => { + const schema = new GraphQLSchema({ query: testType }); + expect(schema.getQueryType()).toEqual(testType); + expect(Object.keys(schema.getTypeMap())).toContain('TestType'); + }); + + it('defines a mutation root', () => { + const schema = new GraphQLSchema({ mutation: testType }); + expect(schema.getMutationType()).toEqual(testType); + expect(Object.keys(schema.getTypeMap())).toContain('TestType'); + }); + + it('defines a subscription root', () => { + const schema = new GraphQLSchema({ subscription: testType }); + expect(schema.getSubscriptionType()).toEqual(testType); + expect(Object.keys(schema.getTypeMap())).toContain('TestType'); + }); + }); + + describe('Type Map', () => { + it('includes interface possible types in the type map', () => { + const SomeInterface = new GraphQLInterfaceType({ + name: 'SomeInterface', + fields: {}, + }); + + const SomeSubtype = new GraphQLObjectType({ + name: 'SomeSubtype', + fields: {}, + interfaces: [SomeInterface], + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + iface: { type: SomeInterface }, + }, + }), + types: [SomeSubtype], + }); + + expect(schema.getType('SomeInterface')).toEqual(SomeInterface); + expect(schema.getType('SomeSubtype')).toEqual(SomeSubtype); + + expect(schema.isSubType(SomeInterface, SomeSubtype)).toEqual(true); + }); + + it("includes interface's thunk subtypes in the type map", () => { + const SomeInterface = new GraphQLInterfaceType({ + name: 'SomeInterface', + fields: {}, + interfaces: () => [AnotherInterface], + }); + + const AnotherInterface = new GraphQLInterfaceType({ + name: 'AnotherInterface', + fields: {}, + }); + + const SomeSubtype = new GraphQLObjectType({ + name: 'SomeSubtype', + fields: {}, + interfaces: () => [SomeInterface], + }); + + const schema = new GraphQLSchema({ types: [SomeSubtype] }); + + expect(schema.getType('SomeInterface')).toEqual(SomeInterface); + expect(schema.getType('AnotherInterface')).toEqual(AnotherInterface); + expect(schema.getType('SomeSubtype')).toEqual(SomeSubtype); + }); + + it('includes nested input objects in the map', () => { + const NestedInputObject = new GraphQLInputObjectType({ + name: 'NestedInputObject', + fields: {}, + }); + + const SomeInputObject = new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { nested: { type: NestedInputObject } }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + something: { + type: GraphQLString, + args: { input: { type: SomeInputObject } }, + }, + }, + }), + }); + + expect(schema.getType('SomeInputObject')).toEqual(SomeInputObject); + expect(schema.getType('NestedInputObject')).toEqual(NestedInputObject); + }); + + it('includes input types only used in directives', () => { + const directive = new GraphQLDirective({ + name: 'dir', + locations: [DirectiveLocation.OBJECT], + args: { + arg: { + type: new GraphQLInputObjectType({ name: 'Foo', fields: {} }), + }, + argList: { + type: new GraphQLList(new GraphQLInputObjectType({ name: 'Bar', fields: {} })), + }, + }, + }); + const schema = new GraphQLSchema({ directives: [directive] }); + + expect(Object.keys(schema.getTypeMap())).toContain('Foo'); + expect(Object.keys(schema.getTypeMap())).toContain('Bar'); + }); + }); + + it('preserves the order of user provided types', () => { + const aType = new GraphQLObjectType({ + name: 'A', + fields: { + sub: { type: new GraphQLScalarType({ name: 'ASub' }) }, + }, + }); + const zType = new GraphQLObjectType({ + name: 'Z', + fields: { + sub: { type: new GraphQLScalarType({ name: 'ZSub' }) }, + }, + }); + const queryType = new GraphQLObjectType({ + name: 'Query', + fields: { + a: { type: aType }, + z: { type: zType }, + sub: { type: new GraphQLScalarType({ name: 'QuerySub' }) }, + }, + }); + const schema = new GraphQLSchema({ + types: [zType, queryType, aType], + query: queryType, + }); + + const typeNames = Object.keys(schema.getTypeMap()); + expect(typeNames).toEqual([ + 'Z', + 'ZSub', + 'Query', + 'QuerySub', + 'A', + 'ASub', + 'Boolean', + 'String', + '__Schema', + '__Type', + '__TypeKind', + '__Field', + '__InputValue', + '__EnumValue', + '__Directive', + '__DirectiveLocation', + ]); + + // Also check that this order is stable + const copySchema = new GraphQLSchema(schema.toConfig()); + expect(Object.keys(copySchema.getTypeMap())).toEqual(typeNames); + }); + + it('can be Object.toStringified', () => { + const schema = new GraphQLSchema({}); + + expect(Object.prototype.toString.call(schema)).toEqual('[object GraphQLSchema]'); + }); + + describe('getField', () => { + const petType = new GraphQLInterfaceType({ + name: 'Pet', + fields: { + name: { type: GraphQLString }, + }, + }); + + const catType = new GraphQLObjectType({ + name: 'Cat', + interfaces: [petType], + fields: { + name: { type: GraphQLString }, + }, + }); + + const dogType = new GraphQLObjectType({ + name: 'Dog', + interfaces: [petType], + fields: { + name: { type: GraphQLString }, + }, + }); + + const catOrDog = new GraphQLUnionType({ + name: 'CatOrDog', + types: [catType, dogType], + }); + + const queryType = new GraphQLObjectType({ + name: 'Query', + fields: { + catOrDog: { type: catOrDog }, + }, + }); + + const mutationType = new GraphQLObjectType({ + name: 'Mutation', + fields: {}, + }); + + const subscriptionType = new GraphQLObjectType({ + name: 'Subscription', + fields: {}, + }); + + const schema = new GraphQLSchema({ + query: queryType, + mutation: mutationType, + subscription: subscriptionType, + }); + + function expectField(parentType: GraphQLCompositeType, name: string) { + return expect(schema.getField(parentType, name)); + } + + it('returns known fields', () => { + expectField(petType, 'name').toEqual(petType.getFields()['name']); + expectField(catType, 'name').toEqual(catType.getFields()['name']); + + expectField(queryType, 'catOrDog').toEqual(queryType.getFields()['catOrDog']); + }); + + it('returns `undefined` for unknown fields', () => { + expectField(catOrDog, 'name').toEqual(undefined); + + expectField(queryType, 'unknown').toEqual(undefined); + expectField(petType, 'unknown').toEqual(undefined); + expectField(catType, 'unknown').toEqual(undefined); + expectField(catOrDog, 'unknown').toEqual(undefined); + }); + + it('handles introspection fields', () => { + expectField(queryType, '__typename').toEqual(TypeNameMetaFieldDef); + expectField(mutationType, '__typename').toEqual(TypeNameMetaFieldDef); + expectField(subscriptionType, '__typename').toEqual(TypeNameMetaFieldDef); + + expectField(petType, '__typename').toEqual(TypeNameMetaFieldDef); + expectField(catType, '__typename').toEqual(TypeNameMetaFieldDef); + expectField(dogType, '__typename').toEqual(TypeNameMetaFieldDef); + expectField(catOrDog, '__typename').toEqual(TypeNameMetaFieldDef); + + expectField(queryType, '__type').toEqual(TypeMetaFieldDef); + expectField(queryType, '__schema').toEqual(SchemaMetaFieldDef); + }); + + it('returns `undefined` for introspection fields in wrong location', () => { + expect(schema.getField(petType, '__type')).toEqual(undefined); + expect(schema.getField(dogType, '__type')).toEqual(undefined); + expect(schema.getField(mutationType, '__type')).toEqual(undefined); + expect(schema.getField(subscriptionType, '__type')).toEqual(undefined); + + expect(schema.getField(petType, '__schema')).toEqual(undefined); + expect(schema.getField(dogType, '__schema')).toEqual(undefined); + expect(schema.getField(mutationType, '__schema')).toEqual(undefined); + expect(schema.getField(subscriptionType, '__schema')).toEqual(undefined); + }); + }); + + describe('Validity', () => { + describe('when not assumed valid', () => { + it('configures the schema to still needing validation', () => { + expect( + new GraphQLSchema({ + assumeValid: false, + }).__validationErrors + ).toEqual(undefined); + }); + }); + + describe('A Schema must contain uniquely named types', () => { + it('rejects a Schema which redefines a built-in type', () => { + const FakeString = new GraphQLScalarType({ name: 'String' }); + + const QueryType = new GraphQLObjectType({ + name: 'Query', + fields: { + normal: { type: GraphQLString }, + fake: { type: FakeString }, + }, + }); + + expect(() => new GraphQLSchema({ query: QueryType })).toThrow( + 'Schema must contain uniquely named types but contains multiple types named "String".' + ); + }); + + it('rejects a Schema which defines an object type twice', () => { + const types = [ + new GraphQLObjectType({ name: 'SameName', fields: {} }), + new GraphQLObjectType({ name: 'SameName', fields: {} }), + ]; + + expect(() => new GraphQLSchema({ types })).toThrow( + 'Schema must contain uniquely named types but contains multiple types named "SameName".' + ); + }); + + it('rejects a Schema which defines fields with conflicting types', () => { + const fields = {}; + const QueryType = new GraphQLObjectType({ + name: 'Query', + fields: { + a: { type: new GraphQLObjectType({ name: 'SameName', fields }) }, + b: { type: new GraphQLObjectType({ name: 'SameName', fields }) }, + }, + }); + + expect(() => new GraphQLSchema({ query: QueryType })).toThrow( + 'Schema must contain uniquely named types but contains multiple types named "SameName".' + ); + }); + }); + + describe('when assumed valid', () => { + it('configures the schema to have no errors', () => { + expect( + new GraphQLSchema({ + assumeValid: true, + }).__validationErrors + ).toEqual([]); + }); + }); + }); +}); diff --git a/packages/graphql/src/type/__tests__/validation-test.ts b/packages/graphql/src/type/__tests__/validation-test.ts new file mode 100644 index 00000000000..75733b7623b --- /dev/null +++ b/packages/graphql/src/type/__tests__/validation-test.ts @@ -0,0 +1,2631 @@ +import { dedent } from '../../__testUtils__/dedent.js'; +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { inspect } from '../../jsutils/inspect.js'; + +import { DirectiveLocation } from '../../language/directiveLocation.js'; +import { parse } from '../../language/parser.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; +import { extendSchema } from '../../utilities/extendSchema.js'; + +import type { + GraphQLArgumentConfig, + GraphQLFieldConfig, + GraphQLInputFieldConfig, + GraphQLInputType, + GraphQLNamedType, + GraphQLOutputType, +} from '../definition.js'; +import { + assertEnumType, + assertInputObjectType, + assertInterfaceType, + assertObjectType, + assertScalarType, + assertUnionType, + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLUnionType, +} from '../definition.js'; +import { assertDirective, GraphQLDirective } from '../directives.js'; +import { GraphQLString } from '../scalars.js'; +import { GraphQLSchema } from '../schema.js'; +import { assertValidSchema, validateSchema } from '../validate.js'; + +const SomeSchema = buildSchema(` + scalar SomeScalar + + interface SomeInterface { f: SomeObject } + + type SomeObject implements SomeInterface { f: SomeObject } + + union SomeUnion = SomeObject + + enum SomeEnum { ONLY } + + input SomeInputObject { val: String = "hello" } + + directive @SomeDirective on QUERY +`); + +const SomeScalarType = assertScalarType(SomeSchema.getType('SomeScalar')); +const SomeInterfaceType = assertInterfaceType(SomeSchema.getType('SomeInterface')); +const SomeObjectType = assertObjectType(SomeSchema.getType('SomeObject')); +const SomeUnionType = assertUnionType(SomeSchema.getType('SomeUnion')); +const SomeEnumType = assertEnumType(SomeSchema.getType('SomeEnum')); +const SomeInputObjectType = assertInputObjectType(SomeSchema.getType('SomeInputObject')); + +const SomeDirective = assertDirective(SomeSchema.getDirective('SomeDirective')); + +function withModifiers( + type: T +): Array | GraphQLNonNull>> { + return [type, new GraphQLList(type), new GraphQLNonNull(type), new GraphQLNonNull(new GraphQLList(type))]; +} + +const outputTypes: ReadonlyArray = [ + ...withModifiers(GraphQLString), + ...withModifiers(SomeScalarType), + ...withModifiers(SomeEnumType), + ...withModifiers(SomeObjectType), + ...withModifiers(SomeUnionType), + ...withModifiers(SomeInterfaceType), +]; + +const notOutputTypes: ReadonlyArray = [...withModifiers(SomeInputObjectType)]; + +const inputTypes: ReadonlyArray = [ + ...withModifiers(GraphQLString), + ...withModifiers(SomeScalarType), + ...withModifiers(SomeEnumType), + ...withModifiers(SomeInputObjectType), +]; + +const notInputTypes: ReadonlyArray = [ + ...withModifiers(SomeObjectType), + ...withModifiers(SomeUnionType), + ...withModifiers(SomeInterfaceType), +]; + +function schemaWithFieldType(type: GraphQLOutputType): GraphQLSchema { + return new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { f: { type } }, + }), + }); +} + +describe('Type System: A Schema must have Object root types', () => { + it('accepts a Schema whose query type is an object type', () => { + const schema = buildSchema(` + type Query { + test: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + + const schemaWithDef = buildSchema(` + schema { + query: QueryRoot + } + + type QueryRoot { + test: String + } + `); + expectJSON(validateSchema(schemaWithDef)).toDeepEqual([]); + }); + + it('accepts a Schema whose query and mutation types are object types', () => { + const schema = buildSchema(` + type Query { + test: String + } + + type Mutation { + test: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + + const schemaWithDef = buildSchema(` + schema { + query: QueryRoot + mutation: MutationRoot + } + + type QueryRoot { + test: String + } + + type MutationRoot { + test: String + } + `); + expectJSON(validateSchema(schemaWithDef)).toDeepEqual([]); + }); + + it('accepts a Schema whose query and subscription types are object types', () => { + const schema = buildSchema(` + type Query { + test: String + } + + type Subscription { + test: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + + const schemaWithDef = buildSchema(` + schema { + query: QueryRoot + subscription: SubscriptionRoot + } + + type QueryRoot { + test: String + } + + type SubscriptionRoot { + test: String + } + `); + expectJSON(validateSchema(schemaWithDef)).toDeepEqual([]); + }); + + it('rejects a Schema without a query type', () => { + const schema = buildSchema(` + type Mutation { + test: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Query root type must be provided.', + }, + ]); + + const schemaWithDef = buildSchema(` + schema { + mutation: MutationRoot + } + + type MutationRoot { + test: String + } + `); + expectJSON(validateSchema(schemaWithDef)).toDeepEqual([ + { + message: 'Query root type must be provided.', + locations: [{ line: 2, column: 7 }], + }, + ]); + }); + + it('rejects a Schema whose query root type is not an Object type', () => { + const schema = buildSchema(` + input Query { + test: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Query root type must be Object type, it cannot be Query.', + locations: [{ line: 2, column: 7 }], + }, + ]); + + const schemaWithDef = buildSchema(` + schema { + query: SomeInputObject + } + + input SomeInputObject { + test: String + } + `); + expectJSON(validateSchema(schemaWithDef)).toDeepEqual([ + { + message: 'Query root type must be Object type, it cannot be SomeInputObject.', + locations: [{ line: 3, column: 16 }], + }, + ]); + }); + + it('rejects a Schema whose mutation type is an input type', () => { + const schema = buildSchema(` + type Query { + field: String + } + + input Mutation { + test: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Mutation root type must be Object type if provided, it cannot be Mutation.', + locations: [{ line: 6, column: 7 }], + }, + ]); + + const schemaWithDef = buildSchema(` + schema { + query: Query + mutation: SomeInputObject + } + + type Query { + field: String + } + + input SomeInputObject { + test: String + } + `); + expectJSON(validateSchema(schemaWithDef)).toDeepEqual([ + { + message: 'Mutation root type must be Object type if provided, it cannot be SomeInputObject.', + locations: [{ line: 4, column: 19 }], + }, + ]); + }); + + it('rejects a Schema whose subscription type is an input type', () => { + const schema = buildSchema(` + type Query { + field: String + } + + input Subscription { + test: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Subscription root type must be Object type if provided, it cannot be Subscription.', + locations: [{ line: 6, column: 7 }], + }, + ]); + + const schemaWithDef = buildSchema(` + schema { + query: Query + subscription: SomeInputObject + } + + type Query { + field: String + } + + input SomeInputObject { + test: String + } + `); + expectJSON(validateSchema(schemaWithDef)).toDeepEqual([ + { + message: 'Subscription root type must be Object type if provided, it cannot be SomeInputObject.', + locations: [{ line: 4, column: 23 }], + }, + ]); + }); + + it('rejects a schema extended with invalid root types', () => { + let schema = buildSchema(` + input SomeInputObject { + test: String + } + + scalar SomeScalar + + enum SomeEnum { + ENUM_VALUE + } + `); + + schema = extendSchema( + schema, + parse(` + extend schema { + query: SomeInputObject + } + `) + ); + + schema = extendSchema( + schema, + parse(` + extend schema { + mutation: SomeScalar + } + `) + ); + + schema = extendSchema( + schema, + parse(` + extend schema { + subscription: SomeEnum + } + `) + ); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Query root type must be Object type, it cannot be SomeInputObject.', + locations: [{ line: 3, column: 18 }], + }, + { + message: 'Mutation root type must be Object type if provided, it cannot be SomeScalar.', + locations: [{ line: 3, column: 21 }], + }, + { + message: 'Subscription root type must be Object type if provided, it cannot be SomeEnum.', + locations: [{ line: 3, column: 25 }], + }, + ]); + }); + + it('rejects a Schema whose types are incorrectly typed', () => { + const schema = new GraphQLSchema({ + query: SomeObjectType, + // @ts-expect-error + types: [{ name: 'SomeType' }, SomeDirective], + }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Expected GraphQL named type but got: { name: "SomeType" }.', + }, + { + message: 'Expected GraphQL named type but got: @SomeDirective.', + locations: [{ line: 14, column: 3 }], + }, + ]); + }); + + it('rejects a Schema whose directives are incorrectly typed', () => { + const schema = new GraphQLSchema({ + query: SomeObjectType, + // @ts-expect-error + directives: [null, 'SomeDirective', SomeScalarType], + }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Expected directive but got: null.', + }, + { + message: 'Expected directive but got: "SomeDirective".', + }, + { + message: 'Expected directive but got: SomeScalar.', + locations: [{ line: 2, column: 3 }], + }, + ]); + }); +}); + +describe('Type System: Root types must all be different if provided', () => { + it('accepts a Schema with different root types', () => { + const schema = buildSchema(` + type SomeObject1 { + field: String + } + + type SomeObject2 { + field: String + } + + type SomeObject3 { + field: String + } + + schema { + query: SomeObject1 + mutation: SomeObject2 + subscription: SomeObject3 + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects a Schema where the same type is used for multiple root types', () => { + const schema = buildSchema(` + type SomeObject { + field: String + } + + type UniqueObject { + field: String + } + + schema { + query: SomeObject + mutation: UniqueObject + subscription: SomeObject + } + `); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'All root types must be different, "SomeObject" type is used as query and subscription root types.', + locations: [ + { line: 11, column: 16 }, + { line: 13, column: 23 }, + ], + }, + ]); + }); + + it('rejects a Schema where the same type is used for all root types', () => { + const schema = buildSchema(` + type SomeObject { + field: String + } + + schema { + query: SomeObject + mutation: SomeObject + subscription: SomeObject + } + `); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'All root types must be different, "SomeObject" type is used as query, mutation, and subscription root types.', + locations: [ + { line: 7, column: 16 }, + { line: 8, column: 19 }, + { line: 9, column: 23 }, + ], + }, + ]); + }); +}); + +describe('Type System: Objects must have fields', () => { + it('accepts an Object type with fields object', () => { + const schema = buildSchema(` + type Query { + field: SomeObject + } + + type SomeObject { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Object type with missing fields', () => { + const schema = buildSchema(` + type Query { + test: IncompleteObject + } + + type IncompleteObject + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type IncompleteObject must define one or more fields.', + locations: [{ line: 6, column: 7 }], + }, + ]); + + const manualSchema = schemaWithFieldType( + new GraphQLObjectType({ + name: 'IncompleteObject', + fields: {}, + }) + ); + expectJSON(validateSchema(manualSchema)).toDeepEqual([ + { + message: 'Type IncompleteObject must define one or more fields.', + }, + ]); + + const manualSchema2 = schemaWithFieldType( + new GraphQLObjectType({ + name: 'IncompleteObject', + fields() { + return {}; + }, + }) + ); + expectJSON(validateSchema(manualSchema2)).toDeepEqual([ + { + message: 'Type IncompleteObject must define one or more fields.', + }, + ]); + }); + + it('rejects an Object type with incorrectly named fields', () => { + const schema = schemaWithFieldType( + new GraphQLObjectType({ + name: 'SomeObject', + fields: { + __badName: { type: GraphQLString }, + }, + }) + ); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Name "__badName" must not begin with "__", which is reserved by GraphQL introspection.', + }, + ]); + }); +}); + +describe('Type System: Fields args must be properly named', () => { + it('accepts field args with valid names', () => { + const schema = schemaWithFieldType( + new GraphQLObjectType({ + name: 'SomeObject', + fields: { + goodField: { + type: GraphQLString, + args: { + goodArg: { type: GraphQLString }, + }, + }, + }, + }) + ); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects field arg with invalid names', () => { + const schema = schemaWithFieldType( + new GraphQLObjectType({ + name: 'SomeObject', + fields: { + badField: { + type: GraphQLString, + args: { + __badName: { type: GraphQLString }, + }, + }, + }, + }) + ); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Name "__badName" must not begin with "__", which is reserved by GraphQL introspection.', + }, + ]); + }); +}); + +describe('Type System: Union types must be valid', () => { + it('accepts a Union type with member types', () => { + const schema = buildSchema(` + type Query { + test: GoodUnion + } + + type TypeA { + field: String + } + + type TypeB { + field: String + } + + union GoodUnion = + | TypeA + | TypeB + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects a Union type with empty types', () => { + let schema = buildSchema(` + type Query { + test: BadUnion + } + + union BadUnion + `); + + schema = extendSchema( + schema, + parse(` + directive @test on UNION + + extend union BadUnion @test + `) + ); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Union type BadUnion must define one or more member types.', + locations: [ + { line: 6, column: 7 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('rejects a Union type with duplicated member type', () => { + let schema = buildSchema(` + type Query { + test: BadUnion + } + + type TypeA { + field: String + } + + type TypeB { + field: String + } + + union BadUnion = + | TypeA + | TypeB + | TypeA + `); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Union type BadUnion can only include type TypeA once.', + locations: [ + { line: 15, column: 11 }, + { line: 17, column: 11 }, + ], + }, + ]); + + schema = extendSchema(schema, parse('extend union BadUnion = TypeB')); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Union type BadUnion can only include type TypeA once.', + locations: [ + { line: 15, column: 11 }, + { line: 17, column: 11 }, + ], + }, + { + message: 'Union type BadUnion can only include type TypeB once.', + locations: [ + { line: 16, column: 11 }, + { line: 1, column: 25 }, + ], + }, + ]); + }); + + it('rejects a Union type with non-Object members types', () => { + let schema = buildSchema(` + type Query { + test: BadUnion + } + + type TypeA { + field: String + } + + type TypeB { + field: String + } + + union BadUnion = + | TypeA + | String + | TypeB + `); + + schema = extendSchema(schema, parse('extend union BadUnion = Int')); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Union type BadUnion can only include Object types, it cannot include String.', + locations: [{ line: 16, column: 11 }], + }, + { + message: 'Union type BadUnion can only include Object types, it cannot include Int.', + locations: [{ line: 1, column: 25 }], + }, + ]); + + const badUnionMemberTypes = [ + GraphQLString, + new GraphQLNonNull(SomeObjectType), + new GraphQLList(SomeObjectType), + SomeInterfaceType, + SomeUnionType, + SomeEnumType, + SomeInputObjectType, + ]; + for (const memberType of badUnionMemberTypes) { + const badUnion = new GraphQLUnionType({ + name: 'BadUnion', + // @ts-expect-error + types: [memberType], + }); + const badSchema = schemaWithFieldType(badUnion); + expectJSON(validateSchema(badSchema)).toDeepEqual([ + { + message: 'Union type BadUnion can only include Object types, ' + `it cannot include ${inspect(memberType)}.`, + }, + ]); + } + }); +}); + +describe('Type System: Input Objects must have fields', () => { + it('accepts an Input Object type with fields', () => { + const schema = buildSchema(` + type Query { + field(arg: SomeInputObject): String + } + + input SomeInputObject { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Input Object type with missing fields', () => { + let schema = buildSchema(` + type Query { + field(arg: SomeInputObject): String + } + + input SomeInputObject + `); + + schema = extendSchema( + schema, + parse(` + directive @test on INPUT_OBJECT + + extend input SomeInputObject @test + `) + ); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Input Object type SomeInputObject must define one or more fields.', + locations: [ + { line: 6, column: 7 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('accepts an Input Object with breakable circular reference', () => { + const schema = buildSchema(` + type Query { + field(arg: SomeInputObject): String + } + + input SomeInputObject { + self: SomeInputObject + arrayOfSelf: [SomeInputObject] + nonNullArrayOfSelf: [SomeInputObject]! + nonNullArrayOfNonNullSelf: [SomeInputObject!]! + intermediateSelf: AnotherInputObject + } + + input AnotherInputObject { + parent: SomeInputObject + } + `); + + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Input Object with non-breakable circular reference', () => { + const schema = buildSchema(` + type Query { + field(arg: SomeInputObject): String + } + + input SomeInputObject { + nonNullSelf: SomeInputObject! + } + `); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Cannot reference Input Object "SomeInputObject" within itself through a series of non-null fields: "nonNullSelf".', + locations: [{ line: 7, column: 9 }], + }, + ]); + }); + + it('rejects Input Objects with non-breakable circular reference spread across them', () => { + const schema = buildSchema(` + type Query { + field(arg: SomeInputObject): String + } + + input SomeInputObject { + startLoop: AnotherInputObject! + } + + input AnotherInputObject { + nextInLoop: YetAnotherInputObject! + } + + input YetAnotherInputObject { + closeLoop: SomeInputObject! + } + `); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Cannot reference Input Object "SomeInputObject" within itself through a series of non-null fields: "startLoop.nextInLoop.closeLoop".', + locations: [ + { line: 7, column: 9 }, + { line: 11, column: 9 }, + { line: 15, column: 9 }, + ], + }, + ]); + }); + + it('rejects Input Objects with multiple non-breakable circular reference', () => { + const schema = buildSchema(` + type Query { + field(arg: SomeInputObject): String + } + + input SomeInputObject { + startLoop: AnotherInputObject! + } + + input AnotherInputObject { + closeLoop: SomeInputObject! + startSecondLoop: YetAnotherInputObject! + } + + input YetAnotherInputObject { + closeSecondLoop: AnotherInputObject! + nonNullSelf: YetAnotherInputObject! + } + `); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Cannot reference Input Object "SomeInputObject" within itself through a series of non-null fields: "startLoop.closeLoop".', + locations: [ + { line: 7, column: 9 }, + { line: 11, column: 9 }, + ], + }, + { + message: + 'Cannot reference Input Object "AnotherInputObject" within itself through a series of non-null fields: "startSecondLoop.closeSecondLoop".', + locations: [ + { line: 12, column: 9 }, + { line: 16, column: 9 }, + ], + }, + { + message: + 'Cannot reference Input Object "YetAnotherInputObject" within itself through a series of non-null fields: "nonNullSelf".', + locations: [{ line: 17, column: 9 }], + }, + ]); + }); + + it('rejects an Input Object type with incorrectly typed fields', () => { + const schema = buildSchema(` + type Query { + field(arg: SomeInputObject): String + } + + type SomeObject { + field: String + } + + union SomeUnion = SomeObject + + input SomeInputObject { + badObject: SomeObject + badUnion: SomeUnion + goodInputObject: SomeInputObject + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of SomeInputObject.badObject must be Input Type but got: SomeObject.', + locations: [{ line: 13, column: 20 }], + }, + { + message: 'The type of SomeInputObject.badUnion must be Input Type but got: SomeUnion.', + locations: [{ line: 14, column: 19 }], + }, + ]); + }); + + it('rejects an Input Object type with required argument that is deprecated', () => { + const schema = buildSchema(` + type Query { + field(arg: SomeInputObject): String + } + + input SomeInputObject { + badField: String! @deprecated + optionalField: String @deprecated + anotherOptionalField: String! = "" @deprecated + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Required input field SomeInputObject.badField cannot be deprecated.', + locations: [ + { line: 7, column: 27 }, + { line: 7, column: 19 }, + ], + }, + ]); + }); +}); + +describe('Type System: Enum types must be well defined', () => { + it('rejects an Enum type without values', () => { + let schema = buildSchema(` + type Query { + field: SomeEnum + } + + enum SomeEnum + `); + + schema = extendSchema( + schema, + parse(` + directive @test on ENUM + + extend enum SomeEnum @test + `) + ); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Enum type SomeEnum must define one or more values.', + locations: [ + { line: 6, column: 7 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('rejects an Enum type with incorrectly named values', () => { + const schema = schemaWithFieldType( + new GraphQLEnumType({ + name: 'SomeEnum', + values: { + __badName: {}, + }, + }) + ); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Name "__badName" must not begin with "__", which is reserved by GraphQL introspection.', + }, + ]); + }); +}); + +describe('Type System: Object fields must have output types', () => { + function schemaWithObjectField(fieldConfig: GraphQLFieldConfig): GraphQLSchema { + const BadObjectType = new GraphQLObjectType({ + name: 'BadObject', + fields: { + badField: fieldConfig, + }, + }); + + return new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + f: { type: BadObjectType }, + }, + }), + types: [SomeObjectType], + }); + } + + for (const type of outputTypes) { + const typeName = inspect(type); + it(`accepts an output type as an Object field type: ${typeName}`, () => { + const schema = schemaWithObjectField({ type }); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + } + + it('rejects an empty Object field type', () => { + // @ts-expect-error (type field must not be undefined) + const schema = schemaWithObjectField({ type: undefined }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of BadObject.badField must be Output Type but got: undefined.', + }, + ]); + }); + + for (const type of notOutputTypes) { + const typeStr = inspect(type); + it(`rejects a non-output type as an Object field type: ${typeStr}`, () => { + // @ts-expect-error + const schema = schemaWithObjectField({ type }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: `The type of BadObject.badField must be Output Type but got: ${typeStr}.`, + }, + ]); + }); + } + + it('rejects a non-type value as an Object field type', () => { + // @ts-expect-error + const schema = schemaWithObjectField({ type: Number }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of BadObject.badField must be Output Type but got: [function Number].', + }, + { + message: 'Expected GraphQL named type but got: [function Number].', + }, + ]); + }); + + it('rejects with relevant locations for a non-output type as an Object field type', () => { + const schema = buildSchema(` + type Query { + field: [SomeInputObject] + } + + input SomeInputObject { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of Query.field must be Output Type but got: [SomeInputObject].', + locations: [{ line: 3, column: 16 }], + }, + ]); + }); +}); + +describe('Type System: Objects can only implement unique interfaces', () => { + it('rejects an Object implementing a non-type value', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'BadObject', + // @ts-expect-error (interfaces must not contain undefined) + interfaces: [undefined], + fields: { f: { type: GraphQLString } }, + }), + }); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type BadObject must only implement Interface types, it cannot implement undefined.', + }, + ]); + }); + + it('rejects an Object implementing a non-Interface type', () => { + const schema = buildSchema(` + type Query { + test: BadObject + } + + input SomeInputObject { + field: String + } + + type BadObject implements SomeInputObject { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type BadObject must only implement Interface types, it cannot implement SomeInputObject.', + locations: [{ line: 10, column: 33 }], + }, + ]); + }); + + it('rejects an Object implementing the same interface twice', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: String + } + + type AnotherObject implements AnotherInterface & AnotherInterface { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type AnotherObject can only implement AnotherInterface once.', + locations: [ + { line: 10, column: 37 }, + { line: 10, column: 56 }, + ], + }, + ]); + }); + + it('rejects an Object implementing the same interface twice due to extension', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: String + } + + type AnotherObject implements AnotherInterface { + field: String + } + `); + const extendedSchema = extendSchema(schema, parse('extend type AnotherObject implements AnotherInterface')); + expectJSON(validateSchema(extendedSchema)).toDeepEqual([ + { + message: 'Type AnotherObject can only implement AnotherInterface once.', + locations: [ + { line: 10, column: 37 }, + { line: 1, column: 38 }, + ], + }, + ]); + }); +}); + +describe('Type System: Interface extensions should be valid', () => { + it('rejects an Object implementing the extended interface due to missing field', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: String + } + + type AnotherObject implements AnotherInterface { + field: String + } + `); + const extendedSchema = extendSchema( + schema, + parse(` + extend interface AnotherInterface { + newField: String + } + + extend type AnotherObject { + differentNewField: String + } + `) + ); + expectJSON(validateSchema(extendedSchema)).toDeepEqual([ + { + message: 'Interface field AnotherInterface.newField expected but AnotherObject does not provide it.', + locations: [ + { line: 3, column: 11 }, + { line: 10, column: 7 }, + { line: 6, column: 9 }, + ], + }, + ]); + }); + + it('rejects an Object implementing the extended interface due to missing field args', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: String + } + + type AnotherObject implements AnotherInterface { + field: String + } + `); + const extendedSchema = extendSchema( + schema, + parse(` + extend interface AnotherInterface { + newField(test: Boolean): String + } + + extend type AnotherObject { + newField: String + } + `) + ); + expectJSON(validateSchema(extendedSchema)).toDeepEqual([ + { + message: + 'Interface field argument AnotherInterface.newField(test:) expected but AnotherObject.newField does not provide it.', + locations: [ + { line: 3, column: 20 }, + { line: 7, column: 11 }, + ], + }, + ]); + }); + + it('rejects Objects implementing the extended interface due to mismatching interface type', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: String + } + + type AnotherObject implements AnotherInterface { + field: String + } + `); + const extendedSchema = extendSchema( + schema, + parse(` + extend interface AnotherInterface { + newInterfaceField: NewInterface + } + + interface NewInterface { + newField: String + } + + interface MismatchingInterface { + newField: String + } + + extend type AnotherObject { + newInterfaceField: MismatchingInterface + } + + # Required to prevent unused interface errors + type DummyObject implements NewInterface & MismatchingInterface { + newField: String + } + `) + ); + expectJSON(validateSchema(extendedSchema)).toDeepEqual([ + { + message: + 'Interface field AnotherInterface.newInterfaceField expects type NewInterface but AnotherObject.newInterfaceField is type MismatchingInterface.', + locations: [ + { line: 3, column: 30 }, + { line: 15, column: 30 }, + ], + }, + ]); + }); +}); + +describe('Type System: Interface fields must have output types', () => { + function schemaWithInterfaceField(fieldConfig: GraphQLFieldConfig): GraphQLSchema { + const fields = { badField: fieldConfig }; + + const BadInterfaceType = new GraphQLInterfaceType({ + name: 'BadInterface', + fields, + }); + + const BadImplementingType = new GraphQLObjectType({ + name: 'BadImplementing', + interfaces: [BadInterfaceType], + fields, + }); + + return new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + f: { type: BadInterfaceType }, + }, + }), + types: [BadImplementingType, SomeObjectType], + }); + } + + for (const type of outputTypes) { + const typeName = inspect(type); + it(`accepts an output type as an Interface field type: ${typeName}`, () => { + const schema = schemaWithInterfaceField({ type }); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + } + + it('rejects an empty Interface field type', () => { + // @ts-expect-error (type field must not be undefined) + const schema = schemaWithInterfaceField({ type: undefined }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of BadImplementing.badField must be Output Type but got: undefined.', + }, + { + message: 'The type of BadInterface.badField must be Output Type but got: undefined.', + }, + ]); + }); + + for (const type of notOutputTypes) { + const typeStr = inspect(type); + it(`rejects a non-output type as an Interface field type: ${typeStr}`, () => { + // @ts-expect-error + const schema = schemaWithInterfaceField({ type }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: `The type of BadImplementing.badField must be Output Type but got: ${typeStr}.`, + }, + { + message: `The type of BadInterface.badField must be Output Type but got: ${typeStr}.`, + }, + ]); + }); + } + + it('rejects a non-type value as an Interface field type', () => { + // @ts-expect-error + const schema = schemaWithInterfaceField({ type: Number }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of BadImplementing.badField must be Output Type but got: [function Number].', + }, + { + message: 'The type of BadInterface.badField must be Output Type but got: [function Number].', + }, + { + message: 'Expected GraphQL named type but got: [function Number].', + }, + ]); + }); + + it('rejects a non-output type as an Interface field type with locations', () => { + const schema = buildSchema(` + type Query { + test: SomeInterface + } + + interface SomeInterface { + field: SomeInputObject + } + + input SomeInputObject { + foo: String + } + + type SomeObject implements SomeInterface { + field: SomeInputObject + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of SomeInterface.field must be Output Type but got: SomeInputObject.', + locations: [{ line: 7, column: 16 }], + }, + { + message: 'The type of SomeObject.field must be Output Type but got: SomeInputObject.', + locations: [{ line: 15, column: 16 }], + }, + ]); + }); + + it('accepts an interface not implemented by at least one object', () => { + const schema = buildSchema(` + type Query { + test: SomeInterface + } + + interface SomeInterface { + foo: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); +}); + +describe('Type System: Arguments must have input types', () => { + function schemaWithArg(argConfig: GraphQLArgumentConfig): GraphQLSchema { + const BadObjectType = new GraphQLObjectType({ + name: 'BadObject', + fields: { + badField: { + type: GraphQLString, + args: { + badArg: argConfig, + }, + }, + }, + }); + + return new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + f: { type: BadObjectType }, + }, + }), + directives: [ + new GraphQLDirective({ + name: 'BadDirective', + args: { + badArg: argConfig, + }, + locations: [DirectiveLocation.QUERY], + }), + ], + }); + } + + for (const type of inputTypes) { + const typeName = inspect(type); + it(`accepts an input type as a field arg type: ${typeName}`, () => { + const schema = schemaWithArg({ type }); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + } + + it('rejects an empty field arg type', () => { + // @ts-expect-error (type field must not be undefined) + const schema = schemaWithArg({ type: undefined }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of @BadDirective(badArg:) must be Input Type but got: undefined.', + }, + { + message: 'The type of BadObject.badField(badArg:) must be Input Type but got: undefined.', + }, + ]); + }); + + for (const type of notInputTypes) { + const typeStr = inspect(type); + it(`rejects a non-input type as a field arg type: ${typeStr}`, () => { + // @ts-expect-error + const schema = schemaWithArg({ type }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: `The type of @BadDirective(badArg:) must be Input Type but got: ${typeStr}.`, + }, + { + message: `The type of BadObject.badField(badArg:) must be Input Type but got: ${typeStr}.`, + }, + ]); + }); + } + + it('rejects a non-type value as a field arg type', () => { + // @ts-expect-error + const schema = schemaWithArg({ type: Number }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of @BadDirective(badArg:) must be Input Type but got: [function Number].', + }, + { + message: 'The type of BadObject.badField(badArg:) must be Input Type but got: [function Number].', + }, + { + message: 'Expected GraphQL named type but got: [function Number].', + }, + ]); + }); + + it('rejects a required argument that is deprecated', () => { + const schema = buildSchema(` + directive @BadDirective( + badArg: String! @deprecated + optionalArg: String @deprecated + anotherOptionalArg: String! = "" @deprecated + ) on FIELD + + type Query { + test( + badArg: String! @deprecated + optionalArg: String @deprecated + anotherOptionalArg: String! = "" @deprecated + ): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Required argument @BadDirective(badArg:) cannot be deprecated.', + locations: [ + { line: 3, column: 25 }, + { line: 3, column: 17 }, + ], + }, + { + message: 'Required argument Query.test(badArg:) cannot be deprecated.', + locations: [ + { line: 10, column: 27 }, + { line: 10, column: 19 }, + ], + }, + ]); + }); + + it('rejects a non-input type as a field arg with locations', () => { + const schema = buildSchema(` + type Query { + test(arg: SomeObject): String + } + + type SomeObject { + foo: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of Query.test(arg:) must be Input Type but got: SomeObject.', + locations: [{ line: 3, column: 19 }], + }, + ]); + }); +}); + +describe('Type System: Input Object fields must have input types', () => { + function schemaWithInputField(inputFieldConfig: GraphQLInputFieldConfig): GraphQLSchema { + const BadInputObjectType = new GraphQLInputObjectType({ + name: 'BadInputObject', + fields: { + badField: inputFieldConfig, + }, + }); + + return new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + f: { + type: GraphQLString, + args: { + badArg: { type: BadInputObjectType }, + }, + }, + }, + }), + }); + } + + for (const type of inputTypes) { + const typeName = inspect(type); + it(`accepts an input type as an input field type: ${typeName}`, () => { + const schema = schemaWithInputField({ type }); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + } + + it('rejects an empty input field type', () => { + // @ts-expect-error (type field must not be undefined) + const schema = schemaWithInputField({ type: undefined }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of BadInputObject.badField must be Input Type but got: undefined.', + }, + ]); + }); + + for (const type of notInputTypes) { + const typeStr = inspect(type); + it(`rejects a non-input type as an input field type: ${typeStr}`, () => { + // @ts-expect-error + const schema = schemaWithInputField({ type }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: `The type of BadInputObject.badField must be Input Type but got: ${typeStr}.`, + }, + ]); + }); + } + + it('rejects a non-type value as an input field type', () => { + // @ts-expect-error + const schema = schemaWithInputField({ type: Number }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of BadInputObject.badField must be Input Type but got: [function Number].', + }, + { + message: 'Expected GraphQL named type but got: [function Number].', + }, + ]); + }); + + it('rejects a non-input type as an input object field with locations', () => { + const schema = buildSchema(` + type Query { + test(arg: SomeInputObject): String + } + + input SomeInputObject { + foo: SomeObject + } + + type SomeObject { + bar: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'The type of SomeInputObject.foo must be Input Type but got: SomeObject.', + locations: [{ line: 7, column: 14 }], + }, + ]); + }); +}); + +describe('Objects must adhere to Interface they implement', () => { + it('accepts an Object which implements an Interface', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(input: String): String + } + + type AnotherObject implements AnotherInterface { + field(input: String): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('accepts an Object which implements an Interface along with more fields', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(input: String): String + } + + type AnotherObject implements AnotherInterface { + field(input: String): String + anotherField: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('accepts an Object which implements an Interface field along with additional optional arguments', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(input: String): String + } + + type AnotherObject implements AnotherInterface { + field(input: String, anotherInput: String): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Object missing an Interface field', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(input: String): String + } + + type AnotherObject implements AnotherInterface { + anotherField: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field AnotherInterface.field expected but AnotherObject does not provide it.', + locations: [ + { line: 7, column: 9 }, + { line: 10, column: 7 }, + ], + }, + ]); + }); + + it('rejects an Object with an incorrectly typed Interface field', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(input: String): String + } + + type AnotherObject implements AnotherInterface { + field(input: String): Int + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field AnotherInterface.field expects type String but AnotherObject.field is type Int.', + locations: [ + { line: 7, column: 31 }, + { line: 11, column: 31 }, + ], + }, + ]); + }); + + it('rejects an Object with a differently typed Interface field', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + type A { foo: String } + type B { foo: String } + + interface AnotherInterface { + field: A + } + + type AnotherObject implements AnotherInterface { + field: B + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field AnotherInterface.field expects type A but AnotherObject.field is type B.', + locations: [ + { line: 10, column: 16 }, + { line: 14, column: 16 }, + ], + }, + ]); + }); + + it('accepts an Object with a subtyped Interface field (interface)', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: AnotherInterface + } + + type AnotherObject implements AnotherInterface { + field: AnotherObject + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('accepts an Object with a subtyped Interface field (union)', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + type SomeObject { + field: String + } + + union SomeUnionType = SomeObject + + interface AnotherInterface { + field: SomeUnionType + } + + type AnotherObject implements AnotherInterface { + field: SomeObject + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Object missing an Interface argument', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(input: String): String + } + + type AnotherObject implements AnotherInterface { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Interface field argument AnotherInterface.field(input:) expected but AnotherObject.field does not provide it.', + locations: [ + { line: 7, column: 15 }, + { line: 11, column: 9 }, + ], + }, + ]); + }); + + it('rejects an Object with an incorrectly typed Interface argument', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(input: String): String + } + + type AnotherObject implements AnotherInterface { + field(input: Int): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Interface field argument AnotherInterface.field(input:) expects type String but AnotherObject.field(input:) is type Int.', + locations: [ + { line: 7, column: 22 }, + { line: 11, column: 22 }, + ], + }, + ]); + }); + + it('rejects an Object with both an incorrectly typed field and argument', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(input: String): String + } + + type AnotherObject implements AnotherInterface { + field(input: Int): Int + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field AnotherInterface.field expects type String but AnotherObject.field is type Int.', + locations: [ + { line: 7, column: 31 }, + { line: 11, column: 28 }, + ], + }, + { + message: + 'Interface field argument AnotherInterface.field(input:) expects type String but AnotherObject.field(input:) is type Int.', + locations: [ + { line: 7, column: 22 }, + { line: 11, column: 22 }, + ], + }, + ]); + }); + + it('rejects an Object which implements an Interface field along with additional required arguments', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field(baseArg: String): String + } + + type AnotherObject implements AnotherInterface { + field( + baseArg: String, + requiredArg: String! + optionalArg1: String, + optionalArg2: String = "", + ): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Object field AnotherObject.field includes required argument requiredArg that is missing from the Interface field AnotherInterface.field.', + locations: [ + { line: 13, column: 11 }, + { line: 7, column: 9 }, + ], + }, + ]); + }); + + it('accepts an Object with an equivalently wrapped Interface field type', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: [String]! + } + + type AnotherObject implements AnotherInterface { + field: [String]! + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Object with a non-list Interface field list type', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: [String] + } + + type AnotherObject implements AnotherInterface { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field AnotherInterface.field expects type [String] but AnotherObject.field is type String.', + locations: [ + { line: 7, column: 16 }, + { line: 11, column: 16 }, + ], + }, + ]); + }); + + it('rejects an Object with a list Interface field non-list type', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: String + } + + type AnotherObject implements AnotherInterface { + field: [String] + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field AnotherInterface.field expects type String but AnotherObject.field is type [String].', + locations: [ + { line: 7, column: 16 }, + { line: 11, column: 16 }, + ], + }, + ]); + }); + + it('accepts an Object with a subset non-null Interface field type', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: String + } + + type AnotherObject implements AnotherInterface { + field: String! + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Object with a superset nullable Interface field type', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface AnotherInterface { + field: String! + } + + type AnotherObject implements AnotherInterface { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field AnotherInterface.field expects type String! but AnotherObject.field is type String.', + locations: [ + { line: 7, column: 16 }, + { line: 11, column: 16 }, + ], + }, + ]); + }); + + it('rejects an Object missing a transitive interface', () => { + const schema = buildSchema(` + type Query { + test: AnotherObject + } + + interface SuperInterface { + field: String! + } + + interface AnotherInterface implements SuperInterface { + field: String! + } + + type AnotherObject implements AnotherInterface { + field: String! + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type AnotherObject must implement SuperInterface because it is implemented by AnotherInterface.', + locations: [ + { line: 10, column: 45 }, + { line: 14, column: 37 }, + ], + }, + ]); + }); +}); + +describe('Interfaces must adhere to Interface they implement', () => { + it('accepts an Interface which implements an Interface', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(input: String): String + } + + interface ChildInterface implements ParentInterface { + field(input: String): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('accepts an Interface which implements an Interface along with more fields', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(input: String): String + } + + interface ChildInterface implements ParentInterface { + field(input: String): String + anotherField: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('accepts an Interface which implements an Interface field along with additional optional arguments', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(input: String): String + } + + interface ChildInterface implements ParentInterface { + field(input: String, anotherInput: String): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Interface missing an Interface field', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(input: String): String + } + + interface ChildInterface implements ParentInterface { + anotherField: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field ParentInterface.field expected but ChildInterface does not provide it.', + locations: [ + { line: 7, column: 9 }, + { line: 10, column: 7 }, + ], + }, + ]); + }); + + it('rejects an Interface with an incorrectly typed Interface field', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(input: String): String + } + + interface ChildInterface implements ParentInterface { + field(input: String): Int + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field ParentInterface.field expects type String but ChildInterface.field is type Int.', + locations: [ + { line: 7, column: 31 }, + { line: 11, column: 31 }, + ], + }, + ]); + }); + + it('rejects an Interface with a differently typed Interface field', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + type A { foo: String } + type B { foo: String } + + interface ParentInterface { + field: A + } + + interface ChildInterface implements ParentInterface { + field: B + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field ParentInterface.field expects type A but ChildInterface.field is type B.', + locations: [ + { line: 10, column: 16 }, + { line: 14, column: 16 }, + ], + }, + ]); + }); + + it('accepts an Interface with a subtyped Interface field (interface)', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field: ParentInterface + } + + interface ChildInterface implements ParentInterface { + field: ChildInterface + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('accepts an Interface with a subtyped Interface field (union)', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + type SomeObject { + field: String + } + + union SomeUnionType = SomeObject + + interface ParentInterface { + field: SomeUnionType + } + + interface ChildInterface implements ParentInterface { + field: SomeObject + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Interface implementing a non-Interface type', () => { + const schema = buildSchema(` + type Query { + field: String + } + + input SomeInputObject { + field: String + } + + interface BadInterface implements SomeInputObject { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type BadInterface must only implement Interface types, it cannot implement SomeInputObject.', + locations: [{ line: 10, column: 41 }], + }, + ]); + }); + + it('rejects an Interface missing an Interface argument', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(input: String): String + } + + interface ChildInterface implements ParentInterface { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Interface field argument ParentInterface.field(input:) expected but ChildInterface.field does not provide it.', + locations: [ + { line: 7, column: 15 }, + { line: 11, column: 9 }, + ], + }, + ]); + }); + + it('rejects an Interface with an incorrectly typed Interface argument', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(input: String): String + } + + interface ChildInterface implements ParentInterface { + field(input: Int): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Interface field argument ParentInterface.field(input:) expects type String but ChildInterface.field(input:) is type Int.', + locations: [ + { line: 7, column: 22 }, + { line: 11, column: 22 }, + ], + }, + ]); + }); + + it('rejects an Interface with both an incorrectly typed field and argument', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(input: String): String + } + + interface ChildInterface implements ParentInterface { + field(input: Int): Int + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field ParentInterface.field expects type String but ChildInterface.field is type Int.', + locations: [ + { line: 7, column: 31 }, + { line: 11, column: 28 }, + ], + }, + { + message: + 'Interface field argument ParentInterface.field(input:) expects type String but ChildInterface.field(input:) is type Int.', + locations: [ + { line: 7, column: 22 }, + { line: 11, column: 22 }, + ], + }, + ]); + }); + + it('rejects an Interface which implements an Interface field along with additional required arguments', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field(baseArg: String): String + } + + interface ChildInterface implements ParentInterface { + field( + baseArg: String, + requiredArg: String! + optionalArg1: String, + optionalArg2: String = "", + ): String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: + 'Object field ChildInterface.field includes required argument requiredArg that is missing from the Interface field ParentInterface.field.', + locations: [ + { line: 13, column: 11 }, + { line: 7, column: 9 }, + ], + }, + ]); + }); + + it('accepts an Interface with an equivalently wrapped Interface field type', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field: [String]! + } + + interface ChildInterface implements ParentInterface { + field: [String]! + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Interface with a non-list Interface field list type', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field: [String] + } + + interface ChildInterface implements ParentInterface { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field ParentInterface.field expects type [String] but ChildInterface.field is type String.', + locations: [ + { line: 7, column: 16 }, + { line: 11, column: 16 }, + ], + }, + ]); + }); + + it('rejects an Interface with a list Interface field non-list type', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field: String + } + + interface ChildInterface implements ParentInterface { + field: [String] + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field ParentInterface.field expects type String but ChildInterface.field is type [String].', + locations: [ + { line: 7, column: 16 }, + { line: 11, column: 16 }, + ], + }, + ]); + }); + + it('accepts an Interface with a subset non-null Interface field type', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field: String + } + + interface ChildInterface implements ParentInterface { + field: String! + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([]); + }); + + it('rejects an Interface with a superset nullable Interface field type', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface ParentInterface { + field: String! + } + + interface ChildInterface implements ParentInterface { + field: String + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Interface field ParentInterface.field expects type String! but ChildInterface.field is type String.', + locations: [ + { line: 7, column: 16 }, + { line: 11, column: 16 }, + ], + }, + ]); + }); + + it('rejects an Object missing a transitive interface', () => { + const schema = buildSchema(` + type Query { + test: ChildInterface + } + + interface SuperInterface { + field: String! + } + + interface ParentInterface implements SuperInterface { + field: String! + } + + interface ChildInterface implements ParentInterface { + field: String! + } + `); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type ChildInterface must implement SuperInterface because it is implemented by ParentInterface.', + locations: [ + { line: 10, column: 44 }, + { line: 14, column: 43 }, + ], + }, + ]); + }); + + it('rejects a self reference interface', () => { + const schema = buildSchema(` + type Query { + test: FooInterface + } + + interface FooInterface implements FooInterface { + field: String + } + `); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type FooInterface cannot implement itself because it would create a circular reference.', + locations: [{ line: 6, column: 41 }], + }, + ]); + }); + + it('rejects a circular Interface implementation', () => { + const schema = buildSchema(` + type Query { + test: FooInterface + } + + interface FooInterface implements BarInterface { + field: String + } + + interface BarInterface implements FooInterface { + field: String + } + `); + + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Type FooInterface cannot implement BarInterface because it would create a circular reference.', + locations: [ + { line: 10, column: 41 }, + { line: 6, column: 41 }, + ], + }, + { + message: 'Type BarInterface cannot implement FooInterface because it would create a circular reference.', + locations: [ + { line: 6, column: 41 }, + { line: 10, column: 41 }, + ], + }, + ]); + }); +}); + +describe('assertValidSchema', () => { + it('does not throw on valid schemas', () => { + const schema = buildSchema(` + type Query { + foo: String + } + `); + expect(() => assertValidSchema(schema)).not.toThrow(); + }); + + it('combines multiple errors', () => { + const schema = buildSchema('type SomeType'); + expect(() => assertValidSchema(schema)).toThrow(dedent` + Query root type must be provided. + + Type SomeType must define one or more fields.`); + }); +}); diff --git a/packages/graphql/src/type/assertName.ts b/packages/graphql/src/type/assertName.ts new file mode 100644 index 00000000000..754dead3f05 --- /dev/null +++ b/packages/graphql/src/type/assertName.ts @@ -0,0 +1,36 @@ +import { GraphQLError } from '../error/GraphQLError.js'; + +import { isNameContinue, isNameStart } from '../language/characterClasses.js'; + +/** + * Upholds the spec rules about naming. + */ +export function assertName(name: string): string { + if (name.length === 0) { + throw new GraphQLError('Expected name to be a non-empty string.'); + } + + for (let i = 1; i < name.length; ++i) { + if (!isNameContinue(name.charCodeAt(i))) { + throw new GraphQLError(`Names must only contain [_a-zA-Z0-9] but "${name}" does not.`); + } + } + + if (!isNameStart(name.charCodeAt(0))) { + throw new GraphQLError(`Names must start with [_a-zA-Z] but "${name}" does not.`); + } + + return name; +} + +/** + * Upholds the spec rules about naming enum values. + * + * @internal + */ +export function assertEnumValueName(name: string): string { + if (name === 'true' || name === 'false' || name === 'null') { + throw new GraphQLError(`Enum values cannot be named: ${name}`); + } + return assertName(name); +} diff --git a/packages/graphql/src/type/definition.ts b/packages/graphql/src/type/definition.ts new file mode 100644 index 00000000000..20d8181a348 --- /dev/null +++ b/packages/graphql/src/type/definition.ts @@ -0,0 +1,1543 @@ +import { devAssert } from '../jsutils/devAssert.js'; +import { didYouMean } from '../jsutils/didYouMean.js'; +import { identityFunc } from '../jsutils/identityFunc.js'; +import { inspect } from '../jsutils/inspect.js'; +import { keyMap } from '../jsutils/keyMap.js'; +import { keyValMap } from '../jsutils/keyValMap.js'; +import { mapValue } from '../jsutils/mapValue.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; +import type { Path } from '../jsutils/Path.js'; +import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js'; +import { suggestionList } from '../jsutils/suggestionList.js'; +import { toObjMap } from '../jsutils/toObjMap.js'; + +import { GraphQLError } from '../error/GraphQLError.js'; + +import type { + EnumTypeDefinitionNode, + EnumTypeExtensionNode, + EnumValueDefinitionNode, + FieldDefinitionNode, + FieldNode, + FragmentDefinitionNode, + InputObjectTypeDefinitionNode, + InputObjectTypeExtensionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + InterfaceTypeExtensionNode, + ObjectTypeDefinitionNode, + ObjectTypeExtensionNode, + OperationDefinitionNode, + ScalarTypeDefinitionNode, + ScalarTypeExtensionNode, + UnionTypeDefinitionNode, + UnionTypeExtensionNode, + ValueNode, +} from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; +import { print } from '../language/printer.js'; + +import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped.js'; + +import { assertEnumValueName, assertName } from './assertName.js'; +import type { GraphQLSchema } from './schema.js'; + +// Predicates & Assertions + +/** + * These are all of the possible kinds of types. + */ +export type GraphQLType = GraphQLNamedType | GraphQLWrappingType; + +export function isType(type: unknown): type is GraphQLType { + return ( + isScalarType(type) || + isObjectType(type) || + isInterfaceType(type) || + isUnionType(type) || + isEnumType(type) || + isInputObjectType(type) || + isListType(type) || + isNonNullType(type) + ); +} + +export function assertType(type: unknown): GraphQLType { + if (!isType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL type.`); + } + return type; +} + +const isGraphQLScalarTypeSymbol = Symbol.for('GraphQLScalarType'); + +/** + * There are predicates for each kind of GraphQL type. + */ +export function isScalarType(type: unknown): type is GraphQLScalarType { + return typeof type === 'object' && type != null && isGraphQLScalarTypeSymbol in type; +} + +export function assertScalarType(type: unknown): GraphQLScalarType { + if (!isScalarType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL Scalar type.`); + } + return type; +} + +const isGraphQLObjectTypeSymbol = Symbol.for('GraphQLObjectType'); + +export function isObjectType(type: unknown): type is GraphQLObjectType { + return typeof type === 'object' && type != null && isGraphQLObjectTypeSymbol in type; +} + +export function assertObjectType(type: unknown): GraphQLObjectType { + if (!isObjectType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL Object type.`); + } + return type; +} + +const isGraphQLInterfaceTypeSymbol = Symbol.for('GraphQLInterfaceType'); + +export function isInterfaceType(type: unknown): type is GraphQLInterfaceType { + return typeof type === 'object' && type != null && isGraphQLInterfaceTypeSymbol in type; +} + +export function assertInterfaceType(type: unknown): GraphQLInterfaceType { + if (!isInterfaceType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL Interface type.`); + } + return type; +} + +const isGraphQLUnionTypeSymbol = Symbol.for('GraphQLUnionType'); + +export function isUnionType(type: unknown): type is GraphQLUnionType { + return typeof type === 'object' && type != null && isGraphQLUnionTypeSymbol in type; +} + +export function assertUnionType(type: unknown): GraphQLUnionType { + if (!isUnionType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL Union type.`); + } + return type; +} + +const isGraphQLEnumTypeSymbol = Symbol.for('GraphQLEnumType'); + +export function isEnumType(type: unknown): type is GraphQLEnumType { + return typeof type === 'object' && type != null && isGraphQLEnumTypeSymbol in type; +} + +export function assertEnumType(type: unknown): GraphQLEnumType { + if (!isEnumType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL Enum type.`); + } + return type; +} + +const isGraphQLInputObjectTypeSymbol = Symbol.for('GraphQLInputObjectType'); + +export function isInputObjectType(type: unknown): type is GraphQLInputObjectType { + return typeof type === 'object' && type != null && isGraphQLInputObjectTypeSymbol in type; +} + +export function assertInputObjectType(type: unknown): GraphQLInputObjectType { + if (!isInputObjectType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL Input Object type.`); + } + return type; +} + +const isGraphQLListTypeSymbol = Symbol.for('GraphQLListType'); + +export function isListType(type: GraphQLInputType): type is GraphQLList; +export function isListType(type: GraphQLOutputType): type is GraphQLList; +export function isListType(type: unknown): type is GraphQLList; +export function isListType(type: unknown): type is GraphQLList { + return typeof type === 'object' && type != null && isGraphQLListTypeSymbol in type; +} + +export function assertListType(type: unknown): GraphQLList { + if (!isListType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL List type.`); + } + return type; +} + +const isGraphQLNonNullTypeSymbol = Symbol.for('GraphQLNonNullType'); + +export function isNonNullType(type: GraphQLInputType): type is GraphQLNonNull; +export function isNonNullType(type: GraphQLOutputType): type is GraphQLNonNull; +export function isNonNullType(type: unknown): type is GraphQLNonNull; +export function isNonNullType(type: unknown): type is GraphQLNonNull { + return typeof type === 'object' && type != null && isGraphQLNonNullTypeSymbol in type; +} + +export function assertNonNullType(type: unknown): GraphQLNonNull { + if (!isNonNullType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL Non-Null type.`); + } + return type; +} + +/** + * These types may be used as input types for arguments and directives. + */ +export type GraphQLNullableInputType = GraphQLNamedInputType | GraphQLList; + +export type GraphQLInputType = GraphQLNullableInputType | GraphQLNonNull; + +export function isInputType(type: unknown): type is GraphQLInputType { + return ( + isScalarType(type) || + isEnumType(type) || + isInputObjectType(type) || + (isWrappingType(type) && isInputType(type.ofType)) + ); +} + +export function assertInputType(type: unknown): GraphQLInputType { + if (!isInputType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL input type.`); + } + return type; +} + +/** + * These types may be used as output types as the result of fields. + */ +export type GraphQLNullableOutputType = GraphQLNamedOutputType | GraphQLList; + +export type GraphQLOutputType = GraphQLNullableOutputType | GraphQLNonNull; + +export function isOutputType(type: unknown): type is GraphQLOutputType { + return ( + isScalarType(type) || + isObjectType(type) || + isInterfaceType(type) || + isUnionType(type) || + isEnumType(type) || + (isWrappingType(type) && isOutputType(type.ofType)) + ); +} + +export function assertOutputType(type: unknown): GraphQLOutputType { + if (!isOutputType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL output type.`); + } + return type; +} + +/** + * These types may describe types which may be leaf values. + */ +export type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType; + +export function isLeafType(type: unknown): type is GraphQLLeafType { + return isScalarType(type) || isEnumType(type); +} + +export function assertLeafType(type: unknown): GraphQLLeafType { + if (!isLeafType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL leaf type.`); + } + return type; +} + +/** + * These types may describe the parent context of a selection set. + */ +export type GraphQLCompositeType = GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType; + +export function isCompositeType(type: unknown): type is GraphQLCompositeType { + return isObjectType(type) || isInterfaceType(type) || isUnionType(type); +} + +export function assertCompositeType(type: unknown): GraphQLCompositeType { + if (!isCompositeType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL composite type.`); + } + return type; +} + +/** + * These types may describe the parent context of a selection set. + */ +export type GraphQLAbstractType = GraphQLInterfaceType | GraphQLUnionType; + +export function isAbstractType(type: unknown): type is GraphQLAbstractType { + return isInterfaceType(type) || isUnionType(type); +} + +export function assertAbstractType(type: unknown): GraphQLAbstractType { + if (!isAbstractType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL abstract type.`); + } + return type; +} + +/** + * List Type Wrapper + * + * A list is a wrapping type which points to another type. + * Lists are often created within the context of defining the fields of + * an object type. + * + * Example: + * + * ```ts + * const PersonType = new GraphQLObjectType({ + * name: 'Person', + * fields: () => ({ + * parents: { type: new GraphQLList(PersonType) }, + * children: { type: new GraphQLList(PersonType) }, + * }) + * }) + * ``` + */ +export class GraphQLList { + readonly [isGraphQLListTypeSymbol]: true = true; + readonly ofType: T; + + constructor(ofType: T) { + this.ofType = ofType; + } + + get [Symbol.toStringTag]() { + return 'GraphQLList'; + } + + toString(): string { + return '[' + String(this.ofType) + ']'; + } + + toJSON(): string { + return this.toString(); + } +} + +/** + * Non-Null Type Wrapper + * + * A non-null is a wrapping type which points to another type. + * Non-null types enforce that their values are never null and can ensure + * an error is raised if this ever occurs during a request. It is useful for + * fields which you can make a strong guarantee on non-nullability, for example + * usually the id field of a database row will never be null. + * + * Example: + * + * ```ts + * const RowType = new GraphQLObjectType({ + * name: 'Row', + * fields: () => ({ + * id: { type: new GraphQLNonNull(GraphQLString) }, + * }) + * }) + * ``` + * Note: the enforcement of non-nullability occurs within the executor. + */ +export class GraphQLNonNull { + readonly [isGraphQLNonNullTypeSymbol]: true = true; + readonly ofType: T; + + constructor(ofType: T) { + this.ofType = ofType; + } + + get [Symbol.toStringTag]() { + return 'GraphQLNonNull'; + } + + toString(): string { + return String(this.ofType) + '!'; + } + + toJSON(): string { + return this.toString(); + } +} + +/** + * These types wrap and modify other types + */ + +export type GraphQLWrappingType = GraphQLList | GraphQLNonNull; + +export function isWrappingType(type: unknown): type is GraphQLWrappingType { + return isListType(type) || isNonNullType(type); +} + +export function assertWrappingType(type: unknown): GraphQLWrappingType { + if (!isWrappingType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL wrapping type.`); + } + return type; +} + +/** + * These types can all accept null as a value. + */ +export type GraphQLNullableType = GraphQLNamedType | GraphQLList; + +export function isNullableType(type: unknown): type is GraphQLNullableType { + return isType(type) && !isNonNullType(type); +} + +export function assertNullableType(type: unknown): GraphQLNullableType { + if (!isNullableType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL nullable type.`); + } + return type; +} + +export function getNullableType(type: undefined | null): void; +export function getNullableType(type: T | GraphQLNonNull): T; +export function getNullableType(type: Maybe): GraphQLNullableType | undefined; +export function getNullableType(type: Maybe): GraphQLNullableType | undefined { + if (type) { + return isNonNullType(type) ? type.ofType : type; + } + return undefined; +} + +/** + * These named types do not include modifiers like List or NonNull. + */ +export type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType; + +export type GraphQLNamedInputType = GraphQLScalarType | GraphQLEnumType | GraphQLInputObjectType; + +export type GraphQLNamedOutputType = + | GraphQLScalarType + | GraphQLObjectType + | GraphQLInterfaceType + | GraphQLUnionType + | GraphQLEnumType; + +export function isNamedType(type: unknown): type is GraphQLNamedType { + return ( + isScalarType(type) || + isObjectType(type) || + isInterfaceType(type) || + isUnionType(type) || + isEnumType(type) || + isInputObjectType(type) + ); +} + +export function assertNamedType(type: unknown): GraphQLNamedType { + if (!isNamedType(type)) { + throw new Error(`Expected ${inspect(type)} to be a GraphQL named type.`); + } + return type; +} + +export function getNamedType(type: undefined | null): void; +export function getNamedType(type: GraphQLInputType): GraphQLNamedInputType; +export function getNamedType(type: GraphQLOutputType): GraphQLNamedOutputType; +export function getNamedType(type: GraphQLType): GraphQLNamedType; +export function getNamedType(type: Maybe): GraphQLNamedType | undefined; +export function getNamedType(type: Maybe): GraphQLNamedType | undefined { + if (type) { + let unwrappedType = type; + while (isWrappingType(unwrappedType)) { + unwrappedType = unwrappedType.ofType; + } + return unwrappedType; + } + return undefined; +} + +/** + * Used while defining GraphQL types to allow for circular references in + * otherwise immutable type definitions. + */ +export type ThunkReadonlyArray = (() => ReadonlyArray) | ReadonlyArray; +export type ThunkObjMap = (() => ObjMap) | ObjMap; + +export function resolveReadonlyArrayThunk(thunk: ThunkReadonlyArray): ReadonlyArray { + return typeof thunk === 'function' ? thunk() : thunk; +} + +export function resolveObjMapThunk(thunk: ThunkObjMap): ObjMap { + return typeof thunk === 'function' ? thunk() : thunk; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLScalarTypeExtensions { + [attributeName: string]: unknown; +} + +/** + * Scalar Type Definition + * + * The leaf values of any request and input values to arguments are + * Scalars (or Enums) and are defined with a name and a series of functions + * used to parse input from ast or variables and to ensure validity. + * + * If a type's serialize function returns `null` or does not return a value + * (i.e. it returns `undefined`) then an error will be raised and a `null` + * value will be returned in the response. It is always better to validate + * + * Example: + * + * ```ts + * const OddType = new GraphQLScalarType({ + * name: 'Odd', + * serialize(value) { + * if (!Number.isFinite(value)) { + * throw new Error( + * `Scalar "Odd" cannot represent "${value}" since it is not a finite number.`, + * ); + * } + * + * if (value % 2 === 0) { + * throw new Error(`Scalar "Odd" cannot represent "${value}" since it is even.`); + * } + * return value; + * } + * }); + * ``` + */ +export class GraphQLScalarType { + readonly [isGraphQLScalarTypeSymbol]: true = true; + name: string; + description: Maybe; + specifiedByURL: Maybe; + serialize: GraphQLScalarSerializer; + parseValue: GraphQLScalarValueParser; + parseLiteral: GraphQLScalarLiteralParser; + extensions: Readonly; + astNode: Maybe; + extensionASTNodes: ReadonlyArray; + + constructor(config: Readonly>) { + const parseValue = config.parseValue ?? (identityFunc as GraphQLScalarValueParser); + + this.name = assertName(config.name); + this.description = config.description; + this.specifiedByURL = config.specifiedByURL; + this.serialize = config.serialize ?? (identityFunc as GraphQLScalarSerializer); + this.parseValue = parseValue; + this.parseLiteral = config.parseLiteral ?? ((node, variables) => parseValue(valueFromASTUntyped(node, variables))); + this.extensions = toObjMap(config.extensions); + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes ?? []; + + if (config.parseLiteral) { + devAssert( + typeof config.parseValue === 'function' && typeof config.parseLiteral === 'function', + `${this.name} must provide both "parseValue" and "parseLiteral" functions.` + ); + } + } + + get [Symbol.toStringTag]() { + return 'GraphQLScalarType'; + } + + toConfig(): GraphQLScalarTypeNormalizedConfig { + return { + name: this.name, + description: this.description, + specifiedByURL: this.specifiedByURL, + serialize: this.serialize, + parseValue: this.parseValue, + parseLiteral: this.parseLiteral, + extensions: this.extensions, + astNode: this.astNode, + extensionASTNodes: this.extensionASTNodes, + }; + } + + toString(): string { + return this.name; + } + + toJSON(): string { + return this.toString(); + } +} + +export type GraphQLScalarSerializer = (outputValue: unknown) => TExternal; + +export type GraphQLScalarValueParser = (inputValue: unknown) => TInternal; + +export type GraphQLScalarLiteralParser = ( + valueNode: ValueNode, + variables?: Maybe> +) => TInternal; + +export interface GraphQLScalarTypeConfig { + name: string; + description?: Maybe; + specifiedByURL?: Maybe; + /** Serializes an internal value to include in a response. */ + serialize?: GraphQLScalarSerializer; + /** Parses an externally provided value to use as an input. */ + parseValue?: GraphQLScalarValueParser; + /** Parses an externally provided literal value to use as an input. */ + parseLiteral?: GraphQLScalarLiteralParser; + extensions?: Maybe>; + astNode?: Maybe; + extensionASTNodes?: Maybe>; +} + +interface GraphQLScalarTypeNormalizedConfig + extends GraphQLScalarTypeConfig { + serialize: GraphQLScalarSerializer; + parseValue: GraphQLScalarValueParser; + parseLiteral: GraphQLScalarLiteralParser; + extensions: Readonly; + extensionASTNodes: ReadonlyArray; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + * + * We've provided these template arguments because this is an open type and + * you may find them useful. + */ +export interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> { + [attributeName: string]: unknown; +} + +/** + * Object Type Definition + * + * Almost all of the GraphQL types you define will be object types. Object types + * have a name, but most importantly describe their fields. + * + * Example: + * + * ```ts + * const AddressType = new GraphQLObjectType({ + * name: 'Address', + * fields: { + * street: { type: GraphQLString }, + * number: { type: GraphQLInt }, + * formatted: { + * type: GraphQLString, + * resolve(obj) { + * return obj.number + ' ' + obj.street + * } + * } + * } + * }); + * ``` + * + * When two types need to refer to each other, or a type needs to refer to + * itself in a field, you can use a function expression (aka a closure or a + * thunk) to supply the fields lazily. + * + * Example: + * + * ```ts + * const PersonType = new GraphQLObjectType({ + * name: 'Person', + * fields: () => ({ + * name: { type: GraphQLString }, + * bestFriend: { type: PersonType }, + * }) + * }); + * ``` + */ +export class GraphQLObjectType { + readonly [isGraphQLObjectTypeSymbol]: true = true; + name: string; + description: Maybe; + isTypeOf: Maybe>; + extensions: Readonly>; + astNode: Maybe; + extensionASTNodes: ReadonlyArray; + + private _fields: ThunkObjMap>; + private _interfaces: ThunkReadonlyArray; + + constructor(config: Readonly>) { + this.name = assertName(config.name); + this.description = config.description; + this.isTypeOf = config.isTypeOf; + this.extensions = toObjMap(config.extensions); + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes ?? []; + + this._fields = () => defineFieldMap(config); + this._interfaces = () => defineInterfaces(config); + } + + get [Symbol.toStringTag]() { + return 'GraphQLObjectType'; + } + + getFields(): GraphQLFieldMap { + if (typeof this._fields === 'function') { + this._fields = this._fields(); + } + return this._fields; + } + + getInterfaces(): ReadonlyArray { + if (typeof this._interfaces === 'function') { + this._interfaces = this._interfaces(); + } + return this._interfaces; + } + + toConfig(): GraphQLObjectTypeNormalizedConfig { + return { + name: this.name, + description: this.description, + interfaces: this.getInterfaces(), + fields: fieldsToFieldsConfig(this.getFields()), + isTypeOf: this.isTypeOf, + extensions: this.extensions, + astNode: this.astNode, + extensionASTNodes: this.extensionASTNodes, + }; + } + + toString(): string { + return this.name; + } + + toJSON(): string { + return this.toString(); + } +} + +function defineInterfaces( + config: Readonly | GraphQLInterfaceTypeConfig> +): ReadonlyArray { + return resolveReadonlyArrayThunk(config.interfaces ?? []); +} + +function defineFieldMap( + config: Readonly | GraphQLInterfaceTypeConfig> +): GraphQLFieldMap { + const fieldMap = resolveObjMapThunk(config.fields); + + return mapValue(fieldMap, (fieldConfig, fieldName) => { + const argsConfig = fieldConfig.args ?? {}; + return { + name: assertName(fieldName), + description: fieldConfig.description, + type: fieldConfig.type, + args: defineArguments(argsConfig), + resolve: fieldConfig.resolve, + subscribe: fieldConfig.subscribe, + deprecationReason: fieldConfig.deprecationReason, + extensions: toObjMap(fieldConfig.extensions), + astNode: fieldConfig.astNode, + }; + }); +} + +export function defineArguments(config: GraphQLFieldConfigArgumentMap): ReadonlyArray { + return Object.entries(config).map(([argName, argConfig]) => ({ + name: assertName(argName), + description: argConfig.description, + type: argConfig.type, + defaultValue: argConfig.defaultValue, + deprecationReason: argConfig.deprecationReason, + extensions: toObjMap(argConfig.extensions), + astNode: argConfig.astNode, + })); +} + +function fieldsToFieldsConfig( + fields: GraphQLFieldMap +): GraphQLFieldConfigMap { + return mapValue(fields, field => ({ + description: field.description, + type: field.type, + args: argsToArgsConfig(field.args), + resolve: field.resolve, + subscribe: field.subscribe, + deprecationReason: field.deprecationReason, + extensions: field.extensions, + astNode: field.astNode, + })); +} + +/** + * @internal + */ +export function argsToArgsConfig(args: ReadonlyArray): GraphQLFieldConfigArgumentMap { + return keyValMap( + args, + arg => arg.name, + arg => ({ + description: arg.description, + type: arg.type, + defaultValue: arg.defaultValue, + deprecationReason: arg.deprecationReason, + extensions: arg.extensions, + astNode: arg.astNode, + }) + ); +} + +export interface GraphQLObjectTypeConfig { + name: string; + description?: Maybe; + interfaces?: ThunkReadonlyArray; + fields: ThunkObjMap>; + isTypeOf?: Maybe>; + extensions?: Maybe>>; + astNode?: Maybe; + extensionASTNodes?: Maybe>; +} + +interface GraphQLObjectTypeNormalizedConfig extends GraphQLObjectTypeConfig { + interfaces: ReadonlyArray; + fields: GraphQLFieldConfigMap; + extensions: Readonly>; + extensionASTNodes: ReadonlyArray; +} + +export type GraphQLTypeResolver = ( + value: TSource, + context: TContext, + info: GraphQLResolveInfo, + abstractType: GraphQLAbstractType +) => PromiseOrValue; + +export type GraphQLIsTypeOfFn = ( + source: TSource, + context: TContext, + info: GraphQLResolveInfo +) => PromiseOrValue; + +export type GraphQLFieldResolver = ( + source: TSource, + args: TArgs, + context: TContext, + info: GraphQLResolveInfo +) => TResult; + +export interface GraphQLResolveInfo { + readonly fieldName: string; + readonly fieldNodes: ReadonlyArray; + readonly returnType: GraphQLOutputType; + readonly parentType: GraphQLObjectType; + readonly path: Path; + readonly schema: GraphQLSchema; + readonly fragments: ObjMap; + readonly rootValue: unknown; + readonly operation: OperationDefinitionNode; + readonly variableValues: { [variable: string]: unknown }; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + * + * We've provided these template arguments because this is an open type and + * you may find them useful. + */ +export interface GraphQLFieldExtensions<_TSource, _TContext, _TArgs = any> { + [attributeName: string]: unknown; +} + +export interface GraphQLFieldConfig { + description?: Maybe; + type: GraphQLOutputType; + args?: GraphQLFieldConfigArgumentMap; + resolve?: GraphQLFieldResolver; + subscribe?: GraphQLFieldResolver; + deprecationReason?: Maybe; + extensions?: Maybe>>; + astNode?: Maybe; +} + +export type GraphQLFieldConfigArgumentMap = ObjMap; + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLArgumentExtensions { + [attributeName: string]: unknown; +} + +export interface GraphQLArgumentConfig { + description?: Maybe; + type: GraphQLInputType; + defaultValue?: unknown; + deprecationReason?: Maybe; + extensions?: Maybe>; + astNode?: Maybe; +} + +export type GraphQLFieldConfigMap = ObjMap>; + +export interface GraphQLField { + name: string; + description: Maybe; + type: GraphQLOutputType; + args: ReadonlyArray; + resolve?: GraphQLFieldResolver; + subscribe?: GraphQLFieldResolver; + deprecationReason: Maybe; + extensions: Readonly>; + astNode: Maybe; +} + +export interface GraphQLArgument { + name: string; + description: Maybe; + type: GraphQLInputType; + defaultValue: unknown; + deprecationReason: Maybe; + extensions: Readonly; + astNode: Maybe; +} + +export function isRequiredArgument(arg: GraphQLArgument): boolean { + return isNonNullType(arg.type) && arg.defaultValue === undefined; +} + +export type GraphQLFieldMap = ObjMap>; + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLInterfaceTypeExtensions { + [attributeName: string]: unknown; +} + +/** + * Interface Type Definition + * + * When a field can return one of a heterogeneous set of types, a Interface type + * is used to describe what types are possible, what fields are in common across + * all types, as well as a function to determine which type is actually used + * when the field is resolved. + * + * Example: + * + * ```ts + * const EntityType = new GraphQLInterfaceType({ + * name: 'Entity', + * fields: { + * name: { type: GraphQLString } + * } + * }); + * ``` + */ +export class GraphQLInterfaceType { + readonly [isGraphQLInterfaceTypeSymbol]: true = true; + name: string; + description: Maybe; + resolveType: Maybe>; + extensions: Readonly; + astNode: Maybe; + extensionASTNodes: ReadonlyArray; + + private _fields: ThunkObjMap>; + private _interfaces: ThunkReadonlyArray; + + constructor(config: Readonly>) { + this.name = assertName(config.name); + this.description = config.description; + this.resolveType = config.resolveType; + this.extensions = toObjMap(config.extensions); + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes ?? []; + + this._fields = defineFieldMap.bind(undefined, config); + this._interfaces = defineInterfaces.bind(undefined, config); + } + + get [Symbol.toStringTag]() { + return 'GraphQLInterfaceType'; + } + + getFields(): GraphQLFieldMap { + if (typeof this._fields === 'function') { + this._fields = this._fields(); + } + return this._fields; + } + + getInterfaces(): ReadonlyArray { + if (typeof this._interfaces === 'function') { + this._interfaces = this._interfaces(); + } + return this._interfaces; + } + + toConfig(): GraphQLInterfaceTypeNormalizedConfig { + return { + name: this.name, + description: this.description, + interfaces: this.getInterfaces(), + fields: fieldsToFieldsConfig(this.getFields()), + resolveType: this.resolveType, + extensions: this.extensions, + astNode: this.astNode, + extensionASTNodes: this.extensionASTNodes, + }; + } + + toString(): string { + return this.name; + } + + toJSON(): string { + return this.toString(); + } +} + +export interface GraphQLInterfaceTypeConfig { + name: string; + description?: Maybe; + interfaces?: ThunkReadonlyArray; + fields: ThunkObjMap>; + /** + * Optionally provide a custom type resolver function. If one is not provided, + * the default implementation will call `isTypeOf` on each implementing + * Object type. + */ + resolveType?: Maybe>; + extensions?: Maybe>; + astNode?: Maybe; + extensionASTNodes?: Maybe>; +} + +export interface GraphQLInterfaceTypeNormalizedConfig extends GraphQLInterfaceTypeConfig { + interfaces: ReadonlyArray; + fields: GraphQLFieldConfigMap; + extensions: Readonly; + extensionASTNodes: ReadonlyArray; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLUnionTypeExtensions { + [attributeName: string]: unknown; +} + +/** + * Union Type Definition + * + * When a field can return one of a heterogeneous set of types, a Union type + * is used to describe what types are possible as well as providing a function + * to determine which type is actually used when the field is resolved. + * + * Example: + * + * ```ts + * const PetType = new GraphQLUnionType({ + * name: 'Pet', + * types: [ DogType, CatType ], + * resolveType(value) { + * if (value instanceof Dog) { + * return DogType; + * } + * if (value instanceof Cat) { + * return CatType; + * } + * } + * }); + * ``` + */ +export class GraphQLUnionType { + readonly [isGraphQLUnionTypeSymbol]: true = true; + name: string; + description: Maybe; + resolveType: Maybe>; + extensions: Readonly; + astNode: Maybe; + extensionASTNodes: ReadonlyArray; + + private _types: ThunkReadonlyArray; + + constructor(config: Readonly>) { + this.name = assertName(config.name); + this.description = config.description; + this.resolveType = config.resolveType; + this.extensions = toObjMap(config.extensions); + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes ?? []; + + this._types = defineTypes.bind(undefined, config); + } + + get [Symbol.toStringTag]() { + return 'GraphQLUnionType'; + } + + getTypes(): ReadonlyArray { + if (typeof this._types === 'function') { + this._types = this._types(); + } + return this._types; + } + + toConfig(): GraphQLUnionTypeNormalizedConfig { + return { + name: this.name, + description: this.description, + types: this.getTypes(), + resolveType: this.resolveType, + extensions: this.extensions, + astNode: this.astNode, + extensionASTNodes: this.extensionASTNodes, + }; + } + + toString(): string { + return this.name; + } + + toJSON(): string { + return this.toString(); + } +} + +function defineTypes(config: Readonly>): ReadonlyArray { + return resolveReadonlyArrayThunk(config.types); +} + +export interface GraphQLUnionTypeConfig { + name: string; + description?: Maybe; + types: ThunkReadonlyArray; + /** + * Optionally provide a custom type resolver function. If one is not provided, + * the default implementation will call `isTypeOf` on each implementing + * Object type. + */ + resolveType?: Maybe>; + extensions?: Maybe>; + astNode?: Maybe; + extensionASTNodes?: Maybe>; +} + +interface GraphQLUnionTypeNormalizedConfig extends GraphQLUnionTypeConfig { + types: ReadonlyArray; + extensions: Readonly; + extensionASTNodes: ReadonlyArray; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLEnumTypeExtensions { + [attributeName: string]: unknown; +} + +/** + * Enum Type Definition + * + * Some leaf values of requests and input values are Enums. GraphQL serializes + * Enum values as strings, however internally Enums can be represented by any + * kind of type, often integers. + * + * Example: + * + * ```ts + * const RGBType = new GraphQLEnumType({ + * name: 'RGB', + * values: { + * RED: { value: 0 }, + * GREEN: { value: 1 }, + * BLUE: { value: 2 } + * } + * }); + * ``` + * + * Note: If a value is not provided in a definition, the name of the enum value + * will be used as its internal value. + */ +export class GraphQLEnumType /* */ { + readonly [isGraphQLEnumTypeSymbol]: true = true; + name: string; + description: Maybe; + extensions: Readonly; + astNode: Maybe; + extensionASTNodes: ReadonlyArray; + + private _values: ReadonlyArray */>; + private _valueLookup: ReadonlyMap; + private _nameLookup: ObjMap; + + constructor(config: Readonly */>) { + this.name = assertName(config.name); + this.description = config.description; + this.extensions = toObjMap(config.extensions); + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes ?? []; + + this._values = Object.entries(config.values).map(([valueName, valueConfig]) => ({ + name: assertEnumValueName(valueName), + description: valueConfig.description, + value: valueConfig.value !== undefined ? valueConfig.value : valueName, + deprecationReason: valueConfig.deprecationReason, + extensions: toObjMap(valueConfig.extensions), + astNode: valueConfig.astNode, + })); + this._valueLookup = new Map(this._values.map(enumValue => [enumValue.value, enumValue])); + this._nameLookup = keyMap(this._values, value => value.name); + } + + get [Symbol.toStringTag]() { + return 'GraphQLEnumType'; + } + + getValues(): ReadonlyArray */> { + return this._values; + } + + getValue(name: string): Maybe { + return this._nameLookup[name]; + } + + serialize(outputValue: unknown /* T */): Maybe { + const enumValue = this._valueLookup.get(outputValue); + if (enumValue === undefined) { + throw new GraphQLError(`Enum "${this.name}" cannot represent value: ${inspect(outputValue)}`); + } + return enumValue.name; + } + + parseValue(inputValue: unknown): Maybe /* T */ { + if (typeof inputValue !== 'string') { + const valueStr = inspect(inputValue); + throw new GraphQLError( + `Enum "${this.name}" cannot represent non-string value: ${valueStr}.` + didYouMeanEnumValue(this, valueStr) + ); + } + + const enumValue = this.getValue(inputValue); + if (enumValue == null) { + throw new GraphQLError( + `Value "${inputValue}" does not exist in "${this.name}" enum.` + didYouMeanEnumValue(this, inputValue) + ); + } + return enumValue.value; + } + + parseLiteral(valueNode: ValueNode, _variables: Maybe>): Maybe /* T */ { + // Note: variables will be resolved to a value before calling this function. + if (valueNode.kind !== Kind.ENUM) { + const valueStr = print(valueNode); + throw new GraphQLError( + `Enum "${this.name}" cannot represent non-enum value: ${valueStr}.` + didYouMeanEnumValue(this, valueStr), + { nodes: valueNode } + ); + } + + const enumValue = this.getValue(valueNode.value); + if (enumValue == null) { + const valueStr = print(valueNode); + throw new GraphQLError( + `Value "${valueStr}" does not exist in "${this.name}" enum.` + didYouMeanEnumValue(this, valueStr), + { nodes: valueNode } + ); + } + return enumValue.value; + } + + toConfig(): GraphQLEnumTypeNormalizedConfig { + const values = keyValMap( + this.getValues(), + value => value.name, + value => ({ + description: value.description, + value: value.value, + deprecationReason: value.deprecationReason, + extensions: value.extensions, + astNode: value.astNode, + }) + ); + + return { + name: this.name, + description: this.description, + values, + extensions: this.extensions, + astNode: this.astNode, + extensionASTNodes: this.extensionASTNodes, + }; + } + + toString(): string { + return this.name; + } + + toJSON(): string { + return this.toString(); + } +} + +function didYouMeanEnumValue(enumType: GraphQLEnumType, unknownValueStr: string): string { + const allNames = enumType.getValues().map(value => value.name); + const suggestedValues = suggestionList(unknownValueStr, allNames); + + return didYouMean('the enum value', suggestedValues); +} + +export interface GraphQLEnumTypeConfig { + name: string; + description?: Maybe; + values: GraphQLEnumValueConfigMap /* */; + extensions?: Maybe>; + astNode?: Maybe; + extensionASTNodes?: Maybe>; +} + +interface GraphQLEnumTypeNormalizedConfig extends GraphQLEnumTypeConfig { + extensions: Readonly; + extensionASTNodes: ReadonlyArray; +} + +export type GraphQLEnumValueConfigMap /* */ = ObjMap */>; + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLEnumValueExtensions { + [attributeName: string]: unknown; +} + +export interface GraphQLEnumValueConfig { + description?: Maybe; + value?: any /* T */; + deprecationReason?: Maybe; + extensions?: Maybe>; + astNode?: Maybe; +} + +export interface GraphQLEnumValue { + name: string; + description: Maybe; + value: any /* T */; + deprecationReason: Maybe; + extensions: Readonly; + astNode: Maybe; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLInputObjectTypeExtensions { + [attributeName: string]: unknown; +} + +/** + * Input Object Type Definition + * + * An input object defines a structured collection of fields which may be + * supplied to a field argument. + * + * Using `NonNull` will ensure that a value must be provided by the query + * + * Example: + * + * ```ts + * const GeoPoint = new GraphQLInputObjectType({ + * name: 'GeoPoint', + * fields: { + * lat: { type: new GraphQLNonNull(GraphQLFloat) }, + * lon: { type: new GraphQLNonNull(GraphQLFloat) }, + * alt: { type: GraphQLFloat, defaultValue: 0 }, + * } + * }); + * ``` + */ +export class GraphQLInputObjectType { + readonly [isGraphQLInputObjectTypeSymbol]: true = true; + name: string; + description: Maybe; + extensions: Readonly; + astNode: Maybe; + extensionASTNodes: ReadonlyArray; + + private _fields: ThunkObjMap; + + constructor(config: Readonly) { + this.name = assertName(config.name); + this.description = config.description; + this.extensions = toObjMap(config.extensions); + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes ?? []; + + this._fields = defineInputFieldMap.bind(undefined, config); + } + + get [Symbol.toStringTag]() { + return 'GraphQLInputObjectType'; + } + + getFields(): GraphQLInputFieldMap { + if (typeof this._fields === 'function') { + this._fields = this._fields(); + } + return this._fields; + } + + toConfig(): GraphQLInputObjectTypeNormalizedConfig { + const fields = mapValue(this.getFields(), field => ({ + description: field.description, + type: field.type, + defaultValue: field.defaultValue, + deprecationReason: field.deprecationReason, + extensions: field.extensions, + astNode: field.astNode, + })); + + return { + name: this.name, + description: this.description, + fields, + extensions: this.extensions, + astNode: this.astNode, + extensionASTNodes: this.extensionASTNodes, + }; + } + + toString(): string { + return this.name; + } + + toJSON(): string { + return this.toString(); + } +} + +function defineInputFieldMap(config: Readonly): GraphQLInputFieldMap { + const fieldMap = resolveObjMapThunk(config.fields); + return mapValue(fieldMap, (fieldConfig, fieldName) => { + devAssert( + !('resolve' in fieldConfig), + `${config.name}.${fieldName} field has a resolve property, but Input Types cannot define resolvers.` + ); + + return { + name: assertName(fieldName), + description: fieldConfig.description, + type: fieldConfig.type, + defaultValue: fieldConfig.defaultValue, + deprecationReason: fieldConfig.deprecationReason, + extensions: toObjMap(fieldConfig.extensions), + astNode: fieldConfig.astNode, + }; + }); +} + +export interface GraphQLInputObjectTypeConfig { + name: string; + description?: Maybe; + fields: ThunkObjMap; + extensions?: Maybe>; + astNode?: Maybe; + extensionASTNodes?: Maybe>; +} + +interface GraphQLInputObjectTypeNormalizedConfig extends GraphQLInputObjectTypeConfig { + fields: GraphQLInputFieldConfigMap; + extensions: Readonly; + extensionASTNodes: ReadonlyArray; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLInputFieldExtensions { + [attributeName: string]: unknown; +} + +export interface GraphQLInputFieldConfig { + description?: Maybe; + type: GraphQLInputType; + defaultValue?: unknown; + deprecationReason?: Maybe; + extensions?: Maybe>; + astNode?: Maybe; +} + +export type GraphQLInputFieldConfigMap = ObjMap; + +export interface GraphQLInputField { + name: string; + description: Maybe; + type: GraphQLInputType; + defaultValue: unknown; + deprecationReason: Maybe; + extensions: Readonly; + astNode: Maybe; +} + +export function isRequiredInputField(field: GraphQLInputField): boolean { + return isNonNullType(field.type) && field.defaultValue === undefined; +} + +export type GraphQLInputFieldMap = ObjMap; diff --git a/packages/graphql/src/type/directives.ts b/packages/graphql/src/type/directives.ts new file mode 100644 index 00000000000..cdadfabf73b --- /dev/null +++ b/packages/graphql/src/type/directives.ts @@ -0,0 +1,193 @@ +import { inspect } from '../jsutils/inspect.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import { toObjMap } from '../jsutils/toObjMap.js'; + +import type { DirectiveDefinitionNode } from '../language/ast.js'; +import { DirectiveLocation } from '../language/directiveLocation.js'; + +import { assertName } from './assertName.js'; +import type { GraphQLArgument, GraphQLFieldConfigArgumentMap } from './definition.js'; +import { argsToArgsConfig, defineArguments, GraphQLNonNull } from './definition.js'; +import { GraphQLBoolean, GraphQLString } from './scalars.js'; + +const isGraphQLDirectiveSymbol = Symbol.for('GraphQLDirective'); + +/** + * Test if the given value is a GraphQL directive. + */ +export function isDirective(directive: unknown): directive is GraphQLDirective { + return typeof directive === 'object' && directive != null && isGraphQLDirectiveSymbol in directive; +} + +export function assertDirective(directive: unknown): GraphQLDirective { + if (!isDirective(directive)) { + throw new Error(`Expected ${inspect(directive)} to be a GraphQL directive.`); + } + return directive; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLDirectiveExtensions { + [attributeName: string]: unknown; +} + +/** + * Directives are used by the GraphQL runtime as a way of modifying execution + * behavior. Type system creators will usually not create these directly. + */ +export class GraphQLDirective { + readonly [isGraphQLDirectiveSymbol]: true = true; + name: string; + description: Maybe; + locations: ReadonlyArray; + args: ReadonlyArray; + isRepeatable: boolean; + extensions: Readonly; + astNode: Maybe; + + constructor(config: Readonly) { + this.name = assertName(config.name); + this.description = config.description; + this.locations = config.locations; + this.isRepeatable = config.isRepeatable ?? false; + this.extensions = toObjMap(config.extensions); + this.astNode = config.astNode; + + const args = config.args ?? {}; + this.args = defineArguments(args); + } + + get [Symbol.toStringTag]() { + return 'GraphQLDirective'; + } + + toConfig(): GraphQLDirectiveNormalizedConfig { + return { + name: this.name, + description: this.description, + locations: this.locations, + args: argsToArgsConfig(this.args), + isRepeatable: this.isRepeatable, + extensions: this.extensions, + astNode: this.astNode, + }; + } + + toString(): string { + return '@' + this.name; + } + + toJSON(): string { + return this.toString(); + } +} + +export interface GraphQLDirectiveConfig { + name: string; + description?: Maybe; + locations: ReadonlyArray; + args?: Maybe; + isRepeatable?: Maybe; + extensions?: Maybe>; + astNode?: Maybe; +} + +interface GraphQLDirectiveNormalizedConfig extends GraphQLDirectiveConfig { + args: GraphQLFieldConfigArgumentMap; + isRepeatable: boolean; + extensions: Readonly; +} + +/** + * Used to conditionally include fields or fragments. + */ +export const GraphQLIncludeDirective: GraphQLDirective = new GraphQLDirective({ + name: 'include', + description: 'Directs the executor to include this field or fragment only when the `if` argument is true.', + locations: [DirectiveLocation.FIELD, DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT], + args: { + if: { + type: new GraphQLNonNull(GraphQLBoolean), + description: 'Included when true.', + }, + }, +}); + +/** + * Used to conditionally skip (exclude) fields or fragments. + */ +export const GraphQLSkipDirective: GraphQLDirective = new GraphQLDirective({ + name: 'skip', + description: 'Directs the executor to skip this field or fragment when the `if` argument is true.', + locations: [DirectiveLocation.FIELD, DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT], + args: { + if: { + type: new GraphQLNonNull(GraphQLBoolean), + description: 'Skipped when true.', + }, + }, +}); + +/** + * Constant string used for default reason for a deprecation. + */ +export const DEFAULT_DEPRECATION_REASON = 'No longer supported'; + +/** + * Used to declare element of a GraphQL schema as deprecated. + */ +export const GraphQLDeprecatedDirective: GraphQLDirective = new GraphQLDirective({ + name: 'deprecated', + description: 'Marks an element of a GraphQL schema as no longer supported.', + locations: [ + DirectiveLocation.FIELD_DEFINITION, + DirectiveLocation.ARGUMENT_DEFINITION, + DirectiveLocation.INPUT_FIELD_DEFINITION, + DirectiveLocation.ENUM_VALUE, + ], + args: { + reason: { + type: GraphQLString, + description: + 'Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).', + defaultValue: DEFAULT_DEPRECATION_REASON, + }, + }, +}); + +/** + * Used to provide a URL for specifying the behavior of custom scalar definitions. + */ +export const GraphQLSpecifiedByDirective: GraphQLDirective = new GraphQLDirective({ + name: 'specifiedBy', + description: 'Exposes a URL that specifies the behavior of this scalar.', + locations: [DirectiveLocation.SCALAR], + args: { + url: { + type: new GraphQLNonNull(GraphQLString), + description: 'The URL that specifies the behavior of this scalar.', + }, + }, +}); + +/** + * The full list of specified directives. + */ +export const specifiedDirectives: ReadonlyArray = Object.freeze([ + GraphQLIncludeDirective, + GraphQLSkipDirective, + GraphQLDeprecatedDirective, + GraphQLSpecifiedByDirective, +]); + +export function isSpecifiedDirective(directive: GraphQLDirective): boolean { + return specifiedDirectives.some(({ name }) => name === directive.name); +} diff --git a/packages/graphql/src/type/index.ts b/packages/graphql/src/type/index.ts new file mode 100644 index 00000000000..044175d89ea --- /dev/null +++ b/packages/graphql/src/type/index.ts @@ -0,0 +1,8 @@ +export type { Path as ResponsePath } from '../jsutils/Path.js'; +export * from './assertName.js'; +export * from './definition.js'; +export * from './directives.js'; +export * from './introspection.js'; +export * from './scalars.js'; +export * from './schema.js'; +export * from './validate.js'; diff --git a/packages/graphql/src/type/introspection.ts b/packages/graphql/src/type/introspection.ts new file mode 100644 index 00000000000..af165823692 --- /dev/null +++ b/packages/graphql/src/type/introspection.ts @@ -0,0 +1,532 @@ +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; + +import { DirectiveLocation } from '../language/directiveLocation.js'; +import { print } from '../language/printer.js'; + +import { astFromValue } from '../utilities/astFromValue.js'; + +import type { + GraphQLEnumValue, + GraphQLField, + GraphQLFieldConfigMap, + GraphQLInputField, + GraphQLNamedType, + GraphQLType, +} from './definition.js'; +import { + GraphQLEnumType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + isAbstractType, + isEnumType, + isInputObjectType, + isInterfaceType, + isListType, + isNonNullType, + isObjectType, + isScalarType, + isUnionType, +} from './definition.js'; +import type { GraphQLDirective } from './directives.js'; +import { GraphQLBoolean, GraphQLString } from './scalars.js'; +import type { GraphQLSchema } from './schema.js'; + +export const __Schema: GraphQLObjectType = new GraphQLObjectType({ + name: '__Schema', + description: + 'A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.', + fields: () => + ({ + description: { + type: GraphQLString, + resolve: schema => schema.description, + }, + types: { + description: 'A list of all types supported by this server.', + type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__Type))), + resolve(schema) { + return Object.values(schema.getTypeMap()); + }, + }, + queryType: { + description: 'The type that query operations will be rooted at.', + type: new GraphQLNonNull(__Type), + resolve: schema => schema.getQueryType(), + }, + mutationType: { + description: 'If this server supports mutation, the type that mutation operations will be rooted at.', + type: __Type, + resolve: schema => schema.getMutationType(), + }, + subscriptionType: { + description: 'If this server support subscription, the type that subscription operations will be rooted at.', + type: __Type, + resolve: schema => schema.getSubscriptionType(), + }, + directives: { + description: 'A list of all directives supported by this server.', + type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__Directive))), + resolve: schema => schema.getDirectives(), + }, + } as GraphQLFieldConfigMap), +}); + +export const __Directive: GraphQLObjectType = new GraphQLObjectType({ + name: '__Directive', + description: + "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.", + fields: () => + ({ + name: { + type: new GraphQLNonNull(GraphQLString), + resolve: directive => directive.name, + }, + description: { + type: GraphQLString, + resolve: directive => directive.description, + }, + isRepeatable: { + type: new GraphQLNonNull(GraphQLBoolean), + resolve: directive => directive.isRepeatable, + }, + locations: { + type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__DirectiveLocation))), + resolve: directive => directive.locations, + }, + args: { + type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__InputValue))), + args: { + includeDeprecated: { + type: GraphQLBoolean, + defaultValue: false, + }, + }, + resolve(field, { includeDeprecated }) { + return includeDeprecated ? field.args : field.args.filter(arg => arg.deprecationReason == null); + }, + }, + } as GraphQLFieldConfigMap), +}); + +export const __DirectiveLocation: GraphQLEnumType = new GraphQLEnumType({ + name: '__DirectiveLocation', + description: + 'A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies.', + values: { + QUERY: { + value: DirectiveLocation.QUERY, + description: 'Location adjacent to a query operation.', + }, + MUTATION: { + value: DirectiveLocation.MUTATION, + description: 'Location adjacent to a mutation operation.', + }, + SUBSCRIPTION: { + value: DirectiveLocation.SUBSCRIPTION, + description: 'Location adjacent to a subscription operation.', + }, + FIELD: { + value: DirectiveLocation.FIELD, + description: 'Location adjacent to a field.', + }, + FRAGMENT_DEFINITION: { + value: DirectiveLocation.FRAGMENT_DEFINITION, + description: 'Location adjacent to a fragment definition.', + }, + FRAGMENT_SPREAD: { + value: DirectiveLocation.FRAGMENT_SPREAD, + description: 'Location adjacent to a fragment spread.', + }, + INLINE_FRAGMENT: { + value: DirectiveLocation.INLINE_FRAGMENT, + description: 'Location adjacent to an inline fragment.', + }, + VARIABLE_DEFINITION: { + value: DirectiveLocation.VARIABLE_DEFINITION, + description: 'Location adjacent to a variable definition.', + }, + SCHEMA: { + value: DirectiveLocation.SCHEMA, + description: 'Location adjacent to a schema definition.', + }, + SCALAR: { + value: DirectiveLocation.SCALAR, + description: 'Location adjacent to a scalar definition.', + }, + OBJECT: { + value: DirectiveLocation.OBJECT, + description: 'Location adjacent to an object type definition.', + }, + FIELD_DEFINITION: { + value: DirectiveLocation.FIELD_DEFINITION, + description: 'Location adjacent to a field definition.', + }, + ARGUMENT_DEFINITION: { + value: DirectiveLocation.ARGUMENT_DEFINITION, + description: 'Location adjacent to an argument definition.', + }, + INTERFACE: { + value: DirectiveLocation.INTERFACE, + description: 'Location adjacent to an interface definition.', + }, + UNION: { + value: DirectiveLocation.UNION, + description: 'Location adjacent to a union definition.', + }, + ENUM: { + value: DirectiveLocation.ENUM, + description: 'Location adjacent to an enum definition.', + }, + ENUM_VALUE: { + value: DirectiveLocation.ENUM_VALUE, + description: 'Location adjacent to an enum value definition.', + }, + INPUT_OBJECT: { + value: DirectiveLocation.INPUT_OBJECT, + description: 'Location adjacent to an input object type definition.', + }, + INPUT_FIELD_DEFINITION: { + value: DirectiveLocation.INPUT_FIELD_DEFINITION, + description: 'Location adjacent to an input object field definition.', + }, + }, +}); + +export const __Type: GraphQLObjectType = new GraphQLObjectType({ + name: '__Type', + description: + 'The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name, description and optional `specifiedByURL`, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.', + fields: () => + ({ + kind: { + type: new GraphQLNonNull(__TypeKind), + resolve(type) { + if (isScalarType(type)) { + return TypeKind.SCALAR; + } + if (isObjectType(type)) { + return TypeKind.OBJECT; + } + if (isInterfaceType(type)) { + return TypeKind.INTERFACE; + } + if (isUnionType(type)) { + return TypeKind.UNION; + } + if (isEnumType(type)) { + return TypeKind.ENUM; + } + if (isInputObjectType(type)) { + return TypeKind.INPUT_OBJECT; + } + if (isListType(type)) { + return TypeKind.LIST; + } + if (isNonNullType(type)) { + return TypeKind.NON_NULL; + } + /* c8 ignore next 3 */ + // Not reachable, all possible types have been considered) + invariant(false, `Unexpected type: "${inspect(type)}".`); + }, + }, + name: { + type: GraphQLString, + resolve: type => ('name' in type ? type.name : undefined), + }, + description: { + type: GraphQLString, + resolve: type => + // FIXME: add test case + /* c8 ignore next */ + 'description' in type ? type.description : undefined, + }, + specifiedByURL: { + type: GraphQLString, + resolve: obj => ('specifiedByURL' in obj ? obj.specifiedByURL : undefined), + }, + fields: { + type: new GraphQLList(new GraphQLNonNull(__Field)), + args: { + includeDeprecated: { type: GraphQLBoolean, defaultValue: false }, + }, + resolve(type, { includeDeprecated }) { + if (isObjectType(type) || isInterfaceType(type)) { + const fields = Object.values(type.getFields()); + return includeDeprecated ? fields : fields.filter(field => field.deprecationReason == null); + } + return undefined; + }, + }, + interfaces: { + type: new GraphQLList(new GraphQLNonNull(__Type)), + resolve(type) { + if (isObjectType(type) || isInterfaceType(type)) { + return type.getInterfaces(); + } + return undefined; + }, + }, + possibleTypes: { + type: new GraphQLList(new GraphQLNonNull(__Type)), + resolve(type, _args, _context, { schema }) { + if (isAbstractType(type)) { + return schema.getPossibleTypes(type); + } + return undefined; + }, + }, + enumValues: { + type: new GraphQLList(new GraphQLNonNull(__EnumValue)), + args: { + includeDeprecated: { type: GraphQLBoolean, defaultValue: false }, + }, + resolve(type, { includeDeprecated }) { + if (isEnumType(type)) { + const values = type.getValues(); + return includeDeprecated ? values : values.filter(field => field.deprecationReason == null); + } + return undefined; + }, + }, + inputFields: { + type: new GraphQLList(new GraphQLNonNull(__InputValue)), + args: { + includeDeprecated: { + type: GraphQLBoolean, + defaultValue: false, + }, + }, + resolve(type, { includeDeprecated }) { + if (isInputObjectType(type)) { + const values = Object.values(type.getFields()); + return includeDeprecated ? values : values.filter(field => field.deprecationReason == null); + } + return undefined; + }, + }, + ofType: { + type: __Type, + resolve: type => ('ofType' in type ? type.ofType : undefined), + }, + } as GraphQLFieldConfigMap), +}); + +export const __Field: GraphQLObjectType = new GraphQLObjectType({ + name: '__Field', + description: + 'Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.', + fields: () => + ({ + name: { + type: new GraphQLNonNull(GraphQLString), + resolve: field => field.name, + }, + description: { + type: GraphQLString, + resolve: field => field.description, + }, + args: { + type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__InputValue))), + args: { + includeDeprecated: { + type: GraphQLBoolean, + defaultValue: false, + }, + }, + resolve(field, { includeDeprecated }) { + return includeDeprecated ? field.args : field.args.filter(arg => arg.deprecationReason == null); + }, + }, + type: { + type: new GraphQLNonNull(__Type), + resolve: field => field.type, + }, + isDeprecated: { + type: new GraphQLNonNull(GraphQLBoolean), + resolve: field => field.deprecationReason != null, + }, + deprecationReason: { + type: GraphQLString, + resolve: field => field.deprecationReason, + }, + } as GraphQLFieldConfigMap, unknown>), +}); + +export const __InputValue: GraphQLObjectType = new GraphQLObjectType({ + name: '__InputValue', + description: + 'Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.', + fields: () => + ({ + name: { + type: new GraphQLNonNull(GraphQLString), + resolve: inputValue => inputValue.name, + }, + description: { + type: GraphQLString, + resolve: inputValue => inputValue.description, + }, + type: { + type: new GraphQLNonNull(__Type), + resolve: inputValue => inputValue.type, + }, + defaultValue: { + type: GraphQLString, + description: 'A GraphQL-formatted string representing the default value for this input value.', + resolve(inputValue) { + const { type, defaultValue } = inputValue; + const valueAST = astFromValue(defaultValue, type); + return valueAST ? print(valueAST) : null; + }, + }, + isDeprecated: { + type: new GraphQLNonNull(GraphQLBoolean), + resolve: field => field.deprecationReason != null, + }, + deprecationReason: { + type: GraphQLString, + resolve: obj => obj.deprecationReason, + }, + } as GraphQLFieldConfigMap), +}); + +export const __EnumValue: GraphQLObjectType = new GraphQLObjectType({ + name: '__EnumValue', + description: + 'One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.', + fields: () => + ({ + name: { + type: new GraphQLNonNull(GraphQLString), + resolve: enumValue => enumValue.name, + }, + description: { + type: GraphQLString, + resolve: enumValue => enumValue.description, + }, + isDeprecated: { + type: new GraphQLNonNull(GraphQLBoolean), + resolve: enumValue => enumValue.deprecationReason != null, + }, + deprecationReason: { + type: GraphQLString, + resolve: enumValue => enumValue.deprecationReason, + }, + } as GraphQLFieldConfigMap), +}); + +export enum TypeKind { + SCALAR = 'SCALAR', + OBJECT = 'OBJECT', + INTERFACE = 'INTERFACE', + UNION = 'UNION', + ENUM = 'ENUM', + INPUT_OBJECT = 'INPUT_OBJECT', + LIST = 'LIST', + NON_NULL = 'NON_NULL', +} + +export const __TypeKind: GraphQLEnumType = new GraphQLEnumType({ + name: '__TypeKind', + description: 'An enum describing what kind of type a given `__Type` is.', + values: { + SCALAR: { + value: TypeKind.SCALAR, + description: 'Indicates this type is a scalar.', + }, + OBJECT: { + value: TypeKind.OBJECT, + description: 'Indicates this type is an object. `fields` and `interfaces` are valid fields.', + }, + INTERFACE: { + value: TypeKind.INTERFACE, + description: 'Indicates this type is an interface. `fields`, `interfaces`, and `possibleTypes` are valid fields.', + }, + UNION: { + value: TypeKind.UNION, + description: 'Indicates this type is a union. `possibleTypes` is a valid field.', + }, + ENUM: { + value: TypeKind.ENUM, + description: 'Indicates this type is an enum. `enumValues` is a valid field.', + }, + INPUT_OBJECT: { + value: TypeKind.INPUT_OBJECT, + description: 'Indicates this type is an input object. `inputFields` is a valid field.', + }, + LIST: { + value: TypeKind.LIST, + description: 'Indicates this type is a list. `ofType` is a valid field.', + }, + NON_NULL: { + value: TypeKind.NON_NULL, + description: 'Indicates this type is a non-null. `ofType` is a valid field.', + }, + }, +}); + +/** + * Note that these are GraphQLField and not GraphQLFieldConfig, + * so the format for args is different. + */ + +export const SchemaMetaFieldDef: GraphQLField = { + name: '__schema', + type: new GraphQLNonNull(__Schema), + description: 'Access the current type schema of this server.', + args: [], + resolve: (_source, _args, _context, { schema }) => schema, + deprecationReason: undefined, + extensions: Object.create(null), + astNode: undefined, +}; + +export const TypeMetaFieldDef: GraphQLField = { + name: '__type', + type: __Type, + description: 'Request the type information of a single type.', + args: [ + { + name: 'name', + description: undefined, + type: new GraphQLNonNull(GraphQLString), + defaultValue: undefined, + deprecationReason: undefined, + extensions: Object.create(null), + astNode: undefined, + }, + ], + resolve: (_source, { name }, _context, { schema }) => schema.getType(name), + deprecationReason: undefined, + extensions: Object.create(null), + astNode: undefined, +}; + +export const TypeNameMetaFieldDef: GraphQLField = { + name: '__typename', + type: new GraphQLNonNull(GraphQLString), + description: 'The name of the current Object type at runtime.', + args: [], + resolve: (_source, _args, _context, { parentType }) => parentType.name, + deprecationReason: undefined, + extensions: Object.create(null), + astNode: undefined, +}; + +export const introspectionTypes: ReadonlyArray = Object.freeze([ + __Schema, + __Directive, + __DirectiveLocation, + __Type, + __Field, + __InputValue, + __EnumValue, + __TypeKind, +]); + +export function isIntrospectionType(type: GraphQLNamedType): boolean { + return introspectionTypes.some(({ name }) => type.name === name); +} diff --git a/packages/graphql/src/type/scalars.ts b/packages/graphql/src/type/scalars.ts new file mode 100644 index 00000000000..1cc05f4883c --- /dev/null +++ b/packages/graphql/src/type/scalars.ts @@ -0,0 +1,245 @@ +import { inspect } from '../jsutils/inspect.js'; +import { isObjectLike } from '../jsutils/isObjectLike.js'; + +import { GraphQLError } from '../error/GraphQLError.js'; + +import { Kind } from '../language/kinds.js'; +import { print } from '../language/printer.js'; + +import type { GraphQLNamedType } from './definition.js'; +import { GraphQLScalarType } from './definition.js'; + +/** + * Maximum possible Int value as per GraphQL Spec (32-bit signed integer). + * n.b. This differs from JavaScript's numbers that are IEEE 754 doubles safe up-to 2^53 - 1 + * */ +export const GRAPHQL_MAX_INT = 2147483647; + +/** + * Minimum possible Int value as per GraphQL Spec (32-bit signed integer). + * n.b. This differs from JavaScript's numbers that are IEEE 754 doubles safe starting at -(2^53 - 1) + * */ +export const GRAPHQL_MIN_INT = -2147483648; + +export const GraphQLInt = new GraphQLScalarType({ + name: 'Int', + description: + 'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.', + + serialize(outputValue) { + const coercedValue = serializeObject(outputValue); + + if (typeof coercedValue === 'boolean') { + return coercedValue ? 1 : 0; + } + + let num = coercedValue; + if (typeof coercedValue === 'string' && coercedValue !== '') { + num = Number(coercedValue); + } + + if (typeof num !== 'number' || !Number.isInteger(num)) { + throw new GraphQLError(`Int cannot represent non-integer value: ${inspect(coercedValue)}`); + } + if (num > GRAPHQL_MAX_INT || num < GRAPHQL_MIN_INT) { + throw new GraphQLError('Int cannot represent non 32-bit signed integer value: ' + inspect(coercedValue)); + } + return num; + }, + + parseValue(inputValue) { + if (typeof inputValue !== 'number' || !Number.isInteger(inputValue)) { + throw new GraphQLError(`Int cannot represent non-integer value: ${inspect(inputValue)}`); + } + if (inputValue > GRAPHQL_MAX_INT || inputValue < GRAPHQL_MIN_INT) { + throw new GraphQLError(`Int cannot represent non 32-bit signed integer value: ${inputValue}`); + } + return inputValue; + }, + + parseLiteral(valueNode) { + if (valueNode.kind !== Kind.INT) { + throw new GraphQLError(`Int cannot represent non-integer value: ${print(valueNode)}`, { nodes: valueNode }); + } + const num = parseInt(valueNode.value, 10); + if (num > GRAPHQL_MAX_INT || num < GRAPHQL_MIN_INT) { + throw new GraphQLError(`Int cannot represent non 32-bit signed integer value: ${valueNode.value}`, { + nodes: valueNode, + }); + } + return num; + }, +}); + +export const GraphQLFloat = new GraphQLScalarType({ + name: 'Float', + description: + 'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).', + + serialize(outputValue) { + const coercedValue = serializeObject(outputValue); + + if (typeof coercedValue === 'boolean') { + return coercedValue ? 1 : 0; + } + + let num = coercedValue; + if (typeof coercedValue === 'string' && coercedValue !== '') { + num = Number(coercedValue); + } + + if (typeof num !== 'number' || !Number.isFinite(num)) { + throw new GraphQLError(`Float cannot represent non numeric value: ${inspect(coercedValue)}`); + } + return num; + }, + + parseValue(inputValue) { + if (typeof inputValue !== 'number' || !Number.isFinite(inputValue)) { + throw new GraphQLError(`Float cannot represent non numeric value: ${inspect(inputValue)}`); + } + return inputValue; + }, + + parseLiteral(valueNode) { + if (valueNode.kind !== Kind.FLOAT && valueNode.kind !== Kind.INT) { + throw new GraphQLError(`Float cannot represent non numeric value: ${print(valueNode)}`, { nodes: valueNode }); + } + return parseFloat(valueNode.value); + }, +}); + +export const GraphQLString = new GraphQLScalarType({ + name: 'String', + description: + 'The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.', + + serialize(outputValue) { + const coercedValue = serializeObject(outputValue); + + // Serialize string, boolean and number values to a string, but do not + // attempt to coerce object, function, symbol, or other types as strings. + if (typeof coercedValue === 'string') { + return coercedValue; + } + if (typeof coercedValue === 'boolean') { + return coercedValue ? 'true' : 'false'; + } + if (typeof coercedValue === 'number' && Number.isFinite(coercedValue)) { + return coercedValue.toString(); + } + throw new GraphQLError(`String cannot represent value: ${inspect(outputValue)}`); + }, + + parseValue(inputValue) { + if (typeof inputValue !== 'string') { + throw new GraphQLError(`String cannot represent a non string value: ${inspect(inputValue)}`); + } + return inputValue; + }, + + parseLiteral(valueNode) { + if (valueNode.kind !== Kind.STRING) { + throw new GraphQLError(`String cannot represent a non string value: ${print(valueNode)}`, { nodes: valueNode }); + } + return valueNode.value; + }, +}); + +export const GraphQLBoolean = new GraphQLScalarType({ + name: 'Boolean', + description: 'The `Boolean` scalar type represents `true` or `false`.', + + serialize(outputValue) { + const coercedValue = serializeObject(outputValue); + + if (typeof coercedValue === 'boolean') { + return coercedValue; + } + if (Number.isFinite(coercedValue)) { + return coercedValue !== 0; + } + throw new GraphQLError(`Boolean cannot represent a non boolean value: ${inspect(coercedValue)}`); + }, + + parseValue(inputValue) { + if (typeof inputValue !== 'boolean') { + throw new GraphQLError(`Boolean cannot represent a non boolean value: ${inspect(inputValue)}`); + } + return inputValue; + }, + + parseLiteral(valueNode) { + if (valueNode.kind !== Kind.BOOLEAN) { + throw new GraphQLError(`Boolean cannot represent a non boolean value: ${print(valueNode)}`, { nodes: valueNode }); + } + return valueNode.value; + }, +}); + +export const GraphQLID = new GraphQLScalarType({ + name: 'ID', + description: + 'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.', + + serialize(outputValue) { + const coercedValue = serializeObject(outputValue); + + if (typeof coercedValue === 'string') { + return coercedValue; + } + if (Number.isInteger(coercedValue)) { + return String(coercedValue); + } + throw new GraphQLError(`ID cannot represent value: ${inspect(outputValue)}`); + }, + + parseValue(inputValue) { + if (typeof inputValue === 'string') { + return inputValue; + } + if (typeof inputValue === 'number' && Number.isInteger(inputValue)) { + return inputValue.toString(); + } + throw new GraphQLError(`ID cannot represent value: ${inspect(inputValue)}`); + }, + + parseLiteral(valueNode) { + if (valueNode.kind !== Kind.STRING && valueNode.kind !== Kind.INT) { + throw new GraphQLError('ID cannot represent a non-string and non-integer value: ' + print(valueNode), { + nodes: valueNode, + }); + } + return valueNode.value; + }, +}); + +export const specifiedScalarTypes: ReadonlyArray = Object.freeze([ + GraphQLString, + GraphQLInt, + GraphQLFloat, + GraphQLBoolean, + GraphQLID, +]); + +export function isSpecifiedScalarType(type: GraphQLNamedType): boolean { + return specifiedScalarTypes.some(({ name }) => type.name === name); +} + +// Support serializing objects with custom valueOf() or toJSON() functions - +// a common way to represent a complex value which can be represented as +// a string (ex: MongoDB id objects). +function serializeObject(outputValue: unknown): unknown { + if (isObjectLike(outputValue)) { + if (typeof outputValue.valueOf === 'function') { + const valueOfResult = outputValue.valueOf(); + if (!isObjectLike(valueOfResult)) { + return valueOfResult; + } + } + if (typeof outputValue['toJSON'] === 'function') { + return outputValue['toJSON'](); + } + } + return outputValue; +} diff --git a/packages/graphql/src/type/schema.ts b/packages/graphql/src/type/schema.ts new file mode 100644 index 00000000000..37dd065db33 --- /dev/null +++ b/packages/graphql/src/type/schema.ts @@ -0,0 +1,431 @@ +import { inspect } from '../jsutils/inspect.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; +import { toObjMap } from '../jsutils/toObjMap.js'; + +import type { GraphQLError } from '../error/GraphQLError.js'; + +import type { SchemaDefinitionNode, SchemaExtensionNode } from '../language/ast.js'; +import { OperationTypeNode } from '../language/ast.js'; + +import type { + GraphQLAbstractType, + GraphQLCompositeType, + GraphQLField, + GraphQLInterfaceType, + GraphQLNamedType, + GraphQLObjectType, + GraphQLType, +} from './definition.js'; +import { getNamedType, isInputObjectType, isInterfaceType, isObjectType, isUnionType } from './definition.js'; +import type { GraphQLDirective } from './directives.js'; +import { isDirective, specifiedDirectives } from './directives.js'; +import { __Schema, SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef } from './introspection.js'; + +const isSchemaSymbol = Symbol.for('GraphQLSchema'); + +/** + * Test if the given value is a GraphQL schema. + */ +export function isSchema(schema: unknown): schema is GraphQLSchema { + return typeof schema === 'object' && schema != null && isSchemaSymbol in schema; +} + +export function assertSchema(schema: unknown): GraphQLSchema { + if (!isSchema(schema)) { + throw new Error(`Expected ${inspect(schema)} to be a GraphQL schema.`); + } + return schema; +} + +/** + * Custom extensions + * + * @remarks + * Use a unique identifier name for your extension, for example the name of + * your library or project. Do not use a shortened identifier as this increases + * the risk of conflicts. We recommend you add at most one extension field, + * an object which can contain all the values you need. + */ +export interface GraphQLSchemaExtensions { + [attributeName: string]: unknown; +} + +/** + * Schema Definition + * + * A Schema is created by supplying the root types of each type of operation, + * query and mutation (optional). A schema definition is then supplied to the + * validator and executor. + * + * Example: + * + * ```ts + * const MyAppSchema = new GraphQLSchema({ + * query: MyAppQueryRootType, + * mutation: MyAppMutationRootType, + * }) + * ``` + * + * Note: When the schema is constructed, by default only the types that are + * reachable by traversing the root types are included, other types must be + * explicitly referenced. + * + * Example: + * + * ```ts + * const characterInterface = new GraphQLInterfaceType({ + * name: 'Character', + * ... + * }); + * + * const humanType = new GraphQLObjectType({ + * name: 'Human', + * interfaces: [characterInterface], + * ... + * }); + * + * const droidType = new GraphQLObjectType({ + * name: 'Droid', + * interfaces: [characterInterface], + * ... + * }); + * + * const schema = new GraphQLSchema({ + * query: new GraphQLObjectType({ + * name: 'Query', + * fields: { + * hero: { type: characterInterface, ... }, + * } + * }), + * ... + * // Since this schema references only the `Character` interface it's + * // necessary to explicitly list the types that implement it if + * // you want them to be included in the final schema. + * types: [humanType, droidType], + * }) + * ``` + * + * Note: If an array of `directives` are provided to GraphQLSchema, that will be + * the exact list of directives represented and allowed. If `directives` is not + * provided then a default set of the specified directives (e.g. `@include` and + * `@skip`) will be used. If you wish to provide *additional* directives to these + * specified directives, you must explicitly declare them. Example: + * + * ```ts + * const MyAppSchema = new GraphQLSchema({ + * ... + * directives: specifiedDirectives.concat([ myCustomDirective ]), + * }) + * ``` + */ +export class GraphQLSchema { + readonly [isSchemaSymbol]: true = true; + description: Maybe; + extensions: Readonly; + astNode: Maybe; + extensionASTNodes: ReadonlyArray; + + // Used as a cache for validateSchema(). + __validationErrors: Maybe>; + + private _queryType: Maybe; + private _mutationType: Maybe; + private _subscriptionType: Maybe; + private _directives: ReadonlyArray; + private _typeMap: TypeMap; + private _subTypeMap: ObjMap>; + private _implementationsMap: ObjMap<{ + objects: Array; + interfaces: Array; + }>; + + constructor(config: Readonly) { + // If this schema was built from a source known to be valid, then it may be + // marked with assumeValid to avoid an additional type system validation. + this.__validationErrors = config.assumeValid === true ? [] : undefined; + + this.description = config.description; + this.extensions = toObjMap(config.extensions); + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes ?? []; + + this._queryType = config.query; + this._mutationType = config.mutation; + this._subscriptionType = config.subscription; + // Provide specified directives (e.g. @include and @skip) by default. + this._directives = config.directives ?? specifiedDirectives; + + // To preserve order of user-provided types, we add first to add them to + // the set of "collected" types, so `collectReferencedTypes` ignore them. + const allReferencedTypes = new Set(config.types); + if (config.types != null) { + for (const type of config.types) { + // When we ready to process this type, we remove it from "collected" types + // and then add it together with all dependent types in the correct position. + allReferencedTypes.delete(type); + collectReferencedTypes(type, allReferencedTypes); + } + } + + if (this._queryType != null) { + collectReferencedTypes(this._queryType, allReferencedTypes); + } + if (this._mutationType != null) { + collectReferencedTypes(this._mutationType, allReferencedTypes); + } + if (this._subscriptionType != null) { + collectReferencedTypes(this._subscriptionType, allReferencedTypes); + } + + for (const directive of this._directives) { + // Directives are not validated until validateSchema() is called. + if (isDirective(directive)) { + for (const arg of directive.args) { + collectReferencedTypes(arg.type, allReferencedTypes); + } + } + } + collectReferencedTypes(__Schema, allReferencedTypes); + + // Storing the resulting map for reference by the schema. + this._typeMap = Object.create(null); + this._subTypeMap = Object.create(null); + // Keep track of all implementations by interface name. + this._implementationsMap = Object.create(null); + + for (const namedType of allReferencedTypes) { + if (namedType == null) { + continue; + } + + const typeName = namedType.name; + if (this._typeMap[typeName] !== undefined) { + throw new Error(`Schema must contain uniquely named types but contains multiple types named "${typeName}".`); + } + this._typeMap[typeName] = namedType; + + if (isInterfaceType(namedType)) { + // Store implementations by interface. + for (const iface of namedType.getInterfaces()) { + if (isInterfaceType(iface)) { + let implementations = this._implementationsMap[iface.name]; + if (implementations === undefined) { + implementations = this._implementationsMap[iface.name] = { + objects: [], + interfaces: [], + }; + } + + implementations.interfaces.push(namedType); + } + } + } else if (isObjectType(namedType)) { + // Store implementations by objects. + for (const iface of namedType.getInterfaces()) { + if (isInterfaceType(iface)) { + let implementations = this._implementationsMap[iface.name]; + if (implementations === undefined) { + implementations = this._implementationsMap[iface.name] = { + objects: [], + interfaces: [], + }; + } + + implementations.objects.push(namedType); + } + } + } + } + } + + get [Symbol.toStringTag]() { + return 'GraphQLSchema'; + } + + getQueryType(): Maybe { + return this._queryType; + } + + getMutationType(): Maybe { + return this._mutationType; + } + + getSubscriptionType(): Maybe { + return this._subscriptionType; + } + + getRootType(operation: OperationTypeNode): Maybe { + switch (operation) { + case OperationTypeNode.QUERY: + return this.getQueryType(); + case OperationTypeNode.MUTATION: + return this.getMutationType(); + case OperationTypeNode.SUBSCRIPTION: + return this.getSubscriptionType(); + } + } + + getTypeMap(): TypeMap { + return this._typeMap; + } + + getType(name: string): GraphQLNamedType | undefined { + return this.getTypeMap()[name]; + } + + getPossibleTypes(abstractType: GraphQLAbstractType): ReadonlyArray { + return isUnionType(abstractType) ? abstractType.getTypes() : this.getImplementations(abstractType).objects; + } + + getImplementations(interfaceType: GraphQLInterfaceType): { + objects: ReadonlyArray; + interfaces: ReadonlyArray; + } { + const implementations = this._implementationsMap[interfaceType.name]; + return implementations ?? { objects: [], interfaces: [] }; + } + + isSubType(abstractType: GraphQLAbstractType, maybeSubType: GraphQLObjectType | GraphQLInterfaceType): boolean { + let map = this._subTypeMap[abstractType.name]; + if (map === undefined) { + map = Object.create(null); + + if (isUnionType(abstractType)) { + for (const type of abstractType.getTypes()) { + map[type.name] = true; + } + } else { + const implementations = this.getImplementations(abstractType); + for (const type of implementations.objects) { + map[type.name] = true; + } + for (const type of implementations.interfaces) { + map[type.name] = true; + } + } + + this._subTypeMap[abstractType.name] = map; + } + return map[maybeSubType.name] !== undefined; + } + + getDirectives(): ReadonlyArray { + return this._directives; + } + + getDirective(name: string): Maybe { + return this.getDirectives().find(directive => directive.name === name); + } + + /** + * This method looks up the field on the given type definition. + * It has special casing for the three introspection fields, `__schema`, + * `__type` and `__typename`. + * + * `__typename` is special because it can always be queried as a field, even + * in situations where no other fields are allowed, like on a Union. + * + * `__schema` and `__type` could get automatically added to the query type, + * but that would require mutating type definitions, which would cause issues. + */ + getField(parentType: GraphQLCompositeType, fieldName: string): GraphQLField | undefined { + switch (fieldName) { + case SchemaMetaFieldDef.name: + return this.getQueryType() === parentType ? SchemaMetaFieldDef : undefined; + case TypeMetaFieldDef.name: + return this.getQueryType() === parentType ? TypeMetaFieldDef : undefined; + case TypeNameMetaFieldDef.name: + return TypeNameMetaFieldDef; + } + + // this function is part "hot" path inside executor and check presence + // of 'getFields' is faster than to use `!isUnionType` + if ('getFields' in parentType) { + return parentType.getFields()[fieldName]; + } + return undefined; + } + + toConfig(): GraphQLSchemaNormalizedConfig { + return { + description: this.description, + query: this.getQueryType(), + mutation: this.getMutationType(), + subscription: this.getSubscriptionType(), + types: Object.values(this.getTypeMap()), + directives: this.getDirectives(), + extensions: this.extensions, + astNode: this.astNode, + extensionASTNodes: this.extensionASTNodes, + assumeValid: this.__validationErrors !== undefined, + }; + } +} + +type TypeMap = ObjMap; + +export interface GraphQLSchemaValidationOptions { + /** + * When building a schema from a GraphQL service's introspection result, it + * might be safe to assume the schema is valid. Set to true to assume the + * produced schema is valid. + * + * Default: false + */ + assumeValid?: boolean; +} + +export interface GraphQLSchemaConfig extends GraphQLSchemaValidationOptions { + description?: Maybe; + query?: Maybe; + mutation?: Maybe; + subscription?: Maybe; + types?: Maybe>; + directives?: Maybe>; + extensions?: Maybe>; + astNode?: Maybe; + extensionASTNodes?: Maybe>; +} + +/** + * @internal + */ +export interface GraphQLSchemaNormalizedConfig extends GraphQLSchemaConfig { + description: Maybe; + types: ReadonlyArray; + directives: ReadonlyArray; + extensions: Readonly; + extensionASTNodes: ReadonlyArray; + assumeValid: boolean; +} + +function collectReferencedTypes(type: GraphQLType, typeSet: Set): Set { + const namedType = getNamedType(type); + + if (!typeSet.has(namedType)) { + typeSet.add(namedType); + if (isUnionType(namedType)) { + for (const memberType of namedType.getTypes()) { + collectReferencedTypes(memberType, typeSet); + } + } else if (isObjectType(namedType) || isInterfaceType(namedType)) { + for (const interfaceType of namedType.getInterfaces()) { + collectReferencedTypes(interfaceType, typeSet); + } + + for (const field of Object.values(namedType.getFields())) { + collectReferencedTypes(field.type, typeSet); + for (const arg of field.args) { + collectReferencedTypes(arg.type, typeSet); + } + } + } else if (isInputObjectType(namedType)) { + for (const field of Object.values(namedType.getFields())) { + collectReferencedTypes(field.type, typeSet); + } + } + } + + return typeSet; +} diff --git a/packages/graphql/src/type/validate.ts b/packages/graphql/src/type/validate.ts new file mode 100644 index 00000000000..4e9f583919b --- /dev/null +++ b/packages/graphql/src/type/validate.ts @@ -0,0 +1,581 @@ +import { AccumulatorMap } from '../jsutils/AccumulatorMap.js'; +import { capitalize } from '../jsutils/capitalize.js'; +import { andList } from '../jsutils/formatList.js'; +import { inspect } from '../jsutils/inspect.js'; +import type { Maybe } from '../jsutils/Maybe.js'; + +import { GraphQLError } from '../error/GraphQLError.js'; + +import type { + ASTNode, + DirectiveNode, + InterfaceTypeDefinitionNode, + InterfaceTypeExtensionNode, + NamedTypeNode, + ObjectTypeDefinitionNode, + ObjectTypeExtensionNode, + UnionTypeDefinitionNode, + UnionTypeExtensionNode, +} from '../language/ast.js'; +import { OperationTypeNode } from '../language/ast.js'; + +import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators.js'; + +import type { + GraphQLEnumType, + GraphQLInputField, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLObjectType, + GraphQLUnionType, +} from './definition.js'; +import { + isEnumType, + isInputObjectType, + isInputType, + isInterfaceType, + isNamedType, + isNonNullType, + isObjectType, + isOutputType, + isRequiredArgument, + isRequiredInputField, + isUnionType, +} from './definition.js'; +import { GraphQLDeprecatedDirective, isDirective } from './directives.js'; +import { isIntrospectionType } from './introspection.js'; +import type { GraphQLSchema } from './schema.js'; +import { assertSchema } from './schema.js'; + +/** + * Implements the "Type Validation" sub-sections of the specification's + * "Type System" section. + * + * Validation runs synchronously, returning an array of encountered errors, or + * an empty array if no errors were encountered and the Schema is valid. + */ +export function validateSchema(schema: GraphQLSchema): ReadonlyArray { + // First check to ensure the provided value is in fact a GraphQLSchema. + assertSchema(schema); + + // If this Schema has already been validated, return the previous results. + if (schema.__validationErrors) { + return schema.__validationErrors; + } + + // Validate the schema, producing a list of errors. + const context = new SchemaValidationContext(schema); + validateRootTypes(context); + validateDirectives(context); + validateTypes(context); + + // Persist the results of validation before returning to ensure validation + // does not run multiple times for this schema. + const errors = context.getErrors(); + schema.__validationErrors = errors; + return errors; +} + +/** + * Utility function which asserts a schema is valid by throwing an error if + * it is invalid. + */ +export function assertValidSchema(schema: GraphQLSchema): void { + const errors = validateSchema(schema); + if (errors.length !== 0) { + throw new Error(errors.map(error => error.message).join('\n\n')); + } +} + +class SchemaValidationContext { + readonly _errors: Array; + readonly schema: GraphQLSchema; + + constructor(schema: GraphQLSchema) { + this._errors = []; + this.schema = schema; + } + + reportError(message: string, nodes?: ReadonlyArray> | Maybe): void { + const _nodes = Array.isArray(nodes) ? (nodes.filter(Boolean) as ReadonlyArray) : (nodes as Maybe); + this._errors.push(new GraphQLError(message, { nodes: _nodes })); + } + + getErrors(): ReadonlyArray { + return this._errors; + } +} + +function validateRootTypes(context: SchemaValidationContext): void { + const schema = context.schema; + + if (schema.getQueryType() == null) { + context.reportError('Query root type must be provided.', schema.astNode); + } + + const rootTypesMap = new AccumulatorMap(); + for (const operationType of Object.values(OperationTypeNode)) { + const rootType = schema.getRootType(operationType); + + if (rootType != null) { + if (!isObjectType(rootType)) { + const operationTypeStr = capitalize(operationType); + const rootTypeStr = inspect(rootType); + context.reportError( + operationType === OperationTypeNode.QUERY + ? `${operationTypeStr} root type must be Object type, it cannot be ${rootTypeStr}.` + : `${operationTypeStr} root type must be Object type if provided, it cannot be ${rootTypeStr}.`, + getOperationTypeNode(schema, operationType) ?? (rootType as any).astNode + ); + } else { + rootTypesMap.add(rootType, operationType); + } + } + } + + for (const [rootType, operationTypes] of rootTypesMap.entries()) { + if (operationTypes.length > 1) { + const operationList = andList(operationTypes); + context.reportError( + `All root types must be different, "${rootType.name}" type is used as ${operationList} root types.`, + operationTypes.map(operationType => getOperationTypeNode(schema, operationType)) + ); + } + } +} + +function getOperationTypeNode(schema: GraphQLSchema, operation: OperationTypeNode): Maybe { + return [schema.astNode, ...schema.extensionASTNodes] + .flatMap( + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + schemaNode => /* c8 ignore next */ schemaNode?.operationTypes ?? [] + ) + .find(operationNode => operationNode.operation === operation)?.type; +} + +function validateDirectives(context: SchemaValidationContext): void { + for (const directive of context.schema.getDirectives()) { + // Ensure all directives are in fact GraphQL directives. + if (!isDirective(directive)) { + context.reportError(`Expected directive but got: ${inspect(directive)}.`, (directive as any)?.astNode); + continue; + } + + // Ensure they are named correctly. + validateName(context, directive); + + // TODO: Ensure proper locations. + + // Ensure the arguments are valid. + for (const arg of directive.args) { + // Ensure they are named correctly. + validateName(context, arg); + + // Ensure the type is an input type. + if (!isInputType(arg.type)) { + context.reportError( + `The type of @${directive.name}(${arg.name}:) must be Input Type ` + `but got: ${inspect(arg.type)}.`, + arg.astNode + ); + } + + if (isRequiredArgument(arg) && arg.deprecationReason != null) { + context.reportError(`Required argument @${directive.name}(${arg.name}:) cannot be deprecated.`, [ + getDeprecatedDirectiveNode(arg.astNode), + arg.astNode?.type, + ]); + } + } + } +} + +function validateName( + context: SchemaValidationContext, + node: { readonly name: string; readonly astNode: Maybe } +): void { + // Ensure names are valid, however introspection types opt out. + if (node.name.startsWith('__')) { + context.reportError( + `Name "${node.name}" must not begin with "__", which is reserved by GraphQL introspection.`, + node.astNode + ); + } +} + +function validateTypes(context: SchemaValidationContext): void { + const validateInputObjectCircularRefs = createInputObjectCircularRefsValidator(context); + const typeMap = context.schema.getTypeMap(); + for (const type of Object.values(typeMap)) { + // Ensure all provided types are in fact GraphQL type. + if (!isNamedType(type)) { + context.reportError(`Expected GraphQL named type but got: ${inspect(type)}.`, (type as any).astNode); + continue; + } + + // Ensure it is named correctly (excluding introspection types). + if (!isIntrospectionType(type)) { + validateName(context, type); + } + + if (isObjectType(type)) { + // Ensure fields are valid + validateFields(context, type); + + // Ensure objects implement the interfaces they claim to. + validateInterfaces(context, type); + } else if (isInterfaceType(type)) { + // Ensure fields are valid. + validateFields(context, type); + + // Ensure interfaces implement the interfaces they claim to. + validateInterfaces(context, type); + } else if (isUnionType(type)) { + // Ensure Unions include valid member types. + validateUnionMembers(context, type); + } else if (isEnumType(type)) { + // Ensure Enums have valid values. + validateEnumValues(context, type); + } else if (isInputObjectType(type)) { + // Ensure Input Object fields are valid. + validateInputFields(context, type); + + // Ensure Input Objects do not contain non-nullable circular references + validateInputObjectCircularRefs(type); + } + } +} + +function validateFields(context: SchemaValidationContext, type: GraphQLObjectType | GraphQLInterfaceType): void { + const fields = Object.values(type.getFields()); + + // Objects and Interfaces both must define one or more fields. + if (fields.length === 0) { + context.reportError(`Type ${type.name} must define one or more fields.`, [type.astNode, ...type.extensionASTNodes]); + } + + for (const field of fields) { + // Ensure they are named correctly. + validateName(context, field); + + // Ensure the type is an output type + if (!isOutputType(field.type)) { + context.reportError( + `The type of ${type.name}.${field.name} must be Output Type ` + `but got: ${inspect(field.type)}.`, + field.astNode?.type + ); + } + + // Ensure the arguments are valid + for (const arg of field.args) { + const argName = arg.name; + + // Ensure they are named correctly. + validateName(context, arg); + + // Ensure the type is an input type + if (!isInputType(arg.type)) { + context.reportError( + `The type of ${type.name}.${field.name}(${argName}:) must be Input ` + `Type but got: ${inspect(arg.type)}.`, + arg.astNode?.type + ); + } + + if (isRequiredArgument(arg) && arg.deprecationReason != null) { + context.reportError(`Required argument ${type.name}.${field.name}(${argName}:) cannot be deprecated.`, [ + getDeprecatedDirectiveNode(arg.astNode), + arg.astNode?.type, + ]); + } + } + } +} + +function validateInterfaces(context: SchemaValidationContext, type: GraphQLObjectType | GraphQLInterfaceType): void { + const ifaceTypeNames = Object.create(null); + for (const iface of type.getInterfaces()) { + if (!isInterfaceType(iface)) { + context.reportError( + `Type ${inspect(type)} must only implement Interface types, ` + `it cannot implement ${inspect(iface)}.`, + getAllImplementsInterfaceNodes(type, iface) + ); + continue; + } + + if (type === iface) { + context.reportError( + `Type ${type.name} cannot implement itself because it would create a circular reference.`, + getAllImplementsInterfaceNodes(type, iface) + ); + continue; + } + + if (ifaceTypeNames[iface.name]) { + context.reportError( + `Type ${type.name} can only implement ${iface.name} once.`, + getAllImplementsInterfaceNodes(type, iface) + ); + continue; + } + + ifaceTypeNames[iface.name] = true; + + validateTypeImplementsAncestors(context, type, iface); + validateTypeImplementsInterface(context, type, iface); + } +} + +function validateTypeImplementsInterface( + context: SchemaValidationContext, + type: GraphQLObjectType | GraphQLInterfaceType, + iface: GraphQLInterfaceType +): void { + const typeFieldMap = type.getFields(); + + // Assert each interface field is implemented. + for (const ifaceField of Object.values(iface.getFields())) { + const fieldName = ifaceField.name; + const typeField = typeFieldMap[fieldName]; + + // Assert interface field exists on type. + if (!typeField) { + context.reportError(`Interface field ${iface.name}.${fieldName} expected but ${type.name} does not provide it.`, [ + ifaceField.astNode, + type.astNode, + ...type.extensionASTNodes, + ]); + continue; + } + + // Assert interface field type is satisfied by type field type, by being + // a valid subtype. (covariant) + if (!isTypeSubTypeOf(context.schema, typeField.type, ifaceField.type)) { + context.reportError( + `Interface field ${iface.name}.${fieldName} expects type ` + + `${inspect(ifaceField.type)} but ${type.name}.${fieldName} ` + + `is type ${inspect(typeField.type)}.`, + [ifaceField.astNode?.type, typeField.astNode?.type] + ); + } + + // Assert each interface field arg is implemented. + for (const ifaceArg of ifaceField.args) { + const argName = ifaceArg.name; + const typeArg = typeField.args.find(arg => arg.name === argName); + + // Assert interface field arg exists on object field. + if (!typeArg) { + context.reportError( + `Interface field argument ${iface.name}.${fieldName}(${argName}:) expected but ${type.name}.${fieldName} does not provide it.`, + [ifaceArg.astNode, typeField.astNode] + ); + continue; + } + + // Assert interface field arg type matches object field arg type. + // (invariant) + // TODO: change to contravariant? + if (!isEqualType(ifaceArg.type, typeArg.type)) { + context.reportError( + `Interface field argument ${iface.name}.${fieldName}(${argName}:) ` + + `expects type ${inspect(ifaceArg.type)} but ` + + `${type.name}.${fieldName}(${argName}:) is type ` + + `${inspect(typeArg.type)}.`, + [ifaceArg.astNode?.type, typeArg.astNode?.type] + ); + } + + // TODO: validate default values? + } + + // Assert additional arguments must not be required. + for (const typeArg of typeField.args) { + const argName = typeArg.name; + const ifaceArg = ifaceField.args.find(arg => arg.name === argName); + if (!ifaceArg && isRequiredArgument(typeArg)) { + context.reportError( + `Object field ${type.name}.${fieldName} includes required argument ${argName} that is missing from the Interface field ${iface.name}.${fieldName}.`, + [typeArg.astNode, ifaceField.astNode] + ); + } + } + } +} + +function validateTypeImplementsAncestors( + context: SchemaValidationContext, + type: GraphQLObjectType | GraphQLInterfaceType, + iface: GraphQLInterfaceType +): void { + const ifaceInterfaces = type.getInterfaces(); + for (const transitive of iface.getInterfaces()) { + if (!ifaceInterfaces.includes(transitive)) { + context.reportError( + transitive === type + ? `Type ${type.name} cannot implement ${iface.name} because it would create a circular reference.` + : `Type ${type.name} must implement ${transitive.name} because it is implemented by ${iface.name}.`, + [...getAllImplementsInterfaceNodes(iface, transitive), ...getAllImplementsInterfaceNodes(type, iface)] + ); + } + } +} + +function validateUnionMembers(context: SchemaValidationContext, union: GraphQLUnionType): void { + const memberTypes = union.getTypes(); + + if (memberTypes.length === 0) { + context.reportError(`Union type ${union.name} must define one or more member types.`, [ + union.astNode, + ...union.extensionASTNodes, + ]); + } + + const includedTypeNames = Object.create(null); + for (const memberType of memberTypes) { + if (includedTypeNames[memberType.name]) { + context.reportError( + `Union type ${union.name} can only include type ${memberType.name} once.`, + getUnionMemberTypeNodes(union, memberType.name) + ); + continue; + } + includedTypeNames[memberType.name] = true; + if (!isObjectType(memberType)) { + context.reportError( + `Union type ${union.name} can only include Object types, ` + `it cannot include ${inspect(memberType)}.`, + getUnionMemberTypeNodes(union, String(memberType)) + ); + } + } +} + +function validateEnumValues(context: SchemaValidationContext, enumType: GraphQLEnumType): void { + const enumValues = enumType.getValues(); + + if (enumValues.length === 0) { + context.reportError(`Enum type ${enumType.name} must define one or more values.`, [ + enumType.astNode, + ...enumType.extensionASTNodes, + ]); + } + + for (const enumValue of enumValues) { + // Ensure valid name. + validateName(context, enumValue); + } +} + +function validateInputFields(context: SchemaValidationContext, inputObj: GraphQLInputObjectType): void { + const fields = Object.values(inputObj.getFields()); + + if (fields.length === 0) { + context.reportError(`Input Object type ${inputObj.name} must define one or more fields.`, [ + inputObj.astNode, + ...inputObj.extensionASTNodes, + ]); + } + + // Ensure the arguments are valid + for (const field of fields) { + // Ensure they are named correctly. + validateName(context, field); + + // Ensure the type is an input type + if (!isInputType(field.type)) { + context.reportError( + `The type of ${inputObj.name}.${field.name} must be Input Type ` + `but got: ${inspect(field.type)}.`, + field.astNode?.type + ); + } + + if (isRequiredInputField(field) && field.deprecationReason != null) { + context.reportError(`Required input field ${inputObj.name}.${field.name} cannot be deprecated.`, [ + getDeprecatedDirectiveNode(field.astNode), + field.astNode?.type, + ]); + } + } +} + +function createInputObjectCircularRefsValidator( + context: SchemaValidationContext +): (inputObj: GraphQLInputObjectType) => void { + // Modified copy of algorithm from 'src/validation/rules/NoFragmentCycles.js'. + // Tracks already visited types to maintain O(N) and to ensure that cycles + // are not redundantly reported. + const visitedTypes = Object.create(null); + + // Array of types nodes used to produce meaningful errors + const fieldPath: Array = []; + + // Position in the type path + const fieldPathIndexByTypeName = Object.create(null); + + return detectCycleRecursive; + + // This does a straight-forward DFS to find cycles. + // It does not terminate when a cycle was found but continues to explore + // the graph to find all possible cycles. + function detectCycleRecursive(inputObj: GraphQLInputObjectType): void { + if (visitedTypes[inputObj.name]) { + return; + } + + visitedTypes[inputObj.name] = true; + fieldPathIndexByTypeName[inputObj.name] = fieldPath.length; + + const fields = Object.values(inputObj.getFields()); + for (const field of fields) { + if (isNonNullType(field.type) && isInputObjectType(field.type.ofType)) { + const fieldType = field.type.ofType; + const cycleIndex = fieldPathIndexByTypeName[fieldType.name]; + + fieldPath.push(field); + if (cycleIndex === undefined) { + detectCycleRecursive(fieldType); + } else { + const cyclePath = fieldPath.slice(cycleIndex); + const pathStr = cyclePath.map(fieldObj => fieldObj.name).join('.'); + context.reportError( + `Cannot reference Input Object "${fieldType.name}" within itself through a series of non-null fields: "${pathStr}".`, + cyclePath.map(fieldObj => fieldObj.astNode) + ); + } + fieldPath.pop(); + } + } + + fieldPathIndexByTypeName[inputObj.name] = undefined; + } +} + +function getAllImplementsInterfaceNodes( + type: GraphQLObjectType | GraphQLInterfaceType, + iface: GraphQLInterfaceType +): ReadonlyArray { + const { astNode, extensionASTNodes } = type; + const nodes: ReadonlyArray< + ObjectTypeDefinitionNode | ObjectTypeExtensionNode | InterfaceTypeDefinitionNode | InterfaceTypeExtensionNode + > = astNode != null ? [astNode, ...extensionASTNodes] : extensionASTNodes; + + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + return nodes + .flatMap(typeNode => /* c8 ignore next */ typeNode.interfaces ?? []) + .filter(ifaceNode => ifaceNode.name.value === iface.name); +} + +function getUnionMemberTypeNodes(union: GraphQLUnionType, typeName: string): Maybe> { + const { astNode, extensionASTNodes } = union; + const nodes: ReadonlyArray = + astNode != null ? [astNode, ...extensionASTNodes] : extensionASTNodes; + + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + return nodes + .flatMap(unionNode => /* c8 ignore next */ unionNode.types ?? []) + .filter(typeNode => typeNode.name.value === typeName); +} + +function getDeprecatedDirectiveNode( + definitionNode: Maybe<{ readonly directives?: ReadonlyArray }> +): Maybe { + return definitionNode?.directives?.find(node => node.name.value === GraphQLDeprecatedDirective.name); +} diff --git a/packages/graphql/src/utilities/TypeInfo.ts b/packages/graphql/src/utilities/TypeInfo.ts new file mode 100644 index 00000000000..31198cd6cf6 --- /dev/null +++ b/packages/graphql/src/utilities/TypeInfo.ts @@ -0,0 +1,324 @@ +import type { Maybe } from '../jsutils/Maybe.js'; + +import type { ASTNode, FieldNode } from '../language/ast.js'; +import { isNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; +import type { ASTVisitor } from '../language/visitor.js'; +import { getEnterLeaveForKind } from '../language/visitor.js'; + +import type { + GraphQLArgument, + GraphQLCompositeType, + GraphQLEnumValue, + GraphQLField, + GraphQLInputField, + GraphQLInputType, + GraphQLOutputType, + GraphQLType, +} from '../type/definition.js'; +import { + getNamedType, + getNullableType, + isCompositeType, + isEnumType, + isInputObjectType, + isInputType, + isListType, + isObjectType, + isOutputType, +} from '../type/definition.js'; +import type { GraphQLDirective } from '../type/directives.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +import { typeFromAST } from './typeFromAST.js'; + +/** + * TypeInfo is a utility class which, given a GraphQL schema, can keep track + * of the current field and type definitions at any point in a GraphQL document + * AST during a recursive descent by calling `enter(node)` and `leave(node)`. + */ +export class TypeInfo { + private _schema: GraphQLSchema; + private _typeStack: Array>; + private _parentTypeStack: Array>; + private _inputTypeStack: Array>; + private _fieldDefStack: Array>>; + private _defaultValueStack: Array>; + private _directive: Maybe; + private _argument: Maybe; + private _enumValue: Maybe; + private _getFieldDef: GetFieldDefFn; + + constructor( + schema: GraphQLSchema, + /** + * Initial type may be provided in rare cases to facilitate traversals + * beginning somewhere other than documents. + */ + initialType?: Maybe, + + /** @deprecated will be removed in 17.0.0 */ + getFieldDefFn?: GetFieldDefFn + ) { + this._schema = schema; + this._typeStack = []; + this._parentTypeStack = []; + this._inputTypeStack = []; + this._fieldDefStack = []; + this._defaultValueStack = []; + this._directive = null; + this._argument = null; + this._enumValue = null; + this._getFieldDef = getFieldDefFn ?? getFieldDef; + if (initialType) { + if (isInputType(initialType)) { + this._inputTypeStack.push(initialType); + } + if (isCompositeType(initialType)) { + this._parentTypeStack.push(initialType); + } + if (isOutputType(initialType)) { + this._typeStack.push(initialType); + } + } + } + + get [Symbol.toStringTag]() { + return 'TypeInfo'; + } + + getType(): Maybe { + if (this._typeStack.length > 0) { + return this._typeStack[this._typeStack.length - 1]; + } + return undefined; + } + + getParentType(): Maybe { + if (this._parentTypeStack.length > 0) { + return this._parentTypeStack[this._parentTypeStack.length - 1]; + } + return undefined; + } + + getInputType(): Maybe { + if (this._inputTypeStack.length > 0) { + return this._inputTypeStack[this._inputTypeStack.length - 1]; + } + return undefined; + } + + getParentInputType(): Maybe { + if (this._inputTypeStack.length > 1) { + return this._inputTypeStack[this._inputTypeStack.length - 2]; + } + return undefined; + } + + getFieldDef(): Maybe> { + if (this._fieldDefStack.length > 0) { + return this._fieldDefStack[this._fieldDefStack.length - 1]; + } + return undefined; + } + + getDefaultValue(): Maybe { + if (this._defaultValueStack.length > 0) { + return this._defaultValueStack[this._defaultValueStack.length - 1]; + } + return undefined; + } + + getDirective(): Maybe { + return this._directive; + } + + getArgument(): Maybe { + return this._argument; + } + + getEnumValue(): Maybe { + return this._enumValue; + } + + enter(node: ASTNode) { + const schema = this._schema; + // Note: many of the types below are explicitly typed as "unknown" to drop + // any assumptions of a valid schema to ensure runtime types are properly + // checked before continuing since TypeInfo is used as part of validation + // which occurs before guarantees of schema and document validity. + switch (node.kind) { + case Kind.SELECTION_SET: { + const namedType: unknown = getNamedType(this.getType()); + this._parentTypeStack.push(isCompositeType(namedType) ? namedType : undefined); + break; + } + case Kind.FIELD: { + const parentType = this.getParentType(); + let fieldDef; + let fieldType: unknown; + if (parentType) { + fieldDef = this._getFieldDef(schema, parentType, node); + if (fieldDef) { + fieldType = fieldDef.type; + } + } + this._fieldDefStack.push(fieldDef); + this._typeStack.push(isOutputType(fieldType) ? fieldType : undefined); + break; + } + case Kind.DIRECTIVE: + this._directive = schema.getDirective(node.name.value); + break; + case Kind.OPERATION_DEFINITION: { + const rootType = schema.getRootType(node.operation); + this._typeStack.push(isObjectType(rootType) ? rootType : undefined); + break; + } + case Kind.INLINE_FRAGMENT: + case Kind.FRAGMENT_DEFINITION: { + const typeConditionAST = node.typeCondition; + const outputType: unknown = typeConditionAST + ? typeFromAST(schema, typeConditionAST) + : getNamedType(this.getType()); + this._typeStack.push(isOutputType(outputType) ? outputType : undefined); + break; + } + case Kind.VARIABLE_DEFINITION: { + const inputType: unknown = typeFromAST(schema, node.type); + this._inputTypeStack.push(isInputType(inputType) ? inputType : undefined); + break; + } + case Kind.ARGUMENT: { + let argDef; + let argType: unknown; + const fieldOrDirective = this.getDirective() ?? this.getFieldDef(); + if (fieldOrDirective) { + argDef = fieldOrDirective.args.find(arg => arg.name === node.name.value); + if (argDef) { + argType = argDef.type; + } + } + this._argument = argDef; + this._defaultValueStack.push(argDef ? argDef.defaultValue : undefined); + this._inputTypeStack.push(isInputType(argType) ? argType : undefined); + break; + } + case Kind.LIST: { + const listType: unknown = getNullableType(this.getInputType()); + const itemType: unknown = isListType(listType) ? listType.ofType : listType; + // List positions never have a default value. + this._defaultValueStack.push(undefined); + this._inputTypeStack.push(isInputType(itemType) ? itemType : undefined); + break; + } + case Kind.OBJECT_FIELD: { + const objectType: unknown = getNamedType(this.getInputType()); + let inputFieldType: GraphQLInputType | undefined; + let inputField: GraphQLInputField | undefined; + if (isInputObjectType(objectType)) { + inputField = objectType.getFields()[node.name.value]; + if (inputField) { + inputFieldType = inputField.type; + } + } + this._defaultValueStack.push(inputField ? inputField.defaultValue : undefined); + this._inputTypeStack.push(isInputType(inputFieldType) ? inputFieldType : undefined); + break; + } + case Kind.ENUM: { + const enumType: unknown = getNamedType(this.getInputType()); + let enumValue; + if (isEnumType(enumType)) { + enumValue = enumType.getValue(node.value); + } + this._enumValue = enumValue; + break; + } + default: + // Ignore other nodes + } + } + + leave(node: ASTNode) { + switch (node.kind) { + case Kind.SELECTION_SET: + this._parentTypeStack.pop(); + break; + case Kind.FIELD: + this._fieldDefStack.pop(); + this._typeStack.pop(); + break; + case Kind.DIRECTIVE: + this._directive = null; + break; + case Kind.OPERATION_DEFINITION: + case Kind.INLINE_FRAGMENT: + case Kind.FRAGMENT_DEFINITION: + this._typeStack.pop(); + break; + case Kind.VARIABLE_DEFINITION: + this._inputTypeStack.pop(); + break; + case Kind.ARGUMENT: + this._argument = null; + this._defaultValueStack.pop(); + this._inputTypeStack.pop(); + break; + case Kind.LIST: + case Kind.OBJECT_FIELD: + this._defaultValueStack.pop(); + this._inputTypeStack.pop(); + break; + case Kind.ENUM: + this._enumValue = null; + break; + default: + // Ignore other nodes + } + } +} + +type GetFieldDefFn = ( + schema: GraphQLSchema, + parentType: GraphQLCompositeType, + fieldNode: FieldNode +) => Maybe>; + +function getFieldDef(schema: GraphQLSchema, parentType: GraphQLCompositeType, fieldNode: FieldNode) { + return schema.getField(parentType, fieldNode.name.value); +} + +/** + * Creates a new visitor instance which maintains a provided TypeInfo instance + * along with visiting visitor. + */ +export function visitWithTypeInfo(typeInfo: TypeInfo, visitor: ASTVisitor): ASTVisitor { + return { + enter(...args) { + const node = args[0]; + typeInfo.enter(node); + const fn = getEnterLeaveForKind(visitor, node.kind).enter; + if (fn) { + const result = fn.apply(visitor, args); + if (result !== undefined) { + typeInfo.leave(node); + if (isNode(result)) { + typeInfo.enter(result); + } + } + return result; + } + }, + leave(...args) { + const node = args[0]; + const fn = getEnterLeaveForKind(visitor, node.kind).leave; + let result; + if (fn) { + result = fn.apply(visitor, args); + } + typeInfo.leave(node); + return result; + }, + }; +} diff --git a/packages/graphql/src/utilities/__tests__/TypeInfo-test.ts b/packages/graphql/src/utilities/__tests__/TypeInfo-test.ts new file mode 100644 index 00000000000..015f23c42cf --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/TypeInfo-test.ts @@ -0,0 +1,491 @@ +import { parse, parseValue } from '../../language/parser.js'; +import { print } from '../../language/printer.js'; +import { visit } from '../../language/visitor.js'; + +import { getNamedType, isCompositeType } from '../../type/definition.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../buildASTSchema.js'; +import { TypeInfo, visitWithTypeInfo } from '../TypeInfo.js'; + +const testSchema = buildSchema(` + interface Pet { + name: String + } + + type Dog implements Pet { + name: String + } + + type Cat implements Pet { + name: String + } + + type Human { + name: String + pets: [Pet] + } + + type Alien { + name(surname: Boolean): String + } + + union HumanOrAlien = Human | Alien + + type QueryRoot { + human(id: ID): Human + alien: Alien + humanOrAlien: HumanOrAlien + pet: Pet + } + + schema { + query: QueryRoot + } +`); + +describe('TypeInfo', () => { + const schema = new GraphQLSchema({}); + + it('can be Object.toStringified', () => { + const typeInfo = new TypeInfo(schema); + + expect(Object.prototype.toString.call(typeInfo)).toEqual('[object TypeInfo]'); + }); + + it('allow all methods to be called before entering any node', () => { + const typeInfo = new TypeInfo(schema); + + expect(typeInfo.getType()).toEqual(undefined); + expect(typeInfo.getParentType()).toEqual(undefined); + expect(typeInfo.getInputType()).toEqual(undefined); + expect(typeInfo.getParentInputType()).toEqual(undefined); + expect(typeInfo.getFieldDef()).toEqual(undefined); + expect(typeInfo.getDefaultValue()).toEqual(undefined); + expect(typeInfo.getDirective()).toEqual(null); + expect(typeInfo.getArgument()).toEqual(null); + expect(typeInfo.getEnumValue()).toEqual(null); + }); +}); + +describe('visitWithTypeInfo', () => { + it('supports different operation types', () => { + const schema = buildSchema(` + schema { + query: QueryRoot + mutation: MutationRoot + subscription: SubscriptionRoot + } + + type QueryRoot { + foo: String + } + + type MutationRoot { + bar: String + } + + type SubscriptionRoot { + baz: String + } + `); + const ast = parse(` + query { foo } + mutation { bar } + subscription { baz } + `); + const typeInfo = new TypeInfo(schema); + + const rootTypes: any = {}; + visit( + ast, + visitWithTypeInfo(typeInfo, { + OperationDefinition(node) { + rootTypes[node.operation] = String(typeInfo.getType()); + }, + }) + ); + + expect(rootTypes).toEqual({ + query: 'QueryRoot', + mutation: 'MutationRoot', + subscription: 'SubscriptionRoot', + }); + }); + + it('provide exact same arguments to wrapped visitor', () => { + const ast = parse('{ human(id: 4) { name, pets { ... { name } }, unknown } }'); + + const visitorArgs: Array = []; + visit(ast, { + enter(...args) { + visitorArgs.push(['enter', ...args]); + }, + leave(...args) { + visitorArgs.push(['leave', ...args]); + }, + }); + + const wrappedVisitorArgs: Array = []; + const typeInfo = new TypeInfo(testSchema); + visit( + ast, + visitWithTypeInfo(typeInfo, { + enter(...args) { + wrappedVisitorArgs.push(['enter', ...args]); + }, + leave(...args) { + wrappedVisitorArgs.push(['leave', ...args]); + }, + }) + ); + + expect(visitorArgs).toEqual(wrappedVisitorArgs); + }); + + it('supports introspection fields', () => { + const typeInfo = new TypeInfo(testSchema); + + const ast = parse(` + { + __typename + __type(name: "Cat") { __typename } + __schema { + __typename # in object type + } + humanOrAlien { + __typename # in union type + } + pet { + __typename # in interface type + } + someUnknownType { + __typename # unknown + } + pet { + __type # unknown + __schema # unknown + } + } + `); + + const visitedFields: Array<[string | undefined, string | undefined]> = []; + visit( + ast, + visitWithTypeInfo(typeInfo, { + Field() { + const typeName = typeInfo.getParentType()?.name; + const fieldName = typeInfo.getFieldDef()?.name; + visitedFields.push([typeName, fieldName]); + }, + }) + ); + + expect(visitedFields).toEqual([ + ['QueryRoot', '__typename'], + ['QueryRoot', '__type'], + ['__Type', '__typename'], + ['QueryRoot', '__schema'], + ['__Schema', '__typename'], + ['QueryRoot', 'humanOrAlien'], + ['HumanOrAlien', '__typename'], + ['QueryRoot', 'pet'], + ['Pet', '__typename'], + ['QueryRoot', undefined], + [undefined, undefined], + ['QueryRoot', 'pet'], + ['Pet', undefined], + ['Pet', undefined], + ]); + }); + + it('maintains type info during visit', () => { + const visited: Array = []; + + const typeInfo = new TypeInfo(testSchema); + + const ast = parse('{ human(id: 4) { name, pets { ... { name } }, unknown } }'); + + visit( + ast, + visitWithTypeInfo(typeInfo, { + enter(node) { + const parentType = typeInfo.getParentType(); + const type = typeInfo.getType(); + const inputType = typeInfo.getInputType(); + visited.push([ + 'enter', + node.kind, + node.kind === 'Name' ? node.value : null, + parentType ? String(parentType) : null, + type ? String(type) : null, + inputType ? String(inputType) : null, + ]); + }, + leave(node) { + const parentType = typeInfo.getParentType(); + const type = typeInfo.getType(); + const inputType = typeInfo.getInputType(); + visited.push([ + 'leave', + node.kind, + node.kind === 'Name' ? node.value : null, + parentType ? String(parentType) : null, + type ? String(type) : null, + inputType ? String(inputType) : null, + ]); + }, + }) + ); + + expect(visited).toEqual([ + ['enter', 'Document', null, null, null, null], + ['enter', 'OperationDefinition', null, null, 'QueryRoot', null], + ['enter', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], + ['enter', 'Field', null, 'QueryRoot', 'Human', null], + ['enter', 'Name', 'human', 'QueryRoot', 'Human', null], + ['leave', 'Name', 'human', 'QueryRoot', 'Human', null], + ['enter', 'Argument', null, 'QueryRoot', 'Human', 'ID'], + ['enter', 'Name', 'id', 'QueryRoot', 'Human', 'ID'], + ['leave', 'Name', 'id', 'QueryRoot', 'Human', 'ID'], + ['enter', 'IntValue', null, 'QueryRoot', 'Human', 'ID'], + ['leave', 'IntValue', null, 'QueryRoot', 'Human', 'ID'], + ['leave', 'Argument', null, 'QueryRoot', 'Human', 'ID'], + ['enter', 'SelectionSet', null, 'Human', 'Human', null], + ['enter', 'Field', null, 'Human', 'String', null], + ['enter', 'Name', 'name', 'Human', 'String', null], + ['leave', 'Name', 'name', 'Human', 'String', null], + ['leave', 'Field', null, 'Human', 'String', null], + ['enter', 'Field', null, 'Human', '[Pet]', null], + ['enter', 'Name', 'pets', 'Human', '[Pet]', null], + ['leave', 'Name', 'pets', 'Human', '[Pet]', null], + ['enter', 'SelectionSet', null, 'Pet', '[Pet]', null], + ['enter', 'InlineFragment', null, 'Pet', 'Pet', null], + ['enter', 'SelectionSet', null, 'Pet', 'Pet', null], + ['enter', 'Field', null, 'Pet', 'String', null], + ['enter', 'Name', 'name', 'Pet', 'String', null], + ['leave', 'Name', 'name', 'Pet', 'String', null], + ['leave', 'Field', null, 'Pet', 'String', null], + ['leave', 'SelectionSet', null, 'Pet', 'Pet', null], + ['leave', 'InlineFragment', null, 'Pet', 'Pet', null], + ['leave', 'SelectionSet', null, 'Pet', '[Pet]', null], + ['leave', 'Field', null, 'Human', '[Pet]', null], + ['enter', 'Field', null, 'Human', null, null], + ['enter', 'Name', 'unknown', 'Human', null, null], + ['leave', 'Name', 'unknown', 'Human', null, null], + ['leave', 'Field', null, 'Human', null, null], + ['leave', 'SelectionSet', null, 'Human', 'Human', null], + ['leave', 'Field', null, 'QueryRoot', 'Human', null], + ['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], + ['leave', 'OperationDefinition', null, null, 'QueryRoot', null], + ['leave', 'Document', null, null, null, null], + ]); + }); + + it('maintains type info during edit', () => { + const visited: Array = []; + const typeInfo = new TypeInfo(testSchema); + + const ast = parse('{ human(id: 4) { name, pets }, alien }'); + const editedAST = visit( + ast, + visitWithTypeInfo(typeInfo, { + enter(node) { + const parentType = typeInfo.getParentType(); + const type = typeInfo.getType(); + const inputType = typeInfo.getInputType(); + visited.push([ + 'enter', + node.kind, + node.kind === 'Name' ? node.value : null, + parentType ? String(parentType) : null, + type ? String(type) : null, + inputType ? String(inputType) : null, + ]); + + // Make a query valid by adding missing selection sets. + if (node.kind === 'Field' && !node.selectionSet && isCompositeType(getNamedType(type))) { + return { + ...node, + selectionSet: { + kind: 'SelectionSet', + selections: [ + { + kind: 'Field', + name: { kind: 'Name', value: '__typename' }, + }, + ], + }, + }; + } + return undefined; + }, + leave(node) { + const parentType = typeInfo.getParentType(); + const type = typeInfo.getType(); + const inputType = typeInfo.getInputType(); + visited.push([ + 'leave', + node.kind, + node.kind === 'Name' ? node.value : null, + parentType ? String(parentType) : null, + type ? String(type) : null, + inputType ? String(inputType) : null, + ]); + }, + }) + ); + + expect(print(ast)).toEqual(print(parse('{ human(id: 4) { name, pets }, alien }'))); + + expect(print(editedAST)).toEqual( + print(parse('{ human(id: 4) { name, pets { __typename } }, alien { __typename } }')) + ); + + expect(visited).toEqual([ + ['enter', 'Document', null, null, null, null], + ['enter', 'OperationDefinition', null, null, 'QueryRoot', null], + ['enter', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], + ['enter', 'Field', null, 'QueryRoot', 'Human', null], + ['enter', 'Name', 'human', 'QueryRoot', 'Human', null], + ['leave', 'Name', 'human', 'QueryRoot', 'Human', null], + ['enter', 'Argument', null, 'QueryRoot', 'Human', 'ID'], + ['enter', 'Name', 'id', 'QueryRoot', 'Human', 'ID'], + ['leave', 'Name', 'id', 'QueryRoot', 'Human', 'ID'], + ['enter', 'IntValue', null, 'QueryRoot', 'Human', 'ID'], + ['leave', 'IntValue', null, 'QueryRoot', 'Human', 'ID'], + ['leave', 'Argument', null, 'QueryRoot', 'Human', 'ID'], + ['enter', 'SelectionSet', null, 'Human', 'Human', null], + ['enter', 'Field', null, 'Human', 'String', null], + ['enter', 'Name', 'name', 'Human', 'String', null], + ['leave', 'Name', 'name', 'Human', 'String', null], + ['leave', 'Field', null, 'Human', 'String', null], + ['enter', 'Field', null, 'Human', '[Pet]', null], + ['enter', 'Name', 'pets', 'Human', '[Pet]', null], + ['leave', 'Name', 'pets', 'Human', '[Pet]', null], + ['enter', 'SelectionSet', null, 'Pet', '[Pet]', null], + ['enter', 'Field', null, 'Pet', 'String!', null], + ['enter', 'Name', '__typename', 'Pet', 'String!', null], + ['leave', 'Name', '__typename', 'Pet', 'String!', null], + ['leave', 'Field', null, 'Pet', 'String!', null], + ['leave', 'SelectionSet', null, 'Pet', '[Pet]', null], + ['leave', 'Field', null, 'Human', '[Pet]', null], + ['leave', 'SelectionSet', null, 'Human', 'Human', null], + ['leave', 'Field', null, 'QueryRoot', 'Human', null], + ['enter', 'Field', null, 'QueryRoot', 'Alien', null], + ['enter', 'Name', 'alien', 'QueryRoot', 'Alien', null], + ['leave', 'Name', 'alien', 'QueryRoot', 'Alien', null], + ['enter', 'SelectionSet', null, 'Alien', 'Alien', null], + ['enter', 'Field', null, 'Alien', 'String!', null], + ['enter', 'Name', '__typename', 'Alien', 'String!', null], + ['leave', 'Name', '__typename', 'Alien', 'String!', null], + ['leave', 'Field', null, 'Alien', 'String!', null], + ['leave', 'SelectionSet', null, 'Alien', 'Alien', null], + ['leave', 'Field', null, 'QueryRoot', 'Alien', null], + ['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], + ['leave', 'OperationDefinition', null, null, 'QueryRoot', null], + ['leave', 'Document', null, null, null, null], + ]); + }); + + it('supports traversals of input values', () => { + const schema = buildSchema(` + input ComplexInput { + stringListField: [String] + } + `); + const ast = parseValue('{ stringListField: ["foo"] }'); + const complexInputType = schema.getType('ComplexInput'); + expect(complexInputType != null).toBeTruthy(); + + const typeInfo = new TypeInfo(schema, complexInputType); + + const visited: Array = []; + visit( + ast, + visitWithTypeInfo(typeInfo, { + enter(node) { + const type = typeInfo.getInputType(); + visited.push(['enter', node.kind, node.kind === 'Name' ? node.value : null, String(type)]); + }, + leave(node) { + const type = typeInfo.getInputType(); + visited.push(['leave', node.kind, node.kind === 'Name' ? node.value : null, String(type)]); + }, + }) + ); + + expect(visited).toEqual([ + ['enter', 'ObjectValue', null, 'ComplexInput'], + ['enter', 'ObjectField', null, '[String]'], + ['enter', 'Name', 'stringListField', '[String]'], + ['leave', 'Name', 'stringListField', '[String]'], + ['enter', 'ListValue', null, 'String'], + ['enter', 'StringValue', null, 'String'], + ['leave', 'StringValue', null, 'String'], + ['leave', 'ListValue', null, 'String'], + ['leave', 'ObjectField', null, '[String]'], + ['leave', 'ObjectValue', null, 'ComplexInput'], + ]); + }); + + it('supports traversals of selection sets', () => { + const humanType = testSchema.getType('Human'); + expect(humanType != null).toBeTruthy(); + + const typeInfo = new TypeInfo(testSchema, humanType); + + const ast = parse('{ name, pets { name } }'); + const operationNode = ast.definitions[0]; + expect(operationNode.kind === 'OperationDefinition').toBeTruthy(); + + const visited: Array = []; + visit( + // @ts-expect-error + operationNode.selectionSet, + visitWithTypeInfo(typeInfo, { + enter(node) { + const parentType = typeInfo.getParentType(); + const type = typeInfo.getType(); + visited.push([ + 'enter', + node.kind, + node.kind === 'Name' ? node.value : null, + String(parentType), + String(type), + ]); + }, + leave(node) { + const parentType = typeInfo.getParentType(); + const type = typeInfo.getType(); + visited.push([ + 'leave', + node.kind, + node.kind === 'Name' ? node.value : null, + String(parentType), + String(type), + ]); + }, + }) + ); + + expect(visited).toEqual([ + ['enter', 'SelectionSet', null, 'Human', 'Human'], + ['enter', 'Field', null, 'Human', 'String'], + ['enter', 'Name', 'name', 'Human', 'String'], + ['leave', 'Name', 'name', 'Human', 'String'], + ['leave', 'Field', null, 'Human', 'String'], + ['enter', 'Field', null, 'Human', '[Pet]'], + ['enter', 'Name', 'pets', 'Human', '[Pet]'], + ['leave', 'Name', 'pets', 'Human', '[Pet]'], + ['enter', 'SelectionSet', null, 'Pet', '[Pet]'], + ['enter', 'Field', null, 'Pet', 'String'], + ['enter', 'Name', 'name', 'Pet', 'String'], + ['leave', 'Name', 'name', 'Pet', 'String'], + ['leave', 'Field', null, 'Pet', 'String'], + ['leave', 'SelectionSet', null, 'Pet', '[Pet]'], + ['leave', 'Field', null, 'Human', '[Pet]'], + ['leave', 'SelectionSet', null, 'Human', 'Human'], + ]); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/astFromValue-test.ts b/packages/graphql/src/utilities/__tests__/astFromValue-test.ts new file mode 100644 index 00000000000..6eccb4e7542 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/astFromValue-test.ts @@ -0,0 +1,345 @@ +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, + GraphQLScalarType, +} from '../../type/definition.js'; +import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, GraphQLString } from '../../type/scalars.js'; + +import { astFromValue } from '../astFromValue.js'; + +describe('astFromValue', () => { + it('converts boolean values to ASTs', () => { + expect(astFromValue(true, GraphQLBoolean)).toEqual({ + kind: 'BooleanValue', + value: true, + }); + + expect(astFromValue(false, GraphQLBoolean)).toEqual({ + kind: 'BooleanValue', + value: false, + }); + + expect(astFromValue(undefined, GraphQLBoolean)).toEqual(null); + + expect(astFromValue(null, GraphQLBoolean)).toEqual({ + kind: 'NullValue', + }); + + expect(astFromValue(0, GraphQLBoolean)).toEqual({ + kind: 'BooleanValue', + value: false, + }); + + expect(astFromValue(1, GraphQLBoolean)).toEqual({ + kind: 'BooleanValue', + value: true, + }); + + const NonNullBoolean = new GraphQLNonNull(GraphQLBoolean); + expect(astFromValue(0, NonNullBoolean)).toEqual({ + kind: 'BooleanValue', + value: false, + }); + }); + + it('converts Int values to Int ASTs', () => { + expect(astFromValue(-1, GraphQLInt)).toEqual({ + kind: 'IntValue', + value: '-1', + }); + + expect(astFromValue(123.0, GraphQLInt)).toEqual({ + kind: 'IntValue', + value: '123', + }); + + expect(astFromValue(1e4, GraphQLInt)).toEqual({ + kind: 'IntValue', + value: '10000', + }); + + // GraphQL spec does not allow coercing non-integer values to Int to avoid + // accidental data loss. + expect(() => astFromValue(123.5, GraphQLInt)).toThrow('Int cannot represent non-integer value: 123.5'); + + // Note: outside the bounds of 32bit signed int. + expect(() => astFromValue(1e40, GraphQLInt)).toThrow('Int cannot represent non 32-bit signed integer value: 1e+40'); + + expect(() => astFromValue(NaN, GraphQLInt)).toThrow('Int cannot represent non-integer value: NaN'); + }); + + it('converts Float values to Int/Float ASTs', () => { + expect(astFromValue(-1, GraphQLFloat)).toEqual({ + kind: 'IntValue', + value: '-1', + }); + + expect(astFromValue(123.0, GraphQLFloat)).toEqual({ + kind: 'IntValue', + value: '123', + }); + + expect(astFromValue(123.5, GraphQLFloat)).toEqual({ + kind: 'FloatValue', + value: '123.5', + }); + + expect(astFromValue(1e4, GraphQLFloat)).toEqual({ + kind: 'IntValue', + value: '10000', + }); + + expect(astFromValue(1e40, GraphQLFloat)).toEqual({ + kind: 'FloatValue', + value: '1e+40', + }); + }); + + it('converts String values to String ASTs', () => { + expect(astFromValue('hello', GraphQLString)).toEqual({ + kind: 'StringValue', + value: 'hello', + }); + + expect(astFromValue('VALUE', GraphQLString)).toEqual({ + kind: 'StringValue', + value: 'VALUE', + }); + + expect(astFromValue('VA\nLUE', GraphQLString)).toEqual({ + kind: 'StringValue', + value: 'VA\nLUE', + }); + + expect(astFromValue(123, GraphQLString)).toEqual({ + kind: 'StringValue', + value: '123', + }); + + expect(astFromValue(false, GraphQLString)).toEqual({ + kind: 'StringValue', + value: 'false', + }); + + expect(astFromValue(null, GraphQLString)).toEqual({ + kind: 'NullValue', + }); + + expect(astFromValue(undefined, GraphQLString)).toEqual(null); + }); + + it('converts ID values to Int/String ASTs', () => { + expect(astFromValue('hello', GraphQLID)).toEqual({ + kind: 'StringValue', + value: 'hello', + }); + + expect(astFromValue('VALUE', GraphQLID)).toEqual({ + kind: 'StringValue', + value: 'VALUE', + }); + + // Note: EnumValues cannot contain non-identifier characters + expect(astFromValue('VA\nLUE', GraphQLID)).toEqual({ + kind: 'StringValue', + value: 'VA\nLUE', + }); + + // Note: IntValues are used when possible. + expect(astFromValue(-1, GraphQLID)).toEqual({ + kind: 'IntValue', + value: '-1', + }); + + expect(astFromValue(123, GraphQLID)).toEqual({ + kind: 'IntValue', + value: '123', + }); + + expect(astFromValue('123', GraphQLID)).toEqual({ + kind: 'IntValue', + value: '123', + }); + + expect(astFromValue('01', GraphQLID)).toEqual({ + kind: 'StringValue', + value: '01', + }); + + expect(() => astFromValue(false, GraphQLID)).toThrow('ID cannot represent value: false'); + + expect(astFromValue(null, GraphQLID)).toEqual({ kind: 'NullValue' }); + + expect(astFromValue(undefined, GraphQLID)).toEqual(null); + }); + + it('converts using serialize from a custom scalar type', () => { + const passthroughScalar = new GraphQLScalarType({ + name: 'PassthroughScalar', + serialize(value) { + return value; + }, + }); + + expect(astFromValue('value', passthroughScalar)).toEqual({ + kind: 'StringValue', + value: 'value', + }); + + expect(() => astFromValue(NaN, passthroughScalar)).toThrow('Cannot convert value to AST: NaN.'); + expect(() => astFromValue(Infinity, passthroughScalar)).toThrow('Cannot convert value to AST: Infinity.'); + + const returnNullScalar = new GraphQLScalarType({ + name: 'ReturnNullScalar', + serialize() { + return null; + }, + }); + + expect(astFromValue('value', returnNullScalar)).toEqual(null); + + class SomeClass {} + + const returnCustomClassScalar = new GraphQLScalarType({ + name: 'ReturnCustomClassScalar', + serialize() { + return new SomeClass(); + }, + }); + + expect(() => astFromValue('value', returnCustomClassScalar)).toThrow('Cannot convert value to AST: {}.'); + }); + + it('does not converts NonNull values to NullValue', () => { + const NonNullBoolean = new GraphQLNonNull(GraphQLBoolean); + expect(astFromValue(null, NonNullBoolean)).toEqual(null); + }); + + const complexValue = { someArbitrary: 'complexValue' }; + + const myEnum = new GraphQLEnumType({ + name: 'MyEnum', + values: { + HELLO: {}, + GOODBYE: {}, + COMPLEX: { value: complexValue }, + }, + }); + + it('converts string values to Enum ASTs if possible', () => { + expect(astFromValue('HELLO', myEnum)).toEqual({ + kind: 'EnumValue', + value: 'HELLO', + }); + + expect(astFromValue(complexValue, myEnum)).toEqual({ + kind: 'EnumValue', + value: 'COMPLEX', + }); + + // Note: case sensitive + expect(() => astFromValue('hello', myEnum)).toThrow('Enum "MyEnum" cannot represent value: "hello"'); + + // Note: Not a valid enum value + expect(() => astFromValue('UNKNOWN_VALUE', myEnum)).toThrow( + 'Enum "MyEnum" cannot represent value: "UNKNOWN_VALUE"' + ); + }); + + it('converts array values to List ASTs', () => { + expect(astFromValue(['FOO', 'BAR'], new GraphQLList(GraphQLString))).toEqual({ + kind: 'ListValue', + values: [ + { kind: 'StringValue', value: 'FOO' }, + { kind: 'StringValue', value: 'BAR' }, + ], + }); + + expect(astFromValue(['HELLO', 'GOODBYE'], new GraphQLList(myEnum))).toEqual({ + kind: 'ListValue', + values: [ + { kind: 'EnumValue', value: 'HELLO' }, + { kind: 'EnumValue', value: 'GOODBYE' }, + ], + }); + + function* listGenerator() { + yield 1; + yield 2; + yield 3; + } + + expect(astFromValue(listGenerator(), new GraphQLList(GraphQLInt))).toEqual({ + kind: 'ListValue', + values: [ + { kind: 'IntValue', value: '1' }, + { kind: 'IntValue', value: '2' }, + { kind: 'IntValue', value: '3' }, + ], + }); + }); + + it('converts list singletons', () => { + expect(astFromValue('FOO', new GraphQLList(GraphQLString))).toEqual({ + kind: 'StringValue', + value: 'FOO', + }); + }); + + it('skip invalid list items', () => { + const ast = astFromValue(['FOO', null, 'BAR'], new GraphQLList(new GraphQLNonNull(GraphQLString))); + + expect(ast).toEqual({ + kind: 'ListValue', + values: [ + { kind: 'StringValue', value: 'FOO' }, + { kind: 'StringValue', value: 'BAR' }, + ], + }); + }); + + const inputObj = new GraphQLInputObjectType({ + name: 'MyInputObj', + fields: { + foo: { type: GraphQLFloat }, + bar: { type: myEnum }, + }, + }); + + it('converts input objects', () => { + expect(astFromValue({ foo: 3, bar: 'HELLO' }, inputObj)).toEqual({ + kind: 'ObjectValue', + fields: [ + { + kind: 'ObjectField', + name: { kind: 'Name', value: 'foo' }, + value: { kind: 'IntValue', value: '3' }, + }, + { + kind: 'ObjectField', + name: { kind: 'Name', value: 'bar' }, + value: { kind: 'EnumValue', value: 'HELLO' }, + }, + ], + }); + }); + + it('converts input objects with explicit nulls', () => { + expect(astFromValue({ foo: null }, inputObj)).toEqual({ + kind: 'ObjectValue', + fields: [ + { + kind: 'ObjectField', + name: { kind: 'Name', value: 'foo' }, + value: { kind: 'NullValue' }, + }, + ], + }); + }); + + it('does not converts non-object values as input objects', () => { + expect(astFromValue(5, inputObj)).toEqual(null); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/buildASTSchema-test.ts b/packages/graphql/src/utilities/__tests__/buildASTSchema-test.ts new file mode 100644 index 00000000000..2cb8210411a --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/buildASTSchema-test.ts @@ -0,0 +1,1056 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import type { Maybe } from '../../jsutils/Maybe.js'; + +import type { ASTNode } from '../../language/ast.js'; +import { Kind } from '../../language/kinds.js'; +import { parse } from '../../language/parser.js'; +import { print } from '../../language/printer.js'; + +import { + assertEnumType, + assertInputObjectType, + assertInterfaceType, + assertObjectType, + assertScalarType, + assertUnionType, +} from '../../type/definition.js'; +import { + assertDirective, + GraphQLDeprecatedDirective, + GraphQLIncludeDirective, + GraphQLSkipDirective, + GraphQLSpecifiedByDirective, +} from '../../type/directives.js'; +import { __EnumValue, __Schema } from '../../type/introspection.js'; +import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; +import { validateSchema } from '../../type/validate.js'; + +import { graphqlSync } from '../../graphql.js'; + +import { buildASTSchema, buildSchema } from '../buildASTSchema.js'; +import { printSchema, printType } from '../printSchema.js'; + +/** + * This function does a full cycle of going from a string with the contents of + * the SDL, parsed in a schema AST, materializing that schema AST into an + * in-memory GraphQLSchema, and then finally printing that object into the SDL + */ +function cycleSDL(sdl: string): string { + return printSchema(buildSchema(sdl)); +} + +function expectASTNode(obj: Maybe<{ readonly astNode: Maybe }>) { + expect(obj?.astNode != null).toBeTruthy(); + // @ts-expect-error + return expect(print(obj.astNode)); +} + +function expectExtensionASTNodes(obj: { readonly extensionASTNodes: ReadonlyArray }) { + return expect(obj.extensionASTNodes.map(print).join('\n\n')); +} + +describe('Schema Builder', () => { + it('can use built schema for limited execution', () => { + const schema = buildASTSchema( + parse(` + type Query { + str: String + } + `) + ); + + const result = graphqlSync({ + schema, + source: '{ str }', + rootValue: { str: 123 }, + }); + expect(result.data).toEqual({ str: '123' }); + }); + + it('can build a schema directly from the source', () => { + const schema = buildSchema(` + type Query { + add(x: Int, y: Int): Int + } + `); + + const source = '{ add(x: 34, y: 55) }'; + const rootValue = { + add: ({ x, y }: { x: number; y: number }) => x + y, + }; + expect(graphqlSync({ schema, source, rootValue })).toEqual({ + data: { add: 89 }, + }); + }); + + it('Ignores non-type system definitions', () => { + const sdl = ` + type Query { + str: String + } + + fragment SomeFragment on Query { + str + } + `; + expect(() => buildSchema(sdl)).not.toThrow(); + }); + + it('Match order of default types and directives', () => { + const schema = new GraphQLSchema({}); + const sdlSchema = buildASTSchema({ + kind: Kind.DOCUMENT, + definitions: [], + }); + + expect(sdlSchema.getDirectives()).toEqual(schema.getDirectives()); + + expect(sdlSchema.getTypeMap()).toEqual(schema.getTypeMap()); + expect(Object.keys(sdlSchema.getTypeMap())).toEqual(Object.keys(schema.getTypeMap())); + }); + + it('Empty type', () => { + const sdl = dedent` + type EmptyType + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple type', () => { + const sdl = dedent` + type Query { + str: String + int: Int + float: Float + id: ID + bool: Boolean + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + + const schema = buildSchema(sdl); + // Built-ins are used + expect(schema.getType('Int')).toEqual(GraphQLInt); + expect(schema.getType('Float')).toEqual(GraphQLFloat); + expect(schema.getType('String')).toEqual(GraphQLString); + expect(schema.getType('Boolean')).toEqual(GraphQLBoolean); + expect(schema.getType('ID')).toEqual(GraphQLID); + }); + + it('include standard type only if it is used', () => { + const schema = buildSchema('type Query'); + + // String and Boolean are always included through introspection types + expect(schema.getType('Int')).toEqual(undefined); + expect(schema.getType('Float')).toEqual(undefined); + expect(schema.getType('ID')).toEqual(undefined); + }); + + it('With directives', () => { + const sdl = dedent` + directive @foo(arg: Int) on FIELD + + directive @repeatableFoo(arg: Int) repeatable on FIELD + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Supports descriptions', () => { + const sdl = dedent` + """Do you agree that this is the most creative schema ever?""" + schema { + query: Query + } + + """This is a directive""" + directive @foo( + """It has an argument""" + arg: Int + ) on FIELD + + """Who knows what inside this scalar?""" + scalar MysteryScalar + + """This is a input object type""" + input FooInput { + """It has a field""" + field: Int + } + + """This is a interface type""" + interface Energy { + """It also has a field""" + str: String + } + + """There is nothing inside!""" + union BlackHole + + """With an enum""" + enum Color { + RED + + """Not a creative color""" + GREEN + BLUE + } + + """What a great type""" + type Query { + """And a field to boot""" + str: String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Maintains @include, @skip & @specifiedBy', () => { + const schema = buildSchema('type Query'); + + expect(schema.getDirectives()).toHaveLength(4); + expect(schema.getDirective('skip')).toEqual(GraphQLSkipDirective); + expect(schema.getDirective('include')).toEqual(GraphQLIncludeDirective); + expect(schema.getDirective('deprecated')).toEqual(GraphQLDeprecatedDirective); + expect(schema.getDirective('specifiedBy')).toEqual(GraphQLSpecifiedByDirective); + }); + + it('Overriding directives excludes specified', () => { + const schema = buildSchema(` + directive @skip on FIELD + directive @include on FIELD + directive @deprecated on FIELD_DEFINITION + directive @specifiedBy on FIELD_DEFINITION + `); + + expect(schema.getDirectives()).toHaveLength(4); + expect(schema.getDirective('skip')).not.toEqual(GraphQLSkipDirective); + expect(schema.getDirective('include')).not.toEqual(GraphQLIncludeDirective); + expect(schema.getDirective('deprecated')).not.toEqual(GraphQLDeprecatedDirective); + expect(schema.getDirective('specifiedBy')).not.toEqual(GraphQLSpecifiedByDirective); + }); + + it('Adding directives maintains @include, @skip & @specifiedBy', () => { + const schema = buildSchema(` + directive @foo(arg: Int) on FIELD + `); + + expect(schema.getDirectives()).toHaveLength(5); + expect(schema.getDirective('skip')).not.toEqual(undefined); + expect(schema.getDirective('include')).not.toEqual(undefined); + expect(schema.getDirective('deprecated')).not.toEqual(undefined); + expect(schema.getDirective('specifiedBy')).not.toEqual(undefined); + }); + + it('Type modifiers', () => { + const sdl = dedent` + type Query { + nonNullStr: String! + listOfStrings: [String] + listOfNonNullStrings: [String!] + nonNullListOfStrings: [String]! + nonNullListOfNonNullStrings: [String!]! + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Recursive type', () => { + const sdl = dedent` + type Query { + str: String + recurse: Query + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Two types circular', () => { + const sdl = dedent` + type TypeOne { + str: String + typeTwo: TypeTwo + } + + type TypeTwo { + str: String + typeOne: TypeOne + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Single argument field', () => { + const sdl = dedent` + type Query { + str(int: Int): String + floatToStr(float: Float): String + idToStr(id: ID): String + booleanToStr(bool: Boolean): String + strToStr(bool: String): String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple type with multiple arguments', () => { + const sdl = dedent` + type Query { + str(int: Int, bool: Boolean): String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Empty interface', () => { + const sdl = dedent` + interface EmptyInterface + `; + + const definition = parse(sdl).definitions[0]; + expect(definition.kind === 'InterfaceTypeDefinition' && definition.interfaces).toEqual([]); + + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple type with interface', () => { + const sdl = dedent` + type Query implements WorldInterface { + str: String + } + + interface WorldInterface { + str: String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple interface hierarchy', () => { + const sdl = dedent` + schema { + query: Child + } + + interface Child implements Parent { + str: String + } + + type Hello implements Parent & Child { + str: String + } + + interface Parent { + str: String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Empty enum', () => { + const sdl = dedent` + enum EmptyEnum + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple output enum', () => { + const sdl = dedent` + enum Hello { + WORLD + } + + type Query { + hello: Hello + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple input enum', () => { + const sdl = dedent` + enum Hello { + WORLD + } + + type Query { + str(hello: Hello): String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Multiple value enum', () => { + const sdl = dedent` + enum Hello { + WO + RLD + } + + type Query { + hello: Hello + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Empty union', () => { + const sdl = dedent` + union EmptyUnion + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple Union', () => { + const sdl = dedent` + union Hello = World + + type Query { + hello: Hello + } + + type World { + str: String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Multiple Union', () => { + const sdl = dedent` + union Hello = WorldOne | WorldTwo + + type Query { + hello: Hello + } + + type WorldOne { + str: String + } + + type WorldTwo { + str: String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Can build recursive Union', () => { + const schema = buildSchema(` + union Hello = Hello + + type Query { + hello: Hello + } + `); + const errors = validateSchema(schema); + expect(errors.length > 0).toBeTruthy(); + }); + + it('Custom Scalar', () => { + const sdl = dedent` + scalar CustomScalar + + type Query { + customScalar: CustomScalar + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Empty Input Object', () => { + const sdl = dedent` + input EmptyInputObject + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple Input Object', () => { + const sdl = dedent` + input Input { + int: Int + } + + type Query { + field(in: Input): String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple argument field with default', () => { + const sdl = dedent` + type Query { + str(int: Int = 2): String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Custom scalar argument field with default', () => { + const sdl = dedent` + scalar CustomScalar + + type Query { + str(int: CustomScalar = 2): String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple type with mutation', () => { + const sdl = dedent` + schema { + query: HelloScalars + mutation: Mutation + } + + type HelloScalars { + str: String + int: Int + bool: Boolean + } + + type Mutation { + addHelloScalars(str: String, int: Int, bool: Boolean): HelloScalars + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Simple type with subscription', () => { + const sdl = dedent` + schema { + query: HelloScalars + subscription: Subscription + } + + type HelloScalars { + str: String + int: Int + bool: Boolean + } + + type Subscription { + subscribeHelloScalars(str: String, int: Int, bool: Boolean): HelloScalars + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Unreferenced type implementing referenced interface', () => { + const sdl = dedent` + type Concrete implements Interface { + key: String + } + + interface Interface { + key: String + } + + type Query { + interface: Interface + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Unreferenced interface implementing referenced interface', () => { + const sdl = dedent` + interface Child implements Parent { + key: String + } + + interface Parent { + key: String + } + + type Query { + interfaceField: Parent + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Unreferenced type implementing referenced union', () => { + const sdl = dedent` + type Concrete { + key: String + } + + type Query { + union: Union + } + + union Union = Concrete + `; + expect(cycleSDL(sdl)).toEqual(sdl); + }); + + it('Supports @deprecated', () => { + const sdl = dedent` + enum MyEnum { + VALUE + OLD_VALUE @deprecated + OTHER_VALUE @deprecated(reason: "Terrible reasons") + } + + input MyInput { + oldInput: String @deprecated + otherInput: String @deprecated(reason: "Use newInput") + newInput: String + } + + type Query { + field1: String @deprecated + field2: Int @deprecated(reason: "Because I said so") + enum: MyEnum + field3(oldArg: String @deprecated, arg: String): String + field4(oldArg: String @deprecated(reason: "Why not?"), arg: String): String + field5(arg: MyInput): String + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + + const schema = buildSchema(sdl); + + const myEnum = assertEnumType(schema.getType('MyEnum')); + + const value = myEnum.getValue('VALUE'); + expect(value).toMatchObject({ deprecationReason: undefined }); + + const oldValue = myEnum.getValue('OLD_VALUE'); + expect(oldValue).toMatchObject({ + deprecationReason: 'No longer supported', + }); + + const otherValue = myEnum.getValue('OTHER_VALUE'); + expect(otherValue).toMatchObject({ + deprecationReason: 'Terrible reasons', + }); + + const rootFields = assertObjectType(schema.getType('Query')).getFields(); + expect(rootFields['field1']).toMatchObject({ + deprecationReason: 'No longer supported', + }); + expect(rootFields['field2']).toMatchObject({ + deprecationReason: 'Because I said so', + }); + + const inputFields = assertInputObjectType(schema.getType('MyInput')).getFields(); + + const newInput = inputFields['newInput']; + expect(newInput).toMatchObject({ + deprecationReason: undefined, + }); + + const oldInput = inputFields['oldInput']; + expect(oldInput).toMatchObject({ + deprecationReason: 'No longer supported', + }); + + const otherInput = inputFields['otherInput']; + expect(otherInput).toMatchObject({ + deprecationReason: 'Use newInput', + }); + + const field3OldArg = rootFields['field3'].args[0]; + expect(field3OldArg).toMatchObject({ + deprecationReason: 'No longer supported', + }); + + const field4OldArg = rootFields['field4'].args[0]; + expect(field4OldArg).toMatchObject({ + deprecationReason: 'Why not?', + }); + }); + + it('Supports @specifiedBy', () => { + const sdl = dedent` + scalar Foo @specifiedBy(url: "https://example.com/foo_spec") + + type Query { + foo: Foo @deprecated + } + `; + expect(cycleSDL(sdl)).toEqual(sdl); + + const schema = buildSchema(sdl); + + expect(schema.getType('Foo')).toMatchObject({ + specifiedByURL: 'https://example.com/foo_spec', + }); + }); + + it('Correctly extend scalar type', () => { + const schema = buildSchema(` + scalar SomeScalar + extend scalar SomeScalar @foo + extend scalar SomeScalar @bar + + directive @foo on SCALAR + directive @bar on SCALAR + `); + + const someScalar = assertScalarType(schema.getType('SomeScalar')); + expect(printType(someScalar)).toEqual(dedent` + scalar SomeScalar + `); + + expectASTNode(someScalar).toEqual('scalar SomeScalar'); + expectExtensionASTNodes(someScalar).toEqual(dedent` + extend scalar SomeScalar @foo + + extend scalar SomeScalar @bar + `); + }); + + it('Correctly extend object type', () => { + const schema = buildSchema(` + type SomeObject implements Foo { + first: String + } + + extend type SomeObject implements Bar { + second: Int + } + + extend type SomeObject implements Baz { + third: Float + } + + interface Foo + interface Bar + interface Baz + `); + + const someObject = assertObjectType(schema.getType('SomeObject')); + expect(printType(someObject)).toEqual(dedent` + type SomeObject implements Foo & Bar & Baz { + first: String + second: Int + third: Float + } + `); + + expectASTNode(someObject).toEqual(dedent` + type SomeObject implements Foo { + first: String + } + `); + expectExtensionASTNodes(someObject).toEqual(dedent` + extend type SomeObject implements Bar { + second: Int + } + + extend type SomeObject implements Baz { + third: Float + } + `); + }); + + it('Correctly extend interface type', () => { + const schema = buildSchema(dedent` + interface SomeInterface { + first: String + } + + extend interface SomeInterface { + second: Int + } + + extend interface SomeInterface { + third: Float + } + `); + + const someInterface = assertInterfaceType(schema.getType('SomeInterface')); + expect(printType(someInterface)).toEqual(dedent` + interface SomeInterface { + first: String + second: Int + third: Float + } + `); + + expectASTNode(someInterface).toEqual(dedent` + interface SomeInterface { + first: String + } + `); + expectExtensionASTNodes(someInterface).toEqual(dedent` + extend interface SomeInterface { + second: Int + } + + extend interface SomeInterface { + third: Float + } + `); + }); + + it('Correctly extend union type', () => { + const schema = buildSchema(` + union SomeUnion = FirstType + extend union SomeUnion = SecondType + extend union SomeUnion = ThirdType + + type FirstType + type SecondType + type ThirdType + `); + + const someUnion = assertUnionType(schema.getType('SomeUnion')); + expect(printType(someUnion)).toEqual(dedent` + union SomeUnion = FirstType | SecondType | ThirdType + `); + + expectASTNode(someUnion).toEqual('union SomeUnion = FirstType'); + expectExtensionASTNodes(someUnion).toEqual(dedent` + extend union SomeUnion = SecondType + + extend union SomeUnion = ThirdType + `); + }); + + it('Correctly extend enum type', () => { + const schema = buildSchema(dedent` + enum SomeEnum { + FIRST + } + + extend enum SomeEnum { + SECOND + } + + extend enum SomeEnum { + THIRD + } + `); + + const someEnum = assertEnumType(schema.getType('SomeEnum')); + expect(printType(someEnum)).toEqual(dedent` + enum SomeEnum { + FIRST + SECOND + THIRD + } + `); + + expectASTNode(someEnum).toEqual(dedent` + enum SomeEnum { + FIRST + } + `); + expectExtensionASTNodes(someEnum).toEqual(dedent` + extend enum SomeEnum { + SECOND + } + + extend enum SomeEnum { + THIRD + } + `); + }); + + it('Correctly extend input object type', () => { + const schema = buildSchema(dedent` + input SomeInput { + first: String + } + + extend input SomeInput { + second: Int + } + + extend input SomeInput { + third: Float + } + `); + + const someInput = assertInputObjectType(schema.getType('SomeInput')); + expect(printType(someInput)).toEqual(dedent` + input SomeInput { + first: String + second: Int + third: Float + } + `); + + expectASTNode(someInput).toEqual(dedent` + input SomeInput { + first: String + } + `); + expectExtensionASTNodes(someInput).toEqual(dedent` + extend input SomeInput { + second: Int + } + + extend input SomeInput { + third: Float + } + `); + }); + + it('Correctly assign AST nodes', () => { + const sdl = dedent` + schema { + query: Query + } + + type Query { + testField(testArg: TestInput): TestUnion + } + + input TestInput { + testInputField: TestEnum + } + + enum TestEnum { + TEST_VALUE + } + + union TestUnion = TestType + + interface TestInterface { + interfaceField: String + } + + type TestType implements TestInterface { + interfaceField: String + } + + scalar TestScalar + + directive @test(arg: TestScalar) on FIELD + `; + const ast = parse(sdl, { noLocation: true }); + + const schema = buildASTSchema(ast); + const query = assertObjectType(schema.getType('Query')); + const testInput = assertInputObjectType(schema.getType('TestInput')); + const testEnum = assertEnumType(schema.getType('TestEnum')); + const testUnion = assertUnionType(schema.getType('TestUnion')); + const testInterface = assertInterfaceType(schema.getType('TestInterface')); + const testType = assertObjectType(schema.getType('TestType')); + const testScalar = assertScalarType(schema.getType('TestScalar')); + const testDirective = assertDirective(schema.getDirective('test')); + + expect([ + schema.astNode, + query.astNode, + testInput.astNode, + testEnum.astNode, + testUnion.astNode, + testInterface.astNode, + testType.astNode, + testScalar.astNode, + testDirective.astNode, + ]).toEqual(ast.definitions); + + const testField = query.getFields()['testField']; + expectASTNode(testField).toEqual('testField(testArg: TestInput): TestUnion'); + expectASTNode(testField.args[0]).toEqual('testArg: TestInput'); + expectASTNode(testInput.getFields()['testInputField']).toEqual('testInputField: TestEnum'); + + expectASTNode(testEnum.getValue('TEST_VALUE')).toEqual('TEST_VALUE'); + + expectASTNode(testInterface.getFields()['interfaceField']).toEqual('interfaceField: String'); + expectASTNode(testType.getFields()['interfaceField']).toEqual('interfaceField: String'); + expectASTNode(testDirective.args[0]).toEqual('arg: TestScalar'); + }); + + it('Root operation types with custom names', () => { + const schema = buildSchema(` + schema { + query: SomeQuery + mutation: SomeMutation + subscription: SomeSubscription + } + type SomeQuery + type SomeMutation + type SomeSubscription + `); + + expect(schema.getQueryType()).toMatchObject({ name: 'SomeQuery' }); + expect(schema.getMutationType()).toMatchObject({ name: 'SomeMutation' }); + expect(schema.getSubscriptionType()).toMatchObject({ + name: 'SomeSubscription', + }); + }); + + it('Default root operation type names', () => { + const schema = buildSchema(` + type Query + type Mutation + type Subscription + `); + + expect(schema.getQueryType()).toMatchObject({ name: 'Query' }); + expect(schema.getMutationType()).toMatchObject({ name: 'Mutation' }); + expect(schema.getSubscriptionType()).toMatchObject({ name: 'Subscription' }); + }); + + it('can build invalid schema', () => { + // Invalid schema, because it is missing query root type + const schema = buildSchema('type Mutation'); + const errors = validateSchema(schema); + expect(errors.length > 0).toBeTruthy(); + }); + + it('Do not override standard types', () => { + // NOTE: not sure it's desired behavior to just silently ignore override + // attempts so just documenting it here. + + const schema = buildSchema(` + scalar ID + + scalar __Schema + `); + + expect(schema.getType('ID')).toEqual(GraphQLID); + expect(schema.getType('__Schema')).toEqual(__Schema); + }); + + it('Allows to reference introspection types', () => { + const schema = buildSchema(` + type Query { + introspectionField: __EnumValue + } + `); + + const queryType = assertObjectType(schema.getType('Query')); + expect(queryType.getFields()).toHaveProperty('introspectionField.type', __EnumValue); + expect(schema.getType('__EnumValue')).toEqual(__EnumValue); + }); + + it('Rejects invalid SDL', () => { + const sdl = ` + type Query { + foo: String @unknown + } + `; + expect(() => buildSchema(sdl)).toThrow('Unknown directive "@unknown".'); + }); + + it('Allows to disable SDL validation', () => { + const sdl = ` + type Query { + foo: String @unknown + } + `; + buildSchema(sdl, { assumeValid: true }); + buildSchema(sdl, { assumeValidSDL: true }); + }); + + it('Throws on unknown types', () => { + const sdl = ` + type Query { + unknown: UnknownType + } + `; + expect(() => buildSchema(sdl, { assumeValidSDL: true })).toThrow('Unknown type: "UnknownType".'); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/buildClientSchema-test.ts b/packages/graphql/src/utilities/__tests__/buildClientSchema-test.ts new file mode 100644 index 00000000000..4c183a69096 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/buildClientSchema-test.ts @@ -0,0 +1,917 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import { assertEnumType, GraphQLEnumType, GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { graphqlSync } from '../../graphql.js'; + +import { buildSchema } from '../buildASTSchema.js'; +import { buildClientSchema } from '../buildClientSchema.js'; +import { introspectionFromSchema } from '../introspectionFromSchema.js'; +import { printSchema } from '../printSchema.js'; + +/** + * This function does a full cycle of going from a string with the contents of + * the SDL, build in-memory GraphQLSchema from it, produce a client-side + * representation of the schema by using "buildClientSchema" and then + * returns that schema printed as SDL. + */ +function cycleIntrospection(sdlString: string): string { + const serverSchema = buildSchema(sdlString); + const initialIntrospection = introspectionFromSchema(serverSchema); + const clientSchema = buildClientSchema(initialIntrospection); + const secondIntrospection = introspectionFromSchema(clientSchema); + + /** + * If the client then runs the introspection query against the client-side + * schema, it should get a result identical to what was returned by the server + */ + expect(secondIntrospection).toEqual(initialIntrospection); + return printSchema(clientSchema); +} + +describe('Type System: build schema from introspection', () => { + it('builds a simple schema', () => { + const sdl = dedent` + """Simple schema""" + schema { + query: Simple + } + + """This is a simple type""" + type Simple { + """This is a string field""" + string: String + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema without the query type', () => { + const sdl = dedent` + type Query { + foo: String + } + `; + + const schema = buildSchema(sdl); + const introspection = introspectionFromSchema(schema); + + // @ts-expect-error + delete introspection.__schema.queryType; + + const clientSchema = buildClientSchema(introspection); + expect(clientSchema.getQueryType()).toEqual(null); + expect(printSchema(clientSchema)).toEqual(sdl); + }); + + it('builds a simple schema with all operation types', () => { + const sdl = dedent` + schema { + query: QueryType + mutation: MutationType + subscription: SubscriptionType + } + + """This is a simple mutation type""" + type MutationType { + """Set the string field""" + string: String + } + + """This is a simple query type""" + type QueryType { + """This is a string field""" + string: String + } + + """This is a simple subscription type""" + type SubscriptionType { + """This is a string field""" + string: String + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('uses built-in scalars when possible', () => { + const sdl = dedent` + scalar CustomScalar + + type Query { + int: Int + float: Float + string: String + boolean: Boolean + id: ID + custom: CustomScalar + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + + const schema = buildSchema(sdl); + const introspection = introspectionFromSchema(schema); + const clientSchema = buildClientSchema(introspection); + + // Built-ins are used + expect(clientSchema.getType('Int')).toEqual(GraphQLInt); + expect(clientSchema.getType('Float')).toEqual(GraphQLFloat); + expect(clientSchema.getType('String')).toEqual(GraphQLString); + expect(clientSchema.getType('Boolean')).toEqual(GraphQLBoolean); + expect(clientSchema.getType('ID')).toEqual(GraphQLID); + + // Custom are built + const customScalar = schema.getType('CustomScalar'); + expect(clientSchema.getType('CustomScalar')).not.toEqual(customScalar); + }); + + it('includes standard types only if they are used', () => { + const schema = buildSchema(` + type Query { + foo: String + } + `); + const introspection = introspectionFromSchema(schema); + const clientSchema = buildClientSchema(introspection); + + expect(clientSchema.getType('Int')).toEqual(undefined); + expect(clientSchema.getType('Float')).toEqual(undefined); + expect(clientSchema.getType('ID')).toEqual(undefined); + }); + + it('builds a schema with a recursive type reference', () => { + const sdl = dedent` + schema { + query: Recur + } + + type Recur { + recur: Recur + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with a circular type reference', () => { + const sdl = dedent` + type Dog { + bestFriend: Human + } + + type Human { + bestFriend: Dog + } + + type Query { + dog: Dog + human: Human + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with an interface', () => { + const sdl = dedent` + type Dog implements Friendly { + bestFriend: Friendly + } + + interface Friendly { + """The best friend of this friendly thing""" + bestFriend: Friendly + } + + type Human implements Friendly { + bestFriend: Friendly + } + + type Query { + friendly: Friendly + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with an interface hierarchy', () => { + const sdl = dedent` + type Dog implements Friendly & Named { + bestFriend: Friendly + name: String + } + + interface Friendly implements Named { + """The best friend of this friendly thing""" + bestFriend: Friendly + name: String + } + + type Human implements Friendly & Named { + bestFriend: Friendly + name: String + } + + interface Named { + name: String + } + + type Query { + friendly: Friendly + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with an implicit interface', () => { + const sdl = dedent` + type Dog implements Friendly { + bestFriend: Friendly + } + + interface Friendly { + """The best friend of this friendly thing""" + bestFriend: Friendly + } + + type Query { + dog: Dog + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with a union', () => { + const sdl = dedent` + type Dog { + bestFriend: Friendly + } + + union Friendly = Dog | Human + + type Human { + bestFriend: Friendly + } + + type Query { + friendly: Friendly + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with complex field values', () => { + const sdl = dedent` + type Query { + string: String + listOfString: [String] + nonNullString: String! + nonNullListOfString: [String]! + nonNullListOfNonNullString: [String!]! + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with field arguments', () => { + const sdl = dedent` + type Query { + """A field with a single arg""" + one( + """This is an int arg""" + intArg: Int + ): String + + """A field with a two args""" + two( + """This is an list of int arg""" + listArg: [Int] + + """This is a required arg""" + requiredArg: Boolean! + ): String + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with default value on custom scalar field', () => { + const sdl = dedent` + scalar CustomScalar + + type Query { + testField(testArg: CustomScalar = "default"): String + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with an enum', () => { + const foodEnum = new GraphQLEnumType({ + name: 'Food', + description: 'Varieties of food stuffs', + values: { + VEGETABLES: { + description: 'Foods that are vegetables.', + value: 1, + }, + FRUITS: { + value: 2, + }, + OILS: { + value: 3, + deprecationReason: 'Too fatty', + }, + }, + }); + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'EnumFields', + fields: { + food: { + description: 'Repeats the arg you give it', + type: foodEnum, + args: { + kind: { + description: 'what kind of food?', + type: foodEnum, + }, + }, + }, + }, + }), + }); + + const introspection = introspectionFromSchema(schema); + const clientSchema = buildClientSchema(introspection); + + const secondIntrospection = introspectionFromSchema(clientSchema); + expect(secondIntrospection).toEqual(introspection); + + // It's also an Enum type on the client. + const clientFoodEnum = assertEnumType(clientSchema.getType('Food')); + + // Client types do not get server-only values, so `value` mirrors `name`, + // rather than using the integers defined in the "server" schema. + expect(clientFoodEnum.getValues()).toEqual([ + { + name: 'VEGETABLES', + description: 'Foods that are vegetables.', + value: 'VEGETABLES', + deprecationReason: null, + extensions: {}, + astNode: undefined, + }, + { + name: 'FRUITS', + description: null, + value: 'FRUITS', + deprecationReason: null, + extensions: {}, + astNode: undefined, + }, + { + name: 'OILS', + description: null, + value: 'OILS', + deprecationReason: 'Too fatty', + extensions: {}, + astNode: undefined, + }, + ]); + }); + + it('builds a schema with an input object', () => { + const sdl = dedent` + """An input address""" + input Address { + """What street is this address?""" + street: String! + + """The city the address is within?""" + city: String! + + """The country (blank will assume USA).""" + country: String = "USA" + } + + type Query { + """Get a geocode from an address""" + geocode( + """The address to lookup""" + address: Address + ): String + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with field arguments with default values', () => { + const sdl = dedent` + input Geo { + lat: Float + lon: Float + } + + type Query { + defaultInt(intArg: Int = 30): String + defaultList(listArg: [Int] = [1, 2, 3]): String + defaultObject(objArg: Geo = { lat: 37.485, lon: -122.148 }): String + defaultNull(intArg: Int = null): String + noDefault(intArg: Int): String + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with custom directives', () => { + const sdl = dedent` + """This is a custom directive""" + directive @customDirective repeatable on FIELD + + type Query { + string: String + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema without directives', () => { + const sdl = dedent` + type Query { + string: String + } + `; + + const schema = buildSchema(sdl); + const introspection = introspectionFromSchema(schema); + + // @ts-expect-error + delete introspection.__schema.directives; + + const clientSchema = buildClientSchema(introspection); + + expect(schema.getDirectives().length > 0).toBeTruthy(); + expect(clientSchema.getDirectives()).toEqual([]); + expect(printSchema(clientSchema)).toEqual(sdl); + }); + + it('builds a schema aware of deprecation', () => { + const sdl = dedent` + directive @someDirective( + """This is a shiny new argument""" + shinyArg: SomeInputObject + + """This was our design mistake :(""" + oldArg: String @deprecated(reason: "Use shinyArg") + ) on QUERY + + enum Color { + """So rosy""" + RED + + """So grassy""" + GREEN + + """So calming""" + BLUE + + """So sickening""" + MAUVE @deprecated(reason: "No longer in fashion") + } + + input SomeInputObject { + """Nothing special about it, just deprecated for some unknown reason""" + oldField: String @deprecated(reason: "Don't use it, use newField instead!") + + """Same field but with a new name""" + newField: String + } + + type Query { + """This is a shiny string field""" + shinyString: String + + """This is a deprecated string field""" + deprecatedString: String @deprecated(reason: "Use shinyString") + + """Color of a week""" + color: Color + + """Some random field""" + someField( + """This is a shiny new argument""" + shinyArg: SomeInputObject + + """This was our design mistake :(""" + oldArg: String @deprecated(reason: "Use shinyArg") + ): String + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with empty deprecation reasons', () => { + const sdl = dedent` + directive @someDirective(someArg: SomeInputObject @deprecated(reason: "")) on QUERY + + type Query { + someField(someArg: SomeInputObject @deprecated(reason: "")): SomeEnum @deprecated(reason: "") + } + + input SomeInputObject { + someInputField: String @deprecated(reason: "") + } + + enum SomeEnum { + SOME_VALUE @deprecated(reason: "") + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('builds a schema with specifiedBy url', () => { + const sdl = dedent` + scalar Foo @specifiedBy(url: "https://example.com/foo_spec") + + type Query { + foo: Foo + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + + it('can use client schema for limited execution', () => { + const schema = buildSchema(` + scalar CustomScalar + + type Query { + foo(custom1: CustomScalar, custom2: CustomScalar): String + } + `); + + const introspection = introspectionFromSchema(schema); + const clientSchema = buildClientSchema(introspection); + + const result = graphqlSync({ + schema: clientSchema, + source: 'query Limited($v: CustomScalar) { foo(custom1: 123, custom2: $v) }', + rootValue: { foo: 'bar', unused: 'value' }, + variableValues: { v: 'baz' }, + }); + + expect(result.data).toEqual({ foo: 'bar' }); + }); + + it('can build invalid schema', () => { + const schema = buildSchema('type Query', { assumeValid: true }); + + const introspection = introspectionFromSchema(schema); + const clientSchema = buildClientSchema(introspection, { + assumeValid: true, + }); + + expect(clientSchema.toConfig().assumeValid).toEqual(true); + }); + + describe('throws when given invalid introspection', () => { + const dummySchema = buildSchema(` + type Query { + foo(bar: String): String + } + + interface SomeInterface { + foo: String + } + + union SomeUnion = Query + + enum SomeEnum { FOO } + + input SomeInputObject { + foo: String + } + + directive @SomeDirective on QUERY + `); + + it('throws when introspection is missing __schema property', () => { + // @ts-expect-error (First parameter expected to be introspection results) + expect(() => buildClientSchema(null)).toThrow( + 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: null.' + ); + + // @ts-expect-error + expect(() => buildClientSchema({})).toThrow( + 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: {}.' + ); + }); + + it('throws when referenced unknown type', () => { + const introspection = introspectionFromSchema(dummySchema); + + // @ts-expect-error + introspection.__schema.types = introspection.__schema.types.filter(({ name }) => name !== 'Query'); + + expect(() => buildClientSchema(introspection)).toThrow( + 'Invalid or incomplete schema, unknown type: Query. Ensure that a full introspection query is used in order to build a client schema.' + ); + }); + + it('throws when missing definition for one of the standard scalars', () => { + const schema = buildSchema(` + type Query { + foo: Float + } + `); + const introspection = introspectionFromSchema(schema); + + // @ts-expect-error + introspection.__schema.types = introspection.__schema.types.filter(({ name }) => name !== 'Float'); + + expect(() => buildClientSchema(introspection)).toThrow( + 'Invalid or incomplete schema, unknown type: Float. Ensure that a full introspection query is used in order to build a client schema.' + ); + }); + + it('throws when type reference is missing name', () => { + const introspection = introspectionFromSchema(dummySchema); + + expect(introspection).toHaveProperty('__schema.queryType.name'); + + // @ts-expect-error + delete introspection.__schema.queryType.name; + + expect(() => buildClientSchema(introspection)).toThrow('Unknown type reference: {}.'); + }); + + it('throws when missing kind', () => { + const introspection = introspectionFromSchema(dummySchema); + const queryTypeIntrospection = introspection.__schema.types.find(({ name }) => name === 'Query'); + + expect(queryTypeIntrospection?.kind === 'OBJECT').toBeTruthy(); + // @ts-expect-error + delete queryTypeIntrospection.kind; + + expect(() => buildClientSchema(introspection)).toThrow( + /Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: { name: "Query", .* }\./ + ); + }); + + it('throws when missing interfaces', () => { + const introspection = introspectionFromSchema(dummySchema); + const queryTypeIntrospection = introspection.__schema.types.find(({ name }) => name === 'Query'); + + expect(queryTypeIntrospection).toHaveProperty('interfaces'); + + expect(queryTypeIntrospection?.kind === 'OBJECT').toBeTruthy(); + // @ts-expect-error + delete queryTypeIntrospection.interfaces; + + expect(() => buildClientSchema(introspection)).toThrow( + /Introspection result missing interfaces: { kind: "OBJECT", name: "Query", .* }\./ + ); + }); + + it('Legacy support for interfaces with null as interfaces field', () => { + const introspection = introspectionFromSchema(dummySchema); + const someInterfaceIntrospection = introspection.__schema.types.find(({ name }) => name === 'SomeInterface'); + + expect(someInterfaceIntrospection?.kind === 'INTERFACE').toBeTruthy(); + // @ts-expect-error + someInterfaceIntrospection.interfaces = null; + + const clientSchema = buildClientSchema(introspection); + expect(printSchema(clientSchema)).toEqual(printSchema(dummySchema)); + }); + + it('throws when missing fields', () => { + const introspection = introspectionFromSchema(dummySchema); + const queryTypeIntrospection = introspection.__schema.types.find(({ name }) => name === 'Query'); + + expect(queryTypeIntrospection?.kind === 'OBJECT').toBeTruthy(); + // @ts-expect-error + delete queryTypeIntrospection.fields; + + expect(() => buildClientSchema(introspection)).toThrow( + /Introspection result missing fields: { kind: "OBJECT", name: "Query", .* }\./ + ); + }); + + it('throws when missing field args', () => { + const introspection = introspectionFromSchema(dummySchema); + const queryTypeIntrospection = introspection.__schema.types.find(({ name }) => name === 'Query'); + + expect(queryTypeIntrospection?.kind === 'OBJECT').toBeTruthy(); + // @ts-expect-error + delete queryTypeIntrospection.fields[0].args; + + expect(() => buildClientSchema(introspection)).toThrow( + /Introspection result missing field args: { name: "foo", .* }\./ + ); + }); + + it('throws when output type is used as an arg type', () => { + const introspection = introspectionFromSchema(dummySchema); + const queryTypeIntrospection = introspection.__schema.types.find(({ name }) => name === 'Query'); + + expect(queryTypeIntrospection?.kind === 'OBJECT').toBeTruthy(); + // @ts-expect-error + const argType = queryTypeIntrospection.fields[0].args[0].type; + expect(argType.kind === 'SCALAR').toBeTruthy(); + + expect(argType).toHaveProperty('name', 'String'); + argType.name = 'SomeUnion'; + + expect(() => buildClientSchema(introspection)).toThrow( + 'Introspection must provide input type for arguments, but received: SomeUnion.' + ); + }); + + it('throws when input type is used as a field type', () => { + const introspection = introspectionFromSchema(dummySchema); + const queryTypeIntrospection = introspection.__schema.types.find(({ name }) => name === 'Query'); + + expect(queryTypeIntrospection?.kind === 'OBJECT').toBeTruthy(); + // @ts-expect-error + const fieldType = queryTypeIntrospection.fields[0].type; + expect(fieldType.kind === 'SCALAR').toBeTruthy(); + + expect(fieldType).toHaveProperty('name', 'String'); + fieldType.name = 'SomeInputObject'; + + expect(() => buildClientSchema(introspection)).toThrow( + 'Introspection must provide output type for fields, but received: SomeInputObject.' + ); + }); + + it('throws when missing possibleTypes', () => { + const introspection = introspectionFromSchema(dummySchema); + const someUnionIntrospection = introspection.__schema.types.find(({ name }) => name === 'SomeUnion'); + + expect(someUnionIntrospection?.kind === 'UNION').toBeTruthy(); + // @ts-expect-error + delete someUnionIntrospection.possibleTypes; + + expect(() => buildClientSchema(introspection)).toThrow( + /Introspection result missing possibleTypes: { kind: "UNION", name: "SomeUnion",.* }\./ + ); + }); + + it('throws when missing enumValues', () => { + const introspection = introspectionFromSchema(dummySchema); + const someEnumIntrospection = introspection.__schema.types.find(({ name }) => name === 'SomeEnum'); + + expect(someEnumIntrospection?.kind === 'ENUM').toBeTruthy(); + // @ts-expect-error + delete someEnumIntrospection.enumValues; + + expect(() => buildClientSchema(introspection)).toThrow( + /Introspection result missing enumValues: { kind: "ENUM", name: "SomeEnum", .* }\./ + ); + }); + + it('throws when missing inputFields', () => { + const introspection = introspectionFromSchema(dummySchema); + const someInputObjectIntrospection = introspection.__schema.types.find(({ name }) => name === 'SomeInputObject'); + + expect(someInputObjectIntrospection?.kind === 'INPUT_OBJECT').toBeTruthy(); + // @ts-expect-error + delete someInputObjectIntrospection.inputFields; + + expect(() => buildClientSchema(introspection)).toThrow( + /Introspection result missing inputFields: { kind: "INPUT_OBJECT", name: "SomeInputObject", .* }\./ + ); + }); + + it('throws when missing directive locations', () => { + const introspection = introspectionFromSchema(dummySchema); + + const someDirectiveIntrospection = introspection.__schema.directives[0]; + expect(someDirectiveIntrospection).toMatchObject({ + name: 'SomeDirective', + locations: ['QUERY'], + }); + + // @ts-expect-error + delete someDirectiveIntrospection.locations; + + expect(() => buildClientSchema(introspection)).toThrow( + /Introspection result missing directive locations: { name: "SomeDirective", .* }\./ + ); + }); + + it('throws when missing directive args', () => { + const introspection = introspectionFromSchema(dummySchema); + + const someDirectiveIntrospection = introspection.__schema.directives[0]; + expect(someDirectiveIntrospection).toMatchObject({ + name: 'SomeDirective', + args: [], + }); + + // @ts-expect-error + delete someDirectiveIntrospection.args; + + expect(() => buildClientSchema(introspection)).toThrow( + /Introspection result missing directive args: { name: "SomeDirective", .* }\./ + ); + }); + }); + + describe('very deep decorators are not supported', () => { + it('fails on very deep (> 7 levels) lists', () => { + const schema = buildSchema(` + type Query { + foo: [[[[[[[[String]]]]]]]] + } + `); + + const introspection = introspectionFromSchema(schema); + expect(() => buildClientSchema(introspection)).toThrow('Decorated type deeper than introspection query.'); + }); + + it('fails on a very deep (> 7 levels) non-null', () => { + const schema = buildSchema(` + type Query { + foo: [[[[String!]!]!]!] + } + `); + + const introspection = introspectionFromSchema(schema); + expect(() => buildClientSchema(introspection)).toThrow('Decorated type deeper than introspection query.'); + }); + + it('succeeds on deep (<= 7 levels) types', () => { + // e.g., fully non-null 3D matrix + const sdl = dedent` + type Query { + foo: [[[String!]!]!]! + } + `; + + expect(cycleIntrospection(sdl)).toEqual(sdl); + }); + }); + + describe('prevents infinite recursion on invalid introspection', () => { + it('recursive interfaces', () => { + const sdl = ` + type Query { + foo: Foo + } + + type Foo implements Foo { + foo: String + } + `; + const schema = buildSchema(sdl, { assumeValid: true }); + const introspection = introspectionFromSchema(schema); + + const fooIntrospection = introspection.__schema.types.find(type => type.name === 'Foo'); + expect(fooIntrospection).toMatchObject({ + name: 'Foo', + interfaces: [{ kind: 'OBJECT', name: 'Foo', ofType: null }], + }); + + expect(() => buildClientSchema(introspection)).toThrow('Expected Foo to be a GraphQL Interface type.'); + }); + + it('recursive union', () => { + const sdl = ` + type Query { + foo: Foo + } + + union Foo = Foo + `; + const schema = buildSchema(sdl, { assumeValid: true }); + const introspection = introspectionFromSchema(schema); + + const fooIntrospection = introspection.__schema.types.find(type => type.name === 'Foo'); + expect(fooIntrospection).toMatchObject({ + name: 'Foo', + possibleTypes: [{ kind: 'UNION', name: 'Foo', ofType: null }], + }); + + expect(() => buildClientSchema(introspection)).toThrow('Expected Foo to be a GraphQL Object type.'); + }); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/coerceInputValue-test.ts b/packages/graphql/src/utilities/__tests__/coerceInputValue-test.ts new file mode 100644 index 00000000000..4323e698c47 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/coerceInputValue-test.ts @@ -0,0 +1,405 @@ +import type { GraphQLInputType } from '../../type/definition.js'; +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, + GraphQLScalarType, +} from '../../type/definition.js'; +import { GraphQLInt } from '../../type/scalars.js'; + +import { coerceInputValue } from '../coerceInputValue.js'; + +interface CoerceResult { + value: unknown; + errors: ReadonlyArray; +} + +interface CoerceError { + path: ReadonlyArray; + value: unknown; + error: string; +} + +function coerceValue(inputValue: unknown, type: GraphQLInputType): CoerceResult { + const errors: Array = []; + const value = coerceInputValue(inputValue, type, (path, invalidValue, error) => { + errors.push({ path, value: invalidValue, error: error.message }); + }); + + return { errors, value }; +} + +function expectValue(result: CoerceResult) { + expect(result.errors).toEqual([]); + return expect(result.value); +} + +function expectErrors(result: CoerceResult) { + return expect(result.errors); +} + +describe('coerceInputValue', () => { + describe('for GraphQLNonNull', () => { + const TestNonNull = new GraphQLNonNull(GraphQLInt); + + it('returns no error for non-null value', () => { + const result = coerceValue(1, TestNonNull); + expectValue(result).toEqual(1); + }); + + it('returns an error for undefined value', () => { + const result = coerceValue(undefined, TestNonNull); + expectErrors(result).toEqual([ + { + error: 'Expected non-nullable type "Int!" not to be null.', + path: [], + value: undefined, + }, + ]); + }); + + it('returns an error for null value', () => { + const result = coerceValue(null, TestNonNull); + expectErrors(result).toEqual([ + { + error: 'Expected non-nullable type "Int!" not to be null.', + path: [], + value: null, + }, + ]); + }); + }); + + describe('for GraphQLScalar', () => { + const TestScalar = new GraphQLScalarType({ + name: 'TestScalar', + parseValue(input: any) { + if (input.error != null) { + throw new Error(input.error); + } + return input.value; + }, + }); + + it('returns no error for valid input', () => { + const result = coerceValue({ value: 1 }, TestScalar); + expectValue(result).toEqual(1); + }); + + it('returns no error for null result', () => { + const result = coerceValue({ value: null }, TestScalar); + expectValue(result).toEqual(null); + }); + + it('returns no error for NaN result', () => { + const result = coerceValue({ value: NaN }, TestScalar); + expectValue(result); + }); + + it('returns an error for undefined result', () => { + const result = coerceValue({ value: undefined }, TestScalar); + expectErrors(result).toEqual([ + { + error: 'Expected type "TestScalar".', + path: [], + value: { value: undefined }, + }, + ]); + }); + + it('returns an error for undefined result', () => { + const inputValue = { error: 'Some error message' }; + const result = coerceValue(inputValue, TestScalar); + expectErrors(result).toEqual([ + { + error: 'Expected type "TestScalar". Some error message', + path: [], + value: { error: 'Some error message' }, + }, + ]); + }); + }); + + describe('for GraphQLEnum', () => { + const TestEnum = new GraphQLEnumType({ + name: 'TestEnum', + values: { + FOO: { value: 'InternalFoo' }, + BAR: { value: 123456789 }, + }, + }); + + it('returns no error for a known enum name', () => { + const fooResult = coerceValue('FOO', TestEnum); + expectValue(fooResult).toEqual('InternalFoo'); + + const barResult = coerceValue('BAR', TestEnum); + expectValue(barResult).toEqual(123456789); + }); + + it('returns an error for misspelled enum value', () => { + const result = coerceValue('foo', TestEnum); + expectErrors(result).toEqual([ + { + error: 'Value "foo" does not exist in "TestEnum" enum. Did you mean the enum value "FOO"?', + path: [], + value: 'foo', + }, + ]); + }); + + it('returns an error for incorrect value type', () => { + const result1 = coerceValue(123, TestEnum); + expectErrors(result1).toEqual([ + { + error: 'Enum "TestEnum" cannot represent non-string value: 123.', + path: [], + value: 123, + }, + ]); + + const result2 = coerceValue({ field: 'value' }, TestEnum); + expectErrors(result2).toEqual([ + { + error: 'Enum "TestEnum" cannot represent non-string value: { field: "value" }.', + path: [], + value: { field: 'value' }, + }, + ]); + }); + }); + + describe('for GraphQLInputObject', () => { + const TestInputObject = new GraphQLInputObjectType({ + name: 'TestInputObject', + fields: { + foo: { type: new GraphQLNonNull(GraphQLInt) }, + bar: { type: GraphQLInt }, + }, + }); + + it('returns no error for a valid input', () => { + const result = coerceValue({ foo: 123 }, TestInputObject); + expectValue(result).toEqual({ foo: 123 }); + }); + + it('returns an error for a non-object type', () => { + const result = coerceValue(123, TestInputObject); + expectErrors(result).toEqual([ + { + error: 'Expected type "TestInputObject" to be an object.', + path: [], + value: 123, + }, + ]); + }); + + it('returns an error for an invalid field', () => { + const result = coerceValue({ foo: NaN }, TestInputObject); + expectErrors(result).toEqual([ + { + error: 'Int cannot represent non-integer value: NaN', + path: ['foo'], + value: NaN, + }, + ]); + }); + + it('returns multiple errors for multiple invalid fields', () => { + const result = coerceValue({ foo: 'abc', bar: 'def' }, TestInputObject); + expectErrors(result).toEqual([ + { + error: 'Int cannot represent non-integer value: "abc"', + path: ['foo'], + value: 'abc', + }, + { + error: 'Int cannot represent non-integer value: "def"', + path: ['bar'], + value: 'def', + }, + ]); + }); + + it('returns error for a missing required field', () => { + const result = coerceValue({ bar: 123 }, TestInputObject); + expectErrors(result).toEqual([ + { + error: 'Field "foo" of required type "Int!" was not provided.', + path: [], + value: { bar: 123 }, + }, + ]); + }); + + it('returns error for an unknown field', () => { + const result = coerceValue({ foo: 123, unknownField: 123 }, TestInputObject); + expectErrors(result).toEqual([ + { + error: 'Field "unknownField" is not defined by type "TestInputObject".', + path: [], + value: { foo: 123, unknownField: 123 }, + }, + ]); + }); + + it('returns error for a misspelled field', () => { + const result = coerceValue({ foo: 123, bart: 123 }, TestInputObject); + expectErrors(result).toEqual([ + { + error: 'Field "bart" is not defined by type "TestInputObject". Did you mean "bar"?', + path: [], + value: { foo: 123, bart: 123 }, + }, + ]); + }); + }); + + describe('for GraphQLInputObject with default value', () => { + const makeTestInputObject = (defaultValue: any) => + new GraphQLInputObjectType({ + name: 'TestInputObject', + fields: { + foo: { + type: new GraphQLScalarType({ name: 'TestScalar' }), + defaultValue, + }, + }, + }); + + it('returns no errors for valid input value', () => { + const result = coerceValue({ foo: 5 }, makeTestInputObject(7)); + expectValue(result).toEqual({ foo: 5 }); + }); + + it('returns object with default value', () => { + const result = coerceValue({}, makeTestInputObject(7)); + expectValue(result).toEqual({ foo: 7 }); + }); + + it('returns null as value', () => { + const result = coerceValue({}, makeTestInputObject(null)); + expectValue(result).toEqual({ foo: null }); + }); + + it('returns NaN as value', () => { + const result = coerceValue({}, makeTestInputObject(NaN)); + expectValue(result).toHaveProperty('foo'); + }); + }); + + describe('for GraphQLList', () => { + const TestList = new GraphQLList(GraphQLInt); + + it('returns no error for a valid input', () => { + const result = coerceValue([1, 2, 3], TestList); + expectValue(result).toEqual([1, 2, 3]); + }); + + it('returns no error for a valid iterable input', () => { + function* listGenerator() { + yield 1; + yield 2; + yield 3; + } + + const result = coerceValue(listGenerator(), TestList); + expectValue(result).toEqual([1, 2, 3]); + }); + + it('returns an error for an invalid input', () => { + const result = coerceValue([1, 'b', true, 4], TestList); + expectErrors(result).toEqual([ + { + error: 'Int cannot represent non-integer value: "b"', + path: [1], + value: 'b', + }, + { + error: 'Int cannot represent non-integer value: true', + path: [2], + value: true, + }, + ]); + }); + + it('returns a list for a non-list value', () => { + const result = coerceValue(42, TestList); + expectValue(result).toEqual([42]); + }); + + it('returns a list for a non-list object value', () => { + const TestListOfObjects = new GraphQLList( + new GraphQLInputObjectType({ + name: 'TestObject', + fields: { + length: { type: GraphQLInt }, + }, + }) + ); + + const result = coerceValue({ length: 100500 }, TestListOfObjects); + expectValue(result).toEqual([{ length: 100500 }]); + }); + + it('returns an error for a non-list invalid value', () => { + const result = coerceValue('INVALID', TestList); + expectErrors(result).toEqual([ + { + error: 'Int cannot represent non-integer value: "INVALID"', + path: [], + value: 'INVALID', + }, + ]); + }); + + it('returns null for a null value', () => { + const result = coerceValue(null, TestList); + expectValue(result).toEqual(null); + }); + }); + + describe('for nested GraphQLList', () => { + const TestNestedList = new GraphQLList(new GraphQLList(GraphQLInt)); + + it('returns no error for a valid input', () => { + const result = coerceValue([[1], [2, 3]], TestNestedList); + expectValue(result).toEqual([[1], [2, 3]]); + }); + + it('returns a list for a non-list value', () => { + const result = coerceValue(42, TestNestedList); + expectValue(result).toEqual([[42]]); + }); + + it('returns null for a null value', () => { + const result = coerceValue(null, TestNestedList); + expectValue(result).toEqual(null); + }); + + it('returns nested lists for nested non-list values', () => { + const result = coerceValue([1, 2, 3], TestNestedList); + expectValue(result).toEqual([[1], [2], [3]]); + }); + + it('returns nested null for nested null values', () => { + const result = coerceValue([42, [null], null], TestNestedList); + expectValue(result).toEqual([[42], [null], null]); + }); + }); + + describe('with default onError', () => { + it('throw error without path', () => { + expect(() => coerceInputValue(null, new GraphQLNonNull(GraphQLInt))).toThrow( + 'Invalid value null: Expected non-nullable type "Int!" not to be null.' + ); + }); + + it('throw error with path', () => { + expect(() => coerceInputValue([null], new GraphQLList(new GraphQLNonNull(GraphQLInt)))).toThrow( + 'Invalid value null at "value[0]": Expected non-nullable type "Int!" not to be null.' + ); + }); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/concatAST-test.ts b/packages/graphql/src/utilities/__tests__/concatAST-test.ts new file mode 100644 index 00000000000..4712dfd55e1 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/concatAST-test.ts @@ -0,0 +1,37 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import { parse } from '../../language/parser.js'; +import { print } from '../../language/printer.js'; +import { Source } from '../../language/source.js'; + +import { concatAST } from '../concatAST.js'; + +describe('concatAST', () => { + it('concatenates two ASTs together', () => { + const sourceA = new Source(` + { a, b, ...Frag } + `); + + const sourceB = new Source(` + fragment Frag on T { + c + } + `); + + const astA = parse(sourceA); + const astB = parse(sourceB); + const astC = concatAST([astA, astB]); + + expect(print(astC)).toEqual(dedent` + { + a + b + ...Frag + } + + fragment Frag on T { + c + } + `); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/extendSchema-test.ts b/packages/graphql/src/utilities/__tests__/extendSchema-test.ts new file mode 100644 index 00000000000..0c6ed5cc3cf --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/extendSchema-test.ts @@ -0,0 +1,1279 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import type { Maybe } from '../../jsutils/Maybe.js'; + +import type { ASTNode } from '../../language/ast.js'; +import { parse } from '../../language/parser.js'; +import { print } from '../../language/printer.js'; + +import { + assertEnumType, + assertInputObjectType, + assertInterfaceType, + assertObjectType, + assertScalarType, + assertUnionType, +} from '../../type/definition.js'; +import { assertDirective, specifiedDirectives } from '../../type/directives.js'; +import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; +import { validateSchema } from '../../type/validate.js'; + +import { graphqlSync } from '../../graphql.js'; + +import { buildSchema } from '../buildASTSchema.js'; +import { concatAST } from '../concatAST.js'; +import { extendSchema } from '../extendSchema.js'; +import { printSchema } from '../printSchema.js'; + +function expectExtensionASTNodes(obj: { readonly extensionASTNodes: ReadonlyArray }) { + return expect(obj.extensionASTNodes.map(print).join('\n\n')); +} + +function expectASTNode(obj: Maybe<{ readonly astNode: Maybe }>) { + expect(obj?.astNode != null).toBeTruthy(); + // @ts-expect-error + return expect(print(obj.astNode)); +} + +function expectSchemaChanges(schema: GraphQLSchema, extendedSchema: GraphQLSchema) { + const schemaDefinitions = parse(printSchema(schema)).definitions.map(print); + return expect( + parse(printSchema(extendedSchema)) + .definitions.map(print) + .filter(def => !schemaDefinitions.includes(def)) + .join('\n\n') + ); +} + +describe('extendSchema', () => { + it('returns the original schema when there are no type definitions', () => { + const schema = buildSchema('type Query'); + const extendedSchema = extendSchema(schema, parse('{ field }')); + expect(extendedSchema).toEqual(schema); + }); + + it('can be used for limited execution', () => { + const schema = buildSchema('type Query'); + const extendAST = parse(` + extend type Query { + newField: String + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + const result = graphqlSync({ + schema: extendedSchema, + source: '{ newField }', + rootValue: { newField: 123 }, + }); + expect(result).toEqual({ + data: { newField: '123' }, + }); + }); + + it('Do not modify built-in types and directives', () => { + const schema = buildSchema(` + type Query { + str: String + int: Int + float: Float + id: ID + bool: Boolean + } + `); + + const extensionSDL = dedent` + extend type Query { + foo: String + } + `; + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + + // Built-ins are used + expect(extendedSchema.getType('Int')).toEqual(GraphQLInt); + expect(extendedSchema.getType('Float')).toEqual(GraphQLFloat); + expect(extendedSchema.getType('String')).toEqual(GraphQLString); + expect(extendedSchema.getType('Boolean')).toEqual(GraphQLBoolean); + expect(extendedSchema.getType('ID')).toEqual(GraphQLID); + + expect(extendedSchema.getDirectives()).toEqual(specifiedDirectives); + }); + + it('extends objects by adding new fields', () => { + const schema = buildSchema(` + type Query { + someObject: SomeObject + } + + type SomeObject implements AnotherInterface & SomeInterface { + self: SomeObject + tree: [SomeObject]! + """Old field description.""" + oldField: String + } + + interface SomeInterface { + self: SomeInterface + } + + interface AnotherInterface { + self: SomeObject + } + `); + const extensionSDL = dedent` + extend type SomeObject { + """New field description.""" + newField(arg: Boolean): String + } + `; + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + type SomeObject implements AnotherInterface & SomeInterface { + self: SomeObject + tree: [SomeObject]! + """Old field description.""" + oldField: String + """New field description.""" + newField(arg: Boolean): String + } + `); + }); + + it('extends objects with standard type fields', () => { + const schema = buildSchema('type Query'); + + // String and Boolean are always included through introspection types + expect(schema.getType('Int')).toEqual(undefined); + expect(schema.getType('Float')).toEqual(undefined); + expect(schema.getType('String')).toEqual(GraphQLString); + expect(schema.getType('Boolean')).toEqual(GraphQLBoolean); + expect(schema.getType('ID')).toEqual(undefined); + + const extendAST = parse(` + extend type Query { + bool: Boolean + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expect(extendedSchema.getType('Int')).toEqual(undefined); + expect(extendedSchema.getType('Float')).toEqual(undefined); + expect(extendedSchema.getType('String')).toEqual(GraphQLString); + expect(extendedSchema.getType('Boolean')).toEqual(GraphQLBoolean); + expect(extendedSchema.getType('ID')).toEqual(undefined); + + const extendTwiceAST = parse(` + extend type Query { + int: Int + float: Float + id: ID + } + `); + const extendedTwiceSchema = extendSchema(schema, extendTwiceAST); + + expect(validateSchema(extendedTwiceSchema)).toEqual([]); + expect(extendedTwiceSchema.getType('Int')).toEqual(GraphQLInt); + expect(extendedTwiceSchema.getType('Float')).toEqual(GraphQLFloat); + expect(extendedTwiceSchema.getType('String')).toEqual(GraphQLString); + expect(extendedTwiceSchema.getType('Boolean')).toEqual(GraphQLBoolean); + expect(extendedTwiceSchema.getType('ID')).toEqual(GraphQLID); + }); + + it('extends enums by adding new values', () => { + const schema = buildSchema(` + type Query { + someEnum(arg: SomeEnum): SomeEnum + } + + directive @foo(arg: SomeEnum) on SCHEMA + + enum SomeEnum { + """Old value description.""" + OLD_VALUE + } + `); + const extendAST = parse(` + extend enum SomeEnum { + """New value description.""" + NEW_VALUE + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + enum SomeEnum { + """Old value description.""" + OLD_VALUE + """New value description.""" + NEW_VALUE + } + `); + }); + + it('extends unions by adding new types', () => { + const schema = buildSchema(` + type Query { + someUnion: SomeUnion + } + + union SomeUnion = Foo | Biz + + type Foo { foo: String } + type Biz { biz: String } + type Bar { bar: String } + `); + const extendAST = parse(` + extend union SomeUnion = Bar + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + union SomeUnion = Foo | Biz | Bar + `); + }); + + it('allows extension of union by adding itself', () => { + const schema = buildSchema(` + union SomeUnion + `); + const extendAST = parse(` + extend union SomeUnion = SomeUnion + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema).length > 0).toBeTruthy(); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + union SomeUnion = SomeUnion + `); + }); + + it('extends inputs by adding new fields', () => { + const schema = buildSchema(` + type Query { + someInput(arg: SomeInput): String + } + + directive @foo(arg: SomeInput) on SCHEMA + + input SomeInput { + """Old field description.""" + oldField: String + } + `); + const extendAST = parse(` + extend input SomeInput { + """New field description.""" + newField: String + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + input SomeInput { + """Old field description.""" + oldField: String + """New field description.""" + newField: String + } + `); + }); + + it('extends scalars by adding new directives', () => { + const schema = buildSchema(` + type Query { + someScalar(arg: SomeScalar): SomeScalar + } + + directive @foo(arg: SomeScalar) on SCALAR + + input FooInput { + foo: SomeScalar + } + + scalar SomeScalar + `); + const extensionSDL = dedent` + extend scalar SomeScalar @foo + `; + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + const someScalar = assertScalarType(extendedSchema.getType('SomeScalar')); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectExtensionASTNodes(someScalar).toEqual(extensionSDL); + }); + + it('extends scalars by adding specifiedBy directive', () => { + const schema = buildSchema(` + type Query { + foo: Foo + } + + scalar Foo + + directive @foo on SCALAR + `); + const extensionSDL = dedent` + extend scalar Foo @foo + + extend scalar Foo @specifiedBy(url: "https://example.com/foo_spec") + `; + + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + const foo = assertScalarType(extendedSchema.getType('Foo')); + + expect(foo.specifiedByURL).toEqual('https://example.com/foo_spec'); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectExtensionASTNodes(foo).toEqual(extensionSDL); + }); + + it('correctly assign AST nodes to new and extended types', () => { + const schema = buildSchema(` + type Query + + scalar SomeScalar + enum SomeEnum + union SomeUnion + input SomeInput + type SomeObject + interface SomeInterface + + directive @foo on SCALAR + `); + const firstExtensionAST = parse(` + extend type Query { + newField(testArg: TestInput): TestEnum + } + + extend scalar SomeScalar @foo + + extend enum SomeEnum { + NEW_VALUE + } + + extend union SomeUnion = SomeObject + + extend input SomeInput { + newField: String + } + + extend interface SomeInterface { + newField: String + } + + enum TestEnum { + TEST_VALUE + } + + input TestInput { + testInputField: TestEnum + } + `); + const extendedSchema = extendSchema(schema, firstExtensionAST); + + const secondExtensionAST = parse(` + extend type Query { + oneMoreNewField: TestUnion + } + + extend scalar SomeScalar @test + + extend enum SomeEnum { + ONE_MORE_NEW_VALUE + } + + extend union SomeUnion = TestType + + extend input SomeInput { + oneMoreNewField: String + } + + extend interface SomeInterface { + oneMoreNewField: String + } + + union TestUnion = TestType + + interface TestInterface { + interfaceField: String + } + + type TestType implements TestInterface { + interfaceField: String + } + + directive @test(arg: Int) repeatable on FIELD | SCALAR + `); + const extendedTwiceSchema = extendSchema(extendedSchema, secondExtensionAST); + + const extendedInOneGoSchema = extendSchema(schema, concatAST([firstExtensionAST, secondExtensionAST])); + expect(printSchema(extendedInOneGoSchema)).toEqual(printSchema(extendedTwiceSchema)); + + const query = assertObjectType(extendedTwiceSchema.getType('Query')); + const someEnum = assertEnumType(extendedTwiceSchema.getType('SomeEnum')); + const someUnion = assertUnionType(extendedTwiceSchema.getType('SomeUnion')); + const someScalar = assertScalarType(extendedTwiceSchema.getType('SomeScalar')); + const someInput = assertInputObjectType(extendedTwiceSchema.getType('SomeInput')); + const someInterface = assertInterfaceType(extendedTwiceSchema.getType('SomeInterface')); + + const testInput = assertInputObjectType(extendedTwiceSchema.getType('TestInput')); + const testEnum = assertEnumType(extendedTwiceSchema.getType('TestEnum')); + const testUnion = assertUnionType(extendedTwiceSchema.getType('TestUnion')); + const testType = assertObjectType(extendedTwiceSchema.getType('TestType')); + const testInterface = assertInterfaceType(extendedTwiceSchema.getType('TestInterface')); + const testDirective = assertDirective(extendedTwiceSchema.getDirective('test')); + + expect(testType.extensionASTNodes).toEqual([]); + expect(testEnum.extensionASTNodes).toEqual([]); + expect(testUnion.extensionASTNodes).toEqual([]); + expect(testInput.extensionASTNodes).toEqual([]); + expect(testInterface.extensionASTNodes).toEqual([]); + + expect([ + testInput.astNode, + testEnum.astNode, + testUnion.astNode, + testInterface.astNode, + testType.astNode, + testDirective.astNode, + ...query.extensionASTNodes, + ...someScalar.extensionASTNodes, + ...someEnum.extensionASTNodes, + ...someUnion.extensionASTNodes, + ...someInput.extensionASTNodes, + ...someInterface.extensionASTNodes, + ]).toEqual(expect.arrayContaining([...firstExtensionAST.definitions, ...secondExtensionAST.definitions])); + + const newField = query.getFields()['newField']; + expectASTNode(newField).toEqual('newField(testArg: TestInput): TestEnum'); + expectASTNode(newField.args[0]).toEqual('testArg: TestInput'); + expectASTNode(query.getFields()['oneMoreNewField']).toEqual('oneMoreNewField: TestUnion'); + + expectASTNode(someEnum.getValue('NEW_VALUE')).toEqual('NEW_VALUE'); + expectASTNode(someEnum.getValue('ONE_MORE_NEW_VALUE')).toEqual('ONE_MORE_NEW_VALUE'); + + expectASTNode(someInput.getFields()['newField']).toEqual('newField: String'); + expectASTNode(someInput.getFields()['oneMoreNewField']).toEqual('oneMoreNewField: String'); + expectASTNode(someInterface.getFields()['newField']).toEqual('newField: String'); + expectASTNode(someInterface.getFields()['oneMoreNewField']).toEqual('oneMoreNewField: String'); + + expectASTNode(testInput.getFields()['testInputField']).toEqual('testInputField: TestEnum'); + + expectASTNode(testEnum.getValue('TEST_VALUE')).toEqual('TEST_VALUE'); + + expectASTNode(testInterface.getFields()['interfaceField']).toEqual('interfaceField: String'); + expectASTNode(testType.getFields()['interfaceField']).toEqual('interfaceField: String'); + expectASTNode(testDirective.args[0]).toEqual('arg: Int'); + }); + + it('builds types with deprecated fields/values', () => { + const schema = new GraphQLSchema({}); + const extendAST = parse(` + type SomeObject { + deprecatedField: String @deprecated(reason: "not used anymore") + } + + enum SomeEnum { + DEPRECATED_VALUE @deprecated(reason: "do not use") + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + const someType = assertObjectType(extendedSchema.getType('SomeObject')); + expect(someType.getFields()['deprecatedField']).toMatchObject({ + deprecationReason: 'not used anymore', + }); + + const someEnum = assertEnumType(extendedSchema.getType('SomeEnum')); + expect(someEnum.getValue('DEPRECATED_VALUE')).toMatchObject({ + deprecationReason: 'do not use', + }); + }); + + it('extends objects with deprecated fields', () => { + const schema = buildSchema('type SomeObject'); + const extendAST = parse(` + extend type SomeObject { + deprecatedField: String @deprecated(reason: "not used anymore") + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + const someType = assertObjectType(extendedSchema.getType('SomeObject')); + expect(someType.getFields()['deprecatedField']).toMatchObject({ + deprecationReason: 'not used anymore', + }); + }); + + it('extends enums with deprecated values', () => { + const schema = buildSchema('enum SomeEnum'); + const extendAST = parse(` + extend enum SomeEnum { + DEPRECATED_VALUE @deprecated(reason: "do not use") + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + const someEnum = assertEnumType(extendedSchema.getType('SomeEnum')); + expect(someEnum.getValue('DEPRECATED_VALUE')).toMatchObject({ + deprecationReason: 'do not use', + }); + }); + + it('adds new unused types', () => { + const schema = buildSchema(` + type Query { + dummy: String + } + `); + const extensionSDL = dedent` + type DummyUnionMember { + someField: String + } + + enum UnusedEnum { + SOME_VALUE + } + + input UnusedInput { + someField: String + } + + interface UnusedInterface { + someField: String + } + + type UnusedObject { + someField: String + } + + union UnusedUnion = DummyUnionMember + `; + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(extensionSDL); + }); + + it('extends objects by adding new fields with arguments', () => { + const schema = buildSchema(` + type SomeObject + + type Query { + someObject: SomeObject + } + `); + const extendAST = parse(` + input NewInputObj { + field1: Int + field2: [Float] + field3: String! + } + + extend type SomeObject { + newField(arg1: String, arg2: NewInputObj!): String + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + type SomeObject { + newField(arg1: String, arg2: NewInputObj!): String + } + + input NewInputObj { + field1: Int + field2: [Float] + field3: String! + } + `); + }); + + it('extends objects by adding new fields with existing types', () => { + const schema = buildSchema(` + type Query { + someObject: SomeObject + } + + type SomeObject + enum SomeEnum { VALUE } + `); + const extendAST = parse(` + extend type SomeObject { + newField(arg1: SomeEnum!): SomeEnum + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + type SomeObject { + newField(arg1: SomeEnum!): SomeEnum + } + `); + }); + + it('extends objects by adding implemented interfaces', () => { + const schema = buildSchema(` + type Query { + someObject: SomeObject + } + + type SomeObject { + foo: String + } + + interface SomeInterface { + foo: String + } + `); + const extendAST = parse(` + extend type SomeObject implements SomeInterface + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + type SomeObject implements SomeInterface { + foo: String + } + `); + }); + + it('extends objects by including new types', () => { + const schema = buildSchema(` + type Query { + someObject: SomeObject + } + + type SomeObject { + oldField: String + } + `); + const newTypesSDL = dedent` + enum NewEnum { + VALUE + } + + interface NewInterface { + baz: String + } + + type NewObject implements NewInterface { + baz: String + } + + scalar NewScalar + + union NewUnion = NewObject`; + const extendAST = parse(` + ${newTypesSDL} + extend type SomeObject { + newObject: NewObject + newInterface: NewInterface + newUnion: NewUnion + newScalar: NewScalar + newEnum: NewEnum + newTree: [SomeObject]! + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + type SomeObject { + oldField: String + newObject: NewObject + newInterface: NewInterface + newUnion: NewUnion + newScalar: NewScalar + newEnum: NewEnum + newTree: [SomeObject]! + } + + ${newTypesSDL} + `); + }); + + it('extends objects by adding implemented new interfaces', () => { + const schema = buildSchema(` + type Query { + someObject: SomeObject + } + + type SomeObject implements OldInterface { + oldField: String + } + + interface OldInterface { + oldField: String + } + `); + const extendAST = parse(` + extend type SomeObject implements NewInterface { + newField: String + } + + interface NewInterface { + newField: String + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + type SomeObject implements OldInterface & NewInterface { + oldField: String + newField: String + } + + interface NewInterface { + newField: String + } + `); + }); + + it('extends different types multiple times', () => { + const schema = buildSchema(` + type Query { + someScalar: SomeScalar + someObject(someInput: SomeInput): SomeObject + someInterface: SomeInterface + someEnum: SomeEnum + someUnion: SomeUnion + } + + scalar SomeScalar + + type SomeObject implements SomeInterface { + oldField: String + } + + interface SomeInterface { + oldField: String + } + + enum SomeEnum { + OLD_VALUE + } + + union SomeUnion = SomeObject + + input SomeInput { + oldField: String + } + `); + const newTypesSDL = dedent` + scalar NewScalar + + scalar AnotherNewScalar + + type NewObject { + foo: String + } + + type AnotherNewObject { + foo: String + } + + interface NewInterface { + newField: String + } + + interface AnotherNewInterface { + anotherNewField: String + } + `; + const schemaWithNewTypes = extendSchema(schema, parse(newTypesSDL)); + expectSchemaChanges(schema, schemaWithNewTypes).toEqual(newTypesSDL); + + const extendAST = parse(` + extend scalar SomeScalar @specifiedBy(url: "http://example.com/foo_spec") + + extend type SomeObject implements NewInterface { + newField: String + } + + extend type SomeObject implements AnotherNewInterface { + anotherNewField: String + } + + extend enum SomeEnum { + NEW_VALUE + } + + extend enum SomeEnum { + ANOTHER_NEW_VALUE + } + + extend union SomeUnion = NewObject + + extend union SomeUnion = AnotherNewObject + + extend input SomeInput { + newField: String + } + + extend input SomeInput { + anotherNewField: String + } + `); + const extendedSchema = extendSchema(schemaWithNewTypes, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + scalar SomeScalar @specifiedBy(url: "http://example.com/foo_spec") + + type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface { + oldField: String + newField: String + anotherNewField: String + } + + enum SomeEnum { + OLD_VALUE + NEW_VALUE + ANOTHER_NEW_VALUE + } + + union SomeUnion = SomeObject | NewObject | AnotherNewObject + + input SomeInput { + oldField: String + newField: String + anotherNewField: String + } + + ${newTypesSDL} + `); + }); + + it('extends interfaces by adding new fields', () => { + const schema = buildSchema(` + interface SomeInterface { + oldField: String + } + + interface AnotherInterface implements SomeInterface { + oldField: String + } + + type SomeObject implements SomeInterface & AnotherInterface { + oldField: String + } + + type Query { + someInterface: SomeInterface + } + `); + const extendAST = parse(` + extend interface SomeInterface { + newField: String + } + + extend interface AnotherInterface { + newField: String + } + + extend type SomeObject { + newField: String + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + interface SomeInterface { + oldField: String + newField: String + } + + interface AnotherInterface implements SomeInterface { + oldField: String + newField: String + } + + type SomeObject implements SomeInterface & AnotherInterface { + oldField: String + newField: String + } + `); + }); + + it('extends interfaces by adding new implemented interfaces', () => { + const schema = buildSchema(` + interface SomeInterface { + oldField: String + } + + interface AnotherInterface implements SomeInterface { + oldField: String + } + + type SomeObject implements SomeInterface & AnotherInterface { + oldField: String + } + + type Query { + someInterface: SomeInterface + } + `); + const extendAST = parse(` + interface NewInterface { + newField: String + } + + extend interface AnotherInterface implements NewInterface { + newField: String + } + + extend type SomeObject implements NewInterface { + newField: String + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + interface AnotherInterface implements SomeInterface & NewInterface { + oldField: String + newField: String + } + + type SomeObject implements SomeInterface & AnotherInterface & NewInterface { + oldField: String + newField: String + } + + interface NewInterface { + newField: String + } + `); + }); + + it('allows extension of interface with missing Object fields', () => { + const schema = buildSchema(` + type Query { + someInterface: SomeInterface + } + + type SomeObject implements SomeInterface { + oldField: SomeInterface + } + + interface SomeInterface { + oldField: SomeInterface + } + `); + const extendAST = parse(` + extend interface SomeInterface { + newField: String + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema).length > 0).toBeTruthy(); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + interface SomeInterface { + oldField: SomeInterface + newField: String + } + `); + }); + + it('extends interfaces multiple times', () => { + const schema = buildSchema(` + type Query { + someInterface: SomeInterface + } + + interface SomeInterface { + some: SomeInterface + } + `); + + const extendAST = parse(` + extend interface SomeInterface { + newFieldA: Int + } + + extend interface SomeInterface { + newFieldB(test: Boolean): String + } + `); + const extendedSchema = extendSchema(schema, extendAST); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(dedent` + interface SomeInterface { + some: SomeInterface + newFieldA: Int + newFieldB(test: Boolean): String + } + `); + }); + + it('may extend mutations and subscriptions', () => { + const mutationSchema = buildSchema(` + type Query { + queryField: String + } + + type Mutation { + mutationField: String + } + + type Subscription { + subscriptionField: String + } + `); + const ast = parse(` + extend type Query { + newQueryField: Int + } + + extend type Mutation { + newMutationField: Int + } + + extend type Subscription { + newSubscriptionField: Int + } + `); + const originalPrint = printSchema(mutationSchema); + const extendedSchema = extendSchema(mutationSchema, ast); + expect(extendedSchema).not.toEqual(mutationSchema); + expect(printSchema(mutationSchema)).toEqual(originalPrint); + expect(printSchema(extendedSchema)).toEqual(dedent` + type Query { + queryField: String + newQueryField: Int + } + + type Mutation { + mutationField: String + newMutationField: Int + } + + type Subscription { + subscriptionField: String + newSubscriptionField: Int + } + `); + }); + + it('may extend directives with new directive', () => { + const schema = buildSchema(` + type Query { + foo: String + } + `); + const extensionSDL = dedent` + """New directive.""" + directive @new(enable: Boolean!, tag: String) repeatable on QUERY | FIELD + `; + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + + expect(validateSchema(extendedSchema)).toEqual([]); + expectSchemaChanges(schema, extendedSchema).toEqual(extensionSDL); + }); + + it('Rejects invalid SDL', () => { + const schema = new GraphQLSchema({}); + const extendAST = parse('extend schema @unknown'); + + expect(() => extendSchema(schema, extendAST)).toThrow('Unknown directive "@unknown".'); + }); + + it('Allows to disable SDL validation', () => { + const schema = new GraphQLSchema({}); + const extendAST = parse('extend schema @unknown'); + + extendSchema(schema, extendAST, { assumeValid: true }); + extendSchema(schema, extendAST, { assumeValidSDL: true }); + }); + + it('Throws on unknown types', () => { + const schema = new GraphQLSchema({}); + const ast = parse(` + type Query { + unknown: UnknownType + } + `); + expect(() => extendSchema(schema, ast, { assumeValidSDL: true })).toThrow('Unknown type: "UnknownType".'); + }); + + it('does not allow replacing a default directive', () => { + const schema = new GraphQLSchema({}); + const extendAST = parse(` + directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD + `); + + expect(() => extendSchema(schema, extendAST)).toThrow( + 'Directive "@include" already exists in the schema. It cannot be redefined.' + ); + }); + + it('does not allow replacing an existing enum value', () => { + const schema = buildSchema(` + enum SomeEnum { + ONE + } + `); + const extendAST = parse(` + extend enum SomeEnum { + ONE + } + `); + + expect(() => extendSchema(schema, extendAST)).toThrow( + 'Enum value "SomeEnum.ONE" already exists in the schema. It cannot also be defined in this type extension.' + ); + }); + + describe('can add additional root operation types', () => { + it('does not automatically include common root type names', () => { + const schema = new GraphQLSchema({}); + const extendedSchema = extendSchema(schema, parse('type Mutation')); + + expect(extendedSchema.getType('Mutation')).not.toEqual(undefined); + expect(extendedSchema.getMutationType()).toEqual(undefined); + }); + + it('adds schema definition missing in the original schema', () => { + const schema = buildSchema(` + directive @foo on SCHEMA + type Foo + `); + expect(schema.getQueryType()).toEqual(undefined); + + const extensionSDL = dedent` + schema @foo { + query: Foo + } + `; + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + + const queryType = extendedSchema.getQueryType(); + expect(queryType).toMatchObject({ name: 'Foo' }); + expectASTNode(extendedSchema).toEqual(extensionSDL); + }); + + it('adds new root types via schema extension', () => { + const schema = buildSchema(` + type Query + type MutationRoot + `); + const extensionSDL = dedent` + extend schema { + mutation: MutationRoot + } + `; + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + + const mutationType = extendedSchema.getMutationType(); + expect(mutationType).toMatchObject({ name: 'MutationRoot' }); + expectExtensionASTNodes(extendedSchema).toEqual(extensionSDL); + }); + + it('adds directive via schema extension', () => { + const schema = buildSchema(` + type Query + + directive @foo on SCHEMA + `); + const extensionSDL = dedent` + extend schema @foo + `; + const extendedSchema = extendSchema(schema, parse(extensionSDL)); + + expectExtensionASTNodes(extendedSchema).toEqual(extensionSDL); + }); + + it('adds multiple new root types via schema extension', () => { + const schema = buildSchema('type Query'); + const extendAST = parse(` + extend schema { + mutation: Mutation + subscription: Subscription + } + + type Mutation + type Subscription + `); + const extendedSchema = extendSchema(schema, extendAST); + + const mutationType = extendedSchema.getMutationType(); + expect(mutationType).toMatchObject({ name: 'Mutation' }); + + const subscriptionType = extendedSchema.getSubscriptionType(); + expect(subscriptionType).toMatchObject({ name: 'Subscription' }); + }); + + it('applies multiple schema extensions', () => { + const schema = buildSchema('type Query'); + const extendAST = parse(` + extend schema { + mutation: Mutation + } + type Mutation + + extend schema { + subscription: Subscription + } + type Subscription + `); + const extendedSchema = extendSchema(schema, extendAST); + + const mutationType = extendedSchema.getMutationType(); + expect(mutationType).toMatchObject({ name: 'Mutation' }); + + const subscriptionType = extendedSchema.getSubscriptionType(); + expect(subscriptionType).toMatchObject({ name: 'Subscription' }); + }); + + it('schema extension AST are available from schema object', () => { + const schema = buildSchema(` + type Query + + directive @foo on SCHEMA + `); + + const extendAST = parse(` + extend schema { + mutation: Mutation + } + type Mutation + + extend schema { + subscription: Subscription + } + type Subscription + `); + const extendedSchema = extendSchema(schema, extendAST); + + const secondExtendAST = parse('extend schema @foo'); + const extendedTwiceSchema = extendSchema(extendedSchema, secondExtendAST); + + expectExtensionASTNodes(extendedTwiceSchema).toEqual(dedent` + extend schema { + mutation: Mutation + } + + extend schema { + subscription: Subscription + } + + extend schema @foo + `); + }); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/findBreakingChanges-test.ts b/packages/graphql/src/utilities/__tests__/findBreakingChanges-test.ts new file mode 100644 index 00000000000..8fba1ef291e --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/findBreakingChanges-test.ts @@ -0,0 +1,1187 @@ +import { + GraphQLDeprecatedDirective, + GraphQLIncludeDirective, + GraphQLSkipDirective, + GraphQLSpecifiedByDirective, +} from '../../type/directives.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../buildASTSchema.js'; +import { + BreakingChangeType, + DangerousChangeType, + findBreakingChanges, + findDangerousChanges, +} from '../findBreakingChanges.js'; + +describe('findBreakingChanges', () => { + it('should detect if a type was removed or not', () => { + const oldSchema = buildSchema(` + type Type1 + type Type2 + `); + + const newSchema = buildSchema(` + type Type2 + `); + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.TYPE_REMOVED, + description: 'Type1 was removed.', + }, + ]); + expect(findBreakingChanges(oldSchema, oldSchema)).toEqual([]); + }); + + it('should detect if a standard scalar was removed', () => { + const oldSchema = buildSchema(` + type Query { + foo: Float + } + `); + + const newSchema = buildSchema(` + type Query { + foo: String + } + `); + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.TYPE_REMOVED, + description: 'Standard scalar Float was removed because it is not referenced anymore.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Query.foo changed type from Float to String.', + }, + ]); + expect(findBreakingChanges(oldSchema, oldSchema)).toEqual([]); + }); + + it('should detect if a type changed its type', () => { + const oldSchema = buildSchema(` + scalar TypeWasScalarBecomesEnum + interface TypeWasInterfaceBecomesUnion + type TypeWasObjectBecomesInputObject + `); + + const newSchema = buildSchema(` + enum TypeWasScalarBecomesEnum + union TypeWasInterfaceBecomesUnion + input TypeWasObjectBecomesInputObject + `); + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.TYPE_CHANGED_KIND, + description: 'TypeWasScalarBecomesEnum changed from a Scalar type to an Enum type.', + }, + { + type: BreakingChangeType.TYPE_CHANGED_KIND, + description: 'TypeWasInterfaceBecomesUnion changed from an Interface type to a Union type.', + }, + { + type: BreakingChangeType.TYPE_CHANGED_KIND, + description: 'TypeWasObjectBecomesInputObject changed from an Object type to an Input type.', + }, + ]); + }); + + it('should detect if a field on a type was deleted or changed type', () => { + const oldSchema = buildSchema(` + type TypeA + type TypeB + + interface Type1 { + field1: TypeA + field2: String + field3: String + field4: TypeA + field6: String + field7: [String] + field8: Int + field9: Int! + field10: [Int]! + field11: Int + field12: [Int] + field13: [Int!] + field14: [Int] + field15: [[Int]] + field16: Int! + field17: [Int] + field18: [[Int!]!] + } + `); + + const newSchema = buildSchema(` + type TypeA + type TypeB + + interface Type1 { + field1: TypeA + field3: Boolean + field4: TypeB + field5: String + field6: [String] + field7: String + field8: Int! + field9: Int + field10: [Int] + field11: [Int]! + field12: [Int!] + field13: [Int] + field14: [[Int]] + field15: [Int] + field16: [Int]! + field17: [Int]! + field18: [[Int!]] + } + `); + + const changes = findBreakingChanges(oldSchema, newSchema); + expect(changes).toEqual([ + { + type: BreakingChangeType.FIELD_REMOVED, + description: 'Type1.field2 was removed.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field3 changed type from String to Boolean.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field4 changed type from TypeA to TypeB.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field6 changed type from String to [String].', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field7 changed type from [String] to String.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field9 changed type from Int! to Int.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field10 changed type from [Int]! to [Int].', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field11 changed type from Int to [Int]!.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field13 changed type from [Int!] to [Int].', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field14 changed type from [Int] to [[Int]].', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field15 changed type from [[Int]] to [Int].', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field16 changed type from Int! to [Int]!.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'Type1.field18 changed type from [[Int!]!] to [[Int!]].', + }, + ]); + }); + + it('should detect if fields on input types changed kind or were removed', () => { + const oldSchema = buildSchema(` + input InputType1 { + field1: String + field2: Boolean + field3: [String] + field4: String! + field5: String + field6: [Int] + field7: [Int]! + field8: Int + field9: [Int] + field10: [Int!] + field11: [Int] + field12: [[Int]] + field13: Int! + field14: [[Int]!] + field15: [[Int]!] + } + `); + + const newSchema = buildSchema(` + input InputType1 { + field1: Int + field3: String + field4: String + field5: String! + field6: [Int]! + field7: [Int] + field8: [Int]! + field9: [Int!] + field10: [Int] + field11: [[Int]] + field12: [Int] + field13: [Int]! + field14: [[Int]] + field15: [[Int!]!] + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.FIELD_REMOVED, + description: 'InputType1.field2 was removed.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field1 changed type from String to Int.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field3 changed type from [String] to String.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field5 changed type from String to String!.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field6 changed type from [Int] to [Int]!.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field8 changed type from Int to [Int]!.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field9 changed type from [Int] to [Int!].', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field11 changed type from [Int] to [[Int]].', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field12 changed type from [[Int]] to [Int].', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field13 changed type from Int! to [Int]!.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'InputType1.field15 changed type from [[Int]!] to [[Int!]!].', + }, + ]); + }); + + it('should detect if a required field is added to an input type', () => { + const oldSchema = buildSchema(` + input InputType1 { + field1: String + } + `); + + const newSchema = buildSchema(` + input InputType1 { + field1: String + requiredField: Int! + optionalField1: Boolean + optionalField2: Boolean! = false + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.REQUIRED_INPUT_FIELD_ADDED, + description: 'A required field requiredField on input type InputType1 was added.', + }, + ]); + }); + + it('should detect if a type was removed from a union type', () => { + const oldSchema = buildSchema(` + type Type1 + type Type2 + type Type3 + + union UnionType1 = Type1 | Type2 + `); + const newSchema = buildSchema(` + type Type1 + type Type2 + type Type3 + + union UnionType1 = Type1 | Type3 + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.TYPE_REMOVED_FROM_UNION, + description: 'Type2 was removed from union type UnionType1.', + }, + ]); + }); + + it('should detect if a value was removed from an enum type', () => { + const oldSchema = buildSchema(` + enum EnumType1 { + VALUE0 + VALUE1 + VALUE2 + } + `); + + const newSchema = buildSchema(` + enum EnumType1 { + VALUE0 + VALUE2 + VALUE3 + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM, + description: 'VALUE1 was removed from enum type EnumType1.', + }, + ]); + }); + + it('should detect if a field argument was removed', () => { + const oldSchema = buildSchema(` + interface Interface1 { + field1(arg1: Boolean, objectArg: String): String + } + + type Type1 { + field1(name: String): String + } + `); + + const newSchema = buildSchema(` + interface Interface1 { + field1: String + } + + type Type1 { + field1: String + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.ARG_REMOVED, + description: 'Interface1.field1 arg arg1 was removed.', + }, + { + type: BreakingChangeType.ARG_REMOVED, + description: 'Interface1.field1 arg objectArg was removed.', + }, + { + type: BreakingChangeType.ARG_REMOVED, + description: 'Type1.field1 arg name was removed.', + }, + ]); + }); + + it('should detect if a field argument has changed type', () => { + const oldSchema = buildSchema(` + type Type1 { + field1( + arg1: String + arg2: String + arg3: [String] + arg4: String + arg5: String! + arg6: String! + arg7: [Int]! + arg8: Int + arg9: [Int] + arg10: [Int!] + arg11: [Int] + arg12: [[Int]] + arg13: Int! + arg14: [[Int]!] + arg15: [[Int]!] + ): String + } + `); + + const newSchema = buildSchema(` + type Type1 { + field1( + arg1: Int + arg2: [String] + arg3: String + arg4: String! + arg5: Int + arg6: Int! + arg7: [Int] + arg8: [Int]! + arg9: [Int!] + arg10: [Int] + arg11: [[Int]] + arg12: [Int] + arg13: [Int]! + arg14: [[Int]] + arg15: [[Int!]!] + ): String + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg1 has changed type from String to Int.', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg2 has changed type from String to [String].', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg3 has changed type from [String] to String.', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg4 has changed type from String to String!.', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg5 has changed type from String! to Int.', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg6 has changed type from String! to Int!.', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg8 has changed type from Int to [Int]!.', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg9 has changed type from [Int] to [Int!].', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg11 has changed type from [Int] to [[Int]].', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg12 has changed type from [[Int]] to [Int].', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg13 has changed type from Int! to [Int]!.', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'Type1.field1 arg arg15 has changed type from [[Int]!] to [[Int!]!].', + }, + ]); + }); + + it('should detect if a required field argument was added', () => { + const oldSchema = buildSchema(` + type Type1 { + field1(arg1: String): String + } + `); + + const newSchema = buildSchema(` + type Type1 { + field1( + arg1: String, + newRequiredArg: String! + newOptionalArg1: Int + newOptionalArg2: Int! = 0 + ): String + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.REQUIRED_ARG_ADDED, + description: 'A required arg newRequiredArg on Type1.field1 was added.', + }, + ]); + }); + + it('should not flag args with the same type signature as breaking', () => { + const oldSchema = buildSchema(` + input InputType1 { + field1: String + } + + type Type1 { + field1(arg1: Int!, arg2: InputType1): Int + } + `); + + const newSchema = buildSchema(` + input InputType1 { + field1: String + } + + type Type1 { + field1(arg1: Int!, arg2: InputType1): Int + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([]); + }); + + it('should consider args that move away from NonNull as non-breaking', () => { + const oldSchema = buildSchema(` + type Type1 { + field1(name: String!): String + } + `); + + const newSchema = buildSchema(` + type Type1 { + field1(name: String): String + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([]); + }); + + it('should detect interfaces removed from types', () => { + const oldSchema = buildSchema(` + interface Interface1 + + type Type1 implements Interface1 + `); + + const newSchema = buildSchema(` + interface Interface1 + + type Type1 + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED, + description: 'Type1 no longer implements interface Interface1.', + }, + ]); + }); + + it('should detect interfaces removed from interfaces', () => { + const oldSchema = buildSchema(` + interface Interface1 + + interface Interface2 implements Interface1 + `); + + const newSchema = buildSchema(` + interface Interface1 + + interface Interface2 + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED, + description: 'Interface2 no longer implements interface Interface1.', + }, + ]); + }); + + it('should ignore changes in order of interfaces', () => { + const oldSchema = buildSchema(` + interface FirstInterface + interface SecondInterface + + type Type1 implements FirstInterface & SecondInterface + `); + + const newSchema = buildSchema(` + interface FirstInterface + interface SecondInterface + + type Type1 implements SecondInterface & FirstInterface + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([]); + }); + + it('should detect all breaking changes', () => { + const oldSchema = buildSchema(` + directive @DirectiveThatIsRemoved on FIELD_DEFINITION + + directive @DirectiveThatRemovesArg(arg1: String) on FIELD_DEFINITION + + directive @NonNullDirectiveAdded on FIELD_DEFINITION + + directive @DirectiveThatWasRepeatable repeatable on FIELD_DEFINITION + + directive @DirectiveName on FIELD_DEFINITION | QUERY + + type ArgThatChanges { + field1(id: Float): String + } + + enum EnumTypeThatLosesAValue { + VALUE0 + VALUE1 + VALUE2 + } + + interface Interface1 + type TypeThatLooseInterface1 implements Interface1 + + type TypeInUnion1 + type TypeInUnion2 + union UnionTypeThatLosesAType = TypeInUnion1 | TypeInUnion2 + + type TypeThatChangesType + + type TypeThatGetsRemoved + + interface TypeThatHasBreakingFieldChanges { + field1: String + field2: String + } + `); + + const newSchema = buildSchema(` + directive @DirectiveThatRemovesArg on FIELD_DEFINITION + + directive @NonNullDirectiveAdded(arg1: Boolean!) on FIELD_DEFINITION + + directive @DirectiveThatWasRepeatable on FIELD_DEFINITION + + directive @DirectiveName on FIELD_DEFINITION + + type ArgThatChanges { + field1(id: String): String + } + + enum EnumTypeThatLosesAValue { + VALUE1 + VALUE2 + } + + interface Interface1 + type TypeThatLooseInterface1 + + type TypeInUnion1 + type TypeInUnion2 + union UnionTypeThatLosesAType = TypeInUnion1 + + interface TypeThatChangesType + + interface TypeThatHasBreakingFieldChanges { + field2: Boolean + } + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.TYPE_REMOVED, + description: 'Standard scalar Float was removed because it is not referenced anymore.', + }, + { + type: BreakingChangeType.TYPE_REMOVED, + description: 'TypeThatGetsRemoved was removed.', + }, + { + type: BreakingChangeType.ARG_CHANGED_KIND, + description: 'ArgThatChanges.field1 arg id has changed type from Float to String.', + }, + { + type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM, + description: 'VALUE0 was removed from enum type EnumTypeThatLosesAValue.', + }, + { + type: BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED, + description: 'TypeThatLooseInterface1 no longer implements interface Interface1.', + }, + { + type: BreakingChangeType.TYPE_REMOVED_FROM_UNION, + description: 'TypeInUnion2 was removed from union type UnionTypeThatLosesAType.', + }, + { + type: BreakingChangeType.TYPE_CHANGED_KIND, + description: 'TypeThatChangesType changed from an Object type to an Interface type.', + }, + { + type: BreakingChangeType.FIELD_REMOVED, + description: 'TypeThatHasBreakingFieldChanges.field1 was removed.', + }, + { + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: 'TypeThatHasBreakingFieldChanges.field2 changed type from String to Boolean.', + }, + { + type: BreakingChangeType.DIRECTIVE_REMOVED, + description: 'DirectiveThatIsRemoved was removed.', + }, + { + type: BreakingChangeType.DIRECTIVE_ARG_REMOVED, + description: 'arg1 was removed from DirectiveThatRemovesArg.', + }, + { + type: BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED, + description: 'A required arg arg1 on directive NonNullDirectiveAdded was added.', + }, + { + type: BreakingChangeType.DIRECTIVE_REPEATABLE_REMOVED, + description: 'Repeatable flag was removed from DirectiveThatWasRepeatable.', + }, + { + type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, + description: 'QUERY was removed from DirectiveName.', + }, + ]); + }); + + it('should detect if a directive was explicitly removed', () => { + const oldSchema = buildSchema(` + directive @DirectiveThatIsRemoved on FIELD_DEFINITION + directive @DirectiveThatStays on FIELD_DEFINITION + `); + + const newSchema = buildSchema(` + directive @DirectiveThatStays on FIELD_DEFINITION + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.DIRECTIVE_REMOVED, + description: 'DirectiveThatIsRemoved was removed.', + }, + ]); + }); + + it('should detect if a directive was implicitly removed', () => { + const oldSchema = new GraphQLSchema({}); + + const newSchema = new GraphQLSchema({ + directives: [GraphQLSkipDirective, GraphQLIncludeDirective, GraphQLSpecifiedByDirective], + }); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.DIRECTIVE_REMOVED, + description: `${GraphQLDeprecatedDirective.name} was removed.`, + }, + ]); + }); + + it('should detect if a directive argument was removed', () => { + const oldSchema = buildSchema(` + directive @DirectiveWithArg(arg1: String) on FIELD_DEFINITION + `); + + const newSchema = buildSchema(` + directive @DirectiveWithArg on FIELD_DEFINITION + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.DIRECTIVE_ARG_REMOVED, + description: 'arg1 was removed from DirectiveWithArg.', + }, + ]); + }); + + it('should detect if an optional directive argument was added', () => { + const oldSchema = buildSchema(` + directive @DirectiveName on FIELD_DEFINITION + `); + + const newSchema = buildSchema(` + directive @DirectiveName( + newRequiredArg: String! + newOptionalArg1: Int + newOptionalArg2: Int! = 0 + ) on FIELD_DEFINITION + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED, + description: 'A required arg newRequiredArg on directive DirectiveName was added.', + }, + ]); + }); + + it('should detect removal of repeatable flag', () => { + const oldSchema = buildSchema(` + directive @DirectiveName repeatable on OBJECT + `); + + const newSchema = buildSchema(` + directive @DirectiveName on OBJECT + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.DIRECTIVE_REPEATABLE_REMOVED, + description: 'Repeatable flag was removed from DirectiveName.', + }, + ]); + }); + + it('should detect locations removed from a directive', () => { + const oldSchema = buildSchema(` + directive @DirectiveName on FIELD_DEFINITION | QUERY + `); + + const newSchema = buildSchema(` + directive @DirectiveName on FIELD_DEFINITION + `); + + expect(findBreakingChanges(oldSchema, newSchema)).toEqual([ + { + type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, + description: 'QUERY was removed from DirectiveName.', + }, + ]); + }); +}); + +describe('findDangerousChanges', () => { + it('should detect if a defaultValue changed on an argument', () => { + const oldSDL = ` + input Input1 { + innerInputArray: [Input2] + } + + input Input2 { + arrayField: [Int] + } + + type Type1 { + field1( + withDefaultValue: String = "TO BE DELETED" + stringArg: String = "test" + emptyArray: [Int!] = [] + valueArray: [[String]] = [["a", "b"], ["c"]] + complexObject: Input1 = { + innerInputArray: [{ arrayField: [1, 2, 3] }] + } + ): String + } + `; + + const oldSchema = buildSchema(oldSDL); + const copyOfOldSchema = buildSchema(oldSDL); + expect(findDangerousChanges(oldSchema, copyOfOldSchema)).toEqual([]); + + const newSchema = buildSchema(` + input Input1 { + innerInputArray: [Input2] + } + + input Input2 { + arrayField: [Int] + } + + type Type1 { + field1( + withDefaultValue: String + stringArg: String = "Test" + emptyArray: [Int!] = [7] + valueArray: [[String]] = [["b", "a"], ["d"]] + complexObject: Input1 = { + innerInputArray: [{ arrayField: [3, 2, 1] }] + } + ): String + } + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([ + { + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: 'Type1.field1 arg withDefaultValue defaultValue was removed.', + }, + { + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: 'Type1.field1 arg stringArg has changed defaultValue from "test" to "Test".', + }, + { + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: 'Type1.field1 arg emptyArray has changed defaultValue from [] to [7].', + }, + { + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: + 'Type1.field1 arg valueArray has changed defaultValue from [["a", "b"], ["c"]] to [["b", "a"], ["d"]].', + }, + { + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: + 'Type1.field1 arg complexObject has changed defaultValue from { innerInputArray: [{ arrayField: [1, 2, 3] }] } to { innerInputArray: [{ arrayField: [3, 2, 1] }] }.', + }, + ]); + }); + + it('should ignore changes in field order of defaultValue', () => { + const oldSchema = buildSchema(` + input Input1 { + a: String + b: String + c: String + } + + type Type1 { + field1( + arg1: Input1 = { a: "a", b: "b", c: "c" } + ): String + } + `); + + const newSchema = buildSchema(` + input Input1 { + a: String + b: String + c: String + } + + type Type1 { + field1( + arg1: Input1 = { c: "c", b: "b", a: "a" } + ): String + } + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([]); + }); + + it('should ignore changes in field definitions order', () => { + const oldSchema = buildSchema(` + input Input1 { + a: String + b: String + c: String + } + + type Type1 { + field1( + arg1: Input1 = { a: "a", b: "b", c: "c" } + ): String + } + `); + + const newSchema = buildSchema(` + input Input1 { + c: String + b: String + a: String + } + + type Type1 { + field1( + arg1: Input1 = { a: "a", b: "b", c: "c" } + ): String + } + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([]); + }); + + it('should detect if a value was added to an enum type', () => { + const oldSchema = buildSchema(` + enum EnumType1 { + VALUE0 + VALUE1 + } + `); + + const newSchema = buildSchema(` + enum EnumType1 { + VALUE0 + VALUE1 + VALUE2 + } + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([ + { + type: DangerousChangeType.VALUE_ADDED_TO_ENUM, + description: 'VALUE2 was added to enum type EnumType1.', + }, + ]); + }); + + it('should detect interfaces added to types', () => { + const oldSchema = buildSchema(` + interface OldInterface + interface NewInterface + + type Type1 implements OldInterface + `); + + const newSchema = buildSchema(` + interface OldInterface + interface NewInterface + + type Type1 implements OldInterface & NewInterface + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([ + { + type: DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED, + description: 'NewInterface added to interfaces implemented by Type1.', + }, + ]); + }); + + it('should detect interfaces added to interfaces', () => { + const oldSchema = buildSchema(` + interface OldInterface + interface NewInterface + + interface Interface1 implements OldInterface + `); + + const newSchema = buildSchema(` + interface OldInterface + interface NewInterface + + interface Interface1 implements OldInterface & NewInterface + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([ + { + type: DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED, + description: 'NewInterface added to interfaces implemented by Interface1.', + }, + ]); + }); + + it('should detect if a type was added to a union type', () => { + const oldSchema = buildSchema(` + type Type1 + type Type2 + + union UnionType1 = Type1 + `); + + const newSchema = buildSchema(` + type Type1 + type Type2 + + union UnionType1 = Type1 | Type2 + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([ + { + type: DangerousChangeType.TYPE_ADDED_TO_UNION, + description: 'Type2 was added to union type UnionType1.', + }, + ]); + }); + + it('should detect if an optional field was added to an input', () => { + const oldSchema = buildSchema(` + input InputType1 { + field1: String + } + `); + + const newSchema = buildSchema(` + input InputType1 { + field1: String + field2: Int + } + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([ + { + type: DangerousChangeType.OPTIONAL_INPUT_FIELD_ADDED, + description: 'An optional field field2 on input type InputType1 was added.', + }, + ]); + }); + + it('should find all dangerous changes', () => { + const oldSchema = buildSchema(` + enum EnumType1 { + VALUE0 + VALUE1 + } + + type Type1 { + field1(argThatChangesDefaultValue: String = "test"): String + } + + interface Interface1 + type TypeThatGainsInterface1 + + type TypeInUnion1 + union UnionTypeThatGainsAType = TypeInUnion1 + `); + + const newSchema = buildSchema(` + enum EnumType1 { + VALUE0 + VALUE1 + VALUE2 + } + + type Type1 { + field1(argThatChangesDefaultValue: String = "Test"): String + } + + interface Interface1 + type TypeThatGainsInterface1 implements Interface1 + + type TypeInUnion1 + type TypeInUnion2 + union UnionTypeThatGainsAType = TypeInUnion1 | TypeInUnion2 + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([ + { + type: DangerousChangeType.VALUE_ADDED_TO_ENUM, + description: 'VALUE2 was added to enum type EnumType1.', + }, + { + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: 'Type1.field1 arg argThatChangesDefaultValue has changed defaultValue from "test" to "Test".', + }, + { + type: DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED, + description: 'Interface1 added to interfaces implemented by TypeThatGainsInterface1.', + }, + { + type: DangerousChangeType.TYPE_ADDED_TO_UNION, + description: 'TypeInUnion2 was added to union type UnionTypeThatGainsAType.', + }, + ]); + }); + + it('should detect if an optional field argument was added', () => { + const oldSchema = buildSchema(` + type Type1 { + field1(arg1: String): String + } + `); + + const newSchema = buildSchema(` + type Type1 { + field1(arg1: String, arg2: String): String + } + `); + + expect(findDangerousChanges(oldSchema, newSchema)).toEqual([ + { + type: DangerousChangeType.OPTIONAL_ARG_ADDED, + description: 'An optional arg arg2 on Type1.field1 was added.', + }, + ]); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/getIntrospectionQuery-test.ts b/packages/graphql/src/utilities/__tests__/getIntrospectionQuery-test.ts new file mode 100644 index 00000000000..d2f030be4de --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/getIntrospectionQuery-test.ts @@ -0,0 +1,98 @@ +import { parse } from '../../language/parser.js'; + +import { validate } from '../../validation/validate.js'; + +import { buildSchema } from '../buildASTSchema.js'; +import type { IntrospectionOptions } from '../getIntrospectionQuery.js'; +import { getIntrospectionQuery } from '../getIntrospectionQuery.js'; + +const dummySchema = buildSchema(` + type Query { + dummy: String + } +`); + +function expectIntrospectionQuery(options?: IntrospectionOptions) { + const query = getIntrospectionQuery(options); + + const validationErrors = validate(dummySchema, parse(query)); + expect(validationErrors).toEqual([]); + + return { + toMatch(name: string, times: number = 1): void { + const pattern = toRegExp(name); + + expect(query).toMatch(pattern); + expect(query.match(pattern)).toHaveLength(times); + }, + toNotMatch(name: string): void { + expect(query).not.toMatch(toRegExp(name)); + }, + }; + + function toRegExp(name: string): RegExp { + return new RegExp('\\b' + name + '\\b', 'g'); + } +} + +describe('getIntrospectionQuery', () => { + it('skip all "description" fields', () => { + expectIntrospectionQuery().toMatch('description', 5); + + expectIntrospectionQuery({ descriptions: true }).toMatch('description', 5); + + expectIntrospectionQuery({ descriptions: false }).toNotMatch('description'); + }); + + it('include "isRepeatable" field on directives', () => { + expectIntrospectionQuery().toNotMatch('isRepeatable'); + + expectIntrospectionQuery({ directiveIsRepeatable: true }).toMatch('isRepeatable'); + + expectIntrospectionQuery({ directiveIsRepeatable: false }).toNotMatch('isRepeatable'); + }); + + it('include "description" field on schema', () => { + expectIntrospectionQuery().toMatch('description', 5); + + expectIntrospectionQuery({ schemaDescription: false }).toMatch('description', 5); + expectIntrospectionQuery({ schemaDescription: true }).toMatch('description', 6); + + expectIntrospectionQuery({ + descriptions: false, + schemaDescription: true, + }).toNotMatch('description'); + }); + + it('include "specifiedBy" field', () => { + expectIntrospectionQuery().toNotMatch('specifiedByURL'); + + expectIntrospectionQuery({ specifiedByUrl: true }).toMatch('specifiedByURL'); + + expectIntrospectionQuery({ specifiedByUrl: false }).toNotMatch('specifiedByURL'); + }); + + it('include "isDeprecated" field on input values', () => { + expectIntrospectionQuery().toMatch('isDeprecated', 2); + + expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch('isDeprecated', 3); + + expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch('isDeprecated', 2); + }); + + it('include "deprecationReason" field on input values', () => { + expectIntrospectionQuery().toMatch('deprecationReason', 2); + + expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch('deprecationReason', 3); + + expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch('deprecationReason', 2); + }); + + it('include deprecated input field and args', () => { + expectIntrospectionQuery().toMatch('includeDeprecated: true', 2); + + expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch('includeDeprecated: true', 5); + + expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch('includeDeprecated: true', 2); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/getOperationAST-test.ts b/packages/graphql/src/utilities/__tests__/getOperationAST-test.ts new file mode 100644 index 00000000000..fb8bdab675b --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/getOperationAST-test.ts @@ -0,0 +1,65 @@ +import { parse } from '../../language/parser.js'; + +import { getOperationAST } from '../getOperationAST.js'; + +describe('getOperationAST', () => { + it('Gets an operation from a simple document', () => { + const doc = parse('{ field }'); + expect(getOperationAST(doc)).toEqual(doc.definitions[0]); + }); + + it('Gets an operation from a document with named op (mutation)', () => { + const doc = parse('mutation Test { field }'); + expect(getOperationAST(doc)).toEqual(doc.definitions[0]); + }); + + it('Gets an operation from a document with named op (subscription)', () => { + const doc = parse('subscription Test { field }'); + expect(getOperationAST(doc)).toEqual(doc.definitions[0]); + }); + + it('Does not get missing operation', () => { + const doc = parse('type Foo { field: String }'); + expect(getOperationAST(doc)).toBeNull(); + }); + + it('Does not get ambiguous unnamed operation', () => { + const doc = parse(` + { field } + mutation Test { field } + subscription TestSub { field } + `); + expect(getOperationAST(doc)).toBeNull(); + }); + + it('Does not get ambiguous named operation', () => { + const doc = parse(` + query TestQ { field } + mutation TestM { field } + subscription TestS { field } + `); + expect(getOperationAST(doc)).toBeNull(); + }); + + it('Does not get misnamed operation', () => { + const doc = parse(` + { field } + + query TestQ { field } + mutation TestM { field } + subscription TestS { field } + `); + expect(getOperationAST(doc, 'Unknown')).toBeNull(); + }); + + it('Gets named operation', () => { + const doc = parse(` + query TestQ { field } + mutation TestM { field } + subscription TestS { field } + `); + expect(getOperationAST(doc, 'TestQ')).toEqual(doc.definitions[0]); + expect(getOperationAST(doc, 'TestM')).toEqual(doc.definitions[1]); + expect(getOperationAST(doc, 'TestS')).toEqual(doc.definitions[2]); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/getOperationRootType-test.ts b/packages/graphql/src/utilities/__tests__/getOperationRootType-test.ts new file mode 100644 index 00000000000..2ac7786cf94 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/getOperationRootType-test.ts @@ -0,0 +1,145 @@ +import { invariant } from '../../jsutils/invariant.js'; + +import type { DocumentNode, OperationDefinitionNode } from '../../language/ast.js'; +import { Kind } from '../../language/kinds.js'; +import { parse } from '../../language/parser.js'; + +import { GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { getOperationRootType } from '../getOperationRootType.js'; + +const queryType = new GraphQLObjectType({ + name: 'FooQuery', + fields: () => ({ + field: { type: GraphQLString }, + }), +}); + +const mutationType = new GraphQLObjectType({ + name: 'FooMutation', + fields: () => ({ + field: { type: GraphQLString }, + }), +}); + +const subscriptionType = new GraphQLObjectType({ + name: 'FooSubscription', + fields: () => ({ + field: { type: GraphQLString }, + }), +}); + +function getOperationNode(doc: DocumentNode): OperationDefinitionNode { + const operationNode = doc.definitions[0]; + invariant(operationNode.kind === Kind.OPERATION_DEFINITION); + return operationNode; +} + +describe('Deprecated - getOperationRootType', () => { + it('Gets a Query type for an unnamed OperationDefinitionNode', () => { + const testSchema = new GraphQLSchema({ + query: queryType, + }); + const doc = parse('{ field }'); + const operationNode = getOperationNode(doc); + expect(getOperationRootType(testSchema, operationNode)).toEqual(queryType); + }); + + it('Gets a Query type for an named OperationDefinitionNode', () => { + const testSchema = new GraphQLSchema({ + query: queryType, + }); + + const doc = parse('query Q { field }'); + const operationNode = getOperationNode(doc); + expect(getOperationRootType(testSchema, operationNode)).toEqual(queryType); + }); + + it('Gets a type for OperationTypeDefinitionNodes', () => { + const testSchema = new GraphQLSchema({ + query: queryType, + mutation: mutationType, + subscription: subscriptionType, + }); + + const doc = parse(` + schema { + query: FooQuery + mutation: FooMutation + subscription: FooSubscription + } + `); + + const schemaNode = doc.definitions[0]; + invariant(schemaNode.kind === Kind.SCHEMA_DEFINITION); + const [queryNode, mutationNode, subscriptionNode] = schemaNode.operationTypes; + + expect(getOperationRootType(testSchema, queryNode)).toEqual(queryType); + expect(getOperationRootType(testSchema, mutationNode)).toEqual(mutationType); + expect(getOperationRootType(testSchema, subscriptionNode)).toEqual(subscriptionType); + }); + + it('Gets a Mutation type for an OperationDefinitionNode', () => { + const testSchema = new GraphQLSchema({ + mutation: mutationType, + }); + + const doc = parse('mutation { field }'); + const operationNode = getOperationNode(doc); + expect(getOperationRootType(testSchema, operationNode)).toEqual(mutationType); + }); + + it('Gets a Subscription type for an OperationDefinitionNode', () => { + const testSchema = new GraphQLSchema({ + subscription: subscriptionType, + }); + + const doc = parse('subscription { field }'); + const operationNode = getOperationNode(doc); + expect(getOperationRootType(testSchema, operationNode)).toEqual(subscriptionType); + }); + + it('Throws when query type not defined in schema', () => { + const testSchema = new GraphQLSchema({}); + + const doc = parse('query { field }'); + const operationNode = getOperationNode(doc); + expect(() => getOperationRootType(testSchema, operationNode)).toThrow( + 'Schema does not define the required query root type.' + ); + }); + + it('Throws when mutation type not defined in schema', () => { + const testSchema = new GraphQLSchema({}); + + const doc = parse('mutation { field }'); + const operationNode = getOperationNode(doc); + expect(() => getOperationRootType(testSchema, operationNode)).toThrow('Schema is not configured for mutations.'); + }); + + it('Throws when subscription type not defined in schema', () => { + const testSchema = new GraphQLSchema({}); + + const doc = parse('subscription { field }'); + const operationNode = getOperationNode(doc); + expect(() => getOperationRootType(testSchema, operationNode)).toThrow( + 'Schema is not configured for subscriptions.' + ); + }); + + it('Throws when operation not a valid operation kind', () => { + const testSchema = new GraphQLSchema({}); + const doc = parse('{ field }'); + const operationNode: OperationDefinitionNode = { + ...getOperationNode(doc), + // @ts-expect-error + operation: 'non_existent_operation', + }; + + expect(() => getOperationRootType(testSchema, operationNode)).toThrow( + 'Can only have query, mutation and subscription operations.' + ); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/introspectionFromSchema-test.ts b/packages/graphql/src/utilities/__tests__/introspectionFromSchema-test.ts new file mode 100644 index 00000000000..ac619df2b8b --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/introspectionFromSchema-test.ts @@ -0,0 +1,63 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import { GraphQLObjectType } from '../../type/definition.js'; +import { GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { buildClientSchema } from '../buildClientSchema.js'; +import type { IntrospectionQuery } from '../getIntrospectionQuery.js'; +import { introspectionFromSchema } from '../introspectionFromSchema.js'; +import { printSchema } from '../printSchema.js'; + +function introspectionToSDL(introspection: IntrospectionQuery): string { + return printSchema(buildClientSchema(introspection)); +} + +describe('introspectionFromSchema', () => { + const schema = new GraphQLSchema({ + description: 'This is a simple schema', + query: new GraphQLObjectType({ + name: 'Simple', + description: 'This is a simple type', + fields: { + string: { + type: GraphQLString, + description: 'This is a string field', + }, + }, + }), + }); + + it('converts a simple schema', () => { + const introspection = introspectionFromSchema(schema); + + expect(introspectionToSDL(introspection)).toEqual(dedent` + """This is a simple schema""" + schema { + query: Simple + } + + """This is a simple type""" + type Simple { + """This is a string field""" + string: String + } + `); + }); + + it('converts a simple schema without descriptions', () => { + const introspection = introspectionFromSchema(schema, { + descriptions: false, + }); + + expect(introspectionToSDL(introspection)).toEqual(dedent` + schema { + query: Simple + } + + type Simple { + string: String + } + `); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/lexicographicSortSchema-test.ts b/packages/graphql/src/utilities/__tests__/lexicographicSortSchema-test.ts new file mode 100644 index 00000000000..f939a49a490 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/lexicographicSortSchema-test.ts @@ -0,0 +1,356 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import { buildSchema } from '../buildASTSchema.js'; +import { lexicographicSortSchema } from '../lexicographicSortSchema.js'; +import { printSchema } from '../printSchema.js'; + +function sortSDL(sdl: string): string { + const schema = buildSchema(sdl); + return printSchema(lexicographicSortSchema(schema)); +} + +describe('lexicographicSortSchema', () => { + it('sort fields', () => { + const sorted = sortSDL(` + input Bar { + barB: String! + barA: String + barC: [String] + } + + interface FooInterface { + fooB: String! + fooA: String + fooC: [String] + } + + type FooType implements FooInterface { + fooC: [String] + fooA: String + fooB: String! + } + + type Query { + dummy(arg: Bar): FooType + } + `); + + expect(sorted).toEqual(dedent` + input Bar { + barA: String + barB: String! + barC: [String] + } + + interface FooInterface { + fooA: String + fooB: String! + fooC: [String] + } + + type FooType implements FooInterface { + fooA: String + fooB: String! + fooC: [String] + } + + type Query { + dummy(arg: Bar): FooType + } + `); + }); + + it('sort implemented interfaces', () => { + const sorted = sortSDL(` + interface FooA { + dummy: String + } + + interface FooB { + dummy: String + } + + interface FooC implements FooB & FooA { + dummy: String + } + + type Query implements FooB & FooA & FooC { + dummy: String + } + `); + + expect(sorted).toEqual(dedent` + interface FooA { + dummy: String + } + + interface FooB { + dummy: String + } + + interface FooC implements FooA & FooB { + dummy: String + } + + type Query implements FooA & FooB & FooC { + dummy: String + } + `); + }); + + it('sort types in union', () => { + const sorted = sortSDL(` + type FooA { + dummy: String + } + + type FooB { + dummy: String + } + + type FooC { + dummy: String + } + + union FooUnion = FooB | FooA | FooC + + type Query { + dummy: FooUnion + } + `); + + expect(sorted).toEqual(dedent` + type FooA { + dummy: String + } + + type FooB { + dummy: String + } + + type FooC { + dummy: String + } + + union FooUnion = FooA | FooB | FooC + + type Query { + dummy: FooUnion + } + `); + }); + + it('sort enum values', () => { + const sorted = sortSDL(` + enum Foo { + B + C + A + } + + type Query { + dummy: Foo + } + `); + + expect(sorted).toEqual(dedent` + enum Foo { + A + B + C + } + + type Query { + dummy: Foo + } + `); + }); + + it('sort field arguments', () => { + const sorted = sortSDL(` + type Query { + dummy(argB: Int!, argA: String, argC: [Float]): ID + } + `); + + expect(sorted).toEqual(dedent` + type Query { + dummy(argA: String, argB: Int!, argC: [Float]): ID + } + `); + }); + + it('sort types', () => { + const sorted = sortSDL(` + type Query { + dummy(arg1: FooF, arg2: FooA, arg3: FooG): FooD + } + + type FooC implements FooE { + dummy: String + } + + enum FooG { + enumValue + } + + scalar FooA + + input FooF { + dummy: String + } + + union FooD = FooC | FooB + + interface FooE { + dummy: String + } + + type FooB { + dummy: String + } + `); + + expect(sorted).toEqual(dedent` + scalar FooA + + type FooB { + dummy: String + } + + type FooC implements FooE { + dummy: String + } + + union FooD = FooB | FooC + + interface FooE { + dummy: String + } + + input FooF { + dummy: String + } + + enum FooG { + enumValue + } + + type Query { + dummy(arg1: FooF, arg2: FooA, arg3: FooG): FooD + } + `); + }); + + it('sort directive arguments', () => { + const sorted = sortSDL(` + directive @test(argC: Float, argA: String, argB: Int) on FIELD + + type Query { + dummy: String + } + `); + + expect(sorted).toEqual(dedent` + directive @test(argA: String, argB: Int, argC: Float) on FIELD + + type Query { + dummy: String + } + `); + }); + + it('sort directive locations', () => { + const sorted = sortSDL(` + directive @test(argC: Float, argA: String, argB: Int) on UNION | FIELD | ENUM + + type Query { + dummy: String + } + `); + + expect(sorted).toEqual(dedent` + directive @test(argA: String, argB: Int, argC: Float) on ENUM | FIELD | UNION + + type Query { + dummy: String + } + `); + }); + + it('sort directives', () => { + const sorted = sortSDL(` + directive @fooC on FIELD + + directive @fooB on UNION + + directive @fooA on ENUM + + type Query { + dummy: String + } + `); + + expect(sorted).toEqual(dedent` + directive @fooA on ENUM + + directive @fooB on UNION + + directive @fooC on FIELD + + type Query { + dummy: String + } + `); + }); + + it('sort recursive types', () => { + const sorted = sortSDL(` + interface FooC { + fooB: FooB + fooA: FooA + fooC: FooC + } + + type FooB implements FooC { + fooB: FooB + fooA: FooA + } + + type FooA implements FooC { + fooB: FooB + fooA: FooA + } + + type Query { + fooC: FooC + fooB: FooB + fooA: FooA + } + `); + + expect(sorted).toEqual(dedent` + type FooA implements FooC { + fooA: FooA + fooB: FooB + } + + type FooB implements FooC { + fooA: FooA + fooB: FooB + } + + interface FooC { + fooA: FooA + fooB: FooB + fooC: FooC + } + + type Query { + fooA: FooA + fooB: FooB + fooC: FooC + } + `); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/printSchema-test.ts b/packages/graphql/src/utilities/__tests__/printSchema-test.ts new file mode 100644 index 00000000000..674ccc3e274 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/printSchema-test.ts @@ -0,0 +1,859 @@ +import { dedent, dedentString } from '../../__testUtils__/dedent.js'; + +import { DirectiveLocation } from '../../language/directiveLocation.js'; + +import type { GraphQLFieldConfig } from '../../type/definition.js'; +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, +} from '../../type/definition.js'; +import { GraphQLDirective } from '../../type/directives.js'; +import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../buildASTSchema.js'; +import { printIntrospectionSchema, printSchema } from '../printSchema.js'; + +function expectPrintedSchema(schema: GraphQLSchema) { + const schemaText = printSchema(schema); + // keep printSchema and buildSchema in sync + expect(printSchema(buildSchema(schemaText))).toEqual(schemaText); + return expect(schemaText); +} + +function buildSingleFieldSchema(fieldConfig: GraphQLFieldConfig) { + const Query = new GraphQLObjectType({ + name: 'Query', + fields: { singleField: fieldConfig }, + }); + return new GraphQLSchema({ query: Query }); +} + +describe('Type System Printer', () => { + it('Prints String Field', () => { + const schema = buildSingleFieldSchema({ type: GraphQLString }); + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField: String + } + `); + }); + + it('Prints [String] Field', () => { + const schema = buildSingleFieldSchema({ + type: new GraphQLList(GraphQLString), + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField: [String] + } + `); + }); + + it('Prints String! Field', () => { + const schema = buildSingleFieldSchema({ + type: new GraphQLNonNull(GraphQLString), + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField: String! + } + `); + }); + + it('Prints [String]! Field', () => { + const schema = buildSingleFieldSchema({ + type: new GraphQLNonNull(new GraphQLList(GraphQLString)), + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField: [String]! + } + `); + }); + + it('Prints [String!] Field', () => { + const schema = buildSingleFieldSchema({ + type: new GraphQLList(new GraphQLNonNull(GraphQLString)), + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField: [String!] + } + `); + }); + + it('Prints [String!]! Field', () => { + const schema = buildSingleFieldSchema({ + type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString))), + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField: [String!]! + } + `); + }); + + it('Print Object Field', () => { + const FooType = new GraphQLObjectType({ + name: 'Foo', + fields: { str: { type: GraphQLString } }, + }); + const schema = new GraphQLSchema({ types: [FooType] }); + + expectPrintedSchema(schema).toEqual(dedent` + type Foo { + str: String + } + `); + }); + + it('Prints String Field With Int Arg', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { argOne: { type: GraphQLInt } }, + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField(argOne: Int): String + } + `); + }); + + it('Prints String Field With Int Arg With Default', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { argOne: { type: GraphQLInt, defaultValue: 2 } }, + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField(argOne: Int = 2): String + } + `); + }); + + it('Prints String Field With String Arg With Default', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { argOne: { type: GraphQLString, defaultValue: 'tes\t de\fault' } }, + }); + + expectPrintedSchema(schema).toEqual( + dedentString(String.raw` + type Query { + singleField(argOne: String = "tes\t de\fault"): String + } + `) + ); + }); + + it('Prints String Field With Int Arg With Default Null', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { argOne: { type: GraphQLInt, defaultValue: null } }, + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField(argOne: Int = null): String + } + `); + }); + + it('Prints String Field With Int! Arg', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { argOne: { type: new GraphQLNonNull(GraphQLInt) } }, + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField(argOne: Int!): String + } + `); + }); + + it('Prints String Field With Multiple Args', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { + argOne: { type: GraphQLInt }, + argTwo: { type: GraphQLString }, + }, + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField(argOne: Int, argTwo: String): String + } + `); + }); + + it('Prints String Field With Multiple Args, First is Default', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { + argOne: { type: GraphQLInt, defaultValue: 1 }, + argTwo: { type: GraphQLString }, + argThree: { type: GraphQLBoolean }, + }, + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String + } + `); + }); + + it('Prints String Field With Multiple Args, Second is Default', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { + argOne: { type: GraphQLInt }, + argTwo: { type: GraphQLString, defaultValue: 'foo' }, + argThree: { type: GraphQLBoolean }, + }, + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String + } + `); + }); + + it('Prints String Field With Multiple Args, Last is Default', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + args: { + argOne: { type: GraphQLInt }, + argTwo: { type: GraphQLString }, + argThree: { type: GraphQLBoolean, defaultValue: false }, + }, + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String + } + `); + }); + + it('Prints schema with description', () => { + const schema = new GraphQLSchema({ + description: 'Schema description.', + query: new GraphQLObjectType({ name: 'Query', fields: {} }), + }); + + expectPrintedSchema(schema).toEqual(dedent` + """Schema description.""" + schema { + query: Query + } + + type Query + `); + }); + + it('Omits schema of common names', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ name: 'Query', fields: {} }), + mutation: new GraphQLObjectType({ name: 'Mutation', fields: {} }), + subscription: new GraphQLObjectType({ name: 'Subscription', fields: {} }), + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query + + type Mutation + + type Subscription + `); + }); + + it('Prints custom query root types', () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ name: 'CustomType', fields: {} }), + }); + + expectPrintedSchema(schema).toEqual(dedent` + schema { + query: CustomType + } + + type CustomType + `); + }); + + it('Prints custom mutation root types', () => { + const schema = new GraphQLSchema({ + mutation: new GraphQLObjectType({ name: 'CustomType', fields: {} }), + }); + + expectPrintedSchema(schema).toEqual(dedent` + schema { + mutation: CustomType + } + + type CustomType + `); + }); + + it('Prints custom subscription root types', () => { + const schema = new GraphQLSchema({ + subscription: new GraphQLObjectType({ name: 'CustomType', fields: {} }), + }); + + expectPrintedSchema(schema).toEqual(dedent` + schema { + subscription: CustomType + } + + type CustomType + `); + }); + + it('Print Interface', () => { + const FooType = new GraphQLInterfaceType({ + name: 'Foo', + fields: { str: { type: GraphQLString } }, + }); + + const BarType = new GraphQLObjectType({ + name: 'Bar', + fields: { str: { type: GraphQLString } }, + interfaces: [FooType], + }); + + const schema = new GraphQLSchema({ types: [BarType] }); + expectPrintedSchema(schema).toEqual(dedent` + type Bar implements Foo { + str: String + } + + interface Foo { + str: String + } + `); + }); + + it('Print Multiple Interface', () => { + const FooType = new GraphQLInterfaceType({ + name: 'Foo', + fields: { str: { type: GraphQLString } }, + }); + + const BazType = new GraphQLInterfaceType({ + name: 'Baz', + fields: { int: { type: GraphQLInt } }, + }); + + const BarType = new GraphQLObjectType({ + name: 'Bar', + fields: { + str: { type: GraphQLString }, + int: { type: GraphQLInt }, + }, + interfaces: [FooType, BazType], + }); + + const schema = new GraphQLSchema({ types: [BarType] }); + expectPrintedSchema(schema).toEqual(dedent` + type Bar implements Foo & Baz { + str: String + int: Int + } + + interface Foo { + str: String + } + + interface Baz { + int: Int + } + `); + }); + + it('Print Hierarchical Interface', () => { + const FooType = new GraphQLInterfaceType({ + name: 'Foo', + fields: { str: { type: GraphQLString } }, + }); + + const BazType = new GraphQLInterfaceType({ + name: 'Baz', + interfaces: [FooType], + fields: { + int: { type: GraphQLInt }, + str: { type: GraphQLString }, + }, + }); + + const BarType = new GraphQLObjectType({ + name: 'Bar', + fields: { + str: { type: GraphQLString }, + int: { type: GraphQLInt }, + }, + interfaces: [FooType, BazType], + }); + + const Query = new GraphQLObjectType({ + name: 'Query', + fields: { bar: { type: BarType } }, + }); + + const schema = new GraphQLSchema({ query: Query, types: [BarType] }); + expectPrintedSchema(schema).toEqual(dedent` + type Bar implements Foo & Baz { + str: String + int: Int + } + + interface Foo { + str: String + } + + interface Baz implements Foo { + int: Int + str: String + } + + type Query { + bar: Bar + } + `); + }); + + it('Print Unions', () => { + const FooType = new GraphQLObjectType({ + name: 'Foo', + fields: { + bool: { type: GraphQLBoolean }, + }, + }); + + const BarType = new GraphQLObjectType({ + name: 'Bar', + fields: { + str: { type: GraphQLString }, + }, + }); + + const SingleUnion = new GraphQLUnionType({ + name: 'SingleUnion', + types: [FooType], + }); + + const MultipleUnion = new GraphQLUnionType({ + name: 'MultipleUnion', + types: [FooType, BarType], + }); + + const schema = new GraphQLSchema({ types: [SingleUnion, MultipleUnion] }); + expectPrintedSchema(schema).toEqual(dedent` + union SingleUnion = Foo + + type Foo { + bool: Boolean + } + + union MultipleUnion = Foo | Bar + + type Bar { + str: String + } + `); + }); + + it('Print Input Type', () => { + const InputType = new GraphQLInputObjectType({ + name: 'InputType', + fields: { + int: { type: GraphQLInt }, + }, + }); + + const schema = new GraphQLSchema({ types: [InputType] }); + expectPrintedSchema(schema).toEqual(dedent` + input InputType { + int: Int + } + `); + }); + + it('Custom Scalar', () => { + const OddType = new GraphQLScalarType({ name: 'Odd' }); + + const schema = new GraphQLSchema({ types: [OddType] }); + expectPrintedSchema(schema).toEqual(dedent` + scalar Odd + `); + }); + + it('Custom Scalar with specifiedByURL', () => { + const FooType = new GraphQLScalarType({ + name: 'Foo', + specifiedByURL: 'https://example.com/foo_spec', + }); + + const schema = new GraphQLSchema({ types: [FooType] }); + expectPrintedSchema(schema).toEqual(dedent` + scalar Foo @specifiedBy(url: "https://example.com/foo_spec") + `); + }); + + it('Enum', () => { + const RGBType = new GraphQLEnumType({ + name: 'RGB', + values: { + RED: {}, + GREEN: {}, + BLUE: {}, + }, + }); + + const schema = new GraphQLSchema({ types: [RGBType] }); + expectPrintedSchema(schema).toEqual(dedent` + enum RGB { + RED + GREEN + BLUE + } + `); + }); + + it('Prints empty types', () => { + const schema = new GraphQLSchema({ + types: [ + new GraphQLEnumType({ name: 'SomeEnum', values: {} }), + new GraphQLInputObjectType({ name: 'SomeInputObject', fields: {} }), + new GraphQLInterfaceType({ name: 'SomeInterface', fields: {} }), + new GraphQLObjectType({ name: 'SomeObject', fields: {} }), + new GraphQLUnionType({ name: 'SomeUnion', types: [] }), + ], + }); + + expectPrintedSchema(schema).toEqual(dedent` + enum SomeEnum + + input SomeInputObject + + interface SomeInterface + + type SomeObject + + union SomeUnion + `); + }); + + it('Prints custom directives', () => { + const SimpleDirective = new GraphQLDirective({ + name: 'simpleDirective', + locations: [DirectiveLocation.FIELD], + }); + const ComplexDirective = new GraphQLDirective({ + name: 'complexDirective', + description: 'Complex Directive', + args: { + stringArg: { type: GraphQLString }, + intArg: { type: GraphQLInt, defaultValue: -1 }, + }, + isRepeatable: true, + locations: [DirectiveLocation.FIELD, DirectiveLocation.QUERY], + }); + + const schema = new GraphQLSchema({ + directives: [SimpleDirective, ComplexDirective], + }); + expectPrintedSchema(schema).toEqual(dedent` + directive @simpleDirective on FIELD + + """Complex Directive""" + directive @complexDirective(stringArg: String, intArg: Int = -1) repeatable on FIELD | QUERY + `); + }); + + it('Prints an empty description', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + description: '', + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + """""" + singleField: String + } + `); + }); + + it('Prints an description with only whitespace', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + description: ' ', + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + " " + singleField: String + } + `); + }); + + it('One-line prints a short description', () => { + const schema = buildSingleFieldSchema({ + type: GraphQLString, + description: 'This field is awesome', + }); + + expectPrintedSchema(schema).toEqual(dedent` + type Query { + """This field is awesome""" + singleField: String + } + `); + }); + + it('Print Introspection Schema', () => { + const schema = new GraphQLSchema({}); + const output = printIntrospectionSchema(schema); + + expect(output).toEqual(dedent` + """ + Directs the executor to include this field or fragment only when the \`if\` argument is true. + """ + directive @include( + """Included when true.""" + if: Boolean! + ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT + + """ + Directs the executor to skip this field or fragment when the \`if\` argument is true. + """ + directive @skip( + """Skipped when true.""" + if: Boolean! + ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT + + """Marks an element of a GraphQL schema as no longer supported.""" + directive @deprecated( + """ + Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/). + """ + reason: String = "No longer supported" + ) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE + + """Exposes a URL that specifies the behavior of this scalar.""" + directive @specifiedBy( + """The URL that specifies the behavior of this scalar.""" + url: String! + ) on SCALAR + + """ + A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations. + """ + type __Schema { + description: String + + """A list of all types supported by this server.""" + types: [__Type!]! + + """The type that query operations will be rooted at.""" + queryType: __Type! + + """ + If this server supports mutation, the type that mutation operations will be rooted at. + """ + mutationType: __Type + + """ + If this server support subscription, the type that subscription operations will be rooted at. + """ + subscriptionType: __Type + + """A list of all directives supported by this server.""" + directives: [__Directive!]! + } + + """ + The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the \`__TypeKind\` enum. + + Depending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name, description and optional \`specifiedByURL\`, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types. + """ + type __Type { + kind: __TypeKind! + name: String + description: String + specifiedByURL: String + fields(includeDeprecated: Boolean = false): [__Field!] + interfaces: [__Type!] + possibleTypes: [__Type!] + enumValues(includeDeprecated: Boolean = false): [__EnumValue!] + inputFields(includeDeprecated: Boolean = false): [__InputValue!] + ofType: __Type + } + + """An enum describing what kind of type a given \`__Type\` is.""" + enum __TypeKind { + """Indicates this type is a scalar.""" + SCALAR + + """ + Indicates this type is an object. \`fields\` and \`interfaces\` are valid fields. + """ + OBJECT + + """ + Indicates this type is an interface. \`fields\`, \`interfaces\`, and \`possibleTypes\` are valid fields. + """ + INTERFACE + + """Indicates this type is a union. \`possibleTypes\` is a valid field.""" + UNION + + """Indicates this type is an enum. \`enumValues\` is a valid field.""" + ENUM + + """ + Indicates this type is an input object. \`inputFields\` is a valid field. + """ + INPUT_OBJECT + + """Indicates this type is a list. \`ofType\` is a valid field.""" + LIST + + """Indicates this type is a non-null. \`ofType\` is a valid field.""" + NON_NULL + } + + """ + Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type. + """ + type __Field { + name: String! + description: String + args(includeDeprecated: Boolean = false): [__InputValue!]! + type: __Type! + isDeprecated: Boolean! + deprecationReason: String + } + + """ + Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value. + """ + type __InputValue { + name: String! + description: String + type: __Type! + + """ + A GraphQL-formatted string representing the default value for this input value. + """ + defaultValue: String + isDeprecated: Boolean! + deprecationReason: String + } + + """ + One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string. + """ + type __EnumValue { + name: String! + description: String + isDeprecated: Boolean! + deprecationReason: String + } + + """ + A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document. + + In some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor. + """ + type __Directive { + name: String! + description: String + isRepeatable: Boolean! + locations: [__DirectiveLocation!]! + args(includeDeprecated: Boolean = false): [__InputValue!]! + } + + """ + A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies. + """ + enum __DirectiveLocation { + """Location adjacent to a query operation.""" + QUERY + + """Location adjacent to a mutation operation.""" + MUTATION + + """Location adjacent to a subscription operation.""" + SUBSCRIPTION + + """Location adjacent to a field.""" + FIELD + + """Location adjacent to a fragment definition.""" + FRAGMENT_DEFINITION + + """Location adjacent to a fragment spread.""" + FRAGMENT_SPREAD + + """Location adjacent to an inline fragment.""" + INLINE_FRAGMENT + + """Location adjacent to a variable definition.""" + VARIABLE_DEFINITION + + """Location adjacent to a schema definition.""" + SCHEMA + + """Location adjacent to a scalar definition.""" + SCALAR + + """Location adjacent to an object type definition.""" + OBJECT + + """Location adjacent to a field definition.""" + FIELD_DEFINITION + + """Location adjacent to an argument definition.""" + ARGUMENT_DEFINITION + + """Location adjacent to an interface definition.""" + INTERFACE + + """Location adjacent to a union definition.""" + UNION + + """Location adjacent to an enum definition.""" + ENUM + + """Location adjacent to an enum value definition.""" + ENUM_VALUE + + """Location adjacent to an input object type definition.""" + INPUT_OBJECT + + """Location adjacent to an input object field definition.""" + INPUT_FIELD_DEFINITION + } + `); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/separateOperations-test.ts b/packages/graphql/src/utilities/__tests__/separateOperations-test.ts new file mode 100644 index 00000000000..53f5b3bfe2e --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/separateOperations-test.ts @@ -0,0 +1,255 @@ +import { dedent } from '../../__testUtils__/dedent.js'; + +import { mapValue } from '../../jsutils/mapValue.js'; + +import { parse } from '../../language/parser.js'; +import { print } from '../../language/printer.js'; + +import { separateOperations } from '../separateOperations.js'; + +describe('separateOperations', () => { + it('separates one AST into multiple, maintaining document order', () => { + const ast = parse(` + { + ...Y + ...X + } + + query One { + foo + bar + ...A + ...X + } + + fragment A on T { + field + ...B + } + + fragment X on T { + fieldX + } + + query Two { + ...A + ...Y + baz + } + + fragment Y on T { + fieldY + } + + fragment B on T { + something + } + `); + + const separatedASTs = mapValue(separateOperations(ast), print); + expect(separatedASTs).toEqual({ + '': dedent` + { + ...Y + ...X + } + + fragment X on T { + fieldX + } + + fragment Y on T { + fieldY + } + `, + One: dedent` + query One { + foo + bar + ...A + ...X + } + + fragment A on T { + field + ...B + } + + fragment X on T { + fieldX + } + + fragment B on T { + something + } + `, + Two: dedent` + fragment A on T { + field + ...B + } + + query Two { + ...A + ...Y + baz + } + + fragment Y on T { + fieldY + } + + fragment B on T { + something + } + `, + }); + }); + + it('survives circular dependencies', () => { + const ast = parse(` + query One { + ...A + } + + fragment A on T { + ...B + } + + fragment B on T { + ...A + } + + query Two { + ...B + } + `); + + const separatedASTs = mapValue(separateOperations(ast), print); + expect(separatedASTs).toEqual({ + One: dedent` + query One { + ...A + } + + fragment A on T { + ...B + } + + fragment B on T { + ...A + } + `, + Two: dedent` + fragment A on T { + ...B + } + + fragment B on T { + ...A + } + + query Two { + ...B + } + `, + }); + }); + + it('distinguish query and fragment names', () => { + const ast = parse(` + { + ...NameClash + } + + fragment NameClash on T { + oneField + } + + query NameClash { + ...ShouldBeSkippedInFirstQuery + } + + fragment ShouldBeSkippedInFirstQuery on T { + twoField + } + `); + + const separatedASTs = mapValue(separateOperations(ast), print); + expect(separatedASTs).toEqual({ + '': dedent` + { + ...NameClash + } + + fragment NameClash on T { + oneField + } + `, + NameClash: dedent` + query NameClash { + ...ShouldBeSkippedInFirstQuery + } + + fragment ShouldBeSkippedInFirstQuery on T { + twoField + } + `, + }); + }); + + it('ignores type definitions', () => { + const ast = parse(` + query Foo { + ...Bar + } + + fragment Bar on T { + baz + } + + scalar Foo + type Bar + `); + + const separatedASTs = mapValue(separateOperations(ast), print); + expect(separatedASTs).toEqual({ + Foo: dedent` + query Foo { + ...Bar + } + + fragment Bar on T { + baz + } + `, + }); + }); + + it('handles unknown fragments', () => { + const ast = parse(` + { + ...Unknown + ...Known + } + + fragment Known on T { + someField + } + `); + + const separatedASTs = mapValue(separateOperations(ast), print); + expect(separatedASTs).toEqual({ + '': dedent` + { + ...Unknown + ...Known + } + + fragment Known on T { + someField + } + `, + }); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/sortValueNode-test.ts b/packages/graphql/src/utilities/__tests__/sortValueNode-test.ts new file mode 100644 index 00000000000..51c5c4280ab --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/sortValueNode-test.ts @@ -0,0 +1,31 @@ +import { parseValue } from '../../language/parser.js'; +import { print } from '../../language/printer.js'; + +import { sortValueNode } from '../sortValueNode.js'; + +describe('sortValueNode', () => { + function expectSortedValue(source: string) { + return expect(print(sortValueNode(parseValue(source)))); + } + + it('do not change non-object values', () => { + expectSortedValue('1').toEqual('1'); + expectSortedValue('3.14').toEqual('3.14'); + expectSortedValue('null').toEqual('null'); + expectSortedValue('true').toEqual('true'); + expectSortedValue('false').toEqual('false'); + expectSortedValue('"cba"').toEqual('"cba"'); + expectSortedValue('"""cba"""').toEqual('"""cba"""'); + expectSortedValue('[1, 3.14, null, false, "cba"]').toEqual('[1, 3.14, null, false, "cba"]'); + expectSortedValue('[[1, 3.14, null, false, "cba"]]').toEqual('[[1, 3.14, null, false, "cba"]]'); + }); + + it('sort input object fields', () => { + expectSortedValue('{ b: 2, a: 1 }').toEqual('{ a: 1, b: 2 }'); + expectSortedValue('{ a: { c: 3, b: 2 } }').toEqual('{ a: { b: 2, c: 3 } }'); + expectSortedValue('[{ b: 2, a: 1 }, { d: 4, c: 3 }]').toEqual('[{ a: 1, b: 2 }, { c: 3, d: 4 }]'); + expectSortedValue('{ b: { g: 7, f: 6 }, c: 3 , a: { d: 4, e: 5 } }').toEqual( + '{ a: { d: 4, e: 5 }, b: { f: 6, g: 7 }, c: 3 }' + ); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/stripIgnoredCharacters-fuzz.ts b/packages/graphql/src/utilities/__tests__/stripIgnoredCharacters-fuzz.ts new file mode 100644 index 00000000000..4b9e4c21f2e --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/stripIgnoredCharacters-fuzz.ts @@ -0,0 +1,223 @@ +import { genFuzzStrings } from '../../__testUtils__/genFuzzStrings.js'; + +import { Lexer } from '../../language/lexer.js'; +import { Source } from '../../language/source.js'; + +import { stripIgnoredCharacters } from '../stripIgnoredCharacters.js'; + +const ignoredTokens = [ + // UnicodeBOM :: + '\uFEFF', // Byte Order Mark (U+FEFF) + + // WhiteSpace :: + '\t', // Horizontal Tab (U+0009) + ' ', // Space (U+0020) + + // LineTerminator :: + '\n', // "New Line (U+000A)" + '\r', // "Carriage Return (U+000D)" [ lookahead ! "New Line (U+000A)" ] + '\r\n', // "Carriage Return (U+000D)" "New Line (U+000A)" + + // Comment :: + '# "Comment" string\n', // `#` CommentChar* + + // Comma :: + ',', // , +]; + +const punctuatorTokens = ['!', '$', '(', ')', '...', ':', '=', '@', '[', ']', '{', '|', '}']; + +const nonPunctuatorTokens = [ + 'name_token', // Name + '1', // IntValue + '3.14', // FloatValue + '"some string value"', // StringValue + '"""block\nstring\nvalue"""', // StringValue(BlockString) +]; + +function lexValue(str: string) { + const lexer = new Lexer(new Source(str)); + const value = lexer.advance().value; + + expect(lexer.advance().kind === '').toBeTruthy(); + return value; +} + +function expectStripped(docString: string) { + return { + toEqual(expected: string): void { + const stripped = stripIgnoredCharacters(docString); + + expect(stripped === expected).toBeTruthy(); + + const strippedTwice = stripIgnoredCharacters(stripped); + + expect(stripped === strippedTwice).toBeTruthy(); + }, + toStayTheSame(): void { + this.toEqual(docString); + }, + }; +} + +describe('stripIgnoredCharacters', () => { + it('strips documents with random combination of ignored characters', () => { + for (const ignored of ignoredTokens) { + expectStripped(ignored).toEqual(''); + + for (const anotherIgnored of ignoredTokens) { + expectStripped(ignored + anotherIgnored).toEqual(''); + } + } + expectStripped(ignoredTokens.join('')).toEqual(''); + }); + + it('strips random leading and trailing ignored tokens', () => { + for (const token of [...punctuatorTokens, ...nonPunctuatorTokens]) { + for (const ignored of ignoredTokens) { + expectStripped(ignored + token).toEqual(token); + expectStripped(token + ignored).toEqual(token); + + for (const anotherIgnored of ignoredTokens) { + expectStripped(token + ignored + ignored).toEqual(token); + expectStripped(ignored + anotherIgnored + token).toEqual(token); + } + } + + expectStripped(ignoredTokens.join('') + token).toEqual(token); + expectStripped(token + ignoredTokens.join('')).toEqual(token); + } + }); + + it('strips random ignored tokens between punctuator tokens', () => { + for (const left of punctuatorTokens) { + for (const right of punctuatorTokens) { + for (const ignored of ignoredTokens) { + expectStripped(left + ignored + right).toEqual(left + right); + + for (const anotherIgnored of ignoredTokens) { + expectStripped(left + ignored + anotherIgnored + right).toEqual(left + right); + } + } + + expectStripped(left + ignoredTokens.join('') + right).toEqual(left + right); + } + } + }); + + it('strips random ignored tokens between punctuator and non-punctuator tokens', () => { + for (const nonPunctuator of nonPunctuatorTokens) { + for (const punctuator of punctuatorTokens) { + for (const ignored of ignoredTokens) { + expectStripped(punctuator + ignored + nonPunctuator).toEqual(punctuator + nonPunctuator); + + for (const anotherIgnored of ignoredTokens) { + expectStripped(punctuator + ignored + anotherIgnored + nonPunctuator).toEqual(punctuator + nonPunctuator); + } + } + + expectStripped(punctuator + ignoredTokens.join('') + nonPunctuator).toEqual(punctuator + nonPunctuator); + } + } + }); + + it('strips random ignored tokens between non-punctuator and punctuator tokens', () => { + for (const nonPunctuator of nonPunctuatorTokens) { + for (const punctuator of punctuatorTokens) { + // Special case for that is handled in the below test + if (punctuator === '...') { + continue; + } + + for (const ignored of ignoredTokens) { + expectStripped(nonPunctuator + ignored + punctuator).toEqual(nonPunctuator + punctuator); + + for (const anotherIgnored of ignoredTokens) { + expectStripped(nonPunctuator + ignored + anotherIgnored + punctuator).toEqual(nonPunctuator + punctuator); + } + } + + expectStripped(nonPunctuator + ignoredTokens.join('') + punctuator).toEqual(nonPunctuator + punctuator); + } + } + }); + + it('replace random ignored tokens between non-punctuator tokens and spread with space', () => { + for (const nonPunctuator of nonPunctuatorTokens) { + for (const ignored of ignoredTokens) { + expectStripped(nonPunctuator + ignored + '...').toEqual(nonPunctuator + ' ...'); + + for (const anotherIgnored of ignoredTokens) { + expectStripped(nonPunctuator + ignored + anotherIgnored + ' ...').toEqual(nonPunctuator + ' ...'); + } + } + + expectStripped(nonPunctuator + ignoredTokens.join('') + '...').toEqual(nonPunctuator + ' ...'); + } + }); + + it('replace random ignored tokens between non-punctuator tokens with space', () => { + for (const left of nonPunctuatorTokens) { + for (const right of nonPunctuatorTokens) { + for (const ignored of ignoredTokens) { + expectStripped(left + ignored + right).toEqual(left + ' ' + right); + + for (const anotherIgnored of ignoredTokens) { + expectStripped(left + ignored + anotherIgnored + right).toEqual(left + ' ' + right); + } + } + + expectStripped(left + ignoredTokens.join('') + right).toEqual(left + ' ' + right); + } + } + }); + + it('does not strip random ignored tokens embedded in the string', () => { + for (const ignored of ignoredTokens) { + expectStripped(JSON.stringify(ignored)).toStayTheSame(); + + for (const anotherIgnored of ignoredTokens) { + expectStripped(JSON.stringify(ignored + anotherIgnored)).toStayTheSame(); + } + } + + expectStripped(JSON.stringify(ignoredTokens.join(''))).toStayTheSame(); + }); + + it('does not strip random ignored tokens embedded in the block string', () => { + const ignoredTokensWithoutFormatting = ignoredTokens.filter( + token => !['\n', '\r', '\r\n', '\t', ' '].includes(token) + ); + for (const ignored of ignoredTokensWithoutFormatting) { + expectStripped('"""|' + ignored + '|"""').toStayTheSame(); + + for (const anotherIgnored of ignoredTokensWithoutFormatting) { + expectStripped('"""|' + ignored + anotherIgnored + '|"""').toStayTheSame(); + } + } + + expectStripped('"""|' + ignoredTokensWithoutFormatting.join('') + '|"""').toStayTheSame(); + }); + + it('strips ignored characters inside random block strings', () => { + // Testing with length >7 is taking exponentially more time. However it is + // highly recommended to test with increased limit if you make any change. + for (const fuzzStr of genFuzzStrings({ + allowedChars: ['\n', '\t', ' ', '"', 'a', '\\'], + maxLength: 7, + })) { + const testStr = '"""' + fuzzStr + '"""'; + + let testValue; + try { + testValue = lexValue(testStr); + } catch (e) { + continue; // skip invalid values + } + + const strippedValue = lexValue(stripIgnoredCharacters(testStr)); + + expect(testValue === strippedValue).toBeTruthy(); + } + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/stripIgnoredCharacters-test.ts b/packages/graphql/src/utilities/__tests__/stripIgnoredCharacters-test.ts new file mode 100644 index 00000000000..7556fab3a1e --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/stripIgnoredCharacters-test.ts @@ -0,0 +1,225 @@ +import { dedent } from '../../__testUtils__/dedent.js'; +import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js'; +import { kitchenSinkSDL } from '../../__testUtils__/kitchenSinkSDL.js'; + +import type { Maybe } from '../../jsutils/Maybe.js'; + +import { Lexer } from '../../language/lexer.js'; +import { parse } from '../../language/parser.js'; +import { Source } from '../../language/source.js'; + +import { stripIgnoredCharacters } from '../stripIgnoredCharacters.js'; + +function lexValue(str: string): Maybe { + const lexer = new Lexer(new Source(str)); + const value = lexer.advance().value; + + expect(lexer.advance().kind === '').toBeTruthy(); + return value; +} + +function expectStripped(docString: string) { + return { + toEqual(expected: string): void { + const stripped = stripIgnoredCharacters(docString); + expect(stripped).toEqual(expected); + + const strippedTwice = stripIgnoredCharacters(stripped); + expect(strippedTwice).toEqual(expected); + }, + toStayTheSame(): void { + this.toEqual(docString); + }, + }; +} + +describe('stripIgnoredCharacters', () => { + it('strips ignored characters from GraphQL query document', () => { + const query = dedent` + query SomeQuery($foo: String!, $bar: String) { + someField(foo: $foo, bar: $bar) { + a + b { + c + d + } + } + } + `; + + expect(stripIgnoredCharacters(query)).toEqual( + 'query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}}' + ); + }); + + it('accepts Source object', () => { + expect(stripIgnoredCharacters(new Source('{ a }'))).toEqual('{a}'); + }); + + it('strips ignored characters from GraphQL SDL document', () => { + const sdl = dedent` + """ + Type description + """ + type Foo { + """ + Field description + """ + bar: String + } + `; + + expect(stripIgnoredCharacters(sdl)).toEqual('"""Type description""" type Foo{"""Field description""" bar:String}'); + }); + + it('report document with invalid token', () => { + let caughtError; + + try { + stripIgnoredCharacters('{ foo(arg: "\n"'); + } catch (e) { + caughtError = e; + } + + expect(String(caughtError)).toEqual(dedent` + Syntax Error: Unterminated string. + + GraphQL request:1:13 + 1 | { foo(arg: " + | ^ + 2 | " + `); + }); + + it('strips non-parsable document', () => { + expectStripped('{ foo(arg: "str"').toEqual('{foo(arg:"str"'); + }); + + it('strips documents with only ignored characters', () => { + expectStripped('\n').toEqual(''); + expectStripped(',').toEqual(''); + expectStripped(',,').toEqual(''); + expectStripped('#comment\n, \n').toEqual(''); + }); + + it('strips leading and trailing ignored tokens', () => { + expectStripped('\n1').toEqual('1'); + expectStripped(',1').toEqual('1'); + expectStripped(',,1').toEqual('1'); + expectStripped('#comment\n, \n1').toEqual('1'); + + expectStripped('1\n').toEqual('1'); + expectStripped('1,').toEqual('1'); + expectStripped('1,,').toEqual('1'); + expectStripped('1#comment\n, \n').toEqual('1'); + }); + + it('strips ignored tokens between punctuator tokens', () => { + expectStripped('[,)').toEqual('[)'); + expectStripped('[\r)').toEqual('[)'); + expectStripped('[\r\r)').toEqual('[)'); + expectStripped('[\r,)').toEqual('[)'); + expectStripped('[,\n)').toEqual('[)'); + }); + + it('strips ignored tokens between punctuator and non-punctuator tokens', () => { + expectStripped('[,1').toEqual('[1'); + expectStripped('[\r1').toEqual('[1'); + expectStripped('[\r\r1').toEqual('[1'); + expectStripped('[\r,1').toEqual('[1'); + expectStripped('[,\n1').toEqual('[1'); + }); + + it('strips ignored tokens between non-punctuator and punctuator tokens', () => { + expectStripped('1,[').toEqual('1['); + expectStripped('1\r[').toEqual('1['); + expectStripped('1\r\r[').toEqual('1['); + expectStripped('1\r,[').toEqual('1['); + expectStripped('1,\n[').toEqual('1['); + }); + + it('replace ignored tokens between non-punctuator tokens and spread with space', () => { + expectStripped('a ...').toEqual('a ...'); + expectStripped('1 ...').toEqual('1 ...'); + expectStripped('1 ... ...').toEqual('1 ......'); + }); + + it('replace ignored tokens between non-punctuator tokens with space', () => { + expectStripped('1 2').toStayTheSame(); + expectStripped('"" ""').toStayTheSame(); + expectStripped('a b').toStayTheSame(); + + expectStripped('a,1').toEqual('a 1'); + expectStripped('a,,1').toEqual('a 1'); + expectStripped('a 1').toEqual('a 1'); + expectStripped('a \t 1').toEqual('a 1'); + }); + + it('does not strip ignored tokens embedded in the string', () => { + expectStripped('" "').toStayTheSame(); + expectStripped('","').toStayTheSame(); + expectStripped('",,"').toStayTheSame(); + expectStripped('",|"').toStayTheSame(); + }); + + it('does not strip ignored tokens embedded in the block string', () => { + expectStripped('""","""').toStayTheSame(); + expectStripped('""",,"""').toStayTheSame(); + expectStripped('""",|"""').toStayTheSame(); + }); + + it('strips ignored characters inside block strings', () => { + function expectStrippedString(blockStr: string) { + const originalValue = lexValue(blockStr); + const strippedValue = lexValue(stripIgnoredCharacters(blockStr)); + + expect(strippedValue).toEqual(originalValue); + return expectStripped(blockStr); + } + + expectStrippedString('""""""').toStayTheSame(); + expectStrippedString('""" """').toEqual('""""""'); + + expectStrippedString('"""a"""').toStayTheSame(); + expectStrippedString('""" a"""').toEqual('""" a"""'); + expectStrippedString('""" a """').toEqual('""" a """'); + + expectStrippedString('"""\n"""').toEqual('""""""'); + expectStrippedString('"""a\nb"""').toEqual('"""a\nb"""'); + expectStrippedString('"""a\rb"""').toEqual('"""a\nb"""'); + expectStrippedString('"""a\r\nb"""').toEqual('"""a\nb"""'); + expectStrippedString('"""a\r\n\nb"""').toEqual('"""a\n\nb"""'); + + expectStrippedString('"""\\\n"""').toStayTheSame(); + expectStrippedString('""""\n"""').toStayTheSame(); + expectStrippedString('"""\\"""\n"""').toEqual('"""\\""""""'); + + expectStrippedString('"""\na\n b"""').toStayTheSame(); + expectStrippedString('"""\n a\n b"""').toEqual('"""a\nb"""'); + expectStrippedString('"""\na\n b\nc"""').toEqual('"""a\n b\nc"""'); + }); + + it('strips kitchen sink query but maintains the exact same AST', () => { + const strippedQuery = stripIgnoredCharacters(kitchenSinkQuery); + expect(stripIgnoredCharacters(strippedQuery)).toEqual(strippedQuery); + + const queryAST = parse(kitchenSinkQuery, { + noLocation: true, + experimentalClientControlledNullability: true, + }); + const strippedAST = parse(strippedQuery, { + noLocation: true, + experimentalClientControlledNullability: true, + }); + expect(strippedAST).toEqual(queryAST); + }); + + it('strips kitchen sink SDL but maintains the exact same AST', () => { + const strippedSDL = stripIgnoredCharacters(kitchenSinkSDL); + expect(stripIgnoredCharacters(strippedSDL)).toEqual(strippedSDL); + + const sdlAST = parse(kitchenSinkSDL, { noLocation: true }); + const strippedAST = parse(strippedSDL, { noLocation: true }); + expect(strippedAST).toEqual(sdlAST); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/typeComparators-test.ts b/packages/graphql/src/utilities/__tests__/typeComparators-test.ts new file mode 100644 index 00000000000..bd8c9bb2fc2 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/typeComparators-test.ts @@ -0,0 +1,136 @@ +import type { GraphQLFieldConfigMap } from '../../type/definition.js'; +import { + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLUnionType, +} from '../../type/definition.js'; +import { GraphQLFloat, GraphQLInt, GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { isEqualType, isTypeSubTypeOf } from '../typeComparators.js'; + +describe('typeComparators', () => { + describe('isEqualType', () => { + it('same reference are equal', () => { + expect(isEqualType(GraphQLString, GraphQLString)).toEqual(true); + }); + + it('int and float are not equal', () => { + expect(isEqualType(GraphQLInt, GraphQLFloat)).toEqual(false); + }); + + it('lists of same type are equal', () => { + expect(isEqualType(new GraphQLList(GraphQLInt), new GraphQLList(GraphQLInt))).toEqual(true); + }); + + it('lists is not equal to item', () => { + expect(isEqualType(new GraphQLList(GraphQLInt), GraphQLInt)).toEqual(false); + }); + + it('non-null of same type are equal', () => { + expect(isEqualType(new GraphQLNonNull(GraphQLInt), new GraphQLNonNull(GraphQLInt))).toEqual(true); + }); + + it('non-null is not equal to nullable', () => { + expect(isEqualType(new GraphQLNonNull(GraphQLInt), GraphQLInt)).toEqual(false); + }); + }); + + describe('isTypeSubTypeOf', () => { + function testSchema(fields: GraphQLFieldConfigMap) { + return new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields, + }), + }); + } + + it('same reference is subtype', () => { + const schema = testSchema({ field: { type: GraphQLString } }); + expect(isTypeSubTypeOf(schema, GraphQLString, GraphQLString)).toEqual(true); + }); + + it('int is not subtype of float', () => { + const schema = testSchema({ field: { type: GraphQLString } }); + expect(isTypeSubTypeOf(schema, GraphQLInt, GraphQLFloat)).toEqual(false); + }); + + it('non-null is subtype of nullable', () => { + const schema = testSchema({ field: { type: GraphQLString } }); + expect(isTypeSubTypeOf(schema, new GraphQLNonNull(GraphQLInt), GraphQLInt)).toEqual(true); + }); + + it('nullable is not subtype of non-null', () => { + const schema = testSchema({ field: { type: GraphQLString } }); + expect(isTypeSubTypeOf(schema, GraphQLInt, new GraphQLNonNull(GraphQLInt))).toEqual(false); + }); + + it('item is not subtype of list', () => { + const schema = testSchema({ field: { type: GraphQLString } }); + expect(isTypeSubTypeOf(schema, GraphQLInt, new GraphQLList(GraphQLInt))).toEqual(false); + }); + + it('list is not subtype of item', () => { + const schema = testSchema({ field: { type: GraphQLString } }); + expect(isTypeSubTypeOf(schema, new GraphQLList(GraphQLInt), GraphQLInt)).toEqual(false); + }); + + it('member is subtype of union', () => { + const member = new GraphQLObjectType({ + name: 'Object', + fields: { + field: { type: GraphQLString }, + }, + }); + const union = new GraphQLUnionType({ name: 'Union', types: [member] }); + const schema = testSchema({ field: { type: union } }); + expect(isTypeSubTypeOf(schema, member, union)).toEqual(true); + }); + + it('implementing object is subtype of interface', () => { + const iface = new GraphQLInterfaceType({ + name: 'Interface', + fields: { + field: { type: GraphQLString }, + }, + }); + const impl = new GraphQLObjectType({ + name: 'Object', + interfaces: [iface], + fields: { + field: { type: GraphQLString }, + }, + }); + const schema = testSchema({ field: { type: impl } }); + expect(isTypeSubTypeOf(schema, impl, iface)).toEqual(true); + }); + + it('implementing interface is subtype of interface', () => { + const iface = new GraphQLInterfaceType({ + name: 'Interface', + fields: { + field: { type: GraphQLString }, + }, + }); + const iface2 = new GraphQLInterfaceType({ + name: 'Interface2', + interfaces: [iface], + fields: { + field: { type: GraphQLString }, + }, + }); + const impl = new GraphQLObjectType({ + name: 'Object', + interfaces: [iface2, iface], + fields: { + field: { type: GraphQLString }, + }, + }); + const schema = testSchema({ field: { type: impl } }); + expect(isTypeSubTypeOf(schema, iface2, iface)).toEqual(true); + }); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/valueFromAST-test.ts b/packages/graphql/src/utilities/__tests__/valueFromAST-test.ts new file mode 100644 index 00000000000..50a4bfbd3d2 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/valueFromAST-test.ts @@ -0,0 +1,223 @@ +import { identityFunc } from '../../jsutils/identityFunc.js'; +import type { ObjMap } from '../../jsutils/ObjMap.js'; + +import { parseValue } from '../../language/parser.js'; + +import type { GraphQLInputType } from '../../type/definition.js'; +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, + GraphQLScalarType, +} from '../../type/definition.js'; +import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, GraphQLString } from '../../type/scalars.js'; + +import { valueFromAST } from '../valueFromAST.js'; + +describe('valueFromAST', () => { + function expectValueFrom(valueText: string, type: GraphQLInputType, variables?: ObjMap) { + const ast = parseValue(valueText); + const value = valueFromAST(ast, type, variables); + return expect(value); + } + + it('rejects empty input', () => { + expect(valueFromAST(null, GraphQLBoolean)).toEqual(undefined); + }); + + it('converts according to input coercion rules', () => { + expectValueFrom('true', GraphQLBoolean).toEqual(true); + expectValueFrom('false', GraphQLBoolean).toEqual(false); + expectValueFrom('123', GraphQLInt).toEqual(123); + expectValueFrom('123', GraphQLFloat).toEqual(123); + expectValueFrom('123.456', GraphQLFloat).toEqual(123.456); + expectValueFrom('"abc123"', GraphQLString).toEqual('abc123'); + expectValueFrom('123456', GraphQLID).toEqual('123456'); + expectValueFrom('"123456"', GraphQLID).toEqual('123456'); + }); + + it('does not convert when input coercion rules reject a value', () => { + expectValueFrom('123', GraphQLBoolean).toEqual(undefined); + expectValueFrom('123.456', GraphQLInt).toEqual(undefined); + expectValueFrom('true', GraphQLInt).toEqual(undefined); + expectValueFrom('"123"', GraphQLInt).toEqual(undefined); + expectValueFrom('"123"', GraphQLFloat).toEqual(undefined); + expectValueFrom('123', GraphQLString).toEqual(undefined); + expectValueFrom('true', GraphQLString).toEqual(undefined); + expectValueFrom('123.456', GraphQLString).toEqual(undefined); + }); + + it('convert using parseLiteral from a custom scalar type', () => { + const passthroughScalar = new GraphQLScalarType({ + name: 'PassthroughScalar', + parseLiteral(node) { + expect(node.kind === 'StringValue').toBeTruthy(); + // @ts-expect-error + return node.value; + }, + parseValue: identityFunc, + }); + + expectValueFrom('"value"', passthroughScalar).toEqual('value'); + + const throwScalar = new GraphQLScalarType({ + name: 'ThrowScalar', + parseLiteral() { + throw new Error('Test'); + }, + parseValue: identityFunc, + }); + + expectValueFrom('value', throwScalar).toEqual(undefined); + + const returnUndefinedScalar = new GraphQLScalarType({ + name: 'ReturnUndefinedScalar', + parseLiteral() { + return undefined; + }, + parseValue: identityFunc, + }); + + expectValueFrom('value', returnUndefinedScalar).toEqual(undefined); + }); + + it('converts enum values according to input coercion rules', () => { + const testEnum = new GraphQLEnumType({ + name: 'TestColor', + values: { + RED: { value: 1 }, + GREEN: { value: 2 }, + BLUE: { value: 3 }, + NULL: { value: null }, + NAN: { value: NaN }, + NO_CUSTOM_VALUE: { value: undefined }, + }, + }); + + expectValueFrom('RED', testEnum).toEqual(1); + expectValueFrom('BLUE', testEnum).toEqual(3); + expectValueFrom('3', testEnum).toEqual(undefined); + expectValueFrom('"BLUE"', testEnum).toEqual(undefined); + expectValueFrom('null', testEnum).toEqual(null); + expectValueFrom('NULL', testEnum).toEqual(null); + expectValueFrom('NULL', new GraphQLNonNull(testEnum)).toEqual(null); + expectValueFrom('NAN', testEnum).toEqual(NaN); + expectValueFrom('NO_CUSTOM_VALUE', testEnum).toEqual('NO_CUSTOM_VALUE'); + }); + + // Boolean! + const nonNullBool = new GraphQLNonNull(GraphQLBoolean); + // [Boolean] + const listOfBool = new GraphQLList(GraphQLBoolean); + // [Boolean!] + const listOfNonNullBool = new GraphQLList(nonNullBool); + // [Boolean]! + const nonNullListOfBool = new GraphQLNonNull(listOfBool); + // [Boolean!]! + const nonNullListOfNonNullBool = new GraphQLNonNull(listOfNonNullBool); + + it('coerces to null unless non-null', () => { + expectValueFrom('null', GraphQLBoolean).toEqual(null); + expectValueFrom('null', nonNullBool).toEqual(undefined); + }); + + it('coerces lists of values', () => { + expectValueFrom('true', listOfBool).toEqual([true]); + expectValueFrom('123', listOfBool).toEqual(undefined); + expectValueFrom('null', listOfBool).toEqual(null); + expectValueFrom('[true, false]', listOfBool).toEqual([true, false]); + expectValueFrom('[true, 123]', listOfBool).toEqual(undefined); + expectValueFrom('[true, null]', listOfBool).toEqual([true, null]); + expectValueFrom('{ true: true }', listOfBool).toEqual(undefined); + }); + + it('coerces non-null lists of values', () => { + expectValueFrom('true', nonNullListOfBool).toEqual([true]); + expectValueFrom('123', nonNullListOfBool).toEqual(undefined); + expectValueFrom('null', nonNullListOfBool).toEqual(undefined); + expectValueFrom('[true, false]', nonNullListOfBool).toEqual([true, false]); + expectValueFrom('[true, 123]', nonNullListOfBool).toEqual(undefined); + expectValueFrom('[true, null]', nonNullListOfBool).toEqual([true, null]); + }); + + it('coerces lists of non-null values', () => { + expectValueFrom('true', listOfNonNullBool).toEqual([true]); + expectValueFrom('123', listOfNonNullBool).toEqual(undefined); + expectValueFrom('null', listOfNonNullBool).toEqual(null); + expectValueFrom('[true, false]', listOfNonNullBool).toEqual([true, false]); + expectValueFrom('[true, 123]', listOfNonNullBool).toEqual(undefined); + expectValueFrom('[true, null]', listOfNonNullBool).toEqual(undefined); + }); + + it('coerces non-null lists of non-null values', () => { + expectValueFrom('true', nonNullListOfNonNullBool).toEqual([true]); + expectValueFrom('123', nonNullListOfNonNullBool).toEqual(undefined); + expectValueFrom('null', nonNullListOfNonNullBool).toEqual(undefined); + expectValueFrom('[true, false]', nonNullListOfNonNullBool).toEqual([true, false]); + expectValueFrom('[true, 123]', nonNullListOfNonNullBool).toEqual(undefined); + expectValueFrom('[true, null]', nonNullListOfNonNullBool).toEqual(undefined); + }); + + const testInputObj = new GraphQLInputObjectType({ + name: 'TestInput', + fields: { + int: { type: GraphQLInt, defaultValue: 42 }, + bool: { type: GraphQLBoolean }, + requiredBool: { type: nonNullBool }, + }, + }); + + it('coerces input objects according to input coercion rules', () => { + expectValueFrom('null', testInputObj).toEqual(null); + expectValueFrom('123', testInputObj).toEqual(undefined); + expectValueFrom('[]', testInputObj).toEqual(undefined); + expectValueFrom('{ int: 123, requiredBool: false }', testInputObj).toEqual({ + int: 123, + requiredBool: false, + }); + expectValueFrom('{ bool: true, requiredBool: false }', testInputObj).toEqual({ + int: 42, + bool: true, + requiredBool: false, + }); + expectValueFrom('{ int: true, requiredBool: true }', testInputObj).toEqual(undefined); + expectValueFrom('{ requiredBool: null }', testInputObj).toEqual(undefined); + expectValueFrom('{ bool: true }', testInputObj).toEqual(undefined); + }); + + it('accepts variable values assuming already coerced', () => { + expectValueFrom('$var', GraphQLBoolean, {}).toEqual(undefined); + expectValueFrom('$var', GraphQLBoolean, { var: true }).toEqual(true); + expectValueFrom('$var', GraphQLBoolean, { var: null }).toEqual(null); + expectValueFrom('$var', nonNullBool, { var: null }).toEqual(undefined); + }); + + it('asserts variables are provided as items in lists', () => { + expectValueFrom('[ $foo ]', listOfBool, {}).toEqual([null]); + expectValueFrom('[ $foo ]', listOfNonNullBool, {}).toEqual(undefined); + expectValueFrom('[ $foo ]', listOfNonNullBool, { + foo: true, + }).toEqual([true]); + // Note: variables are expected to have already been coerced, so we + // do not expect the singleton wrapping behavior for variables. + expectValueFrom('$foo', listOfNonNullBool, { foo: true }).toEqual(true); + expectValueFrom('$foo', listOfNonNullBool, { foo: [true] }).toEqual([true]); + }); + + it('omits input object fields for unprovided variables', () => { + expectValueFrom('{ int: $foo, bool: $foo, requiredBool: true }', testInputObj, {}).toEqual({ + int: 42, + requiredBool: true, + }); + + expectValueFrom('{ requiredBool: $foo }', testInputObj, {}).toEqual(undefined); + + expectValueFrom('{ requiredBool: $foo }', testInputObj, { + foo: true, + }).toEqual({ + int: 42, + requiredBool: true, + }); + }); +}); diff --git a/packages/graphql/src/utilities/__tests__/valueFromASTUntyped-test.ts b/packages/graphql/src/utilities/__tests__/valueFromASTUntyped-test.ts new file mode 100644 index 00000000000..5409fea3db4 --- /dev/null +++ b/packages/graphql/src/utilities/__tests__/valueFromASTUntyped-test.ts @@ -0,0 +1,56 @@ +import type { Maybe } from '../../jsutils/Maybe.js'; +import type { ObjMap } from '../../jsutils/ObjMap.js'; + +import { parseValue } from '../../language/parser.js'; + +import { valueFromASTUntyped } from '../valueFromASTUntyped.js'; + +describe('valueFromASTUntyped', () => { + function expectValueFrom(valueText: string, variables?: Maybe>) { + const ast = parseValue(valueText); + const value = valueFromASTUntyped(ast, variables); + return expect(value); + } + + it('parses simple values', () => { + expectValueFrom('null').toEqual(null); + expectValueFrom('true').toEqual(true); + expectValueFrom('false').toEqual(false); + expectValueFrom('123').toEqual(123); + expectValueFrom('123.456').toEqual(123.456); + expectValueFrom('"abc123"').toEqual('abc123'); + }); + + it('parses lists of values', () => { + expectValueFrom('[true, false]').toEqual([true, false]); + expectValueFrom('[true, 123.45]').toEqual([true, 123.45]); + expectValueFrom('[true, null]').toEqual([true, null]); + expectValueFrom('[true, ["foo", 1.2]]').toEqual([true, ['foo', 1.2]]); + }); + + it('parses input objects', () => { + expectValueFrom('{ int: 123, bool: false }').toEqual({ + int: 123, + bool: false, + }); + expectValueFrom('{ foo: [ { bar: "baz"} ] }').toEqual({ + foo: [{ bar: 'baz' }], + }); + }); + + it('parses enum values as plain strings', () => { + expectValueFrom('TEST_ENUM_VALUE').toEqual('TEST_ENUM_VALUE'); + expectValueFrom('[TEST_ENUM_VALUE]').toEqual(['TEST_ENUM_VALUE']); + }); + + it('parses variables', () => { + expectValueFrom('$testVariable', { testVariable: 'foo' }).toEqual('foo'); + expectValueFrom('[$testVariable]', { testVariable: 'foo' }).toEqual(['foo']); + expectValueFrom('{a:[$testVariable]}', { + testVariable: 'foo', + }).toEqual({ a: ['foo'] }); + expectValueFrom('$testVariable', { testVariable: null }).toEqual(null); + expectValueFrom('$testVariable', {}).toEqual(undefined); + expectValueFrom('$testVariable', null).toEqual(undefined); + }); +}); diff --git a/packages/graphql/src/utilities/addResolversToSchema.ts b/packages/graphql/src/utilities/addResolversToSchema.ts new file mode 100644 index 00000000000..eb169a47a7d --- /dev/null +++ b/packages/graphql/src/utilities/addResolversToSchema.ts @@ -0,0 +1,125 @@ +// TODO: Add tests for me. + +import { + GraphQLEnumType, + GraphQLField, + GraphQLFieldConfig, + isEnumType, + isInterfaceType, + GraphQLScalarType, + GraphQLSchema, + isObjectType, + isScalarType, + isUnionType, +} from '../type/index.js'; + +function setFieldProperties( + field: GraphQLField | GraphQLFieldConfig, + propertiesObj: Record +) { + for (const propertyName in propertiesObj) { + field[propertyName] = propertiesObj[propertyName]; + } +} + +export function addResolversToExistingSchema(schema: GraphQLSchema, resolvers: any) { + const typeMap = schema.getTypeMap(); + for (const typeName in resolvers) { + const type = schema.getType(typeName); + const resolverValue = resolvers[typeName]; + + if (isScalarType(type)) { + for (const fieldName in resolverValue) { + if (fieldName.startsWith('__')) { + type[fieldName.substring(2)] = resolverValue[fieldName]; + } else if (fieldName === 'astNode' && type.astNode != null) { + type.astNode = { + ...type.astNode, + description: (resolverValue as GraphQLScalarType)?.astNode?.description ?? type.astNode.description, + directives: (type.astNode.directives ?? []).concat( + (resolverValue as GraphQLScalarType)?.astNode?.directives ?? [] + ), + }; + } else if (fieldName === 'extensionASTNodes' && type.extensionASTNodes != null) { + type.extensionASTNodes = type.extensionASTNodes.concat( + (resolverValue as GraphQLScalarType)?.extensionASTNodes ?? [] + ); + } else if ( + fieldName === 'extensions' && + type.extensions != null && + (resolverValue as GraphQLScalarType).extensions != null + ) { + type.extensions = Object.assign( + Object.create(null), + type.extensions, + (resolverValue as GraphQLScalarType).extensions + ); + } else { + type[fieldName] = resolverValue[fieldName]; + } + } + } else if (isEnumType(type)) { + const config = type.toConfig(); + const enumValueConfigMap = config.values; + + for (const fieldName in resolverValue) { + if (fieldName.startsWith('__')) { + config[fieldName.substring(2)] = resolverValue[fieldName]; + } else if (fieldName === 'astNode' && config.astNode != null) { + config.astNode = { + ...config.astNode, + description: (resolverValue as GraphQLScalarType)?.astNode?.description ?? config.astNode.description, + directives: (config.astNode.directives ?? []).concat( + (resolverValue as GraphQLEnumType)?.astNode?.directives ?? [] + ), + }; + } else if (fieldName === 'extensionASTNodes' && config.extensionASTNodes != null) { + config.extensionASTNodes = config.extensionASTNodes.concat( + (resolverValue as GraphQLEnumType)?.extensionASTNodes ?? [] + ); + } else if ( + fieldName === 'extensions' && + type.extensions != null && + (resolverValue as GraphQLEnumType).extensions != null + ) { + type.extensions = Object.assign( + Object.create(null), + type.extensions, + (resolverValue as GraphQLEnumType).extensions + ); + } else if (enumValueConfigMap[fieldName]) { + enumValueConfigMap[fieldName].value = resolverValue[fieldName]; + } + } + + Object.assign(typeMap[typeName], new GraphQLEnumType(config)); + } else if (isUnionType(type)) { + for (const fieldName in resolverValue) { + if (fieldName.startsWith('__')) { + type[fieldName.substring(2)] = resolverValue[fieldName]; + } + } + } else if (isObjectType(type) || isInterfaceType(type)) { + for (const fieldName in resolverValue) { + if (fieldName.startsWith('__')) { + // this is for isTypeOf and resolveType and all the other stuff. + type[fieldName.substring(2)] = resolverValue[fieldName]; + continue; + } + + const fields = type.getFields(); + const field = fields[fieldName]; + + if (field != null) { + const fieldResolve = resolverValue[fieldName]; + if (typeof fieldResolve === 'function') { + // for convenience. Allows shorter syntax in resolver definition file + field.resolve = fieldResolve.bind(resolverValue); + } else { + setFieldProperties(field, fieldResolve); + } + } + } + } + } +} diff --git a/packages/graphql/src/utilities/assertValidName.ts b/packages/graphql/src/utilities/assertValidName.ts new file mode 100644 index 00000000000..fd5fea97884 --- /dev/null +++ b/packages/graphql/src/utilities/assertValidName.ts @@ -0,0 +1,40 @@ +import { devAssert } from '../jsutils/devAssert.js'; + +import { GraphQLError } from '../error/GraphQLError.js'; + +import { assertName } from '../type/assertName.js'; + +/* c8 ignore start */ +/** + * Upholds the spec rules about naming. + * @deprecated Please use `assertName` instead. Will be removed in v17 + */ +export function assertValidName(name: string): string { + const error = isValidNameError(name); + if (error) { + throw error; + } + return name; +} + +/** + * Returns an Error if a name is invalid. + * @deprecated Please use `assertName` instead. Will be removed in v17 + */ +export function isValidNameError(name: string): GraphQLError | undefined { + devAssert(typeof name === 'string', 'Expected name to be a string.'); + + if (name.startsWith('__')) { + return new GraphQLError(`Name "${name}" must not begin with "__", which is reserved by GraphQL introspection.`); + } + + try { + assertName(name); + } catch (error) { + // @ts-expect-error We will fix this + return error; + } + + return undefined; +} +/* c8 ignore stop */ diff --git a/packages/graphql/src/utilities/astFromSchema.ts b/packages/graphql/src/utilities/astFromSchema.ts new file mode 100644 index 00000000000..3aa92f54163 --- /dev/null +++ b/packages/graphql/src/utilities/astFromSchema.ts @@ -0,0 +1,738 @@ +import { inspect } from '../jsutils/inspect.js'; +import { Maybe } from '../jsutils/Maybe.js'; +import { + OperationTypeNode, + SchemaDefinitionNode, + OperationTypeDefinitionNode, + SchemaExtensionNode, + Kind, + StringValueNode, + DirectiveDefinitionNode, + TypeNode, + DirectiveNode, + TypeDefinitionNode, + TypeExtensionNode, + EnumValueDefinitionNode, + InputValueDefinitionNode, + ObjectTypeDefinitionNode, + NamedTypeNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + InputObjectTypeDefinitionNode, + EnumTypeDefinitionNode, + ScalarTypeDefinitionNode, + FieldDefinitionNode, + ArgumentNode, + ValueNode, + ObjectFieldNode, +} from '../language/index.js'; +import { + GraphQLArgument, + GraphQLDeprecatedDirective, + GraphQLDirective, + GraphQLEnumType, + GraphQLEnumTypeConfig, + GraphQLEnumValue, + GraphQLEnumValueConfig, + GraphQLField, + GraphQLFieldConfig, + GraphQLInputField, + GraphQLInputFieldConfig, + GraphQLInputObjectType, + GraphQLInputObjectTypeConfig, + GraphQLInterfaceType, + GraphQLInterfaceTypeConfig, + GraphQLNamedType, + GraphQLObjectType, + GraphQLObjectTypeConfig, + GraphQLScalarType, + GraphQLScalarTypeConfig, + GraphQLSchema, + GraphQLSchemaConfig, + GraphQLType, + GraphQLUnionType, + GraphQLUnionTypeConfig, + isListType, + isNonNullType, +} from '../type/index.js'; +import { astFromValue } from './astFromValue.js'; +import { getRootTypeMap } from './getRootTypeMap.js'; + +function isSome(input: T): input is Exclude { + return input != null; +} + +export function astFromType(type: GraphQLType): TypeNode { + if (isNonNullType(type)) { + const innerType = astFromType(type.ofType); + if (innerType.kind === Kind.NON_NULL_TYPE) { + throw new Error(`Invalid type node ${inspect(type)}. Inner type of non-null type cannot be a non-null type.`); + } + return { + kind: Kind.NON_NULL_TYPE, + type: innerType, + }; + } else if (isListType(type)) { + return { + kind: Kind.LIST_TYPE, + type: astFromType(type.ofType), + }; + } + + return { + kind: Kind.NAMED_TYPE, + name: { + kind: Kind.NAME, + value: type.name, + }, + }; +} + +export function astFromSchema( + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): SchemaDefinitionNode | SchemaExtensionNode | null { + const operationTypeMap = new Map([ + ['query', undefined], + ['mutation', undefined], + ['subscription', undefined], + ] as [OperationTypeNode, OperationTypeDefinitionNode | undefined][]); + + const nodes: Array = []; + if (schema.astNode != null) { + nodes.push(schema.astNode); + } + if (schema.extensionASTNodes != null) { + for (const extensionASTNode of schema.extensionASTNodes) { + nodes.push(extensionASTNode); + } + } + + for (const node of nodes) { + if (node.operationTypes) { + for (const operationTypeDefinitionNode of node.operationTypes) { + operationTypeMap.set(operationTypeDefinitionNode.operation, operationTypeDefinitionNode); + } + } + } + + const rootTypeMap = getRootTypeMap(schema); + + for (const [operationTypeNode, operationTypeDefinitionNode] of operationTypeMap) { + const rootType = rootTypeMap.get(operationTypeNode as OperationTypeNode); + if (rootType != null) { + const rootTypeAST = astFromType(rootType); + if (operationTypeDefinitionNode != null) { + (operationTypeDefinitionNode as any).type = rootTypeAST; + } else { + operationTypeMap.set(operationTypeNode, { + kind: Kind.OPERATION_TYPE_DEFINITION, + operation: operationTypeNode, + type: rootTypeAST, + } as OperationTypeDefinitionNode); + } + } + } + + const operationTypes = [...operationTypeMap.values()].filter(isSome); + + const directives = getDirectiveNodes(schema, schema, pathToDirectivesInExtensions); + + if (!operationTypes.length && !directives.length) { + return null; + } + + const schemaNode: SchemaDefinitionNode | SchemaExtensionNode = { + kind: operationTypes != null ? Kind.SCHEMA_DEFINITION : Kind.SCHEMA_EXTENSION, + operationTypes, + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + directives: directives as any, + }; + + // This code is so weird because it needs to support GraphQL.js 14 + // In GraphQL.js 14 there is no `description` value on schemaNode + (schemaNode as unknown as { description?: StringValueNode }).description = + (schema.astNode as unknown as { description: string })?.description ?? + (schema as unknown as { description: string }).description != null + ? { + kind: Kind.STRING, + value: (schema as unknown as { description: string }).description, + block: true, + } + : undefined; + + return schemaNode; +} + +export function astFromDirective( + directive: GraphQLDirective, + schema?: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): DirectiveDefinitionNode { + return { + kind: Kind.DIRECTIVE_DEFINITION, + description: + directive.astNode?.description ?? + (directive.description + ? { + kind: Kind.STRING, + value: directive.description, + } + : undefined), + name: { + kind: Kind.NAME, + value: directive.name, + }, + arguments: directive.args?.map(arg => astFromArg(arg, schema, pathToDirectivesInExtensions)), + repeatable: directive.isRepeatable, + locations: + directive.locations?.map(location => ({ + kind: Kind.NAME, + value: location, + })) || [], + }; +} + +export function getDirectiveNodes( + entity: GraphQLSchema | GraphQLNamedType | GraphQLEnumValue, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): Array { + const directivesInExtensions = getDirectivesInExtensions(entity, pathToDirectivesInExtensions); + + let nodes: Array< + SchemaDefinitionNode | SchemaExtensionNode | TypeDefinitionNode | TypeExtensionNode | EnumValueDefinitionNode + > = []; + if (entity.astNode != null) { + nodes.push(entity.astNode); + } + if ('extensionASTNodes' in entity && entity.extensionASTNodes != null) { + nodes = nodes.concat(entity.extensionASTNodes); + } + + let directives: Array; + if (directivesInExtensions != null) { + directives = makeDirectiveNodes(schema, directivesInExtensions); + } else { + directives = []; + for (const node of nodes) { + if (node.directives) { + directives.push(...node.directives); + } + } + } + + return directives; +} + +export function getDeprecatableDirectiveNodes( + entity: GraphQLArgument | GraphQLField | GraphQLInputField | GraphQLEnumValue, + schema?: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): Array { + let directiveNodesBesidesDeprecated: Array = []; + let deprecatedDirectiveNode: Maybe = null; + + const directivesInExtensions = getDirectivesInExtensions(entity, pathToDirectivesInExtensions); + + let directives: Maybe>; + if (directivesInExtensions != null) { + directives = makeDirectiveNodes(schema, directivesInExtensions); + } else { + directives = entity.astNode?.directives; + } + + if (directives != null) { + directiveNodesBesidesDeprecated = directives.filter(directive => directive.name.value !== 'deprecated'); + if ((entity as unknown as { deprecationReason: string }).deprecationReason != null) { + deprecatedDirectiveNode = directives.filter(directive => directive.name.value === 'deprecated')?.[0]; + } + } + + if ( + (entity as unknown as { deprecationReason: string }).deprecationReason != null && + deprecatedDirectiveNode == null + ) { + deprecatedDirectiveNode = makeDeprecatedDirective( + (entity as unknown as { deprecationReason: string }).deprecationReason + ); + } + + return deprecatedDirectiveNode == null + ? directiveNodesBesidesDeprecated + : [deprecatedDirectiveNode].concat(directiveNodesBesidesDeprecated); +} + +export function astFromArg( + arg: GraphQLArgument, + schema?: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): InputValueDefinitionNode { + return { + kind: Kind.INPUT_VALUE_DEFINITION, + description: + arg.astNode?.description ?? + (arg.description + ? { + kind: Kind.STRING, + value: arg.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: arg.name, + }, + type: astFromType(arg.type), + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + defaultValue: + arg.defaultValue !== undefined ? astFromValue(arg.defaultValue, arg.type) ?? undefined : (undefined as any), + directives: getDeprecatableDirectiveNodes(arg, schema, pathToDirectivesInExtensions) as any, + }; +} + +export function astFromObjectType( + type: GraphQLObjectType, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): ObjectTypeDefinitionNode { + return { + kind: Kind.OBJECT_TYPE_DEFINITION, + description: + type.astNode?.description ?? + (type.description + ? { + kind: Kind.STRING, + value: type.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: type.name, + }, + fields: Object.values(type.getFields()).map(field => astFromField(field, schema, pathToDirectivesInExtensions)), + interfaces: Object.values(type.getInterfaces()).map(iFace => astFromType(iFace) as NamedTypeNode), + directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions) as any, + }; +} + +export function astFromInterfaceType( + type: GraphQLInterfaceType, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): InterfaceTypeDefinitionNode { + const node: InterfaceTypeDefinitionNode = { + kind: Kind.INTERFACE_TYPE_DEFINITION, + description: + type.astNode?.description ?? + (type.description + ? { + kind: Kind.STRING, + value: type.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: type.name, + }, + fields: Object.values(type.getFields()).map(field => astFromField(field, schema, pathToDirectivesInExtensions)), + directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions) as any, + }; + + if ('getInterfaces' in type) { + (node as unknown as { interfaces: Array }).interfaces = Object.values( + (type as unknown as GraphQLObjectType).getInterfaces() + ).map(iFace => astFromType(iFace) as NamedTypeNode); + } + + return node; +} + +export function astFromUnionType( + type: GraphQLUnionType, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): UnionTypeDefinitionNode { + return { + kind: Kind.UNION_TYPE_DEFINITION, + description: + type.astNode?.description ?? + (type.description + ? { + kind: Kind.STRING, + value: type.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: type.name, + }, + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions) as any, + types: type.getTypes().map(type => astFromType(type) as NamedTypeNode), + }; +} + +export function astFromInputObjectType( + type: GraphQLInputObjectType, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): InputObjectTypeDefinitionNode { + return { + kind: Kind.INPUT_OBJECT_TYPE_DEFINITION, + description: + type.astNode?.description ?? + (type.description + ? { + kind: Kind.STRING, + value: type.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: type.name, + }, + fields: Object.values(type.getFields()).map(field => + astFromInputField(field, schema, pathToDirectivesInExtensions) + ), + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions) as any, + }; +} + +export function astFromEnumType( + type: GraphQLEnumType, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): EnumTypeDefinitionNode { + return { + kind: Kind.ENUM_TYPE_DEFINITION, + description: + type.astNode?.description ?? + (type.description + ? { + kind: Kind.STRING, + value: type.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: type.name, + }, + values: Object.values(type.getValues()).map(value => astFromEnumValue(value, schema, pathToDirectivesInExtensions)), + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions) as any, + }; +} + +type DirectableGraphQLObject = + | GraphQLSchema + | GraphQLSchemaConfig + | GraphQLNamedType + | GraphQLObjectTypeConfig + | GraphQLInterfaceTypeConfig + | GraphQLUnionTypeConfig + | GraphQLScalarTypeConfig + | GraphQLEnumTypeConfig + | GraphQLEnumValue + | GraphQLEnumValueConfig + | GraphQLInputObjectTypeConfig + | GraphQLField + | GraphQLInputField + | GraphQLFieldConfig + | GraphQLInputFieldConfig; + +interface DirectiveAnnotation { + name: string; + args?: Record; +} + +function getDirectivesInExtensions( + node: DirectableGraphQLObject, + pathToDirectivesInExtensions = ['directives'] +): Array { + return pathToDirectivesInExtensions.reduce( + (acc, pathSegment) => (acc == null ? acc : acc[pathSegment]), + node?.extensions as unknown as Array + ); +} + +export function astFromScalarType( + type: GraphQLScalarType, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): ScalarTypeDefinitionNode { + const directivesInExtensions = getDirectivesInExtensions(type, pathToDirectivesInExtensions); + + const directives: DirectiveNode[] = directivesInExtensions + ? makeDirectiveNodes(schema, directivesInExtensions) + : (type.astNode?.directives as DirectiveNode[]) || []; + + const specifiedByValue = ((type as any).specifiedByUrl || (type as any).specifiedByURL) as string; + if (specifiedByValue && !directives.some(directiveNode => directiveNode.name.value === 'specifiedBy')) { + const specifiedByArgs = { + url: specifiedByValue, + }; + directives.push(makeDirectiveNode('specifiedBy', specifiedByArgs)); + } + + return { + kind: Kind.SCALAR_TYPE_DEFINITION, + description: + type.astNode?.description ?? + (type.description + ? { + kind: Kind.STRING, + value: type.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: type.name, + }, + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + directives: directives as any, + }; +} + +export function astFromField( + field: GraphQLField, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): FieldDefinitionNode { + return { + kind: Kind.FIELD_DEFINITION, + description: + field.astNode?.description ?? + (field.description + ? { + kind: Kind.STRING, + value: field.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: field.name, + }, + arguments: field.args.map(arg => astFromArg(arg, schema, pathToDirectivesInExtensions)), + type: astFromType(field.type), + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions) as any, + }; +} + +export function astFromInputField( + field: GraphQLInputField, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): InputValueDefinitionNode { + return { + kind: Kind.INPUT_VALUE_DEFINITION, + description: + field.astNode?.description ?? + (field.description + ? { + kind: Kind.STRING, + value: field.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: field.name, + }, + type: astFromType(field.type), + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions) as any, + defaultValue: astFromValue(field.defaultValue, field.type) ?? (undefined as any), + }; +} + +export function astFromEnumValue( + value: GraphQLEnumValue, + schema: GraphQLSchema, + pathToDirectivesInExtensions?: Array +): EnumValueDefinitionNode { + return { + kind: Kind.ENUM_VALUE_DEFINITION, + description: + value.astNode?.description ?? + (value.description + ? { + kind: Kind.STRING, + value: value.description, + block: true, + } + : undefined), + name: { + kind: Kind.NAME, + value: value.name, + }, + // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility + directives: getDeprecatableDirectiveNodes(value, schema, pathToDirectivesInExtensions) as any, + }; +} + +/** + * Produces a GraphQL Value AST given a JavaScript object. + * Function will match JavaScript/JSON values to GraphQL AST schema format + * by using the following mapping. + * + * | JSON Value | GraphQL Value | + * | ------------- | -------------------- | + * | Object | Input Object | + * | Array | List | + * | Boolean | Boolean | + * | String | String | + * | Number | Int / Float | + * | null | NullValue | + * + */ +export function astFromValueUntyped(value: any): ValueNode | null { + // only explicit null, not undefined, NaN + if (value === null) { + return { kind: Kind.NULL }; + } + + // undefined + if (value === undefined) { + return null; + } + + // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but + // the value is not an array, convert the value using the list's item type. + if (Array.isArray(value)) { + const valuesNodes: Array = []; + for (const item of value) { + const itemNode = astFromValueUntyped(item); + if (itemNode != null) { + valuesNodes.push(itemNode); + } + } + return { kind: Kind.LIST, values: valuesNodes }; + } + + if (typeof value === 'object') { + const fieldNodes: Array = []; + for (const fieldName in value) { + const fieldValue = value[fieldName]; + const ast = astFromValueUntyped(fieldValue); + if (ast) { + fieldNodes.push({ + kind: Kind.OBJECT_FIELD, + name: { kind: Kind.NAME, value: fieldName }, + value: ast, + }); + } + } + return { kind: Kind.OBJECT, fields: fieldNodes }; + } + + // Others serialize based on their corresponding JavaScript scalar types. + if (typeof value === 'boolean') { + return { kind: Kind.BOOLEAN, value }; + } + + // JavaScript numbers can be Int or Float values. + if (typeof value === 'number' && isFinite(value)) { + const stringNum = String(value); + return integerStringRegExp.test(stringNum) + ? { kind: Kind.INT, value: stringNum } + : { kind: Kind.FLOAT, value: stringNum }; + } + + if (typeof value === 'string') { + return { kind: Kind.STRING, value }; + } + + throw new TypeError(`Cannot convert value to AST: ${value}.`); +} + +/** + * IntValue: + * - NegativeSign? 0 + * - NegativeSign? NonZeroDigit ( Digit+ )? + */ +const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/; + +function makeDeprecatedDirective(deprecationReason: string): DirectiveNode { + return makeDirectiveNode('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective); +} + +function makeDirectiveNode( + name: string, + args: Record, + directive?: Maybe +): DirectiveNode { + const directiveArguments: Array = []; + + if (directive != null) { + for (const arg of directive.args) { + const argName = arg.name; + const argValue = args[argName]; + if (argValue !== undefined) { + const value = astFromValue(argValue, arg.type); + if (value) { + directiveArguments.push({ + kind: Kind.ARGUMENT, + name: { + kind: Kind.NAME, + value: argName, + }, + value, + }); + } + } + } + } else { + for (const argName in args) { + const argValue = args[argName]; + const value = astFromValueUntyped(argValue); + if (value) { + directiveArguments.push({ + kind: Kind.ARGUMENT, + name: { + kind: Kind.NAME, + value: argName, + }, + value, + }); + } + } + } + + return { + kind: Kind.DIRECTIVE, + name: { + kind: Kind.NAME, + value: name, + }, + arguments: directiveArguments, + }; +} + +export function makeDirectiveNodes( + schema: Maybe, + directiveValues: Record +): Array { + const directiveNodes: Array = []; + for (const directiveName in directiveValues) { + const arrayOrSingleValue = directiveValues[directiveName]; + const directive = schema?.getDirective(directiveName); + if (Array.isArray(arrayOrSingleValue)) { + for (const value of arrayOrSingleValue) { + directiveNodes.push(makeDirectiveNode(directiveName, value, directive)); + } + } else { + directiveNodes.push(makeDirectiveNode(directiveName, arrayOrSingleValue, directive)); + } + } + return directiveNodes; +} diff --git a/packages/graphql/src/utilities/astFromValue.ts b/packages/graphql/src/utilities/astFromValue.ts new file mode 100644 index 00000000000..708b9dd4f5c --- /dev/null +++ b/packages/graphql/src/utilities/astFromValue.ts @@ -0,0 +1,141 @@ +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; +import { isIterableObject } from '../jsutils/isIterableObject.js'; +import { isObjectLike } from '../jsutils/isObjectLike.js'; +import type { Maybe } from '../jsutils/Maybe.js'; + +import type { ObjectFieldNode, ValueNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +import type { GraphQLInputType } from '../type/definition.js'; +import { isEnumType, isInputObjectType, isLeafType, isListType, isNonNullType } from '../type/definition.js'; +import { GraphQLID } from '../type/scalars.js'; + +/** + * Produces a GraphQL Value AST given a JavaScript object. + * Function will match JavaScript/JSON values to GraphQL AST schema format + * by using suggested GraphQLInputType. For example: + * + * astFromValue("value", GraphQLString) + * + * A GraphQL type must be provided, which will be used to interpret different + * JavaScript values. + * + * | JSON Value | GraphQL Value | + * | ------------- | -------------------- | + * | Object | Input Object | + * | Array | List | + * | Boolean | Boolean | + * | String | String / Enum Value | + * | Number | Int / Float | + * | Unknown | Enum Value | + * | null | NullValue | + * + */ +export function astFromValue(value: unknown, type: GraphQLInputType): Maybe { + if (isNonNullType(type)) { + const astValue = astFromValue(value, type.ofType); + if (astValue?.kind === Kind.NULL) { + return null; + } + return astValue; + } + + // only explicit null, not undefined, NaN + if (value === null) { + return { kind: Kind.NULL }; + } + + // undefined + if (value === undefined) { + return null; + } + + // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but + // the value is not an array, convert the value using the list's item type. + if (isListType(type)) { + const itemType = type.ofType; + if (isIterableObject(value)) { + const valuesNodes = []; + for (const item of value) { + const itemNode = astFromValue(item, itemType); + if (itemNode != null) { + valuesNodes.push(itemNode); + } + } + return { kind: Kind.LIST, values: valuesNodes }; + } + return astFromValue(value, itemType); + } + + // Populate the fields of the input object by creating ASTs from each value + // in the JavaScript object according to the fields in the input type. + if (isInputObjectType(type)) { + if (!isObjectLike(value)) { + return null; + } + const fieldNodes: Array = []; + for (const field of Object.values(type.getFields())) { + const fieldValue = astFromValue(value[field.name], field.type); + if (fieldValue) { + fieldNodes.push({ + kind: Kind.OBJECT_FIELD, + name: { kind: Kind.NAME, value: field.name }, + value: fieldValue, + }); + } + } + return { kind: Kind.OBJECT, fields: fieldNodes }; + } + + if (isLeafType(type)) { + // Since value is an internally represented value, it must be serialized + // to an externally represented value before converting into an AST. + const serialized = type.serialize(value); + if (serialized == null) { + return null; + } + + // Others serialize based on their corresponding JavaScript scalar types. + if (typeof serialized === 'boolean') { + return { kind: Kind.BOOLEAN, value: serialized }; + } + + // JavaScript numbers can be Int or Float values. + if (typeof serialized === 'number' && Number.isFinite(serialized)) { + const stringNum = String(serialized); + return integerStringRegExp.test(stringNum) + ? { kind: Kind.INT, value: stringNum } + : { kind: Kind.FLOAT, value: stringNum }; + } + + if (typeof serialized === 'string') { + // Enum types use Enum literals. + if (isEnumType(type)) { + return { kind: Kind.ENUM, value: serialized }; + } + + // ID types can use Int literals. + if (type === GraphQLID && integerStringRegExp.test(serialized)) { + return { kind: Kind.INT, value: serialized }; + } + + return { + kind: Kind.STRING, + value: serialized, + }; + } + + throw new TypeError(`Cannot convert value to AST: ${inspect(serialized)}.`); + } + /* c8 ignore next 3 */ + // Not reachable, all possible types have been considered. + invariant(false, 'Unexpected input type: ' + inspect(type)); +} + +/** + * IntValue: + * - NegativeSign? 0 + * - NegativeSign? NonZeroDigit ( Digit+ )? + */ +const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/; diff --git a/packages/graphql/src/utilities/buildASTSchema.ts b/packages/graphql/src/utilities/buildASTSchema.ts new file mode 100644 index 00000000000..9b78f96aa3c --- /dev/null +++ b/packages/graphql/src/utilities/buildASTSchema.ts @@ -0,0 +1,95 @@ +import type { DocumentNode } from '../language/ast.js'; +import type { ParseOptions } from '../language/parser.js'; +import { parse } from '../language/parser.js'; +import type { Source } from '../language/source.js'; + +import { specifiedDirectives } from '../type/directives.js'; +import type { GraphQLSchemaValidationOptions } from '../type/schema.js'; +import { GraphQLSchema } from '../type/schema.js'; + +import { assertValidSDL } from '../validation/validate.js'; + +import { extendSchemaImpl } from './extendSchema.js'; + +export interface BuildSchemaOptions extends GraphQLSchemaValidationOptions { + /** + * Set to true to assume the SDL is valid. + * + * Default: false + */ + assumeValidSDL?: boolean; +} + +/** + * This takes the ast of a schema document produced by the parse function in + * src/language/parser.js. + * + * If no schema definition is provided, then it will look for types named Query, + * Mutation and Subscription. + * + * Given that AST it constructs a GraphQLSchema. The resulting schema + * has no resolve methods, so execution will use default resolvers. + */ +export function buildASTSchema(documentAST: DocumentNode, options?: BuildSchemaOptions): GraphQLSchema { + if (options?.assumeValid !== true && options?.assumeValidSDL !== true) { + assertValidSDL(documentAST); + } + + const emptySchemaConfig = { + description: undefined, + types: [], + directives: [], + extensions: Object.create(null), + extensionASTNodes: [], + assumeValid: false, + }; + const config = extendSchemaImpl(emptySchemaConfig, documentAST, options); + + if (config.astNode == null) { + for (const type of config.types) { + switch (type.name) { + // Note: While this could make early assertions to get the correctly + // typed values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + case 'Query': + // @ts-expect-error validated in `validateSchema` + config.query = type; + break; + case 'Mutation': + // @ts-expect-error validated in `validateSchema` + config.mutation = type; + break; + case 'Subscription': + // @ts-expect-error validated in `validateSchema` + config.subscription = type; + break; + } + } + } + + const directives = [ + ...config.directives, + // If specified directives were not explicitly declared, add them. + ...specifiedDirectives.filter(stdDirective => + config.directives.every(directive => directive.name !== stdDirective.name) + ), + ]; + + return new GraphQLSchema({ ...config, directives }); +} + +/** + * A helper function to build a GraphQLSchema directly from a source + * document. + */ +export function buildSchema(source: string | Source, options?: BuildSchemaOptions & ParseOptions): GraphQLSchema { + const document = parse(source, { + noLocation: options?.noLocation, + allowLegacyFragmentVariables: options?.allowLegacyFragmentVariables, + }); + + return buildASTSchema(document, { + assumeValidSDL: options?.assumeValidSDL, + assumeValid: options?.assumeValid, + }); +} diff --git a/packages/graphql/src/utilities/buildClientSchema.ts b/packages/graphql/src/utilities/buildClientSchema.ts new file mode 100644 index 00000000000..77092dc8361 --- /dev/null +++ b/packages/graphql/src/utilities/buildClientSchema.ts @@ -0,0 +1,341 @@ +import { devAssert } from '../jsutils/devAssert.js'; +import { inspect } from '../jsutils/inspect.js'; +import { isObjectLike } from '../jsutils/isObjectLike.js'; +import { keyValMap } from '../jsutils/keyValMap.js'; + +import { parseValue } from '../language/parser.js'; + +import type { GraphQLFieldConfig, GraphQLFieldConfigMap, GraphQLNamedType, GraphQLType } from '../type/definition.js'; +import { + assertInterfaceType, + assertNullableType, + assertObjectType, + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, + isInputType, + isOutputType, +} from '../type/definition.js'; +import { GraphQLDirective } from '../type/directives.js'; +import { introspectionTypes, TypeKind } from '../type/introspection.js'; +import { specifiedScalarTypes } from '../type/scalars.js'; +import type { GraphQLSchemaValidationOptions } from '../type/schema.js'; +import { GraphQLSchema } from '../type/schema.js'; + +import type { + IntrospectionDirective, + IntrospectionEnumType, + IntrospectionField, + IntrospectionInputObjectType, + IntrospectionInputValue, + IntrospectionInterfaceType, + IntrospectionNamedTypeRef, + IntrospectionObjectType, + IntrospectionQuery, + IntrospectionScalarType, + IntrospectionType, + IntrospectionTypeRef, + IntrospectionUnionType, +} from './getIntrospectionQuery.js'; +import { valueFromAST } from './valueFromAST.js'; + +/** + * Build a GraphQLSchema for use by client tools. + * + * Given the result of a client running the introspection query, creates and + * returns a GraphQLSchema instance which can be then used with all graphql-js + * tools, but cannot be used to execute a query, as introspection does not + * represent the "resolver", "parse" or "serialize" functions or any other + * server-internal mechanisms. + * + * This function expects a complete introspection result. Don't forget to check + * the "errors" field of a server response before calling this function. + */ +export function buildClientSchema( + introspection: IntrospectionQuery, + options?: GraphQLSchemaValidationOptions +): GraphQLSchema { + // Even even though `introspection` argument is typed in most cases it's received + // as untyped value from server, so we will do an additional check here. + devAssert( + isObjectLike(introspection) && isObjectLike(introspection.__schema), + `Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ${inspect( + introspection + )}.` + ); + + // Get the schema from the introspection result. + const schemaIntrospection = introspection.__schema; + + // Iterate through all types, getting the type definition for each. + const typeMap = keyValMap( + schemaIntrospection.types, + typeIntrospection => typeIntrospection.name, + typeIntrospection => buildType(typeIntrospection) + ); + + // Include standard types only if they are used. + for (const stdType of [...specifiedScalarTypes, ...introspectionTypes]) { + if (typeMap[stdType.name]) { + typeMap[stdType.name] = stdType; + } + } + + // Get the root Query, Mutation, and Subscription types. + const queryType = schemaIntrospection.queryType ? getObjectType(schemaIntrospection.queryType) : null; + + const mutationType = schemaIntrospection.mutationType ? getObjectType(schemaIntrospection.mutationType) : null; + + const subscriptionType = schemaIntrospection.subscriptionType + ? getObjectType(schemaIntrospection.subscriptionType) + : null; + + // Get the directives supported by Introspection, assuming empty-set if + // directives were not queried for. + const directives = schemaIntrospection.directives ? schemaIntrospection.directives.map(buildDirective) : []; + + // Then produce and return a Schema with these types. + return new GraphQLSchema({ + description: schemaIntrospection.description, + query: queryType, + mutation: mutationType, + subscription: subscriptionType, + types: Object.values(typeMap), + directives, + assumeValid: options?.assumeValid, + }); + + // Given a type reference in introspection, return the GraphQLType instance. + // preferring cached instances before building new instances. + function getType(typeRef: IntrospectionTypeRef): GraphQLType { + if (typeRef.kind === TypeKind.LIST) { + const itemRef = typeRef.ofType; + if (!itemRef) { + throw new Error('Decorated type deeper than introspection query.'); + } + return new GraphQLList(getType(itemRef)); + } + if (typeRef.kind === TypeKind.NON_NULL) { + const nullableRef = typeRef.ofType; + if (!nullableRef) { + throw new Error('Decorated type deeper than introspection query.'); + } + const nullableType = getType(nullableRef); + return new GraphQLNonNull(assertNullableType(nullableType)); + } + return getNamedType(typeRef); + } + + function getNamedType(typeRef: IntrospectionNamedTypeRef): GraphQLNamedType { + const typeName = typeRef.name; + if (!typeName) { + throw new Error(`Unknown type reference: ${inspect(typeRef)}.`); + } + + const type = typeMap[typeName]; + if (!type) { + throw new Error( + `Invalid or incomplete schema, unknown type: ${typeName}. Ensure that a full introspection query is used in order to build a client schema.` + ); + } + + return type; + } + + function getObjectType(typeRef: IntrospectionNamedTypeRef): GraphQLObjectType { + return assertObjectType(getNamedType(typeRef)); + } + + function getInterfaceType(typeRef: IntrospectionNamedTypeRef): GraphQLInterfaceType { + return assertInterfaceType(getNamedType(typeRef)); + } + + // Given a type's introspection result, construct the correct + // GraphQLType instance. + function buildType(type: IntrospectionType): GraphQLNamedType { + if (type != null && type.name != null && type.kind != null) { + // FIXME: Properly type IntrospectionType, it's a breaking change so fix in v17 + + switch (type.kind) { + case TypeKind.SCALAR: + return buildScalarDef(type); + case TypeKind.OBJECT: + return buildObjectDef(type); + case TypeKind.INTERFACE: + return buildInterfaceDef(type); + case TypeKind.UNION: + return buildUnionDef(type); + case TypeKind.ENUM: + return buildEnumDef(type); + case TypeKind.INPUT_OBJECT: + return buildInputObjectDef(type); + } + } + const typeStr = inspect(type); + throw new Error( + `Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${typeStr}.` + ); + } + + function buildScalarDef(scalarIntrospection: IntrospectionScalarType): GraphQLScalarType { + return new GraphQLScalarType({ + name: scalarIntrospection.name, + description: scalarIntrospection.description, + specifiedByURL: scalarIntrospection.specifiedByURL, + }); + } + + function buildImplementationsList( + implementingIntrospection: IntrospectionObjectType | IntrospectionInterfaceType + ): Array { + // TODO: Temporary workaround until GraphQL ecosystem will fully support + // 'interfaces' on interface types. + if (implementingIntrospection.interfaces === null && implementingIntrospection.kind === TypeKind.INTERFACE) { + return []; + } + + if (!implementingIntrospection.interfaces) { + const implementingIntrospectionStr = inspect(implementingIntrospection); + throw new Error(`Introspection result missing interfaces: ${implementingIntrospectionStr}.`); + } + + return implementingIntrospection.interfaces.map(getInterfaceType); + } + + function buildObjectDef(objectIntrospection: IntrospectionObjectType): GraphQLObjectType { + return new GraphQLObjectType({ + name: objectIntrospection.name, + description: objectIntrospection.description, + interfaces: () => buildImplementationsList(objectIntrospection), + fields: () => buildFieldDefMap(objectIntrospection), + }); + } + + function buildInterfaceDef(interfaceIntrospection: IntrospectionInterfaceType): GraphQLInterfaceType { + return new GraphQLInterfaceType({ + name: interfaceIntrospection.name, + description: interfaceIntrospection.description, + interfaces: () => buildImplementationsList(interfaceIntrospection), + fields: () => buildFieldDefMap(interfaceIntrospection), + }); + } + + function buildUnionDef(unionIntrospection: IntrospectionUnionType): GraphQLUnionType { + if (!unionIntrospection.possibleTypes) { + const unionIntrospectionStr = inspect(unionIntrospection); + throw new Error(`Introspection result missing possibleTypes: ${unionIntrospectionStr}.`); + } + return new GraphQLUnionType({ + name: unionIntrospection.name, + description: unionIntrospection.description, + types: () => unionIntrospection.possibleTypes.map(getObjectType), + }); + } + + function buildEnumDef(enumIntrospection: IntrospectionEnumType): GraphQLEnumType { + if (!enumIntrospection.enumValues) { + const enumIntrospectionStr = inspect(enumIntrospection); + throw new Error(`Introspection result missing enumValues: ${enumIntrospectionStr}.`); + } + return new GraphQLEnumType({ + name: enumIntrospection.name, + description: enumIntrospection.description, + values: keyValMap( + enumIntrospection.enumValues, + valueIntrospection => valueIntrospection.name, + valueIntrospection => ({ + description: valueIntrospection.description, + deprecationReason: valueIntrospection.deprecationReason, + }) + ), + }); + } + + function buildInputObjectDef(inputObjectIntrospection: IntrospectionInputObjectType): GraphQLInputObjectType { + if (!inputObjectIntrospection.inputFields) { + const inputObjectIntrospectionStr = inspect(inputObjectIntrospection); + throw new Error(`Introspection result missing inputFields: ${inputObjectIntrospectionStr}.`); + } + return new GraphQLInputObjectType({ + name: inputObjectIntrospection.name, + description: inputObjectIntrospection.description, + fields: () => buildInputValueDefMap(inputObjectIntrospection.inputFields), + }); + } + + function buildFieldDefMap( + typeIntrospection: IntrospectionObjectType | IntrospectionInterfaceType + ): GraphQLFieldConfigMap { + if (!typeIntrospection.fields) { + throw new Error(`Introspection result missing fields: ${inspect(typeIntrospection)}.`); + } + + return keyValMap(typeIntrospection.fields, fieldIntrospection => fieldIntrospection.name, buildField); + } + + function buildField(fieldIntrospection: IntrospectionField): GraphQLFieldConfig { + const type = getType(fieldIntrospection.type); + if (!isOutputType(type)) { + const typeStr = inspect(type); + throw new Error(`Introspection must provide output type for fields, but received: ${typeStr}.`); + } + + if (!fieldIntrospection.args) { + const fieldIntrospectionStr = inspect(fieldIntrospection); + throw new Error(`Introspection result missing field args: ${fieldIntrospectionStr}.`); + } + + return { + description: fieldIntrospection.description, + deprecationReason: fieldIntrospection.deprecationReason, + type, + args: buildInputValueDefMap(fieldIntrospection.args), + }; + } + + function buildInputValueDefMap(inputValueIntrospections: ReadonlyArray) { + return keyValMap(inputValueIntrospections, inputValue => inputValue.name, buildInputValue); + } + + function buildInputValue(inputValueIntrospection: IntrospectionInputValue) { + const type = getType(inputValueIntrospection.type); + if (!isInputType(type)) { + const typeStr = inspect(type); + throw new Error(`Introspection must provide input type for arguments, but received: ${typeStr}.`); + } + + const defaultValue = + inputValueIntrospection.defaultValue != null + ? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type) + : undefined; + return { + description: inputValueIntrospection.description, + type, + defaultValue, + deprecationReason: inputValueIntrospection.deprecationReason, + }; + } + + function buildDirective(directiveIntrospection: IntrospectionDirective): GraphQLDirective { + if (!directiveIntrospection.args) { + const directiveIntrospectionStr = inspect(directiveIntrospection); + throw new Error(`Introspection result missing directive args: ${directiveIntrospectionStr}.`); + } + if (!directiveIntrospection.locations) { + const directiveIntrospectionStr = inspect(directiveIntrospection); + throw new Error(`Introspection result missing directive locations: ${directiveIntrospectionStr}.`); + } + return new GraphQLDirective({ + name: directiveIntrospection.name, + description: directiveIntrospection.description, + isRepeatable: directiveIntrospection.isRepeatable, + locations: directiveIntrospection.locations.slice(), + args: buildInputValueDefMap(directiveIntrospection.args), + }); + } +} diff --git a/packages/graphql/src/utilities/coerceInputValue.ts b/packages/graphql/src/utilities/coerceInputValue.ts new file mode 100644 index 00000000000..da84133919d --- /dev/null +++ b/packages/graphql/src/utilities/coerceInputValue.ts @@ -0,0 +1,151 @@ +import { didYouMean } from '../jsutils/didYouMean.js'; +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; +import { isIterableObject } from '../jsutils/isIterableObject.js'; +import { isObjectLike } from '../jsutils/isObjectLike.js'; +import type { Path } from '../jsutils/Path.js'; +import { addPath, pathToArray } from '../jsutils/Path.js'; +import { printPathArray } from '../jsutils/printPathArray.js'; +import { suggestionList } from '../jsutils/suggestionList.js'; + +import { GraphQLError, isGraphQLError } from '../error/GraphQLError.js'; + +import type { GraphQLInputType } from '../type/definition.js'; +import { isInputObjectType, isLeafType, isListType, isNonNullType } from '../type/definition.js'; + +type OnErrorCB = (path: ReadonlyArray, invalidValue: unknown, error: GraphQLError) => void; + +/** + * Coerces a JavaScript value given a GraphQL Input Type. + */ +export function coerceInputValue( + inputValue: unknown, + type: GraphQLInputType, + onError: OnErrorCB = defaultOnError +): unknown { + return coerceInputValueImpl(inputValue, type, onError, undefined); +} + +function defaultOnError(path: ReadonlyArray, invalidValue: unknown, error: GraphQLError): void { + let errorPrefix = 'Invalid value ' + inspect(invalidValue); + if (path.length > 0) { + errorPrefix += ` at "value${printPathArray(path)}"`; + } + error.message = errorPrefix + ': ' + error.message; + throw error; +} + +function coerceInputValueImpl( + inputValue: unknown, + type: GraphQLInputType, + onError: OnErrorCB, + path: Path | undefined +): unknown { + if (isNonNullType(type)) { + if (inputValue != null) { + return coerceInputValueImpl(inputValue, type.ofType, onError, path); + } + onError( + pathToArray(path), + inputValue, + new GraphQLError(`Expected non-nullable type "${inspect(type)}" not to be null.`) + ); + return; + } + + if (inputValue == null) { + // Explicitly return the value null. + return null; + } + + if (isListType(type)) { + const itemType = type.ofType; + if (isIterableObject(inputValue)) { + return Array.from(inputValue, (itemValue, index) => { + const itemPath = addPath(path, index, undefined); + return coerceInputValueImpl(itemValue, itemType, onError, itemPath); + }); + } + // Lists accept a non-list value as a list of one. + return [coerceInputValueImpl(inputValue, itemType, onError, path)]; + } + + if (isInputObjectType(type)) { + if (!isObjectLike(inputValue)) { + onError(pathToArray(path), inputValue, new GraphQLError(`Expected type "${type.name}" to be an object.`)); + return; + } + + const coercedValue: any = {}; + const fieldDefs = type.getFields(); + + for (const field of Object.values(fieldDefs)) { + const fieldValue = inputValue[field.name]; + + if (fieldValue === undefined) { + if (field.defaultValue !== undefined) { + coercedValue[field.name] = field.defaultValue; + } else if (isNonNullType(field.type)) { + const typeStr = inspect(field.type); + onError( + pathToArray(path), + inputValue, + new GraphQLError(`Field "${field.name}" of required type "${typeStr}" was not provided.`) + ); + } + continue; + } + + coercedValue[field.name] = coerceInputValueImpl( + fieldValue, + field.type, + onError, + addPath(path, field.name, type.name) + ); + } + + // Ensure every provided field is defined. + for (const fieldName of Object.keys(inputValue)) { + if (!fieldDefs[fieldName]) { + const suggestions = suggestionList(fieldName, Object.keys(type.getFields())); + onError( + pathToArray(path), + inputValue, + new GraphQLError(`Field "${fieldName}" is not defined by type "${type.name}".` + didYouMean(suggestions)) + ); + } + } + return coercedValue; + } + + if (isLeafType(type)) { + let parseResult; + + // Scalars and Enums determine if a input value is valid via parseValue(), + // which can throw to indicate failure. If it throws, maintain a reference + // to the original error. + try { + parseResult = type.parseValue(inputValue); + } catch (error) { + if (isGraphQLError(error)) { + onError(pathToArray(path), inputValue, error); + } else { + onError( + pathToArray(path), + inputValue, + new GraphQLError(`Expected type "${type.name}". ` + (error as Error).message, { + originalError: error as Error, + }) + ); + } + return; + } + if (parseResult === undefined) { + onError(pathToArray(path), inputValue, new GraphQLError(`Expected type "${type.name}".`)); + } + return parseResult; + } + /* c8 ignore next 3 */ + // Not reachable, all possible types have been considered. + invariant(false, 'Unexpected input type: ' + inspect(type)); +} diff --git a/packages/graphql/src/utilities/concatAST.ts b/packages/graphql/src/utilities/concatAST.ts new file mode 100644 index 00000000000..9783ad8f4f4 --- /dev/null +++ b/packages/graphql/src/utilities/concatAST.ts @@ -0,0 +1,15 @@ +import type { DefinitionNode, DocumentNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +/** + * Provided a collection of ASTs, presumably each from different files, + * concatenate the ASTs together into batched AST, useful for validating many + * GraphQL source files which together represent one conceptual application. + */ +export function concatAST(documents: ReadonlyArray): DocumentNode { + const definitions: Array = []; + for (const doc of documents) { + definitions.push(...doc.definitions); + } + return { kind: Kind.DOCUMENT, definitions }; +} diff --git a/packages/graphql/src/utilities/extendSchema.ts b/packages/graphql/src/utilities/extendSchema.ts new file mode 100644 index 00000000000..39e8586d417 --- /dev/null +++ b/packages/graphql/src/utilities/extendSchema.ts @@ -0,0 +1,629 @@ +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; +import { keyMap } from '../jsutils/keyMap.js'; +import { mapValue } from '../jsutils/mapValue.js'; +import type { Maybe } from '../jsutils/Maybe.js'; + +import type { + DirectiveDefinitionNode, + DocumentNode, + EnumTypeDefinitionNode, + EnumTypeExtensionNode, + EnumValueDefinitionNode, + FieldDefinitionNode, + InputObjectTypeDefinitionNode, + InputObjectTypeExtensionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + InterfaceTypeExtensionNode, + NamedTypeNode, + ObjectTypeDefinitionNode, + ObjectTypeExtensionNode, + ScalarTypeDefinitionNode, + ScalarTypeExtensionNode, + SchemaDefinitionNode, + SchemaExtensionNode, + TypeDefinitionNode, + TypeNode, + UnionTypeDefinitionNode, + UnionTypeExtensionNode, +} from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; +import { isTypeDefinitionNode, isTypeExtensionNode } from '../language/predicates.js'; + +import type { + GraphQLArgumentConfig, + GraphQLEnumValueConfigMap, + GraphQLFieldConfig, + GraphQLFieldConfigArgumentMap, + GraphQLFieldConfigMap, + GraphQLInputFieldConfigMap, + GraphQLNamedType, + GraphQLNullableType, + GraphQLType, +} from '../type/definition.js'; +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, + isEnumType, + isInputObjectType, + isInterfaceType, + isListType, + isNonNullType, + isObjectType, + isScalarType, + isUnionType, +} from '../type/definition.js'; +import { + GraphQLDeprecatedDirective, + GraphQLDirective, + GraphQLSpecifiedByDirective, + isSpecifiedDirective, +} from '../type/directives.js'; +import { introspectionTypes, isIntrospectionType } from '../type/introspection.js'; +import { isSpecifiedScalarType, specifiedScalarTypes } from '../type/scalars.js'; +import type { GraphQLSchemaNormalizedConfig, GraphQLSchemaValidationOptions } from '../type/schema.js'; +import { assertSchema, GraphQLSchema } from '../type/schema.js'; + +import { assertValidSDLExtension } from '../validation/validate.js'; + +import { getDirectiveValues } from '../execution/values.js'; + +import { valueFromAST } from './valueFromAST.js'; + +interface Options extends GraphQLSchemaValidationOptions { + /** + * Set to true to assume the SDL is valid. + * + * Default: false + */ + assumeValidSDL?: boolean; +} + +/** + * Produces a new schema given an existing schema and a document which may + * contain GraphQL type extensions and definitions. The original schema will + * remain unaltered. + * + * Because a schema represents a graph of references, a schema cannot be + * extended without effectively making an entire copy. We do not know until it's + * too late if subgraphs remain unchanged. + * + * This algorithm copies the provided schema, applying extensions while + * producing the copy. The original schema remains unaltered. + */ +export function extendSchema(schema: GraphQLSchema, documentAST: DocumentNode, options?: Options): GraphQLSchema { + assertSchema(schema); + + if (options?.assumeValid !== true && options?.assumeValidSDL !== true) { + assertValidSDLExtension(documentAST, schema); + } + + const schemaConfig = schema.toConfig(); + const extendedConfig = extendSchemaImpl(schemaConfig, documentAST, options); + return schemaConfig === extendedConfig ? schema : new GraphQLSchema(extendedConfig); +} + +/** + * @internal + */ +export function extendSchemaImpl( + schemaConfig: GraphQLSchemaNormalizedConfig, + documentAST: DocumentNode, + options?: Options +): GraphQLSchemaNormalizedConfig { + // Collect the type definitions and extensions found in the document. + const typeDefs: Array = []; + const typeExtensionsMap = Object.create(null); + + // New directives and types are separate because a directives and types can + // have the same name. For example, a type named "skip". + const directiveDefs: Array = []; + + let schemaDef: Maybe; + // Schema extensions are collected which may add additional operation types. + const schemaExtensions: Array = []; + + for (const def of documentAST.definitions) { + if (def.kind === Kind.SCHEMA_DEFINITION) { + schemaDef = def; + } else if (def.kind === Kind.SCHEMA_EXTENSION) { + schemaExtensions.push(def); + } else if (isTypeDefinitionNode(def)) { + typeDefs.push(def); + } else if (isTypeExtensionNode(def)) { + const extendedTypeName = def.name.value; + const existingTypeExtensions = typeExtensionsMap[extendedTypeName]; + typeExtensionsMap[extendedTypeName] = existingTypeExtensions ? existingTypeExtensions.concat([def]) : [def]; + } else if (def.kind === Kind.DIRECTIVE_DEFINITION) { + directiveDefs.push(def); + } + } + + // If this document contains no new types, extensions, or directives then + // return the same unmodified GraphQLSchema instance. + if ( + Object.keys(typeExtensionsMap).length === 0 && + typeDefs.length === 0 && + directiveDefs.length === 0 && + schemaExtensions.length === 0 && + schemaDef == null + ) { + return schemaConfig; + } + + const typeMap = Object.create(null); + for (const existingType of schemaConfig.types) { + typeMap[existingType.name] = extendNamedType(existingType); + } + + for (const typeNode of typeDefs) { + const name = typeNode.name.value; + typeMap[name] = stdTypeMap[name] ?? buildType(typeNode); + } + + const operationTypes = { + // Get the extended root operation types. + query: schemaConfig.query && replaceNamedType(schemaConfig.query), + mutation: schemaConfig.mutation && replaceNamedType(schemaConfig.mutation), + subscription: schemaConfig.subscription && replaceNamedType(schemaConfig.subscription), + // Then, incorporate schema definition and all schema extensions. + ...(schemaDef && getOperationTypes([schemaDef])), + ...getOperationTypes(schemaExtensions), + }; + + // Then produce and return a Schema config with these types. + return { + description: schemaDef?.description?.value, + ...operationTypes, + types: Object.values(typeMap), + directives: [...schemaConfig.directives.map(replaceDirective), ...directiveDefs.map(buildDirective)], + extensions: Object.create(null), + astNode: schemaDef ?? schemaConfig.astNode, + extensionASTNodes: schemaConfig.extensionASTNodes.concat(schemaExtensions), + assumeValid: options?.assumeValid ?? false, + }; + + // Below are functions used for producing this schema that have closed over + // this scope and have access to the schema, cache, and newly defined types. + + function replaceType(type: T): T { + if (isListType(type)) { + // @ts-expect-error + return new GraphQLList(replaceType(type.ofType)); + } + if (isNonNullType(type)) { + // @ts-expect-error + return new GraphQLNonNull(replaceType(type.ofType)); + } + return replaceNamedType(type); + } + + function replaceNamedType(type: T): T { + // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + return typeMap[type.name]; + } + + function replaceDirective(directive: GraphQLDirective): GraphQLDirective { + if (isSpecifiedDirective(directive)) { + // Builtin directives are not extended. + return directive; + } + + const config = directive.toConfig(); + return new GraphQLDirective({ + ...config, + args: mapValue(config.args, extendArg), + }); + } + + function extendNamedType(type: GraphQLNamedType): GraphQLNamedType { + if (isIntrospectionType(type) || isSpecifiedScalarType(type)) { + // Builtin types are not extended. + return type; + } + if (isScalarType(type)) { + return extendScalarType(type); + } + if (isObjectType(type)) { + return extendObjectType(type); + } + if (isInterfaceType(type)) { + return extendInterfaceType(type); + } + if (isUnionType(type)) { + return extendUnionType(type); + } + if (isEnumType(type)) { + return extendEnumType(type); + } + if (isInputObjectType(type)) { + return extendInputObjectType(type); + } + /* c8 ignore next 3 */ + // Not reachable, all possible type definition nodes have been considered. + invariant(false, 'Unexpected type: ' + inspect(type)); + } + + function extendInputObjectType(type: GraphQLInputObjectType): GraphQLInputObjectType { + const config = type.toConfig(); + const extensions = typeExtensionsMap[config.name] ?? []; + + return new GraphQLInputObjectType({ + ...config, + fields: () => ({ + ...mapValue(config.fields, field => ({ + ...field, + type: replaceType(field.type), + })), + ...buildInputFieldMap(extensions), + }), + extensionASTNodes: config.extensionASTNodes.concat(extensions), + }); + } + + function extendEnumType(type: GraphQLEnumType): GraphQLEnumType { + const config = type.toConfig(); + const extensions = typeExtensionsMap[type.name] ?? []; + + return new GraphQLEnumType({ + ...config, + values: { + ...config.values, + ...buildEnumValueMap(extensions), + }, + extensionASTNodes: config.extensionASTNodes.concat(extensions), + }); + } + + function extendScalarType(type: GraphQLScalarType): GraphQLScalarType { + const config = type.toConfig(); + const extensions = typeExtensionsMap[config.name] ?? []; + + let specifiedByURL = config.specifiedByURL; + for (const extensionNode of extensions) { + specifiedByURL = getSpecifiedByURL(extensionNode) ?? specifiedByURL; + } + + return new GraphQLScalarType({ + ...config, + specifiedByURL, + extensionASTNodes: config.extensionASTNodes.concat(extensions), + }); + } + + function extendObjectType(type: GraphQLObjectType): GraphQLObjectType { + const config = type.toConfig(); + const extensions = typeExtensionsMap[config.name] ?? []; + + return new GraphQLObjectType({ + ...config, + interfaces: () => [...type.getInterfaces().map(replaceNamedType), ...buildInterfaces(extensions)], + fields: () => ({ + ...mapValue(config.fields, extendField), + ...buildFieldMap(extensions), + }), + extensionASTNodes: config.extensionASTNodes.concat(extensions), + }); + } + + function extendInterfaceType(type: GraphQLInterfaceType): GraphQLInterfaceType { + const config = type.toConfig(); + const extensions = typeExtensionsMap[config.name] ?? []; + + return new GraphQLInterfaceType({ + ...config, + interfaces: () => [...type.getInterfaces().map(replaceNamedType), ...buildInterfaces(extensions)], + fields: () => ({ + ...mapValue(config.fields, extendField), + ...buildFieldMap(extensions), + }), + extensionASTNodes: config.extensionASTNodes.concat(extensions), + }); + } + + function extendUnionType(type: GraphQLUnionType): GraphQLUnionType { + const config = type.toConfig(); + const extensions = typeExtensionsMap[config.name] ?? []; + + return new GraphQLUnionType({ + ...config, + types: () => [...type.getTypes().map(replaceNamedType), ...buildUnionTypes(extensions)], + extensionASTNodes: config.extensionASTNodes.concat(extensions), + }); + } + + function extendField(field: GraphQLFieldConfig): GraphQLFieldConfig { + return { + ...field, + type: replaceType(field.type), + args: field.args && mapValue(field.args, extendArg), + }; + } + + function extendArg(arg: GraphQLArgumentConfig) { + return { + ...arg, + type: replaceType(arg.type), + }; + } + + function getOperationTypes(nodes: ReadonlyArray): { + query?: Maybe; + mutation?: Maybe; + subscription?: Maybe; + } { + const opTypes = {}; + for (const node of nodes) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + const operationTypesNodes = /* c8 ignore next */ node.operationTypes ?? []; + + for (const operationType of operationTypesNodes) { + // Note: While this could make early assertions to get the correctly + // typed values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + opTypes[operationType.operation] = getNamedType(operationType.type); + } + } + + return opTypes; + } + + function getNamedType(node: NamedTypeNode): GraphQLNamedType { + const name = node.name.value; + const type = stdTypeMap[name] ?? typeMap[name]; + + if (type === undefined) { + throw new Error(`Unknown type: "${name}".`); + } + return type; + } + + function getWrappedType(node: TypeNode): GraphQLType { + if (node.kind === Kind.LIST_TYPE) { + return new GraphQLList(getWrappedType(node.type)); + } + if (node.kind === Kind.NON_NULL_TYPE) { + return new GraphQLNonNull(getWrappedType(node.type) as GraphQLNullableType); + } + return getNamedType(node); + } + + function buildDirective(node: DirectiveDefinitionNode): GraphQLDirective { + return new GraphQLDirective({ + name: node.name.value, + description: node.description?.value, + // @ts-expect-error + locations: node.locations.map(({ value }) => value), + isRepeatable: node.repeatable, + args: buildArgumentMap(node.arguments), + astNode: node, + }); + } + + function buildFieldMap( + nodes: ReadonlyArray< + InterfaceTypeDefinitionNode | InterfaceTypeExtensionNode | ObjectTypeDefinitionNode | ObjectTypeExtensionNode + > + ): GraphQLFieldConfigMap { + const fieldConfigMap = Object.create(null); + for (const node of nodes) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + const nodeFields = /* c8 ignore next */ node.fields ?? []; + + for (const field of nodeFields) { + fieldConfigMap[field.name.value] = { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + // with validateSchema() will produce more actionable results. + type: getWrappedType(field.type), + description: field.description?.value, + args: buildArgumentMap(field.arguments), + deprecationReason: getDeprecationReason(field), + astNode: field, + }; + } + } + return fieldConfigMap; + } + + function buildArgumentMap(args: Maybe>): GraphQLFieldConfigArgumentMap { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + const argsNodes = /* c8 ignore next */ args ?? []; + + const argConfigMap = Object.create(null); + for (const arg of argsNodes) { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + // with validateSchema() will produce more actionable results. + const type: any = getWrappedType(arg.type); + + argConfigMap[arg.name.value] = { + type, + description: arg.description?.value, + defaultValue: valueFromAST(arg.defaultValue, type), + deprecationReason: getDeprecationReason(arg), + astNode: arg, + }; + } + return argConfigMap; + } + + function buildInputFieldMap( + nodes: ReadonlyArray + ): GraphQLInputFieldConfigMap { + const inputFieldMap = Object.create(null); + for (const node of nodes) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + const fieldsNodes = /* c8 ignore next */ node.fields ?? []; + + for (const field of fieldsNodes) { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + // with validateSchema() will produce more actionable results. + const type: any = getWrappedType(field.type); + + inputFieldMap[field.name.value] = { + type, + description: field.description?.value, + defaultValue: valueFromAST(field.defaultValue, type), + deprecationReason: getDeprecationReason(field), + astNode: field, + }; + } + } + return inputFieldMap; + } + + function buildEnumValueMap( + nodes: ReadonlyArray + ): GraphQLEnumValueConfigMap { + const enumValueMap = Object.create(null); + for (const node of nodes) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + const valuesNodes = /* c8 ignore next */ node.values ?? []; + + for (const value of valuesNodes) { + enumValueMap[value.name.value] = { + description: value.description?.value, + deprecationReason: getDeprecationReason(value), + astNode: value, + }; + } + } + return enumValueMap; + } + + function buildInterfaces( + nodes: ReadonlyArray< + InterfaceTypeDefinitionNode | InterfaceTypeExtensionNode | ObjectTypeDefinitionNode | ObjectTypeExtensionNode + > + ): Array { + // Note: While this could make assertions to get the correctly typed + // values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + // @ts-expect-error + return nodes.flatMap( + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + node => /* c8 ignore next */ node.interfaces?.map(getNamedType) ?? [] + ); + } + + function buildUnionTypes( + nodes: ReadonlyArray + ): Array { + // Note: While this could make assertions to get the correctly typed + // values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + // @ts-expect-error + return nodes.flatMap( + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + node => /* c8 ignore next */ node.types?.map(getNamedType) ?? [] + ); + } + + function buildType(astNode: TypeDefinitionNode): GraphQLNamedType { + const name = astNode.name.value; + const extensionASTNodes = typeExtensionsMap[name] ?? []; + + switch (astNode.kind) { + case Kind.OBJECT_TYPE_DEFINITION: { + const allNodes = [astNode, ...extensionASTNodes]; + + return new GraphQLObjectType({ + name, + description: astNode.description?.value, + interfaces: () => buildInterfaces(allNodes), + fields: () => buildFieldMap(allNodes), + astNode, + extensionASTNodes, + }); + } + case Kind.INTERFACE_TYPE_DEFINITION: { + const allNodes = [astNode, ...extensionASTNodes]; + + return new GraphQLInterfaceType({ + name, + description: astNode.description?.value, + interfaces: () => buildInterfaces(allNodes), + fields: () => buildFieldMap(allNodes), + astNode, + extensionASTNodes, + }); + } + case Kind.ENUM_TYPE_DEFINITION: { + const allNodes = [astNode, ...extensionASTNodes]; + + return new GraphQLEnumType({ + name, + description: astNode.description?.value, + values: buildEnumValueMap(allNodes), + astNode, + extensionASTNodes, + }); + } + case Kind.UNION_TYPE_DEFINITION: { + const allNodes = [astNode, ...extensionASTNodes]; + + return new GraphQLUnionType({ + name, + description: astNode.description?.value, + types: () => buildUnionTypes(allNodes), + astNode, + extensionASTNodes, + }); + } + case Kind.SCALAR_TYPE_DEFINITION: { + return new GraphQLScalarType({ + name, + description: astNode.description?.value, + specifiedByURL: getSpecifiedByURL(astNode), + astNode, + extensionASTNodes, + }); + } + case Kind.INPUT_OBJECT_TYPE_DEFINITION: { + const allNodes = [astNode, ...extensionASTNodes]; + + return new GraphQLInputObjectType({ + name, + description: astNode.description?.value, + fields: () => buildInputFieldMap(allNodes), + astNode, + extensionASTNodes, + }); + } + } + } +} + +const stdTypeMap = keyMap([...specifiedScalarTypes, ...introspectionTypes], type => type.name); + +/** + * Given a field or enum value node, returns the string value for the + * deprecation reason. + */ +function getDeprecationReason( + node: EnumValueDefinitionNode | FieldDefinitionNode | InputValueDefinitionNode +): Maybe { + const deprecated = getDirectiveValues(GraphQLDeprecatedDirective, node); + // @ts-expect-error validated by `getDirectiveValues` + return deprecated?.reason; +} + +/** + * Given a scalar node, returns the string value for the specifiedByURL. + */ +function getSpecifiedByURL(node: ScalarTypeDefinitionNode | ScalarTypeExtensionNode): Maybe { + const specifiedBy = getDirectiveValues(GraphQLSpecifiedByDirective, node); + // @ts-expect-error validated by `getDirectiveValues` + return specifiedBy?.url; +} diff --git a/packages/graphql/src/utilities/findBreakingChanges.ts b/packages/graphql/src/utilities/findBreakingChanges.ts new file mode 100644 index 00000000000..d88b8ec91a1 --- /dev/null +++ b/packages/graphql/src/utilities/findBreakingChanges.ts @@ -0,0 +1,504 @@ +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; +import { keyMap } from '../jsutils/keyMap.js'; + +import { print } from '../language/printer.js'; + +import type { + GraphQLEnumType, + GraphQLField, + GraphQLInputObjectType, + GraphQLInputType, + GraphQLInterfaceType, + GraphQLNamedType, + GraphQLObjectType, + GraphQLType, + GraphQLUnionType, +} from '../type/definition.js'; +import { + isEnumType, + isInputObjectType, + isInterfaceType, + isListType, + isNamedType, + isNonNullType, + isObjectType, + isRequiredArgument, + isRequiredInputField, + isScalarType, + isUnionType, +} from '../type/definition.js'; +import { isSpecifiedScalarType } from '../type/scalars.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +import { astFromValue } from './astFromValue.js'; +import { sortValueNode } from './sortValueNode.js'; + +export enum BreakingChangeType { + TYPE_REMOVED = 'TYPE_REMOVED', + TYPE_CHANGED_KIND = 'TYPE_CHANGED_KIND', + TYPE_REMOVED_FROM_UNION = 'TYPE_REMOVED_FROM_UNION', + VALUE_REMOVED_FROM_ENUM = 'VALUE_REMOVED_FROM_ENUM', + REQUIRED_INPUT_FIELD_ADDED = 'REQUIRED_INPUT_FIELD_ADDED', + IMPLEMENTED_INTERFACE_REMOVED = 'IMPLEMENTED_INTERFACE_REMOVED', + FIELD_REMOVED = 'FIELD_REMOVED', + FIELD_CHANGED_KIND = 'FIELD_CHANGED_KIND', + REQUIRED_ARG_ADDED = 'REQUIRED_ARG_ADDED', + ARG_REMOVED = 'ARG_REMOVED', + ARG_CHANGED_KIND = 'ARG_CHANGED_KIND', + DIRECTIVE_REMOVED = 'DIRECTIVE_REMOVED', + DIRECTIVE_ARG_REMOVED = 'DIRECTIVE_ARG_REMOVED', + REQUIRED_DIRECTIVE_ARG_ADDED = 'REQUIRED_DIRECTIVE_ARG_ADDED', + DIRECTIVE_REPEATABLE_REMOVED = 'DIRECTIVE_REPEATABLE_REMOVED', + DIRECTIVE_LOCATION_REMOVED = 'DIRECTIVE_LOCATION_REMOVED', +} + +export enum DangerousChangeType { + VALUE_ADDED_TO_ENUM = 'VALUE_ADDED_TO_ENUM', + TYPE_ADDED_TO_UNION = 'TYPE_ADDED_TO_UNION', + OPTIONAL_INPUT_FIELD_ADDED = 'OPTIONAL_INPUT_FIELD_ADDED', + OPTIONAL_ARG_ADDED = 'OPTIONAL_ARG_ADDED', + IMPLEMENTED_INTERFACE_ADDED = 'IMPLEMENTED_INTERFACE_ADDED', + ARG_DEFAULT_VALUE_CHANGE = 'ARG_DEFAULT_VALUE_CHANGE', +} + +export interface BreakingChange { + type: BreakingChangeType; + description: string; +} + +export interface DangerousChange { + type: DangerousChangeType; + description: string; +} + +/** + * Given two schemas, returns an Array containing descriptions of all the types + * of breaking changes covered by the other functions down below. + */ +export function findBreakingChanges(oldSchema: GraphQLSchema, newSchema: GraphQLSchema): Array { + // @ts-expect-error + return findSchemaChanges(oldSchema, newSchema).filter(change => change.type in BreakingChangeType); +} + +/** + * Given two schemas, returns an Array containing descriptions of all the types + * of potentially dangerous changes covered by the other functions down below. + */ +export function findDangerousChanges(oldSchema: GraphQLSchema, newSchema: GraphQLSchema): Array { + // @ts-expect-error + return findSchemaChanges(oldSchema, newSchema).filter(change => change.type in DangerousChangeType); +} + +function findSchemaChanges( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema +): Array { + return [...findTypeChanges(oldSchema, newSchema), ...findDirectiveChanges(oldSchema, newSchema)]; +} + +function findDirectiveChanges( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema +): Array { + const schemaChanges = []; + + const directivesDiff = diff(oldSchema.getDirectives(), newSchema.getDirectives()); + + for (const oldDirective of directivesDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.DIRECTIVE_REMOVED, + description: `${oldDirective.name} was removed.`, + }); + } + + for (const [oldDirective, newDirective] of directivesDiff.persisted) { + const argsDiff = diff(oldDirective.args, newDirective.args); + + for (const newArg of argsDiff.added) { + if (isRequiredArgument(newArg)) { + schemaChanges.push({ + type: BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED, + description: `A required arg ${newArg.name} on directive ${oldDirective.name} was added.`, + }); + } + } + + for (const oldArg of argsDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.DIRECTIVE_ARG_REMOVED, + description: `${oldArg.name} was removed from ${oldDirective.name}.`, + }); + } + + if (oldDirective.isRepeatable && !newDirective.isRepeatable) { + schemaChanges.push({ + type: BreakingChangeType.DIRECTIVE_REPEATABLE_REMOVED, + description: `Repeatable flag was removed from ${oldDirective.name}.`, + }); + } + + for (const location of oldDirective.locations) { + if (!newDirective.locations.includes(location)) { + schemaChanges.push({ + type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, + description: `${location} was removed from ${oldDirective.name}.`, + }); + } + } + } + + return schemaChanges; +} + +function findTypeChanges(oldSchema: GraphQLSchema, newSchema: GraphQLSchema): Array { + const schemaChanges = []; + + const typesDiff = diff(Object.values(oldSchema.getTypeMap()), Object.values(newSchema.getTypeMap())); + + for (const oldType of typesDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.TYPE_REMOVED, + description: isSpecifiedScalarType(oldType) + ? `Standard scalar ${oldType.name} was removed because it is not referenced anymore.` + : `${oldType.name} was removed.`, + }); + } + + for (const [oldType, newType] of typesDiff.persisted) { + if (isEnumType(oldType) && isEnumType(newType)) { + schemaChanges.push(...findEnumTypeChanges(oldType, newType)); + } else if (isUnionType(oldType) && isUnionType(newType)) { + schemaChanges.push(...findUnionTypeChanges(oldType, newType)); + } else if (isInputObjectType(oldType) && isInputObjectType(newType)) { + schemaChanges.push(...findInputObjectTypeChanges(oldType, newType)); + } else if (isObjectType(oldType) && isObjectType(newType)) { + schemaChanges.push(...findFieldChanges(oldType, newType), ...findImplementedInterfacesChanges(oldType, newType)); + } else if (isInterfaceType(oldType) && isInterfaceType(newType)) { + schemaChanges.push(...findFieldChanges(oldType, newType), ...findImplementedInterfacesChanges(oldType, newType)); + } else if (oldType.constructor !== newType.constructor) { + schemaChanges.push({ + type: BreakingChangeType.TYPE_CHANGED_KIND, + description: `${oldType.name} changed from ` + `${typeKindName(oldType)} to ${typeKindName(newType)}.`, + }); + } + } + + return schemaChanges; +} + +function findInputObjectTypeChanges( + oldType: GraphQLInputObjectType, + newType: GraphQLInputObjectType +): Array { + const schemaChanges = []; + const fieldsDiff = diff(Object.values(oldType.getFields()), Object.values(newType.getFields())); + + for (const newField of fieldsDiff.added) { + if (isRequiredInputField(newField)) { + schemaChanges.push({ + type: BreakingChangeType.REQUIRED_INPUT_FIELD_ADDED, + description: `A required field ${newField.name} on input type ${oldType.name} was added.`, + }); + } else { + schemaChanges.push({ + type: DangerousChangeType.OPTIONAL_INPUT_FIELD_ADDED, + description: `An optional field ${newField.name} on input type ${oldType.name} was added.`, + }); + } + } + + for (const oldField of fieldsDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.FIELD_REMOVED, + description: `${oldType.name}.${oldField.name} was removed.`, + }); + } + + for (const [oldField, newField] of fieldsDiff.persisted) { + const isSafe = isChangeSafeForInputObjectFieldOrFieldArg(oldField.type, newField.type); + if (!isSafe) { + schemaChanges.push({ + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: + `${oldType.name}.${oldField.name} changed type from ` + + `${String(oldField.type)} to ${String(newField.type)}.`, + }); + } + } + + return schemaChanges; +} + +function findUnionTypeChanges( + oldType: GraphQLUnionType, + newType: GraphQLUnionType +): Array { + const schemaChanges = []; + const possibleTypesDiff = diff(oldType.getTypes(), newType.getTypes()); + + for (const newPossibleType of possibleTypesDiff.added) { + schemaChanges.push({ + type: DangerousChangeType.TYPE_ADDED_TO_UNION, + description: `${newPossibleType.name} was added to union type ${oldType.name}.`, + }); + } + + for (const oldPossibleType of possibleTypesDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.TYPE_REMOVED_FROM_UNION, + description: `${oldPossibleType.name} was removed from union type ${oldType.name}.`, + }); + } + + return schemaChanges; +} + +function findEnumTypeChanges( + oldType: GraphQLEnumType, + newType: GraphQLEnumType +): Array { + const schemaChanges = []; + const valuesDiff = diff(oldType.getValues(), newType.getValues()); + + for (const newValue of valuesDiff.added) { + schemaChanges.push({ + type: DangerousChangeType.VALUE_ADDED_TO_ENUM, + description: `${newValue.name} was added to enum type ${oldType.name}.`, + }); + } + + for (const oldValue of valuesDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM, + description: `${oldValue.name} was removed from enum type ${oldType.name}.`, + }); + } + + return schemaChanges; +} + +function findImplementedInterfacesChanges( + oldType: GraphQLObjectType | GraphQLInterfaceType, + newType: GraphQLObjectType | GraphQLInterfaceType +): Array { + const schemaChanges = []; + const interfacesDiff = diff(oldType.getInterfaces(), newType.getInterfaces()); + + for (const newInterface of interfacesDiff.added) { + schemaChanges.push({ + type: DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED, + description: `${newInterface.name} added to interfaces implemented by ${oldType.name}.`, + }); + } + + for (const oldInterface of interfacesDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED, + description: `${oldType.name} no longer implements interface ${oldInterface.name}.`, + }); + } + + return schemaChanges; +} + +function findFieldChanges( + oldType: GraphQLObjectType | GraphQLInterfaceType, + newType: GraphQLObjectType | GraphQLInterfaceType +): Array { + const schemaChanges = []; + const fieldsDiff = diff(Object.values(oldType.getFields()), Object.values(newType.getFields())); + + for (const oldField of fieldsDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.FIELD_REMOVED, + description: `${oldType.name}.${oldField.name} was removed.`, + }); + } + + for (const [oldField, newField] of fieldsDiff.persisted) { + schemaChanges.push(...findArgChanges(oldType, oldField, newField)); + + const isSafe = isChangeSafeForObjectOrInterfaceField(oldField.type, newField.type); + if (!isSafe) { + schemaChanges.push({ + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: + `${oldType.name}.${oldField.name} changed type from ` + + `${String(oldField.type)} to ${String(newField.type)}.`, + }); + } + } + + return schemaChanges; +} + +function findArgChanges( + oldType: GraphQLObjectType | GraphQLInterfaceType, + oldField: GraphQLField, + newField: GraphQLField +): Array { + const schemaChanges = []; + const argsDiff = diff(oldField.args, newField.args); + + for (const oldArg of argsDiff.removed) { + schemaChanges.push({ + type: BreakingChangeType.ARG_REMOVED, + description: `${oldType.name}.${oldField.name} arg ${oldArg.name} was removed.`, + }); + } + + for (const [oldArg, newArg] of argsDiff.persisted) { + const isSafe = isChangeSafeForInputObjectFieldOrFieldArg(oldArg.type, newArg.type); + if (!isSafe) { + schemaChanges.push({ + type: BreakingChangeType.ARG_CHANGED_KIND, + description: + `${oldType.name}.${oldField.name} arg ${oldArg.name} has changed type from ` + + `${String(oldArg.type)} to ${String(newArg.type)}.`, + }); + } else if (oldArg.defaultValue !== undefined) { + if (newArg.defaultValue === undefined) { + schemaChanges.push({ + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: `${oldType.name}.${oldField.name} arg ${oldArg.name} defaultValue was removed.`, + }); + } else { + // Since we looking only for client's observable changes we should + // compare default values in the same representation as they are + // represented inside introspection. + const oldValueStr = stringifyValue(oldArg.defaultValue, oldArg.type); + const newValueStr = stringifyValue(newArg.defaultValue, newArg.type); + + if (oldValueStr !== newValueStr) { + schemaChanges.push({ + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: `${oldType.name}.${oldField.name} arg ${oldArg.name} has changed defaultValue from ${oldValueStr} to ${newValueStr}.`, + }); + } + } + } + } + + for (const newArg of argsDiff.added) { + if (isRequiredArgument(newArg)) { + schemaChanges.push({ + type: BreakingChangeType.REQUIRED_ARG_ADDED, + description: `A required arg ${newArg.name} on ${oldType.name}.${oldField.name} was added.`, + }); + } else { + schemaChanges.push({ + type: DangerousChangeType.OPTIONAL_ARG_ADDED, + description: `An optional arg ${newArg.name} on ${oldType.name}.${oldField.name} was added.`, + }); + } + } + + return schemaChanges; +} + +function isChangeSafeForObjectOrInterfaceField(oldType: GraphQLType, newType: GraphQLType): boolean { + if (isListType(oldType)) { + return ( + // if they're both lists, make sure the underlying types are compatible + (isListType(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType)) || + // moving from nullable to non-null of the same underlying type is safe + (isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType)) + ); + } + + if (isNonNullType(oldType)) { + // if they're both non-null, make sure the underlying types are compatible + return isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType); + } + + return ( + // if they're both named types, see if their names are equivalent + (isNamedType(newType) && oldType.name === newType.name) || + // moving from nullable to non-null of the same underlying type is safe + (isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType)) + ); +} + +function isChangeSafeForInputObjectFieldOrFieldArg(oldType: GraphQLType, newType: GraphQLType): boolean { + if (isListType(oldType)) { + // if they're both lists, make sure the underlying types are compatible + return isListType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType); + } + + if (isNonNullType(oldType)) { + return ( + // if they're both non-null, make sure the underlying types are + // compatible + (isNonNullType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType)) || + // moving from non-null to nullable of the same underlying type is safe + (!isNonNullType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType)) + ); + } + + // if they're both named types, see if their names are equivalent + return isNamedType(newType) && oldType.name === newType.name; +} + +function typeKindName(type: GraphQLNamedType): string { + if (isScalarType(type)) { + return 'a Scalar type'; + } + if (isObjectType(type)) { + return 'an Object type'; + } + if (isInterfaceType(type)) { + return 'an Interface type'; + } + if (isUnionType(type)) { + return 'a Union type'; + } + if (isEnumType(type)) { + return 'an Enum type'; + } + if (isInputObjectType(type)) { + return 'an Input type'; + } + /* c8 ignore next 3 */ + // Not reachable, all possible types have been considered. + invariant(false, 'Unexpected type: ' + inspect(type)); +} + +function stringifyValue(value: unknown, type: GraphQLInputType): string { + const ast = astFromValue(value, type); + invariant(ast != null); + return print(sortValueNode(ast)); +} + +function diff( + oldArray: ReadonlyArray, + newArray: ReadonlyArray +): { + added: ReadonlyArray; + removed: ReadonlyArray; + persisted: ReadonlyArray<[T, T]>; +} { + const added: Array = []; + const removed: Array = []; + const persisted: Array<[T, T]> = []; + + const oldMap = keyMap(oldArray, ({ name }) => name); + const newMap = keyMap(newArray, ({ name }) => name); + + for (const oldItem of oldArray) { + const newItem = newMap[oldItem.name]; + if (newItem === undefined) { + removed.push(oldItem); + } else { + persisted.push([oldItem, newItem]); + } + } + + for (const newItem of newArray) { + if (oldMap[newItem.name] === undefined) { + added.push(newItem); + } + } + + return { added, persisted, removed }; +} diff --git a/packages/graphql/src/utilities/getDocumentNodeFromSchema.ts b/packages/graphql/src/utilities/getDocumentNodeFromSchema.ts new file mode 100644 index 00000000000..e71a15225a9 --- /dev/null +++ b/packages/graphql/src/utilities/getDocumentNodeFromSchema.ts @@ -0,0 +1,82 @@ +// TODO: add tests for me + +import { DefinitionNode, DocumentNode } from '../language/ast.js'; +import { Kind } from '../language/index.js'; +import { + GraphQLSchema, + isSpecifiedDirective, + isSpecifiedScalarType, + isIntrospectionType, + isObjectType, + isInterfaceType, + isEnumType, + isScalarType, + isInputObjectType, + isUnionType, +} from '../type/index.js'; +import { + astFromDirective, + astFromEnumType, + astFromInputObjectType, + astFromInterfaceType, + astFromObjectType, + astFromScalarType, + astFromSchema, + astFromUnionType, +} from './astFromSchema.js'; + +export interface GetDocumentNodeFromSchemaOptions { + pathToDirectivesInExtensions?: Array; +} + +export function getDocumentNodeFromSchema( + schema: GraphQLSchema, + options: GetDocumentNodeFromSchemaOptions = {} +): DocumentNode { + const pathToDirectivesInExtensions = options.pathToDirectivesInExtensions; + + const typesMap = schema.getTypeMap(); + + const schemaNode = astFromSchema(schema, pathToDirectivesInExtensions); + const definitions: Array = schemaNode != null ? [schemaNode] : []; + + const directives = schema.getDirectives(); + for (const directive of directives) { + if (isSpecifiedDirective(directive)) { + continue; + } + + definitions.push(astFromDirective(directive, schema, pathToDirectivesInExtensions)); + } + + for (const typeName in typesMap) { + const type = typesMap[typeName]; + const isPredefinedScalar = isSpecifiedScalarType(type); + const isIntrospection = isIntrospectionType(type); + + if (isPredefinedScalar || isIntrospection) { + continue; + } + + if (isObjectType(type)) { + definitions.push(astFromObjectType(type, schema, pathToDirectivesInExtensions)); + } else if (isInterfaceType(type)) { + definitions.push(astFromInterfaceType(type, schema, pathToDirectivesInExtensions)); + } else if (isUnionType(type)) { + definitions.push(astFromUnionType(type, schema, pathToDirectivesInExtensions)); + } else if (isInputObjectType(type)) { + definitions.push(astFromInputObjectType(type, schema, pathToDirectivesInExtensions)); + } else if (isEnumType(type)) { + definitions.push(astFromEnumType(type, schema, pathToDirectivesInExtensions)); + } else if (isScalarType(type)) { + definitions.push(astFromScalarType(type, schema, pathToDirectivesInExtensions)); + } else { + throw new Error(`Unknown type ${type}.`); + } + } + + return { + kind: Kind.DOCUMENT, + definitions, + }; +} diff --git a/packages/graphql/src/utilities/getIntrospectionQuery.ts b/packages/graphql/src/utilities/getIntrospectionQuery.ts new file mode 100644 index 00000000000..8379f33eb94 --- /dev/null +++ b/packages/graphql/src/utilities/getIntrospectionQuery.ts @@ -0,0 +1,300 @@ +import type { Maybe } from '../jsutils/Maybe.js'; + +import type { DirectiveLocation } from '../language/directiveLocation.js'; + +export interface IntrospectionOptions { + /** + * Whether to include descriptions in the introspection result. + * Default: true + */ + descriptions?: boolean; + + /** + * Whether to include `specifiedByURL` in the introspection result. + * Default: false + */ + specifiedByUrl?: boolean; + + /** + * Whether to include `isRepeatable` flag on directives. + * Default: false + */ + directiveIsRepeatable?: boolean; + + /** + * Whether to include `description` field on schema. + * Default: false + */ + schemaDescription?: boolean; + + /** + * Whether target GraphQL server support deprecation of input values. + * Default: false + */ + inputValueDeprecation?: boolean; +} + +/** + * Produce the GraphQL query recommended for a full schema introspection. + * Accepts optional IntrospectionOptions. + */ +export function getIntrospectionQuery(options?: IntrospectionOptions): string { + const optionsWithDefault = { + descriptions: true, + specifiedByUrl: false, + directiveIsRepeatable: false, + schemaDescription: false, + inputValueDeprecation: false, + ...options, + }; + + const descriptions = optionsWithDefault.descriptions ? 'description' : ''; + const specifiedByUrl = optionsWithDefault.specifiedByUrl ? 'specifiedByURL' : ''; + const directiveIsRepeatable = optionsWithDefault.directiveIsRepeatable ? 'isRepeatable' : ''; + const schemaDescription = optionsWithDefault.schemaDescription ? descriptions : ''; + + function inputDeprecation(str: string) { + return optionsWithDefault.inputValueDeprecation ? str : ''; + } + + return ` + query IntrospectionQuery { + __schema { + ${schemaDescription} + queryType { name } + mutationType { name } + subscriptionType { name } + types { + ...FullType + } + directives { + name + ${descriptions} + ${directiveIsRepeatable} + locations + args${inputDeprecation('(includeDeprecated: true)')} { + ...InputValue + } + } + } + } + + fragment FullType on __Type { + kind + name + ${descriptions} + ${specifiedByUrl} + fields(includeDeprecated: true) { + name + ${descriptions} + args${inputDeprecation('(includeDeprecated: true)')} { + ...InputValue + } + type { + ...TypeRef + } + isDeprecated + deprecationReason + } + inputFields${inputDeprecation('(includeDeprecated: true)')} { + ...InputValue + } + interfaces { + ...TypeRef + } + enumValues(includeDeprecated: true) { + name + ${descriptions} + isDeprecated + deprecationReason + } + possibleTypes { + ...TypeRef + } + } + + fragment InputValue on __InputValue { + name + ${descriptions} + type { ...TypeRef } + defaultValue + ${inputDeprecation('isDeprecated')} + ${inputDeprecation('deprecationReason')} + } + + fragment TypeRef on __Type { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + } + } + } + } + } + } + } + } + `; +} + +export interface IntrospectionQuery { + readonly __schema: IntrospectionSchema; +} + +export interface IntrospectionSchema { + readonly description?: Maybe; + readonly queryType: IntrospectionNamedTypeRef; + readonly mutationType: Maybe>; + readonly subscriptionType: Maybe>; + readonly types: ReadonlyArray; + readonly directives: ReadonlyArray; +} + +export type IntrospectionType = + | IntrospectionScalarType + | IntrospectionObjectType + | IntrospectionInterfaceType + | IntrospectionUnionType + | IntrospectionEnumType + | IntrospectionInputObjectType; + +export type IntrospectionOutputType = + | IntrospectionScalarType + | IntrospectionObjectType + | IntrospectionInterfaceType + | IntrospectionUnionType + | IntrospectionEnumType; + +export type IntrospectionInputType = IntrospectionScalarType | IntrospectionEnumType | IntrospectionInputObjectType; + +export interface IntrospectionScalarType { + readonly kind: 'SCALAR'; + readonly name: string; + readonly description?: Maybe; + readonly specifiedByURL?: Maybe; +} + +export interface IntrospectionObjectType { + readonly kind: 'OBJECT'; + readonly name: string; + readonly description?: Maybe; + readonly fields: ReadonlyArray; + readonly interfaces: ReadonlyArray>; +} + +export interface IntrospectionInterfaceType { + readonly kind: 'INTERFACE'; + readonly name: string; + readonly description?: Maybe; + readonly fields: ReadonlyArray; + readonly interfaces: ReadonlyArray>; + readonly possibleTypes: ReadonlyArray>; +} + +export interface IntrospectionUnionType { + readonly kind: 'UNION'; + readonly name: string; + readonly description?: Maybe; + readonly possibleTypes: ReadonlyArray>; +} + +export interface IntrospectionEnumType { + readonly kind: 'ENUM'; + readonly name: string; + readonly description?: Maybe; + readonly enumValues: ReadonlyArray; +} + +export interface IntrospectionInputObjectType { + readonly kind: 'INPUT_OBJECT'; + readonly name: string; + readonly description?: Maybe; + readonly inputFields: ReadonlyArray; +} + +export interface IntrospectionListTypeRef { + readonly kind: 'LIST'; + readonly ofType: T; +} + +export interface IntrospectionNonNullTypeRef { + readonly kind: 'NON_NULL'; + readonly ofType: T; +} + +export type IntrospectionTypeRef = + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef + | IntrospectionNonNullTypeRef; + +export type IntrospectionOutputTypeRef = + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef + | IntrospectionNonNullTypeRef< + IntrospectionNamedTypeRef | IntrospectionListTypeRef + >; + +export type IntrospectionInputTypeRef = + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef + | IntrospectionNonNullTypeRef< + IntrospectionNamedTypeRef | IntrospectionListTypeRef + >; + +export interface IntrospectionNamedTypeRef { + readonly kind: T['kind']; + readonly name: string; +} + +export interface IntrospectionField { + readonly name: string; + readonly description?: Maybe; + readonly args: ReadonlyArray; + readonly type: IntrospectionOutputTypeRef; + readonly isDeprecated: boolean; + readonly deprecationReason: Maybe; +} + +export interface IntrospectionInputValue { + readonly name: string; + readonly description?: Maybe; + readonly type: IntrospectionInputTypeRef; + readonly defaultValue: Maybe; + readonly isDeprecated?: boolean; + readonly deprecationReason?: Maybe; +} + +export interface IntrospectionEnumValue { + readonly name: string; + readonly description?: Maybe; + readonly isDeprecated: boolean; + readonly deprecationReason: Maybe; +} + +export interface IntrospectionDirective { + readonly name: string; + readonly description?: Maybe; + readonly isRepeatable?: boolean; + readonly locations: ReadonlyArray; + readonly args: ReadonlyArray; +} diff --git a/packages/graphql/src/utilities/getOperationAST.ts b/packages/graphql/src/utilities/getOperationAST.ts new file mode 100644 index 00000000000..942d54b37b4 --- /dev/null +++ b/packages/graphql/src/utilities/getOperationAST.ts @@ -0,0 +1,31 @@ +import type { Maybe } from '../jsutils/Maybe.js'; +import type { DocumentNode, OperationDefinitionNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +/** + * Returns an operation AST given a document AST and optionally an operation + * name. If a name is not provided, an operation is only returned if only one is + * provided in the document. + */ +export function getOperationAST( + documentAST: DocumentNode, + operationName?: Maybe +): Maybe { + let operation = null; + for (const definition of documentAST.definitions) { + if (definition.kind === Kind.OPERATION_DEFINITION) { + if (operationName == null) { + // If no operation name was provided, only return an Operation if there + // is one defined in the document. Upon encountering the second, return + // null. + if (operation) { + return null; + } + operation = definition; + } else if (definition.name?.value === operationName) { + return definition; + } + } + } + return operation; +} diff --git a/packages/graphql/src/utilities/getOperationRootType.ts b/packages/graphql/src/utilities/getOperationRootType.ts new file mode 100644 index 00000000000..b63a6f3211d --- /dev/null +++ b/packages/graphql/src/utilities/getOperationRootType.ts @@ -0,0 +1,46 @@ +import { GraphQLError } from '../error/GraphQLError.js'; + +import type { OperationDefinitionNode, OperationTypeDefinitionNode } from '../language/ast.js'; + +import type { GraphQLObjectType } from '../type/definition.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +/** + * Extracts the root type of the operation from the schema. + * + * @deprecated Please use `GraphQLSchema.getRootType` instead. Will be removed in v17 + */ +export function getOperationRootType( + schema: GraphQLSchema, + operation: OperationDefinitionNode | OperationTypeDefinitionNode +): GraphQLObjectType { + if (operation.operation === 'query') { + const queryType = schema.getQueryType(); + if (!queryType) { + throw new GraphQLError('Schema does not define the required query root type.', { nodes: operation }); + } + return queryType; + } + + if (operation.operation === 'mutation') { + const mutationType = schema.getMutationType(); + if (!mutationType) { + throw new GraphQLError('Schema is not configured for mutations.', { + nodes: operation, + }); + } + return mutationType; + } + + if (operation.operation === 'subscription') { + const subscriptionType = schema.getSubscriptionType(); + if (!subscriptionType) { + throw new GraphQLError('Schema is not configured for subscriptions.', { + nodes: operation, + }); + } + return subscriptionType; + } + + throw new GraphQLError('Can only have query, mutation and subscription operations.', { nodes: operation }); +} diff --git a/packages/graphql/src/utilities/getRootTypeMap.ts b/packages/graphql/src/utilities/getRootTypeMap.ts new file mode 100644 index 00000000000..f387fa71f5b --- /dev/null +++ b/packages/graphql/src/utilities/getRootTypeMap.ts @@ -0,0 +1,26 @@ +import { memoize1 } from '../jsutils/memoize1.js'; +import { OperationTypeNode } from '../language/index.js'; +import { GraphQLSchema, GraphQLObjectType } from '../type/index.js'; + +export const getRootTypeMap = memoize1(function getRootTypeMap( + schema: GraphQLSchema +): Map { + const rootTypeMap: Map = new Map(); + + const queryType = schema.getQueryType(); + if (queryType) { + rootTypeMap.set('query' as OperationTypeNode, queryType); + } + + const mutationType = schema.getMutationType(); + if (mutationType) { + rootTypeMap.set('mutation' as OperationTypeNode, mutationType); + } + + const subscriptionType = schema.getSubscriptionType(); + if (subscriptionType) { + rootTypeMap.set('subscription' as OperationTypeNode, subscriptionType); + } + + return rootTypeMap; +}); diff --git a/packages/graphql/src/utilities/index.ts b/packages/graphql/src/utilities/index.ts new file mode 100644 index 00000000000..da0c573c630 --- /dev/null +++ b/packages/graphql/src/utilities/index.ts @@ -0,0 +1,28 @@ +export * from './addResolversToSchema.js'; +export * from './assertValidName.js'; +export * from './astFromSchema.js'; +export * from './astFromValue.js'; +export * from './buildASTSchema.js'; +export * from './buildClientSchema.js'; +export * from './coerceInputValue.js'; +export * from './concatAST.js'; +export * from './extendSchema.js'; +export * from './findBreakingChanges.js'; +export * from './getDocumentNodeFromSchema.js'; +export * from './getIntrospectionQuery.js'; +export * from './getOperationAST.js'; +export * from './getOperationRootType.js'; +export * from './getRootTypeMap.js'; +export * from './introspectionFromSchema.js'; +export * from './lexicographicSortSchema.js'; +export * from './printSchema.js'; +export * from './printSchemaWithDirectives.js'; +export * from './separateOperations.js'; +export * from './sortValueNode.js'; +export * from './stripIgnoredCharacters.js'; +export * from './typeComparators.js'; +export * from './typeFromAST.js'; +export * from './typedQueryDocumentNode.js'; +export * from './valueFromAST.js'; +export * from './valueFromASTUntyped.js'; +export * from './TypeInfo.js'; diff --git a/packages/graphql/src/utilities/introspectionFromSchema.ts b/packages/graphql/src/utilities/introspectionFromSchema.ts new file mode 100644 index 00000000000..fa2c0517949 --- /dev/null +++ b/packages/graphql/src/utilities/introspectionFromSchema.ts @@ -0,0 +1,34 @@ +import { invariant } from '../jsutils/invariant.js'; + +import { parse } from '../language/parser.js'; + +import type { GraphQLSchema } from '../type/schema.js'; + +import { executeSync } from '../execution/execute.js'; + +import type { IntrospectionOptions, IntrospectionQuery } from './getIntrospectionQuery.js'; +import { getIntrospectionQuery } from './getIntrospectionQuery.js'; + +/** + * Build an IntrospectionQuery from a GraphQLSchema + * + * IntrospectionQuery is useful for utilities that care about type and field + * relationships, but do not need to traverse through those relationships. + * + * This is the inverse of buildClientSchema. The primary use case is outside + * of the server context, for instance when doing schema comparisons. + */ +export function introspectionFromSchema(schema: GraphQLSchema, options?: IntrospectionOptions): IntrospectionQuery { + const optionsWithDefaults = { + specifiedByUrl: true, + directiveIsRepeatable: true, + schemaDescription: true, + inputValueDeprecation: true, + ...options, + }; + + const document = parse(getIntrospectionQuery(optionsWithDefaults)); + const result = executeSync({ schema, document }); + invariant(result.errors == null && result.data != null); + return result.data as any; +} diff --git a/packages/graphql/src/utilities/lexicographicSortSchema.ts b/packages/graphql/src/utilities/lexicographicSortSchema.ts new file mode 100644 index 00000000000..a35e60070fe --- /dev/null +++ b/packages/graphql/src/utilities/lexicographicSortSchema.ts @@ -0,0 +1,174 @@ +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; +import { keyValMap } from '../jsutils/keyValMap.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import { naturalCompare } from '../jsutils/naturalCompare.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; + +import type { + GraphQLFieldConfigArgumentMap, + GraphQLFieldConfigMap, + GraphQLInputFieldConfigMap, + GraphQLNamedType, + GraphQLType, +} from '../type/definition.js'; +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLUnionType, + isEnumType, + isInputObjectType, + isInterfaceType, + isListType, + isNonNullType, + isObjectType, + isScalarType, + isUnionType, +} from '../type/definition.js'; +import { GraphQLDirective } from '../type/directives.js'; +import { isIntrospectionType } from '../type/introspection.js'; +import { GraphQLSchema } from '../type/schema.js'; + +/** + * Sort GraphQLSchema. + * + * This function returns a sorted copy of the given GraphQLSchema. + */ +export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema { + const schemaConfig = schema.toConfig(); + const typeMap = keyValMap(sortByName(schemaConfig.types), type => type.name, sortNamedType); + + return new GraphQLSchema({ + ...schemaConfig, + types: Object.values(typeMap), + directives: sortByName(schemaConfig.directives).map(sortDirective), + query: replaceMaybeType(schemaConfig.query), + mutation: replaceMaybeType(schemaConfig.mutation), + subscription: replaceMaybeType(schemaConfig.subscription), + }); + + function replaceType(type: T): T { + if (isListType(type)) { + // @ts-expect-error + return new GraphQLList(replaceType(type.ofType)); + } else if (isNonNullType(type)) { + // @ts-expect-error + return new GraphQLNonNull(replaceType(type.ofType)); + } + // @ts-expect-error FIXME: TS Conversion + return replaceNamedType(type); + } + + function replaceNamedType(type: T): T { + return typeMap[type.name] as T; + } + + function replaceMaybeType(maybeType: Maybe): Maybe { + return maybeType && replaceNamedType(maybeType); + } + + function sortDirective(directive: GraphQLDirective) { + const config = directive.toConfig(); + return new GraphQLDirective({ + ...config, + locations: sortBy(config.locations, x => x), + args: sortArgs(config.args), + }); + } + + function sortArgs(args: GraphQLFieldConfigArgumentMap) { + return sortObjMap(args, arg => ({ + ...arg, + type: replaceType(arg.type), + })); + } + + function sortFields(fieldsMap: GraphQLFieldConfigMap) { + return sortObjMap(fieldsMap, field => ({ + ...field, + type: replaceType(field.type), + args: field.args && sortArgs(field.args), + })); + } + + function sortInputFields(fieldsMap: GraphQLInputFieldConfigMap) { + return sortObjMap(fieldsMap, field => ({ + ...field, + type: replaceType(field.type), + })); + } + + function sortTypes(array: ReadonlyArray): Array { + return sortByName(array).map(replaceNamedType); + } + + function sortNamedType(type: GraphQLNamedType): GraphQLNamedType { + if (isScalarType(type) || isIntrospectionType(type)) { + return type; + } + if (isObjectType(type)) { + const config = type.toConfig(); + return new GraphQLObjectType({ + ...config, + interfaces: () => sortTypes(config.interfaces), + fields: () => sortFields(config.fields), + }); + } + if (isInterfaceType(type)) { + const config = type.toConfig(); + return new GraphQLInterfaceType({ + ...config, + interfaces: () => sortTypes(config.interfaces), + fields: () => sortFields(config.fields), + }); + } + if (isUnionType(type)) { + const config = type.toConfig(); + return new GraphQLUnionType({ + ...config, + types: () => sortTypes(config.types), + }); + } + if (isEnumType(type)) { + const config = type.toConfig(); + return new GraphQLEnumType({ + ...config, + values: sortObjMap(config.values, value => value), + }); + } + if (isInputObjectType(type)) { + const config = type.toConfig(); + return new GraphQLInputObjectType({ + ...config, + fields: () => sortInputFields(config.fields), + }); + } + /* c8 ignore next 3 */ + // Not reachable, all possible types have been considered. + invariant(false, 'Unexpected type: ' + inspect(type)); + } +} + +function sortObjMap(map: ObjMap, sortValueFn: (value: T) => R): ObjMap { + const sortedMap = Object.create(null); + for (const key of Object.keys(map).sort(naturalCompare)) { + sortedMap[key] = sortValueFn(map[key]); + } + return sortedMap; +} + +function sortByName(array: ReadonlyArray): Array { + return sortBy(array, obj => obj.name); +} + +function sortBy(array: ReadonlyArray, mapToKey: (item: T) => string): Array { + return array.slice().sort((obj1, obj2) => { + const key1 = mapToKey(obj1); + const key2 = mapToKey(obj2); + return naturalCompare(key1, key2); + }); +} diff --git a/packages/graphql/src/utilities/printSchema.ts b/packages/graphql/src/utilities/printSchema.ts new file mode 100644 index 00000000000..8a1896f6ef2 --- /dev/null +++ b/packages/graphql/src/utilities/printSchema.ts @@ -0,0 +1,289 @@ +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; +import type { Maybe } from '../jsutils/Maybe.js'; + +import { isPrintableAsBlockString } from '../language/blockString.js'; +import { Kind } from '../language/kinds.js'; +import { print } from '../language/printer.js'; + +import type { + GraphQLArgument, + GraphQLEnumType, + GraphQLInputField, + GraphQLInputObjectType, + GraphQLInterfaceType, + GraphQLNamedType, + GraphQLObjectType, + GraphQLScalarType, + GraphQLUnionType, +} from '../type/definition.js'; +import { + isEnumType, + isInputObjectType, + isInterfaceType, + isObjectType, + isScalarType, + isUnionType, +} from '../type/definition.js'; +import type { GraphQLDirective } from '../type/directives.js'; +import { DEFAULT_DEPRECATION_REASON, isSpecifiedDirective } from '../type/directives.js'; +import { isIntrospectionType } from '../type/introspection.js'; +import { isSpecifiedScalarType } from '../type/scalars.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +import { astFromValue } from './astFromValue.js'; + +export function printSchema(schema: GraphQLSchema): string { + return printFilteredSchema(schema, n => !isSpecifiedDirective(n), isDefinedType); +} + +export function printIntrospectionSchema(schema: GraphQLSchema): string { + return printFilteredSchema(schema, isSpecifiedDirective, isIntrospectionType); +} + +function isDefinedType(type: GraphQLNamedType): boolean { + return !isSpecifiedScalarType(type) && !isIntrospectionType(type); +} + +function printFilteredSchema( + schema: GraphQLSchema, + directiveFilter: (type: GraphQLDirective) => boolean, + typeFilter: (type: GraphQLNamedType) => boolean +): string { + const directives = schema.getDirectives().filter(directiveFilter); + const types = Object.values(schema.getTypeMap()).filter(typeFilter); + + return [ + printSchemaDefinition(schema), + ...directives.map(directive => printDirective(directive)), + ...types.map(type => printType(type)), + ] + .filter(Boolean) + .join('\n\n'); +} + +function printSchemaDefinition(schema: GraphQLSchema): Maybe { + if (schema.description == null && isSchemaOfCommonNames(schema)) { + return; + } + + const operationTypes = []; + + const queryType = schema.getQueryType(); + if (queryType) { + operationTypes.push(` query: ${queryType.name}`); + } + + const mutationType = schema.getMutationType(); + if (mutationType) { + operationTypes.push(` mutation: ${mutationType.name}`); + } + + const subscriptionType = schema.getSubscriptionType(); + if (subscriptionType) { + operationTypes.push(` subscription: ${subscriptionType.name}`); + } + + return printDescription(schema) + `schema {\n${operationTypes.join('\n')}\n}`; +} + +/** + * GraphQL schema define root types for each type of operation. These types are + * the same as any other type and can be named in any manner, however there is + * a common naming convention: + * + * ```graphql + * schema { + * query: Query + * mutation: Mutation + * subscription: Subscription + * } + * ``` + * + * When using this naming convention, the schema description can be omitted. + */ +function isSchemaOfCommonNames(schema: GraphQLSchema): boolean { + const queryType = schema.getQueryType(); + if (queryType && queryType.name !== 'Query') { + return false; + } + + const mutationType = schema.getMutationType(); + if (mutationType && mutationType.name !== 'Mutation') { + return false; + } + + const subscriptionType = schema.getSubscriptionType(); + if (subscriptionType && subscriptionType.name !== 'Subscription') { + return false; + } + + return true; +} + +export function printType(type: GraphQLNamedType): string { + if (isScalarType(type)) { + return printScalar(type); + } + if (isObjectType(type)) { + return printObject(type); + } + if (isInterfaceType(type)) { + return printInterface(type); + } + if (isUnionType(type)) { + return printUnion(type); + } + if (isEnumType(type)) { + return printEnum(type); + } + if (isInputObjectType(type)) { + return printInputObject(type); + } + /* c8 ignore next 3 */ + // Not reachable, all possible types have been considered. + invariant(false, 'Unexpected type: ' + inspect(type)); +} + +function printScalar(type: GraphQLScalarType): string { + return printDescription(type) + `scalar ${type.name}` + printSpecifiedByURL(type); +} + +function printImplementedInterfaces(type: GraphQLObjectType | GraphQLInterfaceType): string { + const interfaces = type.getInterfaces(); + return interfaces.length ? ' implements ' + interfaces.map(i => i.name).join(' & ') : ''; +} + +function printObject(type: GraphQLObjectType): string { + return printDescription(type) + `type ${type.name}` + printImplementedInterfaces(type) + printFields(type); +} + +function printInterface(type: GraphQLInterfaceType): string { + return printDescription(type) + `interface ${type.name}` + printImplementedInterfaces(type) + printFields(type); +} + +function printUnion(type: GraphQLUnionType): string { + const types = type.getTypes(); + const possibleTypes = types.length ? ' = ' + types.join(' | ') : ''; + return printDescription(type) + 'union ' + type.name + possibleTypes; +} + +function printEnum(type: GraphQLEnumType): string { + const values = type + .getValues() + .map( + (value, i) => printDescription(value, ' ', !i) + ' ' + value.name + printDeprecated(value.deprecationReason) + ); + + return printDescription(type) + `enum ${type.name}` + printBlock(values); +} + +function printInputObject(type: GraphQLInputObjectType): string { + const fields = Object.values(type.getFields()).map( + (f, i) => printDescription(f, ' ', !i) + ' ' + printInputValue(f) + ); + return printDescription(type) + `input ${type.name}` + printBlock(fields); +} + +function printFields(type: GraphQLObjectType | GraphQLInterfaceType): string { + const fields = Object.values(type.getFields()).map( + (f, i) => + printDescription(f, ' ', !i) + + ' ' + + f.name + + printArgs(f.args, ' ') + + ': ' + + String(f.type) + + printDeprecated(f.deprecationReason) + ); + return printBlock(fields); +} + +function printBlock(items: ReadonlyArray): string { + return items.length !== 0 ? ' {\n' + items.join('\n') + '\n}' : ''; +} + +function printArgs(args: ReadonlyArray, indentation: string = ''): string { + if (args.length === 0) { + return ''; + } + + // If every arg does not have a description, print them on one line. + if (args.every(arg => !arg.description)) { + return '(' + args.map(printInputValue).join(', ') + ')'; + } + + return ( + '(\n' + + args + .map((arg, i) => printDescription(arg, ' ' + indentation, !i) + ' ' + indentation + printInputValue(arg)) + .join('\n') + + '\n' + + indentation + + ')' + ); +} + +function printInputValue(arg: GraphQLInputField): string { + const defaultAST = astFromValue(arg.defaultValue, arg.type); + let argDecl = arg.name + ': ' + String(arg.type); + if (defaultAST) { + argDecl += ` = ${print(defaultAST)}`; + } + return argDecl + printDeprecated(arg.deprecationReason); +} + +function printDirective(directive: GraphQLDirective): string { + return ( + printDescription(directive) + + 'directive @' + + directive.name + + printArgs(directive.args) + + (directive.isRepeatable ? ' repeatable' : '') + + ' on ' + + directive.locations.join(' | ') + ); +} + +function printDeprecated(reason: Maybe): string { + if (reason == null) { + return ''; + } + if (reason !== DEFAULT_DEPRECATION_REASON) { + const astValue = print({ kind: Kind.STRING, value: reason }); + return ` @deprecated(reason: ${astValue})`; + } + return ' @deprecated'; +} + +function printSpecifiedByURL(scalar: GraphQLScalarType): string { + if (scalar.specifiedByURL == null) { + return ''; + } + const astValue = print({ + kind: Kind.STRING, + value: scalar.specifiedByURL, + }); + return ` @specifiedBy(url: ${astValue})`; +} + +function printDescription( + def: { readonly description: Maybe }, + indentation: string = '', + firstInBlock: boolean = true +): string { + const { description } = def; + if (description == null) { + return ''; + } + + const blockString = print({ + kind: Kind.STRING, + value: description, + block: isPrintableAsBlockString(description), + }); + + const prefix = indentation && !firstInBlock ? '\n' + indentation : indentation; + + return prefix + blockString.replace(/\n/g, '\n' + indentation) + '\n'; +} diff --git a/packages/graphql/src/utilities/printSchemaWithDirectives.ts b/packages/graphql/src/utilities/printSchemaWithDirectives.ts new file mode 100644 index 00000000000..bd583d30e32 --- /dev/null +++ b/packages/graphql/src/utilities/printSchemaWithDirectives.ts @@ -0,0 +1,27 @@ +import { print } from '../language/index.js'; +import { GraphQLSchema } from '../type/index.js'; +import { getDocumentNodeFromSchema } from './getDocumentNodeFromSchema.js'; + +interface PrintSchemaWithDirectivesOptions { + /** + * Descriptions are defined as preceding string literals, however an older + * experimental version of the SDL supported preceding comments as + * descriptions. Set to true to enable this deprecated behavior. + * This option is provided to ease adoption and will be removed in v16. + * + * Default: false + */ + commentDescriptions?: boolean; + assumeValid?: boolean; + pathToDirectivesInExtensions?: Array; +} + +// this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible +// currently does not allow customization of printSchema options having to do with comments. +export function printSchemaWithDirectives( + schema: GraphQLSchema, + options: PrintSchemaWithDirectivesOptions = {} +): string { + const documentNode = getDocumentNodeFromSchema(schema, options); + return print(documentNode); +} diff --git a/packages/graphql/src/utilities/separateOperations.ts b/packages/graphql/src/utilities/separateOperations.ts new file mode 100644 index 00000000000..b977bbce2bc --- /dev/null +++ b/packages/graphql/src/utilities/separateOperations.ts @@ -0,0 +1,83 @@ +import type { ObjMap } from '../jsutils/ObjMap.js'; + +import type { DocumentNode, OperationDefinitionNode, SelectionSetNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; +import { visit } from '../language/visitor.js'; + +/** + * separateOperations accepts a single AST document which may contain many + * operations and fragments and returns a collection of AST documents each of + * which contains a single operation as well the fragment definitions it + * refers to. + */ +export function separateOperations(documentAST: DocumentNode): ObjMap { + const operations: Array = []; + const depGraph: DepGraph = Object.create(null); + + // Populate metadata and build a dependency graph. + for (const definitionNode of documentAST.definitions) { + switch (definitionNode.kind) { + case Kind.OPERATION_DEFINITION: + operations.push(definitionNode); + break; + case Kind.FRAGMENT_DEFINITION: + depGraph[definitionNode.name.value] = collectDependencies(definitionNode.selectionSet); + break; + default: + // ignore non-executable definitions + } + } + + // For each operation, produce a new synthesized AST which includes only what + // is necessary for completing that operation. + const separatedDocumentASTs = Object.create(null); + for (const operation of operations) { + const dependencies = new Set(); + + for (const fragmentName of collectDependencies(operation.selectionSet)) { + collectTransitiveDependencies(dependencies, depGraph, fragmentName); + } + + // Provides the empty string for anonymous operations. + const operationName = operation.name ? operation.name.value : ''; + + // The list of definition nodes to be included for this operation, sorted + // to retain the same order as the original document. + separatedDocumentASTs[operationName] = { + kind: Kind.DOCUMENT, + definitions: documentAST.definitions.filter( + node => node === operation || (node.kind === Kind.FRAGMENT_DEFINITION && dependencies.has(node.name.value)) + ), + }; + } + + return separatedDocumentASTs; +} + +type DepGraph = ObjMap>; + +// From a dependency graph, collects a list of transitive dependencies by +// recursing through a dependency graph. +function collectTransitiveDependencies(collected: Set, depGraph: DepGraph, fromName: string): void { + if (!collected.has(fromName)) { + collected.add(fromName); + + const immediateDeps = depGraph[fromName]; + if (immediateDeps !== undefined) { + for (const toName of immediateDeps) { + collectTransitiveDependencies(collected, depGraph, toName); + } + } + } +} + +function collectDependencies(selectionSet: SelectionSetNode): Array { + const dependencies: Array = []; + + visit(selectionSet, { + FragmentSpread(node) { + dependencies.push(node.name.value); + }, + }); + return dependencies; +} diff --git a/packages/graphql/src/utilities/sortValueNode.ts b/packages/graphql/src/utilities/sortValueNode.ts new file mode 100644 index 00000000000..9ae223d7033 --- /dev/null +++ b/packages/graphql/src/utilities/sortValueNode.ts @@ -0,0 +1,43 @@ +import { naturalCompare } from '../jsutils/naturalCompare.js'; + +import type { ObjectFieldNode, ValueNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +/** + * Sort ValueNode. + * + * This function returns a sorted copy of the given ValueNode. + * + * @internal + */ +export function sortValueNode(valueNode: ValueNode): ValueNode { + switch (valueNode.kind) { + case Kind.OBJECT: + return { + ...valueNode, + fields: sortFields(valueNode.fields), + }; + case Kind.LIST: + return { + ...valueNode, + values: valueNode.values.map(sortValueNode), + }; + case Kind.INT: + case Kind.FLOAT: + case Kind.STRING: + case Kind.BOOLEAN: + case Kind.NULL: + case Kind.ENUM: + case Kind.VARIABLE: + return valueNode; + } +} + +function sortFields(fields: ReadonlyArray): Array { + return fields + .map(fieldNode => ({ + ...fieldNode, + value: sortValueNode(fieldNode.value), + })) + .sort((fieldA, fieldB) => naturalCompare(fieldA.name.value, fieldB.name.value)); +} diff --git a/packages/graphql/src/utilities/stripIgnoredCharacters.ts b/packages/graphql/src/utilities/stripIgnoredCharacters.ts new file mode 100644 index 00000000000..8c0acf1522e --- /dev/null +++ b/packages/graphql/src/utilities/stripIgnoredCharacters.ts @@ -0,0 +1,101 @@ +import { printBlockString } from '../language/blockString.js'; +import { isPunctuatorTokenKind, Lexer } from '../language/lexer.js'; +import { isSource, Source } from '../language/source.js'; +import { TokenKind } from '../language/tokenKind.js'; + +/** + * Strips characters that are not significant to the validity or execution + * of a GraphQL document: + * - UnicodeBOM + * - WhiteSpace + * - LineTerminator + * - Comment + * - Comma + * - BlockString indentation + * + * Note: It is required to have a delimiter character between neighboring + * non-punctuator tokens and this function always uses single space as delimiter. + * + * It is guaranteed that both input and output documents if parsed would result + * in the exact same AST except for nodes location. + * + * Warning: It is guaranteed that this function will always produce stable results. + * However, it's not guaranteed that it will stay the same between different + * releases due to bugfixes or changes in the GraphQL specification. + * + * Query example: + * + * ```graphql + * query SomeQuery($foo: String!, $bar: String) { + * someField(foo: $foo, bar: $bar) { + * a + * b { + * c + * d + * } + * } + * } + * ``` + * + * Becomes: + * + * ```graphql + * query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}} + * ``` + * + * SDL example: + * + * ```graphql + * """ + * Type description + * """ + * type Foo { + * """ + * Field description + * """ + * bar: String + * } + * ``` + * + * Becomes: + * + * ```graphql + * """Type description""" type Foo{"""Field description""" bar:String} + * ``` + */ +export function stripIgnoredCharacters(source: string | Source): string { + const sourceObj = isSource(source) ? source : new Source(source); + + const body = sourceObj.body; + const lexer = new Lexer(sourceObj); + let strippedBody = ''; + + let wasLastAddedTokenNonPunctuator = false; + while (lexer.advance().kind !== TokenKind.EOF) { + const currentToken = lexer.token; + const tokenKind = currentToken.kind; + + /** + * Every two non-punctuator tokens should have space between them. + * Also prevent case of non-punctuator token following by spread resulting + * in invalid token (e.g. `1...` is invalid Float token). + */ + const isNonPunctuator = !isPunctuatorTokenKind(currentToken.kind); + if (wasLastAddedTokenNonPunctuator) { + if (isNonPunctuator || currentToken.kind === TokenKind.SPREAD) { + strippedBody += ' '; + } + } + + const tokenBody = body.slice(currentToken.start, currentToken.end); + if (tokenKind === TokenKind.BLOCK_STRING) { + strippedBody += printBlockString(currentToken.value, { minimize: true }); + } else { + strippedBody += tokenBody; + } + + wasLastAddedTokenNonPunctuator = isNonPunctuator; + } + + return strippedBody; +} diff --git a/packages/graphql/src/utilities/typeComparators.ts b/packages/graphql/src/utilities/typeComparators.ts new file mode 100644 index 00000000000..4a1d0a88241 --- /dev/null +++ b/packages/graphql/src/utilities/typeComparators.ts @@ -0,0 +1,107 @@ +import type { GraphQLCompositeType, GraphQLType } from '../type/definition.js'; +import { isAbstractType, isInterfaceType, isListType, isNonNullType, isObjectType } from '../type/definition.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +/** + * Provided two types, return true if the types are equal (invariant). + */ +export function isEqualType(typeA: GraphQLType, typeB: GraphQLType): boolean { + // Equivalent types are equal. + if (typeA === typeB) { + return true; + } + + // If either type is non-null, the other must also be non-null. + if (isNonNullType(typeA) && isNonNullType(typeB)) { + return isEqualType(typeA.ofType, typeB.ofType); + } + + // If either type is a list, the other must also be a list. + if (isListType(typeA) && isListType(typeB)) { + return isEqualType(typeA.ofType, typeB.ofType); + } + + // Otherwise the types are not equal. + return false; +} + +/** + * Provided a type and a super type, return true if the first type is either + * equal or a subset of the second super type (covariant). + */ +export function isTypeSubTypeOf(schema: GraphQLSchema, maybeSubType: GraphQLType, superType: GraphQLType): boolean { + // Equivalent type is a valid subtype + if (maybeSubType === superType) { + return true; + } + + // If superType is non-null, maybeSubType must also be non-null. + if (isNonNullType(superType)) { + if (isNonNullType(maybeSubType)) { + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType); + } + return false; + } + if (isNonNullType(maybeSubType)) { + // If superType is nullable, maybeSubType may be non-null or nullable. + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType); + } + + // If superType type is a list, maybeSubType type must also be a list. + if (isListType(superType)) { + if (isListType(maybeSubType)) { + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType); + } + return false; + } + if (isListType(maybeSubType)) { + // If superType is not a list, maybeSubType must also be not a list. + return false; + } + + // If superType type is an abstract type, check if it is super type of maybeSubType. + // Otherwise, the child type is not a valid subtype of the parent type. + return ( + isAbstractType(superType) && + (isInterfaceType(maybeSubType) || isObjectType(maybeSubType)) && + schema.isSubType(superType, maybeSubType) + ); +} + +/** + * Provided two composite types, determine if they "overlap". Two composite + * types overlap when the Sets of possible concrete types for each intersect. + * + * This is often used to determine if a fragment of a given type could possibly + * be visited in a context of another type. + * + * This function is commutative. + */ +export function doTypesOverlap( + schema: GraphQLSchema, + typeA: GraphQLCompositeType, + typeB: GraphQLCompositeType +): boolean { + // Equivalent types overlap + if (typeA === typeB) { + return true; + } + + if (isAbstractType(typeA)) { + if (isAbstractType(typeB)) { + // If both types are abstract, then determine if there is any intersection + // between possible concrete types of each. + return schema.getPossibleTypes(typeA).some(type => schema.isSubType(typeB, type)); + } + // Determine if the latter type is a possible concrete type of the former. + return schema.isSubType(typeA, typeB); + } + + if (isAbstractType(typeB)) { + // Determine if the former type is a possible concrete type of the latter. + return schema.isSubType(typeB, typeA); + } + + // Otherwise the types do not overlap. + return false; +} diff --git a/packages/graphql/src/utilities/typeFromAST.ts b/packages/graphql/src/utilities/typeFromAST.ts new file mode 100644 index 00000000000..670c17735de --- /dev/null +++ b/packages/graphql/src/utilities/typeFromAST.ts @@ -0,0 +1,32 @@ +import type { ListTypeNode, NamedTypeNode, NonNullTypeNode, TypeNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +import type { GraphQLNamedType, GraphQLNullableType, GraphQLType } from '../type/definition.js'; +import { GraphQLList, GraphQLNonNull } from '../type/definition.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +/** + * Given a Schema and an AST node describing a type, return a GraphQLType + * definition which applies to that type. For example, if provided the parsed + * AST node for `[User]`, a GraphQLList instance will be returned, containing + * the type called "User" found in the schema. If a type called "User" is not + * found in the schema, then undefined will be returned. + */ +export function typeFromAST(schema: GraphQLSchema, typeNode: NamedTypeNode): GraphQLNamedType | undefined; +export function typeFromAST(schema: GraphQLSchema, typeNode: ListTypeNode): GraphQLList | undefined; +export function typeFromAST(schema: GraphQLSchema, typeNode: NonNullTypeNode): GraphQLNonNull | undefined; +export function typeFromAST(schema: GraphQLSchema, typeNode: TypeNode): GraphQLType | undefined; +export function typeFromAST(schema: GraphQLSchema, typeNode: TypeNode): GraphQLType | undefined { + switch (typeNode.kind) { + case Kind.LIST_TYPE: { + const innerType = typeFromAST(schema, typeNode.type); + return innerType && new GraphQLList(innerType); + } + case Kind.NON_NULL_TYPE: { + const innerType = typeFromAST(schema, typeNode.type) as GraphQLNullableType; + return innerType && new GraphQLNonNull(innerType); + } + case Kind.NAMED_TYPE: + return schema.getType(typeNode.name.value); + } +} diff --git a/packages/graphql/src/utilities/typedQueryDocumentNode.ts b/packages/graphql/src/utilities/typedQueryDocumentNode.ts new file mode 100644 index 00000000000..65e67a36258 --- /dev/null +++ b/packages/graphql/src/utilities/typedQueryDocumentNode.ts @@ -0,0 +1,17 @@ +import type { DocumentNode, ExecutableDefinitionNode } from '../language/ast.js'; +/** + * Wrapper type that contains DocumentNode and types that can be deduced from it. + */ +export interface TypedQueryDocumentNode< + TResponseData = { [key: string]: any }, + TRequestVariables = { [key: string]: any } +> extends DocumentNode { + readonly definitions: ReadonlyArray; + // FIXME: remove once TS implements proper way to enforce nominal typing + /** + * This type is used to ensure that the variables you pass in to the query are assignable to Variables + * and that the Result is assignable to whatever you pass your result to. The method is never actually + * implemented, but the type is valid because we list it as optional + */ + __ensureTypesOfVariablesAndResultMatching?: (variables: TRequestVariables) => TResponseData; +} diff --git a/packages/graphql/src/utilities/valueFromAST.ts b/packages/graphql/src/utilities/valueFromAST.ts new file mode 100644 index 00000000000..56dfe8b8ae4 --- /dev/null +++ b/packages/graphql/src/utilities/valueFromAST.ts @@ -0,0 +1,150 @@ +import { inspect } from '../jsutils/inspect.js'; +import { invariant } from '../jsutils/invariant.js'; +import { keyMap } from '../jsutils/keyMap.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; + +import type { ValueNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +import type { GraphQLInputType } from '../type/definition.js'; +import { isInputObjectType, isLeafType, isListType, isNonNullType } from '../type/definition.js'; + +/** + * Produces a JavaScript value given a GraphQL Value AST. + * + * A GraphQL type must be provided, which will be used to interpret different + * GraphQL Value literals. + * + * Returns `undefined` when the value could not be validly coerced according to + * the provided type. + * + * | GraphQL Value | JSON Value | + * | -------------------- | ------------- | + * | Input Object | Object | + * | List | Array | + * | Boolean | Boolean | + * | String | String | + * | Int / Float | Number | + * | Enum Value | Unknown | + * | NullValue | null | + * + */ +export function valueFromAST( + valueNode: Maybe, + type: GraphQLInputType, + variables?: Maybe> +): unknown { + if (!valueNode) { + // When there is no node, then there is also no value. + // Importantly, this is different from returning the value null. + return; + } + + if (valueNode.kind === Kind.VARIABLE) { + const variableName = valueNode.name.value; + if (variables == null || variables[variableName] === undefined) { + // No valid return value. + return; + } + const variableValue = variables[variableName]; + if (variableValue === null && isNonNullType(type)) { + return; // Invalid: intentionally return no value. + } + // Note: This does no further checking that this variable is correct. + // This assumes that this query has been validated and the variable + // usage here is of the correct type. + return variableValue; + } + + if (isNonNullType(type)) { + if (valueNode.kind === Kind.NULL) { + return; // Invalid: intentionally return no value. + } + return valueFromAST(valueNode, type.ofType, variables); + } + + if (valueNode.kind === Kind.NULL) { + // This is explicitly returning the value null. + return null; + } + + if (isListType(type)) { + const itemType = type.ofType; + if (valueNode.kind === Kind.LIST) { + const coercedValues = []; + for (const itemNode of valueNode.values) { + if (isMissingVariable(itemNode, variables)) { + // If an array contains a missing variable, it is either coerced to + // null or if the item type is non-null, it considered invalid. + if (isNonNullType(itemType)) { + return; // Invalid: intentionally return no value. + } + coercedValues.push(null); + } else { + const itemValue = valueFromAST(itemNode, itemType, variables); + if (itemValue === undefined) { + return; // Invalid: intentionally return no value. + } + coercedValues.push(itemValue); + } + } + return coercedValues; + } + const coercedValue = valueFromAST(valueNode, itemType, variables); + if (coercedValue === undefined) { + return; // Invalid: intentionally return no value. + } + return [coercedValue]; + } + + if (isInputObjectType(type)) { + if (valueNode.kind !== Kind.OBJECT) { + return; // Invalid: intentionally return no value. + } + const coercedObj = Object.create(null); + const fieldNodes = keyMap(valueNode.fields, field => field.name.value); + for (const field of Object.values(type.getFields())) { + const fieldNode = fieldNodes[field.name]; + if (!fieldNode || isMissingVariable(fieldNode.value, variables)) { + if (field.defaultValue !== undefined) { + coercedObj[field.name] = field.defaultValue; + } else if (isNonNullType(field.type)) { + return; // Invalid: intentionally return no value. + } + continue; + } + const fieldValue = valueFromAST(fieldNode.value, field.type, variables); + if (fieldValue === undefined) { + return; // Invalid: intentionally return no value. + } + coercedObj[field.name] = fieldValue; + } + return coercedObj; + } + + if (isLeafType(type)) { + // Scalars and Enums fulfill parsing a literal value via parseLiteral(). + // Invalid values represent a failure to parse correctly, in which case + // no value is returned. + let result; + try { + result = type.parseLiteral(valueNode, variables); + } catch (_error) { + return; // Invalid: intentionally return no value. + } + if (result === undefined) { + return; // Invalid: intentionally return no value. + } + return result; + } + /* c8 ignore next 3 */ + // Not reachable, all possible input types have been considered. + invariant(false, 'Unexpected input type: ' + inspect(type)); +} + +// Returns true if the provided valueNode is a variable which is not defined +// in the set of variables. +function isMissingVariable(valueNode: ValueNode, variables: Maybe>): boolean { + return valueNode.kind === Kind.VARIABLE && (variables == null || variables[valueNode.name.value] === undefined); +} diff --git a/packages/graphql/src/utilities/valueFromASTUntyped.ts b/packages/graphql/src/utilities/valueFromASTUntyped.ts new file mode 100644 index 00000000000..5eae980648f --- /dev/null +++ b/packages/graphql/src/utilities/valueFromASTUntyped.ts @@ -0,0 +1,47 @@ +import { keyValMap } from '../jsutils/keyValMap.js'; +import type { Maybe } from '../jsutils/Maybe.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; + +import type { ValueNode } from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; + +/** + * Produces a JavaScript value given a GraphQL Value AST. + * + * Unlike `valueFromAST()`, no type is provided. The resulting JavaScript value + * will reflect the provided GraphQL value AST. + * + * | GraphQL Value | JavaScript Value | + * | -------------------- | ---------------- | + * | Input Object | Object | + * | List | Array | + * | Boolean | Boolean | + * | String / Enum | String | + * | Int / Float | Number | + * | Null | null | + * + */ +export function valueFromASTUntyped(valueNode: ValueNode, variables?: Maybe>): unknown { + switch (valueNode.kind) { + case Kind.NULL: + return null; + case Kind.INT: + return parseInt(valueNode.value, 10); + case Kind.FLOAT: + return parseFloat(valueNode.value); + case Kind.STRING: + case Kind.ENUM: + case Kind.BOOLEAN: + return valueNode.value; + case Kind.LIST: + return valueNode.values.map(node => valueFromASTUntyped(node, variables)); + case Kind.OBJECT: + return keyValMap( + valueNode.fields, + field => field.name.value, + field => valueFromASTUntyped(field.value, variables) + ); + case Kind.VARIABLE: + return variables?.[valueNode.name.value]; + } +} diff --git a/packages/graphql/src/validation/ValidationContext.ts b/packages/graphql/src/validation/ValidationContext.ts new file mode 100644 index 00000000000..dcc54ec6fc8 --- /dev/null +++ b/packages/graphql/src/validation/ValidationContext.ts @@ -0,0 +1,245 @@ +import type { Maybe } from '../jsutils/Maybe.js'; +import type { ObjMap } from '../jsutils/ObjMap.js'; + +import type { GraphQLError } from '../error/GraphQLError.js'; + +import type { + DocumentNode, + FragmentDefinitionNode, + FragmentSpreadNode, + OperationDefinitionNode, + SelectionSetNode, + VariableNode, +} from '../language/ast.js'; +import { Kind } from '../language/kinds.js'; +import type { ASTVisitor } from '../language/visitor.js'; +import { visit } from '../language/visitor.js'; + +import type { + GraphQLArgument, + GraphQLCompositeType, + GraphQLEnumValue, + GraphQLField, + GraphQLInputType, + GraphQLOutputType, +} from '../type/definition.js'; +import type { GraphQLDirective } from '../type/directives.js'; +import type { GraphQLSchema } from '../type/schema.js'; + +import { TypeInfo, visitWithTypeInfo } from '../utilities/TypeInfo.js'; + +type NodeWithSelectionSet = OperationDefinitionNode | FragmentDefinitionNode; +interface VariableUsage { + readonly node: VariableNode; + readonly type: Maybe; + readonly defaultValue: Maybe; +} + +/** + * An instance of this class is passed as the "this" context to all validators, + * allowing access to commonly useful contextual information from within a + * validation rule. + */ +export class ASTValidationContext { + private _ast: DocumentNode; + private _onError: (error: GraphQLError) => void; + private _fragments: ObjMap | undefined; + private _fragmentSpreads: Map>; + private _recursivelyReferencedFragments: Map>; + + constructor(ast: DocumentNode, onError: (error: GraphQLError) => void) { + this._ast = ast; + this._fragments = undefined; + this._fragmentSpreads = new Map(); + this._recursivelyReferencedFragments = new Map(); + this._onError = onError; + } + + get [Symbol.toStringTag]() { + return 'ASTValidationContext'; + } + + reportError(error: GraphQLError): void { + this._onError(error); + } + + getDocument(): DocumentNode { + return this._ast; + } + + getFragment(name: string): Maybe { + let fragments: ObjMap; + if (this._fragments) { + fragments = this._fragments; + } else { + fragments = Object.create(null); + for (const defNode of this.getDocument().definitions) { + if (defNode.kind === Kind.FRAGMENT_DEFINITION) { + fragments[defNode.name.value] = defNode; + } + } + this._fragments = fragments; + } + return fragments[name]; + } + + getFragmentSpreads(node: SelectionSetNode): ReadonlyArray { + let spreads = this._fragmentSpreads.get(node); + if (!spreads) { + spreads = []; + const setsToVisit: Array = [node]; + let set: SelectionSetNode | undefined; + while ((set = setsToVisit.pop())) { + for (const selection of set.selections) { + if (selection.kind === Kind.FRAGMENT_SPREAD) { + spreads.push(selection); + } else if (selection.selectionSet) { + setsToVisit.push(selection.selectionSet); + } + } + } + this._fragmentSpreads.set(node, spreads); + } + return spreads; + } + + getRecursivelyReferencedFragments(operation: OperationDefinitionNode): ReadonlyArray { + let fragments = this._recursivelyReferencedFragments.get(operation); + if (!fragments) { + fragments = []; + const collectedNames = Object.create(null); + const nodesToVisit: Array = [operation.selectionSet]; + let node: SelectionSetNode | undefined; + while ((node = nodesToVisit.pop())) { + for (const spread of this.getFragmentSpreads(node)) { + const fragName = spread.name.value; + if (collectedNames[fragName] !== true) { + collectedNames[fragName] = true; + const fragment = this.getFragment(fragName); + if (fragment) { + fragments.push(fragment); + nodesToVisit.push(fragment.selectionSet); + } + } + } + } + this._recursivelyReferencedFragments.set(operation, fragments); + } + return fragments; + } +} + +export type ASTValidationRule = (context: ASTValidationContext) => ASTVisitor; + +export class SDLValidationContext extends ASTValidationContext { + private _schema: Maybe; + + constructor(ast: DocumentNode, schema: Maybe, onError: (error: GraphQLError) => void) { + super(ast, onError); + this._schema = schema; + } + + get [Symbol.toStringTag]() { + return 'SDLValidationContext'; + } + + getSchema(): Maybe { + return this._schema; + } +} + +export type SDLValidationRule = (context: SDLValidationContext) => ASTVisitor; + +export class ValidationContext extends ASTValidationContext { + private _schema: GraphQLSchema; + private _typeInfo: TypeInfo; + private _variableUsages: Map>; + + private _recursiveVariableUsages: Map>; + + constructor(schema: GraphQLSchema, ast: DocumentNode, typeInfo: TypeInfo, onError: (error: GraphQLError) => void) { + super(ast, onError); + this._schema = schema; + this._typeInfo = typeInfo; + this._variableUsages = new Map(); + this._recursiveVariableUsages = new Map(); + } + + get [Symbol.toStringTag]() { + return 'ValidationContext'; + } + + getSchema(): GraphQLSchema { + return this._schema; + } + + getVariableUsages(node: NodeWithSelectionSet): ReadonlyArray { + let usages = this._variableUsages.get(node); + if (!usages) { + const newUsages: Array = []; + const typeInfo = new TypeInfo(this._schema); + visit( + node, + visitWithTypeInfo(typeInfo, { + VariableDefinition: () => false, + Variable(variable) { + newUsages.push({ + node: variable, + type: typeInfo.getInputType(), + defaultValue: typeInfo.getDefaultValue(), + }); + }, + }) + ); + usages = newUsages; + this._variableUsages.set(node, usages); + } + return usages; + } + + getRecursiveVariableUsages(operation: OperationDefinitionNode): ReadonlyArray { + let usages = this._recursiveVariableUsages.get(operation); + if (!usages) { + usages = this.getVariableUsages(operation); + for (const frag of this.getRecursivelyReferencedFragments(operation)) { + usages = usages.concat(this.getVariableUsages(frag)); + } + this._recursiveVariableUsages.set(operation, usages); + } + return usages; + } + + getType(): Maybe { + return this._typeInfo.getType(); + } + + getParentType(): Maybe { + return this._typeInfo.getParentType(); + } + + getInputType(): Maybe { + return this._typeInfo.getInputType(); + } + + getParentInputType(): Maybe { + return this._typeInfo.getParentInputType(); + } + + getFieldDef(): Maybe> { + return this._typeInfo.getFieldDef(); + } + + getDirective(): Maybe { + return this._typeInfo.getDirective(); + } + + getArgument(): Maybe { + return this._typeInfo.getArgument(); + } + + getEnumValue(): Maybe { + return this._typeInfo.getEnumValue(); + } +} + +export type ValidationRule = (context: ValidationContext) => ASTVisitor; diff --git a/packages/graphql/src/validation/__tests__/ExecutableDefinitionsRule-test.ts b/packages/graphql/src/validation/__tests__/ExecutableDefinitionsRule-test.ts new file mode 100644 index 00000000000..94fcb2d49e2 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/ExecutableDefinitionsRule-test.ts @@ -0,0 +1,92 @@ +import { ExecutableDefinitionsRule } from '../rules/ExecutableDefinitionsRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(ExecutableDefinitionsRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Executable definitions', () => { + it('with only operation', () => { + expectValid(` + query Foo { + dog { + name + } + } + `); + }); + + it('with operation and fragment', () => { + expectValid(` + query Foo { + dog { + name + ...Frag + } + } + + fragment Frag on Dog { + name + } + `); + }); + + it('with type definition', () => { + expectErrors(` + query Foo { + dog { + name + } + } + + type Cow { + name: String + } + + extend type Dog { + color: String + } + `).toDeepEqual([ + { + message: 'The "Cow" definition is not executable.', + locations: [{ line: 8, column: 7 }], + }, + { + message: 'The "Dog" definition is not executable.', + locations: [{ line: 12, column: 7 }], + }, + ]); + }); + + it('with schema definition', () => { + expectErrors(` + schema { + query: Query + } + + type Query { + test: String + } + + extend schema @directive + `).toDeepEqual([ + { + message: 'The schema definition is not executable.', + locations: [{ line: 2, column: 7 }], + }, + { + message: 'The "Query" definition is not executable.', + locations: [{ line: 6, column: 7 }], + }, + { + message: 'The schema definition is not executable.', + locations: [{ line: 10, column: 7 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/FieldsOnCorrectTypeRule-test.ts b/packages/graphql/src/validation/__tests__/FieldsOnCorrectTypeRule-test.ts new file mode 100644 index 00000000000..bcd9e31b6de --- /dev/null +++ b/packages/graphql/src/validation/__tests__/FieldsOnCorrectTypeRule-test.ts @@ -0,0 +1,430 @@ +import { parse } from '../../language/parser.js'; + +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { FieldsOnCorrectTypeRule } from '../rules/FieldsOnCorrectTypeRule.js'; +import { validate } from '../validate.js'; + +import { expectValidationErrorsWithSchema } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrorsWithSchema(testSchema, FieldsOnCorrectTypeRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +const testSchema = buildSchema(` + interface Pet { + name: String + } + + type Dog implements Pet { + name: String + nickname: String + barkVolume: Int + } + + type Cat implements Pet { + name: String + nickname: String + meowVolume: Int + } + + union CatOrDog = Cat | Dog + + type Human { + name: String + pets: [Pet] + } + + type Query { + human: Human + } +`); + +describe('Validate: Fields on correct type', () => { + it('Object field selection', () => { + expectValid(` + fragment objectFieldSelection on Dog { + __typename + name + } + `); + }); + + it('Aliased object field selection', () => { + expectValid(` + fragment aliasedObjectFieldSelection on Dog { + tn : __typename + otherName : name + } + `); + }); + + it('Interface field selection', () => { + expectValid(` + fragment interfaceFieldSelection on Pet { + __typename + name + } + `); + }); + + it('Aliased interface field selection', () => { + expectValid(` + fragment interfaceFieldSelection on Pet { + otherName : name + } + `); + }); + + it('Lying alias selection', () => { + expectValid(` + fragment lyingAliasSelection on Dog { + name : nickname + } + `); + }); + + it('Ignores fields on unknown type', () => { + expectValid(` + fragment unknownSelection on UnknownType { + unknownField + } + `); + }); + + it('reports errors when type is known again', () => { + expectErrors(` + fragment typeKnownAgain on Pet { + unknown_pet_field { + ... on Cat { + unknown_cat_field + } + } + } + `).toDeepEqual([ + { + message: 'Cannot query field "unknown_pet_field" on type "Pet".', + locations: [{ line: 3, column: 9 }], + }, + { + message: 'Cannot query field "unknown_cat_field" on type "Cat".', + locations: [{ line: 5, column: 13 }], + }, + ]); + }); + + it('Field not defined on fragment', () => { + expectErrors(` + fragment fieldNotDefined on Dog { + meowVolume + } + `).toDeepEqual([ + { + message: 'Cannot query field "meowVolume" on type "Dog". Did you mean "barkVolume"?', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('Ignores deeply unknown field', () => { + expectErrors(` + fragment deepFieldNotDefined on Dog { + unknown_field { + deeper_unknown_field + } + } + `).toDeepEqual([ + { + message: 'Cannot query field "unknown_field" on type "Dog".', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('Sub-field not defined', () => { + expectErrors(` + fragment subFieldNotDefined on Human { + pets { + unknown_field + } + } + `).toDeepEqual([ + { + message: 'Cannot query field "unknown_field" on type "Pet".', + locations: [{ line: 4, column: 11 }], + }, + ]); + }); + + it('Field not defined on inline fragment', () => { + expectErrors(` + fragment fieldNotDefined on Pet { + ... on Dog { + meowVolume + } + } + `).toDeepEqual([ + { + message: 'Cannot query field "meowVolume" on type "Dog". Did you mean "barkVolume"?', + locations: [{ line: 4, column: 11 }], + }, + ]); + }); + + it('Aliased field target not defined', () => { + expectErrors(` + fragment aliasedFieldTargetNotDefined on Dog { + volume : mooVolume + } + `).toDeepEqual([ + { + message: 'Cannot query field "mooVolume" on type "Dog". Did you mean "barkVolume"?', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('Aliased lying field target not defined', () => { + expectErrors(` + fragment aliasedLyingFieldTargetNotDefined on Dog { + barkVolume : kawVolume + } + `).toDeepEqual([ + { + message: 'Cannot query field "kawVolume" on type "Dog". Did you mean "barkVolume"?', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('Not defined on interface', () => { + expectErrors(` + fragment notDefinedOnInterface on Pet { + tailLength + } + `).toDeepEqual([ + { + message: 'Cannot query field "tailLength" on type "Pet".', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('Defined on implementors but not on interface', () => { + expectErrors(` + fragment definedOnImplementorsButNotInterface on Pet { + nickname + } + `).toDeepEqual([ + { + message: + 'Cannot query field "nickname" on type "Pet". Did you mean to use an inline fragment on "Cat" or "Dog"?', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('Meta field selection on union', () => { + expectValid(` + fragment directFieldSelectionOnUnion on CatOrDog { + __typename + } + `); + }); + + it('Direct field selection on union', () => { + expectErrors(` + fragment directFieldSelectionOnUnion on CatOrDog { + directField + } + `).toDeepEqual([ + { + message: 'Cannot query field "directField" on type "CatOrDog".', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('Defined on implementors queried on union', () => { + expectErrors(` + fragment definedOnImplementorsQueriedOnUnion on CatOrDog { + name + } + `).toDeepEqual([ + { + message: + 'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Pet", "Cat", or "Dog"?', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('valid field in inline fragment', () => { + expectValid(` + fragment objectFieldSelection on Pet { + ... on Dog { + name + } + ... { + name + } + } + `); + }); + + describe('Fields on correct type error message', () => { + function expectErrorMessage(schema: GraphQLSchema, queryStr: string) { + const errors = validate(schema, parse(queryStr), [FieldsOnCorrectTypeRule]); + expect(errors.length).toEqual(1); + return expect(errors[0].message); + } + + it('Works with no suggestions', () => { + const schema = buildSchema(` + type T { + fieldWithVeryLongNameThatWillNeverBeSuggested: String + } + type Query { t: T } + `); + + expectErrorMessage(schema, '{ t { f } }').toEqual('Cannot query field "f" on type "T".'); + }); + + it('Works with no small numbers of type suggestions', () => { + const schema = buildSchema(` + union T = A | B + type Query { t: T } + + type A { f: String } + type B { f: String } + `); + + expectErrorMessage(schema, '{ t { f } }').toEqual( + 'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A" or "B"?' + ); + }); + + it('Works with no small numbers of field suggestions', () => { + const schema = buildSchema(` + type T { + y: String + z: String + } + type Query { t: T } + `); + + expectErrorMessage(schema, '{ t { f } }').toEqual('Cannot query field "f" on type "T". Did you mean "y" or "z"?'); + }); + + it('Only shows one set of suggestions at a time, preferring types', () => { + const schema = buildSchema(` + interface T { + y: String + z: String + } + type Query { t: T } + + type A implements T { + f: String + y: String + z: String + } + type B implements T { + f: String + y: String + z: String + } + `); + + expectErrorMessage(schema, '{ t { f } }').toEqual( + 'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A" or "B"?' + ); + }); + + it('Sort type suggestions based on inheritance order', () => { + const interfaceSchema = buildSchema(` + interface T { bar: String } + type Query { t: T } + + interface Z implements T { + foo: String + bar: String + } + + interface Y implements Z & T { + foo: String + bar: String + } + + type X implements Y & Z & T { + foo: String + bar: String + } + `); + + expectErrorMessage(interfaceSchema, '{ t { foo } }').toEqual( + 'Cannot query field "foo" on type "T". Did you mean to use an inline fragment on "Z", "Y", or "X"?' + ); + + const unionSchema = buildSchema(` + interface Animal { name: String } + interface Mammal implements Animal { name: String } + + interface Canine implements Animal & Mammal { name: String } + type Dog implements Animal & Mammal & Canine { name: String } + + interface Feline implements Animal & Mammal { name: String } + type Cat implements Animal & Mammal & Feline { name: String } + + union CatOrDog = Cat | Dog + type Query { catOrDog: CatOrDog } + `); + + expectErrorMessage(unionSchema, '{ catOrDog { name } }').toEqual( + 'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Animal", "Mammal", "Canine", "Dog", or "Feline"?' + ); + }); + + it('Limits lots of type suggestions', () => { + const schema = buildSchema(` + union T = A | B | C | D | E | F + type Query { t: T } + + type A { f: String } + type B { f: String } + type C { f: String } + type D { f: String } + type E { f: String } + type F { f: String } + `); + + expectErrorMessage(schema, '{ t { f } }').toEqual( + 'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A", "B", "C", "D", or "E"?' + ); + }); + + it('Limits lots of field suggestions', () => { + const schema = buildSchema(` + type T { + u: String + v: String + w: String + x: String + y: String + z: String + } + type Query { t: T } + `); + + expectErrorMessage(schema, '{ t { f } }').toEqual( + 'Cannot query field "f" on type "T". Did you mean "u", "v", "w", "x", or "y"?' + ); + }); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/FragmentsOnCompositeTypesRule-test.ts b/packages/graphql/src/validation/__tests__/FragmentsOnCompositeTypesRule-test.ts new file mode 100644 index 00000000000..8bfd865c7b8 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/FragmentsOnCompositeTypesRule-test.ts @@ -0,0 +1,121 @@ +import { FragmentsOnCompositeTypesRule } from '../rules/FragmentsOnCompositeTypesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(FragmentsOnCompositeTypesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Fragments on composite types', () => { + it('object is valid fragment type', () => { + expectValid(` + fragment validFragment on Dog { + barks + } + `); + }); + + it('interface is valid fragment type', () => { + expectValid(` + fragment validFragment on Pet { + name + } + `); + }); + + it('object is valid inline fragment type', () => { + expectValid(` + fragment validFragment on Pet { + ... on Dog { + barks + } + } + `); + }); + + it('interface is valid inline fragment type', () => { + expectValid(` + fragment validFragment on Mammal { + ... on Canine { + name + } + } + `); + }); + + it('inline fragment without type is valid', () => { + expectValid(` + fragment validFragment on Pet { + ... { + name + } + } + `); + }); + + it('union is valid fragment type', () => { + expectValid(` + fragment validFragment on CatOrDog { + __typename + } + `); + }); + + it('scalar is invalid fragment type', () => { + expectErrors(` + fragment scalarFragment on Boolean { + bad + } + `).toDeepEqual([ + { + message: 'Fragment "scalarFragment" cannot condition on non composite type "Boolean".', + locations: [{ line: 2, column: 34 }], + }, + ]); + }); + + it('enum is invalid fragment type', () => { + expectErrors(` + fragment scalarFragment on FurColor { + bad + } + `).toDeepEqual([ + { + message: 'Fragment "scalarFragment" cannot condition on non composite type "FurColor".', + locations: [{ line: 2, column: 34 }], + }, + ]); + }); + + it('input object is invalid fragment type', () => { + expectErrors(` + fragment inputFragment on ComplexInput { + stringField + } + `).toDeepEqual([ + { + message: 'Fragment "inputFragment" cannot condition on non composite type "ComplexInput".', + locations: [{ line: 2, column: 33 }], + }, + ]); + }); + + it('scalar is invalid inline fragment type', () => { + expectErrors(` + fragment invalidFragment on Pet { + ... on String { + barks + } + } + `).toDeepEqual([ + { + message: 'Fragment cannot condition on non composite type "String".', + locations: [{ line: 3, column: 16 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/KnownArgumentNamesRule-test.ts b/packages/graphql/src/validation/__tests__/KnownArgumentNamesRule-test.ts new file mode 100644 index 00000000000..5b25f2a6ba7 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/KnownArgumentNamesRule-test.ts @@ -0,0 +1,317 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { KnownArgumentNamesOnDirectivesRule, KnownArgumentNamesRule } from '../rules/KnownArgumentNamesRule.js'; + +import { expectSDLValidationErrors, expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(KnownArgumentNamesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, KnownArgumentNamesOnDirectivesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string) { + expectSDLErrors(sdlStr).toDeepEqual([]); +} + +describe('Validate: Known argument names', () => { + it('single arg is known', () => { + expectValid(` + fragment argOnRequiredArg on Dog { + doesKnowCommand(dogCommand: SIT) + } + `); + }); + + it('multiple args are known', () => { + expectValid(` + fragment multipleArgs on ComplicatedArgs { + multipleReqs(req1: 1, req2: 2) + } + `); + }); + + it('ignores args of unknown fields', () => { + expectValid(` + fragment argOnUnknownField on Dog { + unknownField(unknownArg: SIT) + } + `); + }); + + it('multiple args in reverse order are known', () => { + expectValid(` + fragment multipleArgsReverseOrder on ComplicatedArgs { + multipleReqs(req2: 2, req1: 1) + } + `); + }); + + it('no args on optional arg', () => { + expectValid(` + fragment noArgOnOptionalArg on Dog { + isHouseTrained + } + `); + }); + + it('args are known deeply', () => { + expectValid(` + { + dog { + doesKnowCommand(dogCommand: SIT) + } + human { + pet { + ... on Dog { + doesKnowCommand(dogCommand: SIT) + } + } + } + } + `); + }); + + it('directive args are known', () => { + expectValid(` + { + dog @skip(if: true) + } + `); + }); + + it('field args are invalid', () => { + expectErrors(` + { + dog @skip(unless: true) + } + `).toDeepEqual([ + { + message: 'Unknown argument "unless" on directive "@skip".', + locations: [{ line: 3, column: 19 }], + }, + ]); + }); + + it('directive without args is valid', () => { + expectValid(` + { + dog @onField + } + `); + }); + + it('arg passed to directive without arg is reported', () => { + expectErrors(` + { + dog @onField(if: true) + } + `).toDeepEqual([ + { + message: 'Unknown argument "if" on directive "@onField".', + locations: [{ line: 3, column: 22 }], + }, + ]); + }); + + it('misspelled directive args are reported', () => { + expectErrors(` + { + dog @skip(iff: true) + } + `).toDeepEqual([ + { + message: 'Unknown argument "iff" on directive "@skip". Did you mean "if"?', + locations: [{ line: 3, column: 19 }], + }, + ]); + }); + + it('invalid arg name', () => { + expectErrors(` + fragment invalidArgName on Dog { + doesKnowCommand(unknown: true) + } + `).toDeepEqual([ + { + message: 'Unknown argument "unknown" on field "Dog.doesKnowCommand".', + locations: [{ line: 3, column: 25 }], + }, + ]); + }); + + it('misspelled arg name is reported', () => { + expectErrors(` + fragment invalidArgName on Dog { + doesKnowCommand(DogCommand: true) + } + `).toDeepEqual([ + { + message: 'Unknown argument "DogCommand" on field "Dog.doesKnowCommand". Did you mean "dogCommand"?', + locations: [{ line: 3, column: 25 }], + }, + ]); + }); + + it('unknown args amongst known args', () => { + expectErrors(` + fragment oneGoodArgOneInvalidArg on Dog { + doesKnowCommand(whoKnows: 1, dogCommand: SIT, unknown: true) + } + `).toDeepEqual([ + { + message: 'Unknown argument "whoKnows" on field "Dog.doesKnowCommand".', + locations: [{ line: 3, column: 25 }], + }, + { + message: 'Unknown argument "unknown" on field "Dog.doesKnowCommand".', + locations: [{ line: 3, column: 55 }], + }, + ]); + }); + + it('unknown args deeply', () => { + expectErrors(` + { + dog { + doesKnowCommand(unknown: true) + } + human { + pet { + ... on Dog { + doesKnowCommand(unknown: true) + } + } + } + } + `).toDeepEqual([ + { + message: 'Unknown argument "unknown" on field "Dog.doesKnowCommand".', + locations: [{ line: 4, column: 27 }], + }, + { + message: 'Unknown argument "unknown" on field "Dog.doesKnowCommand".', + locations: [{ line: 9, column: 31 }], + }, + ]); + }); + + describe('within SDL', () => { + it('known arg on directive defined inside SDL', () => { + expectValidSDL(` + type Query { + foo: String @test(arg: "") + } + + directive @test(arg: String) on FIELD_DEFINITION + `); + }); + + it('unknown arg on directive defined inside SDL', () => { + expectSDLErrors(` + type Query { + foo: String @test(unknown: "") + } + + directive @test(arg: String) on FIELD_DEFINITION + `).toDeepEqual([ + { + message: 'Unknown argument "unknown" on directive "@test".', + locations: [{ line: 3, column: 29 }], + }, + ]); + }); + + it('misspelled arg name is reported on directive defined inside SDL', () => { + expectSDLErrors(` + type Query { + foo: String @test(agr: "") + } + + directive @test(arg: String) on FIELD_DEFINITION + `).toDeepEqual([ + { + message: 'Unknown argument "agr" on directive "@test". Did you mean "arg"?', + locations: [{ line: 3, column: 29 }], + }, + ]); + }); + + it('unknown arg on standard directive', () => { + expectSDLErrors(` + type Query { + foo: String @deprecated(unknown: "") + } + `).toDeepEqual([ + { + message: 'Unknown argument "unknown" on directive "@deprecated".', + locations: [{ line: 3, column: 35 }], + }, + ]); + }); + + it('unknown arg on overridden standard directive', () => { + expectSDLErrors(` + type Query { + foo: String @deprecated(reason: "") + } + directive @deprecated(arg: String) on FIELD + `).toDeepEqual([ + { + message: 'Unknown argument "reason" on directive "@deprecated".', + locations: [{ line: 3, column: 35 }], + }, + ]); + }); + + it('unknown arg on directive defined in schema extension', () => { + const schema = buildSchema(` + type Query { + foo: String + } + `); + expectSDLErrors( + ` + directive @test(arg: String) on OBJECT + + extend type Query @test(unknown: "") + `, + schema + ).toDeepEqual([ + { + message: 'Unknown argument "unknown" on directive "@test".', + locations: [{ line: 4, column: 36 }], + }, + ]); + }); + + it('unknown arg on directive used in schema extension', () => { + const schema = buildSchema(` + directive @test(arg: String) on OBJECT + + type Query { + foo: String + } + `); + expectSDLErrors( + ` + extend type Query @test(unknown: "") + `, + schema + ).toDeepEqual([ + { + message: 'Unknown argument "unknown" on directive "@test".', + locations: [{ line: 2, column: 35 }], + }, + ]); + }); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/KnownDirectivesRule-test.ts b/packages/graphql/src/validation/__tests__/KnownDirectivesRule-test.ts new file mode 100644 index 00000000000..7ac6cad29e0 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/KnownDirectivesRule-test.ts @@ -0,0 +1,438 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { KnownDirectivesRule } from '../rules/KnownDirectivesRule.js'; + +import { expectSDLValidationErrors, expectValidationErrorsWithSchema } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrorsWithSchema(schemaWithDirectives, KnownDirectivesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, KnownDirectivesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +const schemaWithDirectives = buildSchema(` + type Query { + dummy: String + } + + directive @onQuery on QUERY + directive @onMutation on MUTATION + directive @onSubscription on SUBSCRIPTION + directive @onField on FIELD + directive @onFragmentDefinition on FRAGMENT_DEFINITION + directive @onFragmentSpread on FRAGMENT_SPREAD + directive @onInlineFragment on INLINE_FRAGMENT + directive @onVariableDefinition on VARIABLE_DEFINITION +`); + +const schemaWithSDLDirectives = buildSchema(` + directive @onSchema on SCHEMA + directive @onScalar on SCALAR + directive @onObject on OBJECT + directive @onFieldDefinition on FIELD_DEFINITION + directive @onArgumentDefinition on ARGUMENT_DEFINITION + directive @onInterface on INTERFACE + directive @onUnion on UNION + directive @onEnum on ENUM + directive @onEnumValue on ENUM_VALUE + directive @onInputObject on INPUT_OBJECT + directive @onInputFieldDefinition on INPUT_FIELD_DEFINITION +`); + +describe('Validate: Known directives', () => { + it('with no directives', () => { + expectValid(` + query Foo { + name + ...Frag + } + + fragment Frag on Dog { + name + } + `); + }); + + it('with standard directives', () => { + expectValid(` + { + human @skip(if: false) { + name + pets { + ... on Dog @include(if: true) { + name + } + } + } + } + `); + }); + + it('with unknown directive', () => { + expectErrors(` + { + human @unknown(directive: "value") { + name + } + } + `).toDeepEqual([ + { + message: 'Unknown directive "@unknown".', + locations: [{ line: 3, column: 15 }], + }, + ]); + }); + + it('with many unknown directives', () => { + expectErrors(` + { + __typename @unknown + human @unknown { + name + pets @unknown { + name + } + } + } + `).toDeepEqual([ + { + message: 'Unknown directive "@unknown".', + locations: [{ line: 3, column: 20 }], + }, + { + message: 'Unknown directive "@unknown".', + locations: [{ line: 4, column: 15 }], + }, + { + message: 'Unknown directive "@unknown".', + locations: [{ line: 6, column: 16 }], + }, + ]); + }); + + it('with well placed directives', () => { + expectValid(` + query ($var: Boolean @onVariableDefinition) @onQuery { + human @onField { + ...Frag @onFragmentSpread + ... @onInlineFragment { + name @onField + } + } + } + + mutation @onMutation { + someField @onField + } + + subscription @onSubscription { + someField @onField + } + + fragment Frag on Human @onFragmentDefinition { + name @onField + } + `); + }); + + it('with misplaced directives', () => { + expectErrors(` + query ($var: Boolean @onQuery) @onMutation { + human @onQuery { + ...Frag @onQuery + ... @onQuery { + name @onQuery + } + } + } + + mutation @onQuery { + someField @onQuery + } + + subscription @onQuery { + someField @onQuery + } + + fragment Frag on Human @onQuery { + name @onQuery + } + `).toDeepEqual([ + { + message: 'Directive "@onQuery" may not be used on VARIABLE_DEFINITION.', + locations: [{ line: 2, column: 28 }], + }, + { + message: 'Directive "@onMutation" may not be used on QUERY.', + locations: [{ line: 2, column: 38 }], + }, + { + message: 'Directive "@onQuery" may not be used on FIELD.', + locations: [{ line: 3, column: 15 }], + }, + { + message: 'Directive "@onQuery" may not be used on FRAGMENT_SPREAD.', + locations: [{ line: 4, column: 19 }], + }, + { + message: 'Directive "@onQuery" may not be used on INLINE_FRAGMENT.', + locations: [{ line: 5, column: 15 }], + }, + { + message: 'Directive "@onQuery" may not be used on FIELD.', + locations: [{ line: 6, column: 18 }], + }, + { + message: 'Directive "@onQuery" may not be used on MUTATION.', + locations: [{ line: 11, column: 16 }], + }, + { + message: 'Directive "@onQuery" may not be used on FIELD.', + locations: [{ column: 19, line: 12 }], + }, + { + message: 'Directive "@onQuery" may not be used on SUBSCRIPTION.', + locations: [{ column: 20, line: 15 }], + }, + { + message: 'Directive "@onQuery" may not be used on FIELD.', + locations: [{ column: 19, line: 16 }], + }, + { + message: 'Directive "@onQuery" may not be used on FRAGMENT_DEFINITION.', + locations: [{ column: 30, line: 19 }], + }, + { + message: 'Directive "@onQuery" may not be used on FIELD.', + locations: [{ column: 14, line: 20 }], + }, + ]); + }); + + describe('within SDL', () => { + it('with directive defined inside SDL', () => { + expectValidSDL(` + type Query { + foo: String @test + } + + directive @test on FIELD_DEFINITION + `); + }); + + it('with standard directive', () => { + expectValidSDL(` + type Query { + foo: String @deprecated + } + `); + }); + + it('with overridden standard directive', () => { + expectValidSDL(` + schema @deprecated { + query: Query + } + directive @deprecated on SCHEMA + `); + }); + + it('with directive defined in schema extension', () => { + const schema = buildSchema(` + type Query { + foo: String + } + `); + expectValidSDL( + ` + directive @test on OBJECT + + extend type Query @test + `, + schema + ); + }); + + it('with directive used in schema extension', () => { + const schema = buildSchema(` + directive @test on OBJECT + + type Query { + foo: String + } + `); + expectValidSDL( + ` + extend type Query @test + `, + schema + ); + }); + + it('with unknown directive in schema extension', () => { + const schema = buildSchema(` + type Query { + foo: String + } + `); + expectSDLErrors( + ` + extend type Query @unknown + `, + schema + ).toDeepEqual([ + { + message: 'Unknown directive "@unknown".', + locations: [{ line: 2, column: 29 }], + }, + ]); + }); + + it('with well placed directives', () => { + expectValidSDL( + ` + type MyObj implements MyInterface @onObject { + myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition + } + + extend type MyObj @onObject + + scalar MyScalar @onScalar + + extend scalar MyScalar @onScalar + + interface MyInterface @onInterface { + myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition + } + + extend interface MyInterface @onInterface + + union MyUnion @onUnion = MyObj | Other + + extend union MyUnion @onUnion + + enum MyEnum @onEnum { + MY_VALUE @onEnumValue + } + + extend enum MyEnum @onEnum + + input MyInput @onInputObject { + myField: Int @onInputFieldDefinition + } + + extend input MyInput @onInputObject + + schema @onSchema { + query: MyQuery + } + + extend schema @onSchema + `, + schemaWithSDLDirectives + ); + }); + + it('with misplaced directives', () => { + expectSDLErrors( + ` + type MyObj implements MyInterface @onInterface { + myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition + } + + scalar MyScalar @onEnum + + interface MyInterface @onObject { + myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition + } + + union MyUnion @onEnumValue = MyObj | Other + + enum MyEnum @onScalar { + MY_VALUE @onUnion + } + + input MyInput @onEnum { + myField: Int @onArgumentDefinition + } + + schema @onObject { + query: MyQuery + } + + extend schema @onObject + `, + schemaWithSDLDirectives + ).toDeepEqual([ + { + message: 'Directive "@onInterface" may not be used on OBJECT.', + locations: [{ line: 2, column: 45 }], + }, + { + message: 'Directive "@onInputFieldDefinition" may not be used on ARGUMENT_DEFINITION.', + locations: [{ line: 3, column: 32 }], + }, + { + message: 'Directive "@onInputFieldDefinition" may not be used on FIELD_DEFINITION.', + locations: [{ line: 3, column: 65 }], + }, + { + message: 'Directive "@onEnum" may not be used on SCALAR.', + locations: [{ line: 6, column: 27 }], + }, + { + message: 'Directive "@onObject" may not be used on INTERFACE.', + locations: [{ line: 8, column: 33 }], + }, + { + message: 'Directive "@onInputFieldDefinition" may not be used on ARGUMENT_DEFINITION.', + locations: [{ line: 9, column: 32 }], + }, + { + message: 'Directive "@onInputFieldDefinition" may not be used on FIELD_DEFINITION.', + locations: [{ line: 9, column: 65 }], + }, + { + message: 'Directive "@onEnumValue" may not be used on UNION.', + locations: [{ line: 12, column: 25 }], + }, + { + message: 'Directive "@onScalar" may not be used on ENUM.', + locations: [{ line: 14, column: 23 }], + }, + { + message: 'Directive "@onUnion" may not be used on ENUM_VALUE.', + locations: [{ line: 15, column: 22 }], + }, + { + message: 'Directive "@onEnum" may not be used on INPUT_OBJECT.', + locations: [{ line: 18, column: 25 }], + }, + { + message: 'Directive "@onArgumentDefinition" may not be used on INPUT_FIELD_DEFINITION.', + locations: [{ line: 19, column: 26 }], + }, + { + message: 'Directive "@onObject" may not be used on SCHEMA.', + locations: [{ line: 22, column: 18 }], + }, + { + message: 'Directive "@onObject" may not be used on SCHEMA.', + locations: [{ line: 26, column: 25 }], + }, + ]); + }); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/KnownFragmentNamesRule-test.ts b/packages/graphql/src/validation/__tests__/KnownFragmentNamesRule-test.ts new file mode 100644 index 00000000000..ee1a6c9a449 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/KnownFragmentNamesRule-test.ts @@ -0,0 +1,69 @@ +import { KnownFragmentNamesRule } from '../rules/KnownFragmentNamesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(KnownFragmentNamesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Known fragment names', () => { + it('known fragment names are valid', () => { + expectValid(` + { + human(id: 4) { + ...HumanFields1 + ... on Human { + ...HumanFields2 + } + ... { + name + } + } + } + fragment HumanFields1 on Human { + name + ...HumanFields3 + } + fragment HumanFields2 on Human { + name + } + fragment HumanFields3 on Human { + name + } + `); + }); + + it('unknown fragment names are invalid', () => { + expectErrors(` + { + human(id: 4) { + ...UnknownFragment1 + ... on Human { + ...UnknownFragment2 + } + } + } + fragment HumanFields on Human { + name + ...UnknownFragment3 + } + `).toDeepEqual([ + { + message: 'Unknown fragment "UnknownFragment1".', + locations: [{ line: 4, column: 14 }], + }, + { + message: 'Unknown fragment "UnknownFragment2".', + locations: [{ line: 6, column: 16 }], + }, + { + message: 'Unknown fragment "UnknownFragment3".', + locations: [{ line: 12, column: 12 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/KnownTypeNamesRule-test.ts b/packages/graphql/src/validation/__tests__/KnownTypeNamesRule-test.ts new file mode 100644 index 00000000000..2d1f21137ac --- /dev/null +++ b/packages/graphql/src/validation/__tests__/KnownTypeNamesRule-test.ts @@ -0,0 +1,356 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { KnownTypeNamesRule } from '../rules/KnownTypeNamesRule.js'; + +import { expectSDLValidationErrors, expectValidationErrors, expectValidationErrorsWithSchema } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(KnownTypeNamesRule, queryStr); +} + +function expectErrorsWithSchema(schema: GraphQLSchema, queryStr: string) { + return expectValidationErrorsWithSchema(schema, KnownTypeNamesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, KnownTypeNamesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +describe('Validate: Known type names', () => { + it('known type names are valid', () => { + expectValid(` + query Foo( + $var: String + $required: [Int!]! + $introspectionType: __EnumValue + ) { + user(id: 4) { + pets { ... on Pet { name }, ...PetFields, ... { name } } + } + } + + fragment PetFields on Pet { + name + } + `); + }); + + it('unknown type names are invalid', () => { + expectErrors(` + query Foo($var: [JumbledUpLetters!]!) { + user(id: 4) { + name + pets { ... on Badger { name }, ...PetFields } + } + } + fragment PetFields on Peat { + name + } + `).toDeepEqual([ + { + message: 'Unknown type "JumbledUpLetters".', + locations: [{ line: 2, column: 24 }], + }, + { + message: 'Unknown type "Badger".', + locations: [{ line: 5, column: 25 }], + }, + { + message: 'Unknown type "Peat". Did you mean "Pet" or "Cat"?', + locations: [{ line: 8, column: 29 }], + }, + ]); + }); + + it('references to standard scalars that are missing in schema', () => { + const schema = buildSchema('type Query { foo: String }'); + const query = ` + query ($id: ID, $float: Float, $int: Int) { + __typename + } + `; + expectErrorsWithSchema(schema, query).toDeepEqual([ + { + message: 'Unknown type "ID".', + locations: [{ line: 2, column: 19 }], + }, + { + message: 'Unknown type "Float".', + locations: [{ line: 2, column: 31 }], + }, + { + message: 'Unknown type "Int".', + locations: [{ line: 2, column: 44 }], + }, + ]); + }); + + describe('within SDL', () => { + it('use standard types', () => { + expectValidSDL(` + type Query { + string: String + int: Int + float: Float + boolean: Boolean + id: ID + introspectionType: __EnumValue + } + `); + }); + + it('reference types defined inside the same document', () => { + expectValidSDL(` + union SomeUnion = SomeObject | AnotherObject + + type SomeObject implements SomeInterface { + someScalar(arg: SomeInputObject): SomeScalar + } + + type AnotherObject { + foo(arg: SomeInputObject): String + } + + type SomeInterface { + someScalar(arg: SomeInputObject): SomeScalar + } + + input SomeInputObject { + someScalar: SomeScalar + } + + scalar SomeScalar + + type RootQuery { + someInterface: SomeInterface + someUnion: SomeUnion + someScalar: SomeScalar + someObject: SomeObject + } + + schema { + query: RootQuery + } + `); + }); + + it('unknown type references', () => { + expectSDLErrors(` + type A + type B + + type SomeObject implements C { + e(d: D): E + } + + union SomeUnion = F | G + + interface SomeInterface { + i(h: H): I + } + + input SomeInput { + j: J + } + + directive @SomeDirective(k: K) on QUERY + + schema { + query: L + mutation: M + subscription: N + } + `).toDeepEqual([ + { + message: 'Unknown type "C". Did you mean "A" or "B"?', + locations: [{ line: 5, column: 36 }], + }, + { + message: 'Unknown type "D". Did you mean "A", "B", or "ID"?', + locations: [{ line: 6, column: 16 }], + }, + { + message: 'Unknown type "E". Did you mean "A" or "B"?', + locations: [{ line: 6, column: 20 }], + }, + { + message: 'Unknown type "F". Did you mean "A" or "B"?', + locations: [{ line: 9, column: 27 }], + }, + { + message: 'Unknown type "G". Did you mean "A" or "B"?', + locations: [{ line: 9, column: 31 }], + }, + { + message: 'Unknown type "H". Did you mean "A" or "B"?', + locations: [{ line: 12, column: 16 }], + }, + { + message: 'Unknown type "I". Did you mean "A", "B", or "ID"?', + locations: [{ line: 12, column: 20 }], + }, + { + message: 'Unknown type "J". Did you mean "A" or "B"?', + locations: [{ line: 16, column: 14 }], + }, + { + message: 'Unknown type "K". Did you mean "A" or "B"?', + locations: [{ line: 19, column: 37 }], + }, + { + message: 'Unknown type "L". Did you mean "A" or "B"?', + locations: [{ line: 22, column: 18 }], + }, + { + message: 'Unknown type "M". Did you mean "A" or "B"?', + locations: [{ line: 23, column: 21 }], + }, + { + message: 'Unknown type "N". Did you mean "A" or "B"?', + locations: [{ line: 24, column: 25 }], + }, + ]); + }); + + it('does not consider non-type definitions', () => { + expectSDLErrors(` + query Foo { __typename } + fragment Foo on Query { __typename } + directive @Foo on QUERY + + type Query { + foo: Foo + } + `).toDeepEqual([ + { + message: 'Unknown type "Foo".', + locations: [{ line: 7, column: 16 }], + }, + ]); + }); + + it('reference standard types inside extension document', () => { + const schema = buildSchema('type Foo'); + const sdl = ` + type SomeType { + string: String + int: Int + float: Float + boolean: Boolean + id: ID + introspectionType: __EnumValue + } + `; + + expectValidSDL(sdl, schema); + }); + + it('reference types inside extension document', () => { + const schema = buildSchema('type Foo'); + const sdl = ` + type QueryRoot { + foo: Foo + bar: Bar + } + + scalar Bar + + schema { + query: QueryRoot + } + `; + + expectValidSDL(sdl, schema); + }); + + it('unknown type references inside extension document', () => { + const schema = buildSchema('type A'); + const sdl = ` + type B + + type SomeObject implements C { + e(d: D): E + } + + union SomeUnion = F | G + + interface SomeInterface { + i(h: H): I + } + + input SomeInput { + j: J + } + + directive @SomeDirective(k: K) on QUERY + + schema { + query: L + mutation: M + subscription: N + } + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: 'Unknown type "C". Did you mean "A" or "B"?', + locations: [{ line: 4, column: 36 }], + }, + { + message: 'Unknown type "D". Did you mean "A", "B", or "ID"?', + locations: [{ line: 5, column: 16 }], + }, + { + message: 'Unknown type "E". Did you mean "A" or "B"?', + locations: [{ line: 5, column: 20 }], + }, + { + message: 'Unknown type "F". Did you mean "A" or "B"?', + locations: [{ line: 8, column: 27 }], + }, + { + message: 'Unknown type "G". Did you mean "A" or "B"?', + locations: [{ line: 8, column: 31 }], + }, + { + message: 'Unknown type "H". Did you mean "A" or "B"?', + locations: [{ line: 11, column: 16 }], + }, + { + message: 'Unknown type "I". Did you mean "A", "B", or "ID"?', + locations: [{ line: 11, column: 20 }], + }, + { + message: 'Unknown type "J". Did you mean "A" or "B"?', + locations: [{ line: 15, column: 14 }], + }, + { + message: 'Unknown type "K". Did you mean "A" or "B"?', + locations: [{ line: 18, column: 37 }], + }, + { + message: 'Unknown type "L". Did you mean "A" or "B"?', + locations: [{ line: 21, column: 18 }], + }, + { + message: 'Unknown type "M". Did you mean "A" or "B"?', + locations: [{ line: 22, column: 21 }], + }, + { + message: 'Unknown type "N". Did you mean "A" or "B"?', + locations: [{ line: 23, column: 25 }], + }, + ]); + }); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/LoneAnonymousOperationRule-test.ts b/packages/graphql/src/validation/__tests__/LoneAnonymousOperationRule-test.ts new file mode 100644 index 00000000000..627d787975b --- /dev/null +++ b/packages/graphql/src/validation/__tests__/LoneAnonymousOperationRule-test.ts @@ -0,0 +1,104 @@ +import { LoneAnonymousOperationRule } from '../rules/LoneAnonymousOperationRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(LoneAnonymousOperationRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Anonymous operation must be alone', () => { + it('no operations', () => { + expectValid(` + fragment fragA on Type { + field + } + `); + }); + + it('one anon operation', () => { + expectValid(` + { + field + } + `); + }); + + it('multiple named operations', () => { + expectValid(` + query Foo { + field + } + + query Bar { + field + } + `); + }); + + it('anon operation with fragment', () => { + expectValid(` + { + ...Foo + } + fragment Foo on Type { + field + } + `); + }); + + it('multiple anon operations', () => { + expectErrors(` + { + fieldA + } + { + fieldB + } + `).toDeepEqual([ + { + message: 'This anonymous operation must be the only defined operation.', + locations: [{ line: 2, column: 7 }], + }, + { + message: 'This anonymous operation must be the only defined operation.', + locations: [{ line: 5, column: 7 }], + }, + ]); + }); + + it('anon operation with a mutation', () => { + expectErrors(` + { + fieldA + } + mutation Foo { + fieldB + } + `).toDeepEqual([ + { + message: 'This anonymous operation must be the only defined operation.', + locations: [{ line: 2, column: 7 }], + }, + ]); + }); + + it('anon operation with a subscription', () => { + expectErrors(` + { + fieldA + } + subscription Foo { + fieldB + } + `).toDeepEqual([ + { + message: 'This anonymous operation must be the only defined operation.', + locations: [{ line: 2, column: 7 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/LoneSchemaDefinitionRule-test.ts b/packages/graphql/src/validation/__tests__/LoneSchemaDefinitionRule-test.ts new file mode 100644 index 00000000000..d2bfae67808 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/LoneSchemaDefinitionRule-test.ts @@ -0,0 +1,156 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { LoneSchemaDefinitionRule } from '../rules/LoneSchemaDefinitionRule.js'; + +import { expectSDLValidationErrors } from './harness.js'; + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, LoneSchemaDefinitionRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +describe('Validate: Schema definition should be alone', () => { + it('no schema', () => { + expectValidSDL(` + type Query { + foo: String + } + `); + }); + + it('one schema definition', () => { + expectValidSDL(` + schema { + query: Foo + } + + type Foo { + foo: String + } + `); + }); + + it('multiple schema definitions', () => { + expectSDLErrors(` + schema { + query: Foo + } + + type Foo { + foo: String + } + + schema { + mutation: Foo + } + + schema { + subscription: Foo + } + `).toDeepEqual([ + { + message: 'Must provide only one schema definition.', + locations: [{ line: 10, column: 7 }], + }, + { + message: 'Must provide only one schema definition.', + locations: [{ line: 14, column: 7 }], + }, + ]); + }); + + it('define schema in schema extension', () => { + const schema = buildSchema(` + type Foo { + foo: String + } + `); + + expectSDLErrors( + ` + schema { + query: Foo + } + `, + schema + ).toDeepEqual([]); + }); + + it('redefine schema in schema extension', () => { + const schema = buildSchema(` + schema { + query: Foo + } + + type Foo { + foo: String + } + `); + + expectSDLErrors( + ` + schema { + mutation: Foo + } + `, + schema + ).toDeepEqual([ + { + message: 'Cannot define a new schema within a schema extension.', + locations: [{ line: 2, column: 9 }], + }, + ]); + }); + + it('redefine implicit schema in schema extension', () => { + const schema = buildSchema(` + type Query { + fooField: Foo + } + + type Foo { + foo: String + } + `); + + expectSDLErrors( + ` + schema { + mutation: Foo + } + `, + schema + ).toDeepEqual([ + { + message: 'Cannot define a new schema within a schema extension.', + locations: [{ line: 2, column: 9 }], + }, + ]); + }); + + it('extend schema in schema extension', () => { + const schema = buildSchema(` + type Query { + fooField: Foo + } + + type Foo { + foo: String + } + `); + + expectValidSDL( + ` + extend schema { + mutation: Foo + } + `, + schema + ); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/NoDeprecatedCustomRule-test.ts b/packages/graphql/src/validation/__tests__/NoDeprecatedCustomRule-test.ts new file mode 100644 index 00000000000..924a804db85 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/NoDeprecatedCustomRule-test.ts @@ -0,0 +1,261 @@ +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { NoDeprecatedCustomRule } from '../rules/custom/NoDeprecatedCustomRule.js'; + +import { expectValidationErrorsWithSchema } from './harness.js'; + +function buildAssertion(sdlStr: string) { + const schema = buildSchema(sdlStr); + return { expectErrors, expectValid }; + + function expectErrors(queryStr: string) { + return expectValidationErrorsWithSchema(schema, NoDeprecatedCustomRule, queryStr); + } + + function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); + } +} + +describe('Validate: no deprecated', () => { + describe('no deprecated fields', () => { + const { expectValid, expectErrors } = buildAssertion(` + type Query { + normalField: String + deprecatedField: String @deprecated(reason: "Some field reason.") + } + `); + + it('ignores fields that are not deprecated', () => { + expectValid(` + { + normalField + } + `); + }); + + it('ignores unknown fields', () => { + expectValid(` + { + unknownField + } + + fragment UnknownFragment on UnknownType { + deprecatedField + } + `); + }); + + it('reports error when a deprecated field is selected', () => { + const message = 'The field Query.deprecatedField is deprecated. Some field reason.'; + + expectErrors(` + { + deprecatedField + } + + fragment QueryFragment on Query { + deprecatedField + } + `).toDeepEqual([ + { message, locations: [{ line: 3, column: 11 }] }, + { message, locations: [{ line: 7, column: 11 }] }, + ]); + }); + }); + + describe('no deprecated arguments on fields', () => { + const { expectValid, expectErrors } = buildAssertion(` + type Query { + someField( + normalArg: String, + deprecatedArg: String @deprecated(reason: "Some arg reason."), + ): String + } + `); + + it('ignores arguments that are not deprecated', () => { + expectValid(` + { + normalField(normalArg: "") + } + `); + }); + + it('ignores unknown arguments', () => { + expectValid(` + { + someField(unknownArg: "") + unknownField(deprecatedArg: "") + } + `); + }); + + it('reports error when a deprecated argument is used', () => { + expectErrors(` + { + someField(deprecatedArg: "") + } + `).toDeepEqual([ + { + message: 'Field "Query.someField" argument "deprecatedArg" is deprecated. Some arg reason.', + locations: [{ line: 3, column: 21 }], + }, + ]); + }); + }); + + describe('no deprecated arguments on directives', () => { + const { expectValid, expectErrors } = buildAssertion(` + type Query { + someField: String + } + + directive @someDirective( + normalArg: String, + deprecatedArg: String @deprecated(reason: "Some arg reason."), + ) on FIELD + `); + + it('ignores arguments that are not deprecated', () => { + expectValid(` + { + someField @someDirective(normalArg: "") + } + `); + }); + + it('ignores unknown arguments', () => { + expectValid(` + { + someField @someDirective(unknownArg: "") + someField @unknownDirective(deprecatedArg: "") + } + `); + }); + + it('reports error when a deprecated argument is used', () => { + expectErrors(` + { + someField @someDirective(deprecatedArg: "") + } + `).toDeepEqual([ + { + message: 'Directive "@someDirective" argument "deprecatedArg" is deprecated. Some arg reason.', + locations: [{ line: 3, column: 36 }], + }, + ]); + }); + }); + + describe('no deprecated input fields', () => { + const { expectValid, expectErrors } = buildAssertion(` + input InputType { + normalField: String + deprecatedField: String @deprecated(reason: "Some input field reason.") + } + + type Query { + someField(someArg: InputType): String + } + + directive @someDirective(someArg: InputType) on FIELD + `); + + it('ignores input fields that are not deprecated', () => { + expectValid(` + { + someField( + someArg: { normalField: "" } + ) @someDirective(someArg: { normalField: "" }) + } + `); + }); + + it('ignores unknown input fields', () => { + expectValid(` + { + someField( + someArg: { unknownField: "" } + ) + + someField( + unknownArg: { unknownField: "" } + ) + + unknownField( + unknownArg: { unknownField: "" } + ) + } + `); + }); + + it('reports error when a deprecated input field is used', () => { + const message = 'The input field InputType.deprecatedField is deprecated. Some input field reason.'; + + expectErrors(` + { + someField( + someArg: { deprecatedField: "" } + ) @someDirective(someArg: { deprecatedField: "" }) + } + `).toDeepEqual([ + { message, locations: [{ line: 4, column: 24 }] }, + { message, locations: [{ line: 5, column: 39 }] }, + ]); + }); + }); + + describe('no deprecated enum values', () => { + const { expectValid, expectErrors } = buildAssertion(` + enum EnumType { + NORMAL_VALUE + DEPRECATED_VALUE @deprecated(reason: "Some enum reason.") + } + + type Query { + someField(enumArg: EnumType): String + } + `); + + it('ignores enum values that are not deprecated', () => { + expectValid(` + { + normalField(enumArg: NORMAL_VALUE) + } + `); + }); + + it('ignores unknown enum values', () => { + expectValid(` + query ( + $unknownValue: EnumType = UNKNOWN_VALUE + $unknownType: UnknownType = UNKNOWN_VALUE + ) { + someField(enumArg: UNKNOWN_VALUE) + someField(unknownArg: UNKNOWN_VALUE) + unknownField(unknownArg: UNKNOWN_VALUE) + } + + fragment SomeFragment on Query { + someField(enumArg: UNKNOWN_VALUE) + } + `); + }); + + it('reports error when a deprecated enum value is used', () => { + const message = 'The enum value "EnumType.DEPRECATED_VALUE" is deprecated. Some enum reason.'; + + expectErrors(` + query ( + $variable: EnumType = DEPRECATED_VALUE + ) { + someField(enumArg: DEPRECATED_VALUE) + } + `).toDeepEqual([ + { message, locations: [{ line: 3, column: 33 }] }, + { message, locations: [{ line: 5, column: 30 }] }, + ]); + }); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/NoFragmentCyclesRule-test.ts b/packages/graphql/src/validation/__tests__/NoFragmentCyclesRule-test.ts new file mode 100644 index 00000000000..902d295f0cf --- /dev/null +++ b/packages/graphql/src/validation/__tests__/NoFragmentCyclesRule-test.ts @@ -0,0 +1,255 @@ +import { NoFragmentCyclesRule } from '../rules/NoFragmentCyclesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(NoFragmentCyclesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: No circular fragment spreads', () => { + it('single reference is valid', () => { + expectValid(` + fragment fragA on Dog { ...fragB } + fragment fragB on Dog { name } + `); + }); + + it('spreading twice is not circular', () => { + expectValid(` + fragment fragA on Dog { ...fragB, ...fragB } + fragment fragB on Dog { name } + `); + }); + + it('spreading twice indirectly is not circular', () => { + expectValid(` + fragment fragA on Dog { ...fragB, ...fragC } + fragment fragB on Dog { ...fragC } + fragment fragC on Dog { name } + `); + }); + + it('double spread within abstract types', () => { + expectValid(` + fragment nameFragment on Pet { + ... on Dog { name } + ... on Cat { name } + } + + fragment spreadsInAnon on Pet { + ... on Dog { ...nameFragment } + ... on Cat { ...nameFragment } + } + `); + }); + + it('does not false positive on unknown fragment', () => { + expectValid(` + fragment nameFragment on Pet { + ...UnknownFragment + } + `); + }); + + it('spreading recursively within field fails', () => { + expectErrors(` + fragment fragA on Human { relatives { ...fragA } }, + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragA" within itself.', + locations: [{ line: 2, column: 45 }], + }, + ]); + }); + + it('no spreading itself directly', () => { + expectErrors(` + fragment fragA on Dog { ...fragA } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragA" within itself.', + locations: [{ line: 2, column: 31 }], + }, + ]); + }); + + it('no spreading itself directly within inline fragment', () => { + expectErrors(` + fragment fragA on Pet { + ... on Dog { + ...fragA + } + } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragA" within itself.', + locations: [{ line: 4, column: 11 }], + }, + ]); + }); + + it('no spreading itself indirectly', () => { + expectErrors(` + fragment fragA on Dog { ...fragB } + fragment fragB on Dog { ...fragA } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragA" within itself via "fragB".', + locations: [ + { line: 2, column: 31 }, + { line: 3, column: 31 }, + ], + }, + ]); + }); + + it('no spreading itself indirectly reports opposite order', () => { + expectErrors(` + fragment fragB on Dog { ...fragA } + fragment fragA on Dog { ...fragB } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragB" within itself via "fragA".', + locations: [ + { line: 2, column: 31 }, + { line: 3, column: 31 }, + ], + }, + ]); + }); + + it('no spreading itself indirectly within inline fragment', () => { + expectErrors(` + fragment fragA on Pet { + ... on Dog { + ...fragB + } + } + fragment fragB on Pet { + ... on Dog { + ...fragA + } + } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragA" within itself via "fragB".', + locations: [ + { line: 4, column: 11 }, + { line: 9, column: 11 }, + ], + }, + ]); + }); + + it('no spreading itself deeply', () => { + expectErrors(` + fragment fragA on Dog { ...fragB } + fragment fragB on Dog { ...fragC } + fragment fragC on Dog { ...fragO } + fragment fragX on Dog { ...fragY } + fragment fragY on Dog { ...fragZ } + fragment fragZ on Dog { ...fragO } + fragment fragO on Dog { ...fragP } + fragment fragP on Dog { ...fragA, ...fragX } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragA" within itself via "fragB", "fragC", "fragO", "fragP".', + locations: [ + { line: 2, column: 31 }, + { line: 3, column: 31 }, + { line: 4, column: 31 }, + { line: 8, column: 31 }, + { line: 9, column: 31 }, + ], + }, + { + message: 'Cannot spread fragment "fragO" within itself via "fragP", "fragX", "fragY", "fragZ".', + locations: [ + { line: 8, column: 31 }, + { line: 9, column: 41 }, + { line: 5, column: 31 }, + { line: 6, column: 31 }, + { line: 7, column: 31 }, + ], + }, + ]); + }); + + it('no spreading itself deeply two paths', () => { + expectErrors(` + fragment fragA on Dog { ...fragB, ...fragC } + fragment fragB on Dog { ...fragA } + fragment fragC on Dog { ...fragA } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragA" within itself via "fragB".', + locations: [ + { line: 2, column: 31 }, + { line: 3, column: 31 }, + ], + }, + { + message: 'Cannot spread fragment "fragA" within itself via "fragC".', + locations: [ + { line: 2, column: 41 }, + { line: 4, column: 31 }, + ], + }, + ]); + }); + + it('no spreading itself deeply two paths -- alt traverse order', () => { + expectErrors(` + fragment fragA on Dog { ...fragC } + fragment fragB on Dog { ...fragC } + fragment fragC on Dog { ...fragA, ...fragB } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragA" within itself via "fragC".', + locations: [ + { line: 2, column: 31 }, + { line: 4, column: 31 }, + ], + }, + { + message: 'Cannot spread fragment "fragC" within itself via "fragB".', + locations: [ + { line: 4, column: 41 }, + { line: 3, column: 31 }, + ], + }, + ]); + }); + + it('no spreading itself deeply and immediately', () => { + expectErrors(` + fragment fragA on Dog { ...fragB } + fragment fragB on Dog { ...fragB, ...fragC } + fragment fragC on Dog { ...fragA, ...fragB } + `).toDeepEqual([ + { + message: 'Cannot spread fragment "fragB" within itself.', + locations: [{ line: 3, column: 31 }], + }, + { + message: 'Cannot spread fragment "fragA" within itself via "fragB", "fragC".', + locations: [ + { line: 2, column: 31 }, + { line: 3, column: 41 }, + { line: 4, column: 31 }, + ], + }, + { + message: 'Cannot spread fragment "fragB" within itself via "fragC".', + locations: [ + { line: 3, column: 41 }, + { line: 4, column: 41 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/NoSchemaIntrospectionCustomRule-test.ts b/packages/graphql/src/validation/__tests__/NoSchemaIntrospectionCustomRule-test.ts new file mode 100644 index 00000000000..21bc72a5a73 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/NoSchemaIntrospectionCustomRule-test.ts @@ -0,0 +1,128 @@ +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { NoSchemaIntrospectionCustomRule } from '../rules/custom/NoSchemaIntrospectionCustomRule.js'; + +import { expectValidationErrorsWithSchema } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrorsWithSchema(schema, NoSchemaIntrospectionCustomRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +const schema = buildSchema(` + type Query { + someQuery: SomeType + } + + type SomeType { + someField: String + introspectionField: __EnumValue + } +`); + +describe('Validate: Prohibit introspection queries', () => { + it('ignores valid fields including __typename', () => { + expectValid(` + { + someQuery { + __typename + someField + } + } + `); + }); + + it('ignores fields not in the schema', () => { + expectValid(` + { + __introspect + } + `); + }); + + it('reports error when a field with an introspection type is requested', () => { + expectErrors(` + { + __schema { + queryType { + name + } + } + } + `).toDeepEqual([ + { + message: 'GraphQL introspection has been disabled, but the requested query contained the field "__schema".', + locations: [{ line: 3, column: 9 }], + }, + { + message: 'GraphQL introspection has been disabled, but the requested query contained the field "queryType".', + locations: [{ line: 4, column: 11 }], + }, + ]); + }); + + it('reports error when a field with an introspection type is requested and aliased', () => { + expectErrors(` + { + s: __schema { + queryType { + name + } + } + } + `).toDeepEqual([ + { + message: 'GraphQL introspection has been disabled, but the requested query contained the field "__schema".', + locations: [{ line: 3, column: 9 }], + }, + { + message: 'GraphQL introspection has been disabled, but the requested query contained the field "queryType".', + locations: [{ line: 4, column: 11 }], + }, + ]); + }); + + it('reports error when using a fragment with a field with an introspection type', () => { + expectErrors(` + { + ...QueryFragment + } + + fragment QueryFragment on Query { + __schema { + queryType { + name + } + } + } + `).toDeepEqual([ + { + message: 'GraphQL introspection has been disabled, but the requested query contained the field "__schema".', + locations: [{ line: 7, column: 9 }], + }, + { + message: 'GraphQL introspection has been disabled, but the requested query contained the field "queryType".', + locations: [{ line: 8, column: 11 }], + }, + ]); + }); + + it('reports error for non-standard introspection fields', () => { + expectErrors(` + { + someQuery { + introspectionField + } + } + `).toDeepEqual([ + { + message: + 'GraphQL introspection has been disabled, but the requested query contained the field "introspectionField".', + locations: [{ line: 4, column: 11 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/NoUndefinedVariablesRule-test.ts b/packages/graphql/src/validation/__tests__/NoUndefinedVariablesRule-test.ts new file mode 100644 index 00000000000..95a0e683d59 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/NoUndefinedVariablesRule-test.ts @@ -0,0 +1,405 @@ +import { NoUndefinedVariablesRule } from '../rules/NoUndefinedVariablesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(NoUndefinedVariablesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: No undefined variables', () => { + it('all variables defined', () => { + expectValid(` + query Foo($a: String, $b: String, $c: String) { + field(a: $a, b: $b, c: $c) + } + `); + }); + + it('all variables deeply defined', () => { + expectValid(` + query Foo($a: String, $b: String, $c: String) { + field(a: $a) { + field(b: $b) { + field(c: $c) + } + } + } + `); + }); + + it('all variables deeply in inline fragments defined', () => { + expectValid(` + query Foo($a: String, $b: String, $c: String) { + ... on Type { + field(a: $a) { + field(b: $b) { + ... on Type { + field(c: $c) + } + } + } + } + } + `); + }); + + it('all variables in fragments deeply defined', () => { + expectValid(` + query Foo($a: String, $b: String, $c: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) { + ...FragB + } + } + fragment FragB on Type { + field(b: $b) { + ...FragC + } + } + fragment FragC on Type { + field(c: $c) + } + `); + }); + + it('variable within single fragment defined in multiple operations', () => { + expectValid(` + query Foo($a: String) { + ...FragA + } + query Bar($a: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) + } + `); + }); + + it('variable within fragments defined in operations', () => { + expectValid(` + query Foo($a: String) { + ...FragA + } + query Bar($b: String) { + ...FragB + } + fragment FragA on Type { + field(a: $a) + } + fragment FragB on Type { + field(b: $b) + } + `); + }); + + it('variable within recursive fragment defined', () => { + expectValid(` + query Foo($a: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) { + ...FragA + } + } + `); + }); + + it('variable not defined', () => { + expectErrors(` + query Foo($a: String, $b: String, $c: String) { + field(a: $a, b: $b, c: $c, d: $d) + } + `).toDeepEqual([ + { + message: 'Variable "$d" is not defined by operation "Foo".', + locations: [ + { line: 3, column: 39 }, + { line: 2, column: 7 }, + ], + }, + ]); + }); + + it('variable not defined by un-named query', () => { + expectErrors(` + { + field(a: $a) + } + `).toDeepEqual([ + { + message: 'Variable "$a" is not defined.', + locations: [ + { line: 3, column: 18 }, + { line: 2, column: 7 }, + ], + }, + ]); + }); + + it('multiple variables not defined', () => { + expectErrors(` + query Foo($b: String) { + field(a: $a, b: $b, c: $c) + } + `).toDeepEqual([ + { + message: 'Variable "$a" is not defined by operation "Foo".', + locations: [ + { line: 3, column: 18 }, + { line: 2, column: 7 }, + ], + }, + { + message: 'Variable "$c" is not defined by operation "Foo".', + locations: [ + { line: 3, column: 32 }, + { line: 2, column: 7 }, + ], + }, + ]); + }); + + it('variable in fragment not defined by un-named query', () => { + expectErrors(` + { + ...FragA + } + fragment FragA on Type { + field(a: $a) + } + `).toDeepEqual([ + { + message: 'Variable "$a" is not defined.', + locations: [ + { line: 6, column: 18 }, + { line: 2, column: 7 }, + ], + }, + ]); + }); + + it('variable in fragment not defined by operation', () => { + expectErrors(` + query Foo($a: String, $b: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) { + ...FragB + } + } + fragment FragB on Type { + field(b: $b) { + ...FragC + } + } + fragment FragC on Type { + field(c: $c) + } + `).toDeepEqual([ + { + message: 'Variable "$c" is not defined by operation "Foo".', + locations: [ + { line: 16, column: 18 }, + { line: 2, column: 7 }, + ], + }, + ]); + }); + + it('multiple variables in fragments not defined', () => { + expectErrors(` + query Foo($b: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) { + ...FragB + } + } + fragment FragB on Type { + field(b: $b) { + ...FragC + } + } + fragment FragC on Type { + field(c: $c) + } + `).toDeepEqual([ + { + message: 'Variable "$a" is not defined by operation "Foo".', + locations: [ + { line: 6, column: 18 }, + { line: 2, column: 7 }, + ], + }, + { + message: 'Variable "$c" is not defined by operation "Foo".', + locations: [ + { line: 16, column: 18 }, + { line: 2, column: 7 }, + ], + }, + ]); + }); + + it('single variable in fragment not defined by multiple operations', () => { + expectErrors(` + query Foo($a: String) { + ...FragAB + } + query Bar($a: String) { + ...FragAB + } + fragment FragAB on Type { + field(a: $a, b: $b) + } + `).toDeepEqual([ + { + message: 'Variable "$b" is not defined by operation "Foo".', + locations: [ + { line: 9, column: 25 }, + { line: 2, column: 7 }, + ], + }, + { + message: 'Variable "$b" is not defined by operation "Bar".', + locations: [ + { line: 9, column: 25 }, + { line: 5, column: 7 }, + ], + }, + ]); + }); + + it('variables in fragment not defined by multiple operations', () => { + expectErrors(` + query Foo($b: String) { + ...FragAB + } + query Bar($a: String) { + ...FragAB + } + fragment FragAB on Type { + field(a: $a, b: $b) + } + `).toDeepEqual([ + { + message: 'Variable "$a" is not defined by operation "Foo".', + locations: [ + { line: 9, column: 18 }, + { line: 2, column: 7 }, + ], + }, + { + message: 'Variable "$b" is not defined by operation "Bar".', + locations: [ + { line: 9, column: 25 }, + { line: 5, column: 7 }, + ], + }, + ]); + }); + + it('variable in fragment used by other operation', () => { + expectErrors(` + query Foo($b: String) { + ...FragA + } + query Bar($a: String) { + ...FragB + } + fragment FragA on Type { + field(a: $a) + } + fragment FragB on Type { + field(b: $b) + } + `).toDeepEqual([ + { + message: 'Variable "$a" is not defined by operation "Foo".', + locations: [ + { line: 9, column: 18 }, + { line: 2, column: 7 }, + ], + }, + { + message: 'Variable "$b" is not defined by operation "Bar".', + locations: [ + { line: 12, column: 18 }, + { line: 5, column: 7 }, + ], + }, + ]); + }); + + it('multiple undefined variables produce multiple errors', () => { + expectErrors(` + query Foo($b: String) { + ...FragAB + } + query Bar($a: String) { + ...FragAB + } + fragment FragAB on Type { + field1(a: $a, b: $b) + ...FragC + field3(a: $a, b: $b) + } + fragment FragC on Type { + field2(c: $c) + } + `).toDeepEqual([ + { + message: 'Variable "$a" is not defined by operation "Foo".', + locations: [ + { line: 9, column: 19 }, + { line: 2, column: 7 }, + ], + }, + { + message: 'Variable "$a" is not defined by operation "Foo".', + locations: [ + { line: 11, column: 19 }, + { line: 2, column: 7 }, + ], + }, + { + message: 'Variable "$c" is not defined by operation "Foo".', + locations: [ + { line: 14, column: 19 }, + { line: 2, column: 7 }, + ], + }, + { + message: 'Variable "$b" is not defined by operation "Bar".', + locations: [ + { line: 9, column: 26 }, + { line: 5, column: 7 }, + ], + }, + { + message: 'Variable "$b" is not defined by operation "Bar".', + locations: [ + { line: 11, column: 26 }, + { line: 5, column: 7 }, + ], + }, + { + message: 'Variable "$c" is not defined by operation "Bar".', + locations: [ + { line: 14, column: 19 }, + { line: 5, column: 7 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/NoUnusedFragmentsRule-test.ts b/packages/graphql/src/validation/__tests__/NoUnusedFragmentsRule-test.ts new file mode 100644 index 00000000000..4673e4b5c10 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/NoUnusedFragmentsRule-test.ts @@ -0,0 +1,161 @@ +import { NoUnusedFragmentsRule } from '../rules/NoUnusedFragmentsRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(NoUnusedFragmentsRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: No unused fragments', () => { + it('all fragment names are used', () => { + expectValid(` + { + human(id: 4) { + ...HumanFields1 + ... on Human { + ...HumanFields2 + } + } + } + fragment HumanFields1 on Human { + name + ...HumanFields3 + } + fragment HumanFields2 on Human { + name + } + fragment HumanFields3 on Human { + name + } + `); + }); + + it('all fragment names are used by multiple operations', () => { + expectValid(` + query Foo { + human(id: 4) { + ...HumanFields1 + } + } + query Bar { + human(id: 4) { + ...HumanFields2 + } + } + fragment HumanFields1 on Human { + name + ...HumanFields3 + } + fragment HumanFields2 on Human { + name + } + fragment HumanFields3 on Human { + name + } + `); + }); + + it('contains unknown fragments', () => { + expectErrors(` + query Foo { + human(id: 4) { + ...HumanFields1 + } + } + query Bar { + human(id: 4) { + ...HumanFields2 + } + } + fragment HumanFields1 on Human { + name + ...HumanFields3 + } + fragment HumanFields2 on Human { + name + } + fragment HumanFields3 on Human { + name + } + fragment Unused1 on Human { + name + } + fragment Unused2 on Human { + name + } + `).toDeepEqual([ + { + message: 'Fragment "Unused1" is never used.', + locations: [{ line: 22, column: 7 }], + }, + { + message: 'Fragment "Unused2" is never used.', + locations: [{ line: 25, column: 7 }], + }, + ]); + }); + + it('contains unknown fragments with ref cycle', () => { + expectErrors(` + query Foo { + human(id: 4) { + ...HumanFields1 + } + } + query Bar { + human(id: 4) { + ...HumanFields2 + } + } + fragment HumanFields1 on Human { + name + ...HumanFields3 + } + fragment HumanFields2 on Human { + name + } + fragment HumanFields3 on Human { + name + } + fragment Unused1 on Human { + name + ...Unused2 + } + fragment Unused2 on Human { + name + ...Unused1 + } + `).toDeepEqual([ + { + message: 'Fragment "Unused1" is never used.', + locations: [{ line: 22, column: 7 }], + }, + { + message: 'Fragment "Unused2" is never used.', + locations: [{ line: 26, column: 7 }], + }, + ]); + }); + + it('contains unknown and undef fragments', () => { + expectErrors(` + query Foo { + human(id: 4) { + ...bar + } + } + fragment foo on Human { + name + } + `).toDeepEqual([ + { + message: 'Fragment "foo" is never used.', + locations: [{ line: 7, column: 7 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/NoUnusedVariablesRule-test.ts b/packages/graphql/src/validation/__tests__/NoUnusedVariablesRule-test.ts new file mode 100644 index 00000000000..939bb00f06b --- /dev/null +++ b/packages/graphql/src/validation/__tests__/NoUnusedVariablesRule-test.ts @@ -0,0 +1,231 @@ +import { NoUnusedVariablesRule } from '../rules/NoUnusedVariablesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(NoUnusedVariablesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: No unused variables', () => { + it('uses all variables', () => { + expectValid(` + query ($a: String, $b: String, $c: String) { + field(a: $a, b: $b, c: $c) + } + `); + }); + + it('uses all variables deeply', () => { + expectValid(` + query Foo($a: String, $b: String, $c: String) { + field(a: $a) { + field(b: $b) { + field(c: $c) + } + } + } + `); + }); + + it('uses all variables deeply in inline fragments', () => { + expectValid(` + query Foo($a: String, $b: String, $c: String) { + ... on Type { + field(a: $a) { + field(b: $b) { + ... on Type { + field(c: $c) + } + } + } + } + } + `); + }); + + it('uses all variables in fragments', () => { + expectValid(` + query Foo($a: String, $b: String, $c: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) { + ...FragB + } + } + fragment FragB on Type { + field(b: $b) { + ...FragC + } + } + fragment FragC on Type { + field(c: $c) + } + `); + }); + + it('variable used by fragment in multiple operations', () => { + expectValid(` + query Foo($a: String) { + ...FragA + } + query Bar($b: String) { + ...FragB + } + fragment FragA on Type { + field(a: $a) + } + fragment FragB on Type { + field(b: $b) + } + `); + }); + + it('variable used by recursive fragment', () => { + expectValid(` + query Foo($a: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) { + ...FragA + } + } + `); + }); + + it('variable not used', () => { + expectErrors(` + query ($a: String, $b: String, $c: String) { + field(a: $a, b: $b) + } + `).toDeepEqual([ + { + message: 'Variable "$c" is never used.', + locations: [{ line: 2, column: 38 }], + }, + ]); + }); + + it('multiple variables not used', () => { + expectErrors(` + query Foo($a: String, $b: String, $c: String) { + field(b: $b) + } + `).toDeepEqual([ + { + message: 'Variable "$a" is never used in operation "Foo".', + locations: [{ line: 2, column: 17 }], + }, + { + message: 'Variable "$c" is never used in operation "Foo".', + locations: [{ line: 2, column: 41 }], + }, + ]); + }); + + it('variable not used in fragments', () => { + expectErrors(` + query Foo($a: String, $b: String, $c: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) { + ...FragB + } + } + fragment FragB on Type { + field(b: $b) { + ...FragC + } + } + fragment FragC on Type { + field + } + `).toDeepEqual([ + { + message: 'Variable "$c" is never used in operation "Foo".', + locations: [{ line: 2, column: 41 }], + }, + ]); + }); + + it('multiple variables not used in fragments', () => { + expectErrors(` + query Foo($a: String, $b: String, $c: String) { + ...FragA + } + fragment FragA on Type { + field { + ...FragB + } + } + fragment FragB on Type { + field(b: $b) { + ...FragC + } + } + fragment FragC on Type { + field + } + `).toDeepEqual([ + { + message: 'Variable "$a" is never used in operation "Foo".', + locations: [{ line: 2, column: 17 }], + }, + { + message: 'Variable "$c" is never used in operation "Foo".', + locations: [{ line: 2, column: 41 }], + }, + ]); + }); + + it('variable not used by unreferenced fragment', () => { + expectErrors(` + query Foo($b: String) { + ...FragA + } + fragment FragA on Type { + field(a: $a) + } + fragment FragB on Type { + field(b: $b) + } + `).toDeepEqual([ + { + message: 'Variable "$b" is never used in operation "Foo".', + locations: [{ line: 2, column: 17 }], + }, + ]); + }); + + it('variable not used by fragment used by other operation', () => { + expectErrors(` + query Foo($b: String) { + ...FragA + } + query Bar($a: String) { + ...FragB + } + fragment FragA on Type { + field(a: $a) + } + fragment FragB on Type { + field(b: $b) + } + `).toDeepEqual([ + { + message: 'Variable "$b" is never used in operation "Foo".', + locations: [{ line: 2, column: 17 }], + }, + { + message: 'Variable "$a" is never used in operation "Bar".', + locations: [{ line: 5, column: 17 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts b/packages/graphql/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts new file mode 100644 index 00000000000..ec246c4bd35 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts @@ -0,0 +1,1132 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { OverlappingFieldsCanBeMergedRule } from '../rules/OverlappingFieldsCanBeMergedRule.js'; + +import { expectValidationErrors, expectValidationErrorsWithSchema } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(OverlappingFieldsCanBeMergedRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +function expectErrorsWithSchema(schema: GraphQLSchema, queryStr: string) { + return expectValidationErrorsWithSchema(schema, OverlappingFieldsCanBeMergedRule, queryStr); +} + +function expectValidWithSchema(schema: GraphQLSchema, queryStr: string) { + expectErrorsWithSchema(schema, queryStr).toDeepEqual([]); +} + +describe('Validate: Overlapping fields can be merged', () => { + it('unique fields', () => { + expectValid(` + fragment uniqueFields on Dog { + name + nickname + } + `); + }); + + it('identical fields', () => { + expectValid(` + fragment mergeIdenticalFields on Dog { + name + name + } + `); + }); + + it('identical fields with identical args', () => { + expectValid(` + fragment mergeIdenticalFieldsWithIdenticalArgs on Dog { + doesKnowCommand(dogCommand: SIT) + doesKnowCommand(dogCommand: SIT) + } + `); + }); + + it('identical fields with identical directives', () => { + expectValid(` + fragment mergeSameFieldsWithSameDirectives on Dog { + name @include(if: true) + name @include(if: true) + } + `); + }); + + it('different args with different aliases', () => { + expectValid(` + fragment differentArgsWithDifferentAliases on Dog { + knowsSit: doesKnowCommand(dogCommand: SIT) + knowsDown: doesKnowCommand(dogCommand: DOWN) + } + `); + }); + + it('different directives with different aliases', () => { + expectValid(` + fragment differentDirectivesWithDifferentAliases on Dog { + nameIfTrue: name @include(if: true) + nameIfFalse: name @include(if: false) + } + `); + }); + + it('different skip/include directives accepted', () => { + // Note: Differing skip/include directives don't create an ambiguous return + // value and are acceptable in conditions where differing runtime values + // may have the same desired effect of including or skipping a field. + expectValid(` + fragment differentDirectivesWithDifferentAliases on Dog { + name @include(if: true) + name @include(if: false) + } + `); + }); + + it('Same aliases with different field targets', () => { + expectErrors(` + fragment sameAliasesWithDifferentFieldTargets on Dog { + fido: name + fido: nickname + } + `).toDeepEqual([ + { + message: + 'Fields "fido" conflict because "name" and "nickname" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('Same aliases allowed on non-overlapping fields', () => { + // This is valid since no object can be both a "Dog" and a "Cat", thus + // these fields can never overlap. + expectValid(` + fragment sameAliasesWithDifferentFieldTargets on Pet { + ... on Dog { + name + } + ... on Cat { + name: nickname + } + } + `); + }); + + it('Alias masking direct field access', () => { + expectErrors(` + fragment aliasMaskingDirectFieldAccess on Dog { + name: nickname + name + } + `).toDeepEqual([ + { + message: + 'Fields "name" conflict because "nickname" and "name" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('different args, second adds an argument', () => { + expectErrors(` + fragment conflictingArgs on Dog { + doesKnowCommand + doesKnowCommand(dogCommand: HEEL) + } + `).toDeepEqual([ + { + message: + 'Fields "doesKnowCommand" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('different args, second missing an argument', () => { + expectErrors(` + fragment conflictingArgs on Dog { + doesKnowCommand(dogCommand: SIT) + doesKnowCommand + } + `).toDeepEqual([ + { + message: + 'Fields "doesKnowCommand" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('conflicting arg values', () => { + expectErrors(` + fragment conflictingArgs on Dog { + doesKnowCommand(dogCommand: SIT) + doesKnowCommand(dogCommand: HEEL) + } + `).toDeepEqual([ + { + message: + 'Fields "doesKnowCommand" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('conflicting arg names', () => { + expectErrors(` + fragment conflictingArgs on Dog { + isAtLocation(x: 0) + isAtLocation(y: 0) + } + `).toDeepEqual([ + { + message: + 'Fields "isAtLocation" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 9 }, + ], + }, + ]); + }); + + it('allows different args where no conflict is possible', () => { + // This is valid since no object can be both a "Dog" and a "Cat", thus + // these fields can never overlap. + expectValid(` + fragment conflictingArgs on Pet { + ... on Dog { + name(surname: true) + } + ... on Cat { + name + } + } + `); + }); + + it('allows different order of args', () => { + const schema = buildSchema(` + type Query { + someField(a: String, b: String): String + } + `); + + // This is valid since arguments are unordered, see: + // https://spec.graphql.org/draft/#sec-Language.Arguments.Arguments-are-unordered + expectValidWithSchema( + schema, + ` + { + someField(a: null, b: null) + someField(b: null, a: null) + } + ` + ); + }); + + it('allows different order of input object fields in arg values', () => { + const schema = buildSchema(` + input SomeInput { + a: String + b: String + } + + type Query { + someField(arg: SomeInput): String + } + `); + + // This is valid since input object fields are unordered, see: + // https://spec.graphql.org/draft/#sec-Input-Object-Values.Input-object-fields-are-unordered + expectValidWithSchema( + schema, + ` + { + someField(arg: { a: null, b: null }) + someField(arg: { b: null, a: null }) + } + ` + ); + }); + + it('encounters conflict in fragments', () => { + expectErrors(` + { + ...A + ...B + } + fragment A on Type { + x: a + } + fragment B on Type { + x: b + } + `).toDeepEqual([ + { + message: + 'Fields "x" conflict because "a" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 7, column: 9 }, + { line: 10, column: 9 }, + ], + }, + ]); + }); + + it('reports each conflict once', () => { + expectErrors(` + { + f1 { + ...A + ...B + } + f2 { + ...B + ...A + } + f3 { + ...A + ...B + x: c + } + } + fragment A on Type { + x: a + } + fragment B on Type { + x: b + } + `).toDeepEqual([ + { + message: + 'Fields "x" conflict because "a" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 18, column: 9 }, + { line: 21, column: 9 }, + ], + }, + { + message: + 'Fields "x" conflict because "c" and "a" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 14, column: 11 }, + { line: 18, column: 9 }, + ], + }, + { + message: + 'Fields "x" conflict because "c" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 14, column: 11 }, + { line: 21, column: 9 }, + ], + }, + ]); + }); + + it('deep conflict', () => { + expectErrors(` + { + field { + x: a + }, + field { + x: b + } + } + `).toDeepEqual([ + { + message: + 'Fields "field" conflict because subfields "x" conflict because "a" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 11 }, + { line: 6, column: 9 }, + { line: 7, column: 11 }, + ], + }, + ]); + }); + + it('deep conflict with multiple issues', () => { + expectErrors(` + { + field { + x: a + y: c + }, + field { + x: b + y: d + } + } + `).toDeepEqual([ + { + message: + 'Fields "field" conflict because subfields "x" conflict because "a" and "b" are different fields and subfields "y" conflict because "c" and "d" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 11 }, + { line: 5, column: 11 }, + { line: 7, column: 9 }, + { line: 8, column: 11 }, + { line: 9, column: 11 }, + ], + }, + ]); + }); + + it('very deep conflict', () => { + expectErrors(` + { + field { + deepField { + x: a + } + }, + field { + deepField { + x: b + } + } + } + `).toDeepEqual([ + { + message: + 'Fields "field" conflict because subfields "deepField" conflict because subfields "x" conflict because "a" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 4, column: 11 }, + { line: 5, column: 13 }, + { line: 8, column: 9 }, + { line: 9, column: 11 }, + { line: 10, column: 13 }, + ], + }, + ]); + }); + + it('reports deep conflict to nearest common ancestor', () => { + expectErrors(` + { + field { + deepField { + x: a + } + deepField { + x: b + } + }, + field { + deepField { + y + } + } + } + `).toDeepEqual([ + { + message: + 'Fields "deepField" conflict because subfields "x" conflict because "a" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 4, column: 11 }, + { line: 5, column: 13 }, + { line: 7, column: 11 }, + { line: 8, column: 13 }, + ], + }, + ]); + }); + + it('reports deep conflict to nearest common ancestor in fragments', () => { + expectErrors(` + { + field { + ...F + } + field { + ...F + } + } + fragment F on T { + deepField { + deeperField { + x: a + } + deeperField { + x: b + } + }, + deepField { + deeperField { + y + } + } + } + `).toDeepEqual([ + { + message: + 'Fields "deeperField" conflict because subfields "x" conflict because "a" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 12, column: 11 }, + { line: 13, column: 13 }, + { line: 15, column: 11 }, + { line: 16, column: 13 }, + ], + }, + ]); + }); + + it('reports deep conflict in nested fragments', () => { + expectErrors(` + { + field { + ...F + } + field { + ...I + } + } + fragment F on T { + x: a + ...G + } + fragment G on T { + y: c + } + fragment I on T { + y: d + ...J + } + fragment J on T { + x: b + } + `).toDeepEqual([ + { + message: + 'Fields "field" conflict because subfields "x" conflict because "a" and "b" are different fields and subfields "y" conflict because "c" and "d" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 11, column: 9 }, + { line: 15, column: 9 }, + { line: 6, column: 9 }, + { line: 22, column: 9 }, + { line: 18, column: 9 }, + ], + }, + ]); + }); + + it('ignores unknown fragments', () => { + expectValid(` + { + field + ...Unknown + ...Known + } + + fragment Known on T { + field + ...OtherUnknown + } + `); + }); + + describe('return types must be unambiguous', () => { + const schema = buildSchema(` + interface SomeBox { + deepBox: SomeBox + unrelatedField: String + } + + type StringBox implements SomeBox { + scalar: String + deepBox: StringBox + unrelatedField: String + listStringBox: [StringBox] + stringBox: StringBox + intBox: IntBox + } + + type IntBox implements SomeBox { + scalar: Int + deepBox: IntBox + unrelatedField: String + listStringBox: [StringBox] + stringBox: StringBox + intBox: IntBox + } + + interface NonNullStringBox1 { + scalar: String! + } + + type NonNullStringBox1Impl implements SomeBox & NonNullStringBox1 { + scalar: String! + unrelatedField: String + deepBox: SomeBox + } + + interface NonNullStringBox2 { + scalar: String! + } + + type NonNullStringBox2Impl implements SomeBox & NonNullStringBox2 { + scalar: String! + unrelatedField: String + deepBox: SomeBox + } + + type Connection { + edges: [Edge] + } + + type Edge { + node: Node + } + + type Node { + id: ID + name: String + } + + type Query { + someBox: SomeBox + connection: Connection + } + `); + + it('conflicting return types which potentially overlap', () => { + // This is invalid since an object could potentially be both the Object + // type IntBox and the interface type NonNullStringBox1. While that + // condition does not exist in the current schema, the schema could + // expand in the future to allow this. Thus it is invalid. + expectErrorsWithSchema( + schema, + ` + { + someBox { + ...on IntBox { + scalar + } + ...on NonNullStringBox1 { + scalar + } + } + } + ` + ).toDeepEqual([ + { + message: + 'Fields "scalar" conflict because they return conflicting types "Int" and "String!". Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 5, column: 17 }, + { line: 8, column: 17 }, + ], + }, + ]); + }); + + it('compatible return shapes on different return types', () => { + // In this case `deepBox` returns `SomeBox` in the first usage, and + // `StringBox` in the second usage. These return types are not the same! + // however this is valid because the return *shapes* are compatible. + expectValidWithSchema( + schema, + ` + { + someBox { + ... on SomeBox { + deepBox { + unrelatedField + } + } + ... on StringBox { + deepBox { + unrelatedField + } + } + } + } + ` + ); + }); + + it('disallows differing return types despite no overlap', () => { + expectErrorsWithSchema( + schema, + ` + { + someBox { + ... on IntBox { + scalar + } + ... on StringBox { + scalar + } + } + } + ` + ).toDeepEqual([ + { + message: + 'Fields "scalar" conflict because they return conflicting types "Int" and "String". Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 5, column: 17 }, + { line: 8, column: 17 }, + ], + }, + ]); + }); + + it('reports correctly when a non-exclusive follows an exclusive', () => { + expectErrorsWithSchema( + schema, + ` + { + someBox { + ... on IntBox { + deepBox { + ...X + } + } + } + someBox { + ... on StringBox { + deepBox { + ...Y + } + } + } + memoed: someBox { + ... on IntBox { + deepBox { + ...X + } + } + } + memoed: someBox { + ... on StringBox { + deepBox { + ...Y + } + } + } + other: someBox { + ...X + } + other: someBox { + ...Y + } + } + fragment X on SomeBox { + scalar + } + fragment Y on SomeBox { + scalar: unrelatedField + } + ` + ).toDeepEqual([ + { + message: + 'Fields "other" conflict because subfields "scalar" conflict because "scalar" and "unrelatedField" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 31, column: 13 }, + { line: 39, column: 13 }, + { line: 34, column: 13 }, + { line: 42, column: 13 }, + ], + }, + ]); + }); + + it('disallows differing return type nullability despite no overlap', () => { + expectErrorsWithSchema( + schema, + ` + { + someBox { + ... on NonNullStringBox1 { + scalar + } + ... on StringBox { + scalar + } + } + } + ` + ).toDeepEqual([ + { + message: + 'Fields "scalar" conflict because they return conflicting types "String!" and "String". Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 5, column: 17 }, + { line: 8, column: 17 }, + ], + }, + ]); + }); + + it('disallows differing return type list despite no overlap', () => { + expectErrorsWithSchema( + schema, + ` + { + someBox { + ... on IntBox { + box: listStringBox { + scalar + } + } + ... on StringBox { + box: stringBox { + scalar + } + } + } + } + ` + ).toDeepEqual([ + { + message: + 'Fields "box" conflict because they return conflicting types "[StringBox]" and "StringBox". Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 5, column: 17 }, + { line: 10, column: 17 }, + ], + }, + ]); + + expectErrorsWithSchema( + schema, + ` + { + someBox { + ... on IntBox { + box: stringBox { + scalar + } + } + ... on StringBox { + box: listStringBox { + scalar + } + } + } + } + ` + ).toDeepEqual([ + { + message: + 'Fields "box" conflict because they return conflicting types "StringBox" and "[StringBox]". Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 5, column: 17 }, + { line: 10, column: 17 }, + ], + }, + ]); + }); + + it('disallows differing subfields', () => { + expectErrorsWithSchema( + schema, + ` + { + someBox { + ... on IntBox { + box: stringBox { + val: scalar + val: unrelatedField + } + } + ... on StringBox { + box: stringBox { + val: scalar + } + } + } + } + ` + ).toDeepEqual([ + { + message: + 'Fields "val" conflict because "scalar" and "unrelatedField" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 6, column: 19 }, + { line: 7, column: 19 }, + ], + }, + ]); + }); + + it('disallows differing deep return types despite no overlap', () => { + expectErrorsWithSchema( + schema, + ` + { + someBox { + ... on IntBox { + box: stringBox { + scalar + } + } + ... on StringBox { + box: intBox { + scalar + } + } + } + } + ` + ).toDeepEqual([ + { + message: + 'Fields "box" conflict because subfields "scalar" conflict because they return conflicting types "String" and "Int". Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 5, column: 17 }, + { line: 6, column: 19 }, + { line: 10, column: 17 }, + { line: 11, column: 19 }, + ], + }, + ]); + }); + + it('allows non-conflicting overlapping types', () => { + expectValidWithSchema( + schema, + ` + { + someBox { + ... on IntBox { + scalar: unrelatedField + } + ... on StringBox { + scalar + } + } + } + ` + ); + }); + + it('same wrapped scalar return types', () => { + expectValidWithSchema( + schema, + ` + { + someBox { + ...on NonNullStringBox1 { + scalar + } + ...on NonNullStringBox2 { + scalar + } + } + } + ` + ); + }); + + it('allows inline fragments without type condition', () => { + expectValidWithSchema( + schema, + ` + { + a + ... { + a + } + } + ` + ); + }); + + it('compares deep types including list', () => { + expectErrorsWithSchema( + schema, + ` + { + connection { + ...edgeID + edges { + node { + id: name + } + } + } + } + + fragment edgeID on Connection { + edges { + node { + id + } + } + } + ` + ).toDeepEqual([ + { + message: + 'Fields "edges" conflict because subfields "node" conflict because subfields "id" conflict because "name" and "id" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 5, column: 15 }, + { line: 6, column: 17 }, + { line: 7, column: 19 }, + { line: 14, column: 13 }, + { line: 15, column: 15 }, + { line: 16, column: 17 }, + ], + }, + ]); + }); + + it('ignores unknown types', () => { + expectValidWithSchema( + schema, + ` + { + someBox { + ...on UnknownType { + scalar + } + ...on NonNullStringBox2 { + scalar + } + } + } + ` + ); + }); + + it('works for field names that are JS keywords', () => { + const schemaWithKeywords = buildSchema(` + type Foo { + constructor: String + } + + type Query { + foo: Foo + } + `); + + expectValidWithSchema( + schemaWithKeywords, + ` + { + foo { + constructor + } + } + ` + ); + }); + }); + + it('does not infinite loop on recursive fragment', () => { + expectValid(` + { + ...fragA + } + + fragment fragA on Human { name, relatives { name, ...fragA } } + `); + }); + + it('does not infinite loop on immediately recursive fragment', () => { + expectValid(` + { + ...fragA + } + + fragment fragA on Human { name, ...fragA } + `); + }); + + it('does not infinite loop on recursive fragment with a field named after fragment', () => { + expectValid(` + { + ...fragA + fragA + } + + fragment fragA on Query { ...fragA } + `); + }); + + it('finds invalid cases even with field named after fragment', () => { + expectErrors(` + { + fragA + ...fragA + } + + fragment fragA on Type { + fragA: b + } + `).toDeepEqual([ + { + message: + 'Fields "fragA" conflict because "fragA" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 3, column: 9 }, + { line: 8, column: 9 }, + ], + }, + ]); + }); + + it('does not infinite loop on transitively recursive fragment', () => { + expectValid(` + { + ...fragA + fragB + } + + fragment fragA on Human { name, ...fragB } + fragment fragB on Human { name, ...fragC } + fragment fragC on Human { name, ...fragA } + `); + }); + + it('finds invalid case even with immediately recursive fragment', () => { + expectErrors(` + fragment sameAliasesWithDifferentFieldTargets on Dog { + ...sameAliasesWithDifferentFieldTargets + fido: name + fido: nickname + } + `).toDeepEqual([ + { + message: + 'Fields "fido" conflict because "name" and "nickname" are different fields. Use different aliases on the fields to fetch both if this was intentional.', + locations: [ + { line: 4, column: 9 }, + { line: 5, column: 9 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/PossibleFragmentSpreadsRule-test.ts b/packages/graphql/src/validation/__tests__/PossibleFragmentSpreadsRule-test.ts new file mode 100644 index 00000000000..11252220748 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/PossibleFragmentSpreadsRule-test.ts @@ -0,0 +1,294 @@ +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { PossibleFragmentSpreadsRule } from '../rules/PossibleFragmentSpreadsRule.js'; + +import { expectValidationErrorsWithSchema } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrorsWithSchema(testSchema, PossibleFragmentSpreadsRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +const testSchema = buildSchema(` + interface Being { + name: String + } + + interface Pet implements Being { + name: String + } + + type Dog implements Being & Pet { + name: String + barkVolume: Int + } + + type Cat implements Being & Pet { + name: String + meowVolume: Int + } + + union CatOrDog = Cat | Dog + + interface Intelligent { + iq: Int + } + + type Human implements Being & Intelligent { + name: String + pets: [Pet] + iq: Int + } + + type Alien implements Being & Intelligent { + name: String + iq: Int + } + + union DogOrHuman = Dog | Human + + union HumanOrAlien = Human | Alien + + type Query { + catOrDog: CatOrDog + dogOrHuman: DogOrHuman + humanOrAlien: HumanOrAlien + } +`); + +describe('Validate: Possible fragment spreads', () => { + it('of the same object', () => { + expectValid(` + fragment objectWithinObject on Dog { ...dogFragment } + fragment dogFragment on Dog { barkVolume } + `); + }); + + it('of the same object with inline fragment', () => { + expectValid(` + fragment objectWithinObjectAnon on Dog { ... on Dog { barkVolume } } + `); + }); + + it('object into an implemented interface', () => { + expectValid(` + fragment objectWithinInterface on Pet { ...dogFragment } + fragment dogFragment on Dog { barkVolume } + `); + }); + + it('object into containing union', () => { + expectValid(` + fragment objectWithinUnion on CatOrDog { ...dogFragment } + fragment dogFragment on Dog { barkVolume } + `); + }); + + it('union into contained object', () => { + expectValid(` + fragment unionWithinObject on Dog { ...catOrDogFragment } + fragment catOrDogFragment on CatOrDog { __typename } + `); + }); + + it('union into overlapping interface', () => { + expectValid(` + fragment unionWithinInterface on Pet { ...catOrDogFragment } + fragment catOrDogFragment on CatOrDog { __typename } + `); + }); + + it('union into overlapping union', () => { + expectValid(` + fragment unionWithinUnion on DogOrHuman { ...catOrDogFragment } + fragment catOrDogFragment on CatOrDog { __typename } + `); + }); + + it('interface into implemented object', () => { + expectValid(` + fragment interfaceWithinObject on Dog { ...petFragment } + fragment petFragment on Pet { name } + `); + }); + + it('interface into overlapping interface', () => { + expectValid(` + fragment interfaceWithinInterface on Pet { ...beingFragment } + fragment beingFragment on Being { name } + `); + }); + + it('interface into overlapping interface in inline fragment', () => { + expectValid(` + fragment interfaceWithinInterface on Pet { ... on Being { name } } + `); + }); + + it('interface into overlapping union', () => { + expectValid(` + fragment interfaceWithinUnion on CatOrDog { ...petFragment } + fragment petFragment on Pet { name } + `); + }); + + it('ignores incorrect type (caught by FragmentsOnCompositeTypesRule)', () => { + expectValid(` + fragment petFragment on Pet { ...badInADifferentWay } + fragment badInADifferentWay on String { name } + `); + }); + + it('ignores unknown fragments (caught by KnownFragmentNamesRule)', () => { + expectValid(` + fragment petFragment on Pet { ...UnknownFragment } + `); + }); + + it('different object into object', () => { + expectErrors(` + fragment invalidObjectWithinObject on Cat { ...dogFragment } + fragment dogFragment on Dog { barkVolume } + `).toDeepEqual([ + { + message: 'Fragment "dogFragment" cannot be spread here as objects of type "Cat" can never be of type "Dog".', + locations: [{ line: 2, column: 51 }], + }, + ]); + }); + + it('different object into object in inline fragment', () => { + expectErrors(` + fragment invalidObjectWithinObjectAnon on Cat { + ... on Dog { barkVolume } + } + `).toDeepEqual([ + { + message: 'Fragment cannot be spread here as objects of type "Cat" can never be of type "Dog".', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('object into not implementing interface', () => { + expectErrors(` + fragment invalidObjectWithinInterface on Pet { ...humanFragment } + fragment humanFragment on Human { pets { name } } + `).toDeepEqual([ + { + message: + 'Fragment "humanFragment" cannot be spread here as objects of type "Pet" can never be of type "Human".', + locations: [{ line: 2, column: 54 }], + }, + ]); + }); + + it('object into not containing union', () => { + expectErrors(` + fragment invalidObjectWithinUnion on CatOrDog { ...humanFragment } + fragment humanFragment on Human { pets { name } } + `).toDeepEqual([ + { + message: + 'Fragment "humanFragment" cannot be spread here as objects of type "CatOrDog" can never be of type "Human".', + locations: [{ line: 2, column: 55 }], + }, + ]); + }); + + it('union into not contained object', () => { + expectErrors(` + fragment invalidUnionWithinObject on Human { ...catOrDogFragment } + fragment catOrDogFragment on CatOrDog { __typename } + `).toDeepEqual([ + { + message: + 'Fragment "catOrDogFragment" cannot be spread here as objects of type "Human" can never be of type "CatOrDog".', + locations: [{ line: 2, column: 52 }], + }, + ]); + }); + + it('union into non overlapping interface', () => { + expectErrors(` + fragment invalidUnionWithinInterface on Pet { ...humanOrAlienFragment } + fragment humanOrAlienFragment on HumanOrAlien { __typename } + `).toDeepEqual([ + { + message: + 'Fragment "humanOrAlienFragment" cannot be spread here as objects of type "Pet" can never be of type "HumanOrAlien".', + locations: [{ line: 2, column: 53 }], + }, + ]); + }); + + it('union into non overlapping union', () => { + expectErrors(` + fragment invalidUnionWithinUnion on CatOrDog { ...humanOrAlienFragment } + fragment humanOrAlienFragment on HumanOrAlien { __typename } + `).toDeepEqual([ + { + message: + 'Fragment "humanOrAlienFragment" cannot be spread here as objects of type "CatOrDog" can never be of type "HumanOrAlien".', + locations: [{ line: 2, column: 54 }], + }, + ]); + }); + + it('interface into non implementing object', () => { + expectErrors(` + fragment invalidInterfaceWithinObject on Cat { ...intelligentFragment } + fragment intelligentFragment on Intelligent { iq } + `).toDeepEqual([ + { + message: + 'Fragment "intelligentFragment" cannot be spread here as objects of type "Cat" can never be of type "Intelligent".', + locations: [{ line: 2, column: 54 }], + }, + ]); + }); + + it('interface into non overlapping interface', () => { + expectErrors(` + fragment invalidInterfaceWithinInterface on Pet { + ...intelligentFragment + } + fragment intelligentFragment on Intelligent { iq } + `).toDeepEqual([ + { + message: + 'Fragment "intelligentFragment" cannot be spread here as objects of type "Pet" can never be of type "Intelligent".', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('interface into non overlapping interface in inline fragment', () => { + expectErrors(` + fragment invalidInterfaceWithinInterfaceAnon on Pet { + ...on Intelligent { iq } + } + `).toDeepEqual([ + { + message: 'Fragment cannot be spread here as objects of type "Pet" can never be of type "Intelligent".', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('interface into non overlapping union', () => { + expectErrors(` + fragment invalidInterfaceWithinUnion on HumanOrAlien { ...petFragment } + fragment petFragment on Pet { name } + `).toDeepEqual([ + { + message: + 'Fragment "petFragment" cannot be spread here as objects of type "HumanOrAlien" can never be of type "Pet".', + locations: [{ line: 2, column: 62 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/PossibleTypeExtensionsRule-test.ts b/packages/graphql/src/validation/__tests__/PossibleTypeExtensionsRule-test.ts new file mode 100644 index 00000000000..f89f345c222 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/PossibleTypeExtensionsRule-test.ts @@ -0,0 +1,267 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { PossibleTypeExtensionsRule } from '../rules/PossibleTypeExtensionsRule.js'; + +import { expectSDLValidationErrors } from './harness.js'; + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, PossibleTypeExtensionsRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +describe('Validate: Possible type extensions', () => { + it('no extensions', () => { + expectValidSDL(` + scalar FooScalar + type FooObject + interface FooInterface + union FooUnion + enum FooEnum + input FooInputObject + `); + }); + + it('one extension per type', () => { + expectValidSDL(` + scalar FooScalar + type FooObject + interface FooInterface + union FooUnion + enum FooEnum + input FooInputObject + + extend scalar FooScalar @dummy + extend type FooObject @dummy + extend interface FooInterface @dummy + extend union FooUnion @dummy + extend enum FooEnum @dummy + extend input FooInputObject @dummy + `); + }); + + it('many extensions per type', () => { + expectValidSDL(` + scalar FooScalar + type FooObject + interface FooInterface + union FooUnion + enum FooEnum + input FooInputObject + + extend scalar FooScalar @dummy + extend type FooObject @dummy + extend interface FooInterface @dummy + extend union FooUnion @dummy + extend enum FooEnum @dummy + extend input FooInputObject @dummy + + extend scalar FooScalar @dummy + extend type FooObject @dummy + extend interface FooInterface @dummy + extend union FooUnion @dummy + extend enum FooEnum @dummy + extend input FooInputObject @dummy + `); + }); + + it('extending unknown type', () => { + const message = 'Cannot extend type "Unknown" because it is not defined. Did you mean "Known"?'; + + expectSDLErrors(` + type Known + + extend scalar Unknown @dummy + extend type Unknown @dummy + extend interface Unknown @dummy + extend union Unknown @dummy + extend enum Unknown @dummy + extend input Unknown @dummy + `).toDeepEqual([ + { message, locations: [{ line: 4, column: 21 }] }, + { message, locations: [{ line: 5, column: 19 }] }, + { message, locations: [{ line: 6, column: 24 }] }, + { message, locations: [{ line: 7, column: 20 }] }, + { message, locations: [{ line: 8, column: 19 }] }, + { message, locations: [{ line: 9, column: 20 }] }, + ]); + }); + + it('does not consider non-type definitions', () => { + const message = 'Cannot extend type "Foo" because it is not defined.'; + + expectSDLErrors(` + query Foo { __typename } + fragment Foo on Query { __typename } + directive @Foo on SCHEMA + + extend scalar Foo @dummy + extend type Foo @dummy + extend interface Foo @dummy + extend union Foo @dummy + extend enum Foo @dummy + extend input Foo @dummy + `).toDeepEqual([ + { message, locations: [{ line: 6, column: 21 }] }, + { message, locations: [{ line: 7, column: 19 }] }, + { message, locations: [{ line: 8, column: 24 }] }, + { message, locations: [{ line: 9, column: 20 }] }, + { message, locations: [{ line: 10, column: 19 }] }, + { message, locations: [{ line: 11, column: 20 }] }, + ]); + }); + + it('extending with different kinds', () => { + expectSDLErrors(` + scalar FooScalar + type FooObject + interface FooInterface + union FooUnion + enum FooEnum + input FooInputObject + + extend type FooScalar @dummy + extend interface FooObject @dummy + extend union FooInterface @dummy + extend enum FooUnion @dummy + extend input FooEnum @dummy + extend scalar FooInputObject @dummy + `).toDeepEqual([ + { + message: 'Cannot extend non-object type "FooScalar".', + locations: [ + { line: 2, column: 7 }, + { line: 9, column: 7 }, + ], + }, + { + message: 'Cannot extend non-interface type "FooObject".', + locations: [ + { line: 3, column: 7 }, + { line: 10, column: 7 }, + ], + }, + { + message: 'Cannot extend non-union type "FooInterface".', + locations: [ + { line: 4, column: 7 }, + { line: 11, column: 7 }, + ], + }, + { + message: 'Cannot extend non-enum type "FooUnion".', + locations: [ + { line: 5, column: 7 }, + { line: 12, column: 7 }, + ], + }, + { + message: 'Cannot extend non-input object type "FooEnum".', + locations: [ + { line: 6, column: 7 }, + { line: 13, column: 7 }, + ], + }, + { + message: 'Cannot extend non-scalar type "FooInputObject".', + locations: [ + { line: 7, column: 7 }, + { line: 14, column: 7 }, + ], + }, + ]); + }); + + it('extending types within existing schema', () => { + const schema = buildSchema(` + scalar FooScalar + type FooObject + interface FooInterface + union FooUnion + enum FooEnum + input FooInputObject + `); + const sdl = ` + extend scalar FooScalar @dummy + extend type FooObject @dummy + extend interface FooInterface @dummy + extend union FooUnion @dummy + extend enum FooEnum @dummy + extend input FooInputObject @dummy + `; + + expectValidSDL(sdl, schema); + }); + + it('extending unknown types within existing schema', () => { + const schema = buildSchema('type Known'); + const sdl = ` + extend scalar Unknown @dummy + extend type Unknown @dummy + extend interface Unknown @dummy + extend union Unknown @dummy + extend enum Unknown @dummy + extend input Unknown @dummy + `; + + const message = 'Cannot extend type "Unknown" because it is not defined. Did you mean "Known"?'; + expectSDLErrors(sdl, schema).toDeepEqual([ + { message, locations: [{ line: 2, column: 21 }] }, + { message, locations: [{ line: 3, column: 19 }] }, + { message, locations: [{ line: 4, column: 24 }] }, + { message, locations: [{ line: 5, column: 20 }] }, + { message, locations: [{ line: 6, column: 19 }] }, + { message, locations: [{ line: 7, column: 20 }] }, + ]); + }); + + it('extending types with different kinds within existing schema', () => { + const schema = buildSchema(` + scalar FooScalar + type FooObject + interface FooInterface + union FooUnion + enum FooEnum + input FooInputObject + `); + const sdl = ` + extend type FooScalar @dummy + extend interface FooObject @dummy + extend union FooInterface @dummy + extend enum FooUnion @dummy + extend input FooEnum @dummy + extend scalar FooInputObject @dummy + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: 'Cannot extend non-object type "FooScalar".', + locations: [{ line: 2, column: 7 }], + }, + { + message: 'Cannot extend non-interface type "FooObject".', + locations: [{ line: 3, column: 7 }], + }, + { + message: 'Cannot extend non-union type "FooInterface".', + locations: [{ line: 4, column: 7 }], + }, + { + message: 'Cannot extend non-enum type "FooUnion".', + locations: [{ line: 5, column: 7 }], + }, + { + message: 'Cannot extend non-input object type "FooEnum".', + locations: [{ line: 6, column: 7 }], + }, + { + message: 'Cannot extend non-scalar type "FooInputObject".', + locations: [{ line: 7, column: 7 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/ProvidedRequiredArgumentsRule-test.ts b/packages/graphql/src/validation/__tests__/ProvidedRequiredArgumentsRule-test.ts new file mode 100644 index 00000000000..6903b09950a --- /dev/null +++ b/packages/graphql/src/validation/__tests__/ProvidedRequiredArgumentsRule-test.ts @@ -0,0 +1,339 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { + ProvidedRequiredArgumentsOnDirectivesRule, + ProvidedRequiredArgumentsRule, +} from '../rules/ProvidedRequiredArgumentsRule.js'; + +import { expectSDLValidationErrors, expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(ProvidedRequiredArgumentsRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, ProvidedRequiredArgumentsOnDirectivesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string) { + expectSDLErrors(sdlStr).toDeepEqual([]); +} + +describe('Validate: Provided required arguments', () => { + it('ignores unknown arguments', () => { + expectValid(` + { + dog { + isHouseTrained(unknownArgument: true) + } + } + `); + }); + + describe('Valid non-nullable value', () => { + it('Arg on optional arg', () => { + expectValid(` + { + dog { + isHouseTrained(atOtherHomes: true) + } + } + `); + }); + + it('No Arg on optional arg', () => { + expectValid(` + { + dog { + isHouseTrained + } + } + `); + }); + + it('No arg on non-null field with default', () => { + expectValid(` + { + complicatedArgs { + nonNullFieldWithDefault + } + } + `); + }); + + it('Multiple args', () => { + expectValid(` + { + complicatedArgs { + multipleReqs(req1: 1, req2: 2) + } + } + `); + }); + + it('Multiple args reverse order', () => { + expectValid(` + { + complicatedArgs { + multipleReqs(req2: 2, req1: 1) + } + } + `); + }); + + it('No args on multiple optional', () => { + expectValid(` + { + complicatedArgs { + multipleOpts + } + } + `); + }); + + it('One arg on multiple optional', () => { + expectValid(` + { + complicatedArgs { + multipleOpts(opt1: 1) + } + } + `); + }); + + it('Second arg on multiple optional', () => { + expectValid(` + { + complicatedArgs { + multipleOpts(opt2: 1) + } + } + `); + }); + + it('Multiple required args on mixedList', () => { + expectValid(` + { + complicatedArgs { + multipleOptAndReq(req1: 3, req2: 4) + } + } + `); + }); + + it('Multiple required and one optional arg on mixedList', () => { + expectValid(` + { + complicatedArgs { + multipleOptAndReq(req1: 3, req2: 4, opt1: 5) + } + } + `); + }); + + it('All required and optional args on mixedList', () => { + expectValid(` + { + complicatedArgs { + multipleOptAndReq(req1: 3, req2: 4, opt1: 5, opt2: 6) + } + } + `); + }); + }); + + describe('Invalid non-nullable value', () => { + it('Missing one non-nullable argument', () => { + expectErrors(` + { + complicatedArgs { + multipleReqs(req2: 2) + } + } + `).toDeepEqual([ + { + message: 'Field "multipleReqs" argument "req1" of type "Int!" is required, but it was not provided.', + locations: [{ line: 4, column: 13 }], + }, + ]); + }); + + it('Missing multiple non-nullable arguments', () => { + expectErrors(` + { + complicatedArgs { + multipleReqs + } + } + `).toDeepEqual([ + { + message: 'Field "multipleReqs" argument "req1" of type "Int!" is required, but it was not provided.', + locations: [{ line: 4, column: 13 }], + }, + { + message: 'Field "multipleReqs" argument "req2" of type "Int!" is required, but it was not provided.', + locations: [{ line: 4, column: 13 }], + }, + ]); + }); + + it('Incorrect value and missing argument', () => { + expectErrors(` + { + complicatedArgs { + multipleReqs(req1: "one") + } + } + `).toDeepEqual([ + { + message: 'Field "multipleReqs" argument "req2" of type "Int!" is required, but it was not provided.', + locations: [{ line: 4, column: 13 }], + }, + ]); + }); + }); + + describe('Directive arguments', () => { + it('ignores unknown directives', () => { + expectValid(` + { + dog @unknown + } + `); + }); + + it('with directives of valid types', () => { + expectValid(` + { + dog @include(if: true) { + name + } + human @skip(if: false) { + name + } + } + `); + }); + + it('with directive with missing types', () => { + expectErrors(` + { + dog @include { + name @skip + } + } + `).toDeepEqual([ + { + message: 'Directive "@include" argument "if" of type "Boolean!" is required, but it was not provided.', + locations: [{ line: 3, column: 15 }], + }, + { + message: 'Directive "@skip" argument "if" of type "Boolean!" is required, but it was not provided.', + locations: [{ line: 4, column: 18 }], + }, + ]); + }); + }); + + describe('within SDL', () => { + it('Missing optional args on directive defined inside SDL', () => { + expectValidSDL(` + type Query { + foo: String @test + } + + directive @test(arg1: String, arg2: String! = "") on FIELD_DEFINITION + `); + }); + + it('Missing arg on directive defined inside SDL', () => { + expectSDLErrors(` + type Query { + foo: String @test + } + + directive @test(arg: String!) on FIELD_DEFINITION + `).toDeepEqual([ + { + message: 'Directive "@test" argument "arg" of type "String!" is required, but it was not provided.', + locations: [{ line: 3, column: 23 }], + }, + ]); + }); + + it('Missing arg on standard directive', () => { + expectSDLErrors(` + type Query { + foo: String @include + } + `).toDeepEqual([ + { + message: 'Directive "@include" argument "if" of type "Boolean!" is required, but it was not provided.', + locations: [{ line: 3, column: 23 }], + }, + ]); + }); + + it('Missing arg on overridden standard directive', () => { + expectSDLErrors(` + type Query { + foo: String @deprecated + } + directive @deprecated(reason: String!) on FIELD + `).toDeepEqual([ + { + message: 'Directive "@deprecated" argument "reason" of type "String!" is required, but it was not provided.', + locations: [{ line: 3, column: 23 }], + }, + ]); + }); + + it('Missing arg on directive defined in schema extension', () => { + const schema = buildSchema(` + type Query { + foo: String + } + `); + expectSDLErrors( + ` + directive @test(arg: String!) on OBJECT + + extend type Query @test + `, + schema + ).toDeepEqual([ + { + message: 'Directive "@test" argument "arg" of type "String!" is required, but it was not provided.', + locations: [{ line: 4, column: 30 }], + }, + ]); + }); + + it('Missing arg on directive used in schema extension', () => { + const schema = buildSchema(` + directive @test(arg: String!) on OBJECT + + type Query { + foo: String + } + `); + expectSDLErrors( + ` + extend type Query @test + `, + schema + ).toDeepEqual([ + { + message: 'Directive "@test" argument "arg" of type "String!" is required, but it was not provided.', + locations: [{ line: 2, column: 29 }], + }, + ]); + }); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/ScalarLeafsRule-test.ts b/packages/graphql/src/validation/__tests__/ScalarLeafsRule-test.ts new file mode 100644 index 00000000000..21db2040836 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/ScalarLeafsRule-test.ts @@ -0,0 +1,120 @@ +import { ScalarLeafsRule } from '../rules/ScalarLeafsRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(ScalarLeafsRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Scalar leafs', () => { + it('valid scalar selection', () => { + expectValid(` + fragment scalarSelection on Dog { + barks + } + `); + }); + + it('object type missing selection', () => { + expectErrors(` + query directQueryOnObjectWithoutSubFields { + human + } + `).toDeepEqual([ + { + message: 'Field "human" of type "Human" must have a selection of subfields. Did you mean "human { ... }"?', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('interface type missing selection', () => { + expectErrors(` + { + human { pets } + } + `).toDeepEqual([ + { + message: 'Field "pets" of type "[Pet]" must have a selection of subfields. Did you mean "pets { ... }"?', + locations: [{ line: 3, column: 17 }], + }, + ]); + }); + + it('valid scalar selection with args', () => { + expectValid(` + fragment scalarSelectionWithArgs on Dog { + doesKnowCommand(dogCommand: SIT) + } + `); + }); + + it('scalar selection not allowed on Boolean', () => { + expectErrors(` + fragment scalarSelectionsNotAllowedOnBoolean on Dog { + barks { sinceWhen } + } + `).toDeepEqual([ + { + message: 'Field "barks" must not have a selection since type "Boolean" has no subfields.', + locations: [{ line: 3, column: 15 }], + }, + ]); + }); + + it('scalar selection not allowed on Enum', () => { + expectErrors(` + fragment scalarSelectionsNotAllowedOnEnum on Cat { + furColor { inHexDec } + } + `).toDeepEqual([ + { + message: 'Field "furColor" must not have a selection since type "FurColor" has no subfields.', + locations: [{ line: 3, column: 18 }], + }, + ]); + }); + + it('scalar selection not allowed with args', () => { + expectErrors(` + fragment scalarSelectionsNotAllowedWithArgs on Dog { + doesKnowCommand(dogCommand: SIT) { sinceWhen } + } + `).toDeepEqual([ + { + message: 'Field "doesKnowCommand" must not have a selection since type "Boolean" has no subfields.', + locations: [{ line: 3, column: 42 }], + }, + ]); + }); + + it('Scalar selection not allowed with directives', () => { + expectErrors(` + fragment scalarSelectionsNotAllowedWithDirectives on Dog { + name @include(if: true) { isAlsoHumanName } + } + `).toDeepEqual([ + { + message: 'Field "name" must not have a selection since type "String" has no subfields.', + locations: [{ line: 3, column: 33 }], + }, + ]); + }); + + it('Scalar selection not allowed with directives and args', () => { + expectErrors(` + fragment scalarSelectionsNotAllowedWithDirectivesAndArgs on Dog { + doesKnowCommand(dogCommand: SIT) @include(if: true) { sinceWhen } + } + `).toDeepEqual([ + { + message: 'Field "doesKnowCommand" must not have a selection since type "Boolean" has no subfields.', + locations: [{ line: 3, column: 61 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/SingleFieldSubscriptionsRule-test.ts b/packages/graphql/src/validation/__tests__/SingleFieldSubscriptionsRule-test.ts new file mode 100644 index 00000000000..9a00c6a5b96 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/SingleFieldSubscriptionsRule-test.ts @@ -0,0 +1,291 @@ +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { SingleFieldSubscriptionsRule } from '../rules/SingleFieldSubscriptionsRule.js'; + +import { expectValidationErrorsWithSchema } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrorsWithSchema(schema, SingleFieldSubscriptionsRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +const schema = buildSchema(` + type Message { + body: String + sender: String + } + + type SubscriptionRoot { + importantEmails: [String] + notImportantEmails: [String] + moreImportantEmails: [String] + spamEmails: [String] + deletedEmails: [String] + newMessage: Message + } + + type QueryRoot { + dummy: String + } + + schema { + query: QueryRoot + subscription: SubscriptionRoot + } +`); + +describe('Validate: Subscriptions with single field', () => { + it('valid subscription', () => { + expectValid(` + subscription ImportantEmails { + importantEmails + } + `); + }); + + it('valid subscription with fragment', () => { + // From https://spec.graphql.org/draft/#example-13061 + expectValid(` + subscription sub { + ...newMessageFields + } + + fragment newMessageFields on SubscriptionRoot { + newMessage { + body + sender + } + } + `); + }); + + it('valid subscription with fragment and field', () => { + // From https://spec.graphql.org/draft/#example-13061 + expectValid(` + subscription sub { + newMessage { + body + } + ...newMessageFields + } + + fragment newMessageFields on SubscriptionRoot { + newMessage { + body + sender + } + } + `); + }); + + it('fails with more than one root field', () => { + expectErrors(` + subscription ImportantEmails { + importantEmails + notImportantEmails + } + `).toDeepEqual([ + { + message: 'Subscription "ImportantEmails" must select only one top level field.', + locations: [{ line: 4, column: 9 }], + }, + ]); + }); + + it('fails with more than one root field including introspection', () => { + expectErrors(` + subscription ImportantEmails { + importantEmails + __typename + } + `).toDeepEqual([ + { + message: 'Subscription "ImportantEmails" must select only one top level field.', + locations: [{ line: 4, column: 9 }], + }, + { + message: 'Subscription "ImportantEmails" must not select an introspection top level field.', + locations: [{ line: 4, column: 9 }], + }, + ]); + }); + + it('fails with more than one root field including aliased introspection via fragment', () => { + expectErrors(` + subscription ImportantEmails { + importantEmails + ...Introspection + } + fragment Introspection on SubscriptionRoot { + typename: __typename + } + `).toDeepEqual([ + { + message: 'Subscription "ImportantEmails" must select only one top level field.', + locations: [{ line: 7, column: 9 }], + }, + { + message: 'Subscription "ImportantEmails" must not select an introspection top level field.', + locations: [{ line: 7, column: 9 }], + }, + ]); + }); + + it('fails with many more than one root field', () => { + expectErrors(` + subscription ImportantEmails { + importantEmails + notImportantEmails + spamEmails + } + `).toDeepEqual([ + { + message: 'Subscription "ImportantEmails" must select only one top level field.', + locations: [ + { line: 4, column: 9 }, + { line: 5, column: 9 }, + ], + }, + ]); + }); + + it('fails with many more than one root field via fragments', () => { + expectErrors(` + subscription ImportantEmails { + importantEmails + ... { + more: moreImportantEmails + } + ...NotImportantEmails + } + fragment NotImportantEmails on SubscriptionRoot { + notImportantEmails + deleted: deletedEmails + ...SpamEmails + } + fragment SpamEmails on SubscriptionRoot { + spamEmails + } + `).toDeepEqual([ + { + message: 'Subscription "ImportantEmails" must select only one top level field.', + locations: [ + { line: 5, column: 11 }, + { line: 10, column: 9 }, + { line: 11, column: 9 }, + { line: 15, column: 9 }, + ], + }, + ]); + }); + + it('does not infinite loop on recursive fragments', () => { + expectErrors(` + subscription NoInfiniteLoop { + ...A + } + fragment A on SubscriptionRoot { + ...A + } + `).toDeepEqual([]); + }); + + it('fails with many more than one root field via fragments (anonymous)', () => { + expectErrors(` + subscription { + importantEmails + ... { + more: moreImportantEmails + ...NotImportantEmails + } + ...NotImportantEmails + } + fragment NotImportantEmails on SubscriptionRoot { + notImportantEmails + deleted: deletedEmails + ... { + ... { + archivedEmails + } + } + ...SpamEmails + } + fragment SpamEmails on SubscriptionRoot { + spamEmails + ...NonExistentFragment + } + `).toDeepEqual([ + { + message: 'Anonymous Subscription must select only one top level field.', + locations: [ + { line: 5, column: 11 }, + { line: 11, column: 9 }, + { line: 12, column: 9 }, + { line: 15, column: 13 }, + { line: 21, column: 9 }, + ], + }, + ]); + }); + + it('fails with more than one root field in anonymous subscriptions', () => { + expectErrors(` + subscription { + importantEmails + notImportantEmails + } + `).toDeepEqual([ + { + message: 'Anonymous Subscription must select only one top level field.', + locations: [{ line: 4, column: 9 }], + }, + ]); + }); + + it('fails with introspection field', () => { + expectErrors(` + subscription ImportantEmails { + __typename + } + `).toDeepEqual([ + { + message: 'Subscription "ImportantEmails" must not select an introspection top level field.', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('fails with introspection field in anonymous subscription', () => { + expectErrors(` + subscription { + __typename + } + `).toDeepEqual([ + { + message: 'Anonymous Subscription must not select an introspection top level field.', + locations: [{ line: 3, column: 9 }], + }, + ]); + }); + + it('skips if not subscription type', () => { + const emptySchema = buildSchema(` + type Query { + dummy: String + } + `); + + expectValidationErrorsWithSchema( + emptySchema, + SingleFieldSubscriptionsRule, + ` + subscription { + __typename + } + ` + ).toDeepEqual([]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueArgumentDefinitionNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueArgumentDefinitionNamesRule-test.ts new file mode 100644 index 00000000000..78f04a761ee --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueArgumentDefinitionNamesRule-test.ts @@ -0,0 +1,164 @@ +import { UniqueArgumentDefinitionNamesRule } from '../rules/UniqueArgumentDefinitionNamesRule.js'; + +import { expectSDLValidationErrors } from './harness.js'; + +function expectSDLErrors(sdlStr: string) { + return expectSDLValidationErrors(undefined, UniqueArgumentDefinitionNamesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string) { + expectSDLErrors(sdlStr).toDeepEqual([]); +} + +describe('Validate: Unique argument definition names', () => { + it('no args', () => { + expectValidSDL(` + type SomeObject { + someField: String + } + + interface SomeInterface { + someField: String + } + + directive @someDirective on QUERY + `); + }); + + it('one argument', () => { + expectValidSDL(` + type SomeObject { + someField(foo: String): String + } + + interface SomeInterface { + someField(foo: String): String + } + + extend type SomeObject { + anotherField(foo: String): String + } + + extend interface SomeInterface { + anotherField(foo: String): String + } + + directive @someDirective(foo: String) on QUERY + `); + }); + + it('multiple arguments', () => { + expectValidSDL(` + type SomeObject { + someField( + foo: String + bar: String + ): String + } + + interface SomeInterface { + someField( + foo: String + bar: String + ): String + } + + extend type SomeObject { + anotherField( + foo: String + bar: String + ): String + } + + extend interface SomeInterface { + anotherField( + foo: String + bar: String + ): String + } + + directive @someDirective( + foo: String + bar: String + ) on QUERY + `); + }); + + it('duplicating arguments', () => { + expectSDLErrors(` + type SomeObject { + someField( + foo: String + bar: String + foo: String + ): String + } + + interface SomeInterface { + someField( + foo: String + bar: String + foo: String + ): String + } + + extend type SomeObject { + anotherField( + foo: String + bar: String + bar: String + ): String + } + + extend interface SomeInterface { + anotherField( + bar: String + foo: String + foo: String + ): String + } + + directive @someDirective( + foo: String + bar: String + foo: String + ) on QUERY + `).toDeepEqual([ + { + message: 'Argument "SomeObject.someField(foo:)" can only be defined once.', + locations: [ + { line: 4, column: 11 }, + { line: 6, column: 11 }, + ], + }, + { + message: 'Argument "SomeInterface.someField(foo:)" can only be defined once.', + locations: [ + { line: 12, column: 11 }, + { line: 14, column: 11 }, + ], + }, + { + message: 'Argument "SomeObject.anotherField(bar:)" can only be defined once.', + locations: [ + { line: 21, column: 11 }, + { line: 22, column: 11 }, + ], + }, + { + message: 'Argument "SomeInterface.anotherField(foo:)" can only be defined once.', + locations: [ + { line: 29, column: 11 }, + { line: 30, column: 11 }, + ], + }, + { + message: 'Argument "@someDirective(foo:)" can only be defined once.', + locations: [ + { line: 35, column: 9 }, + { line: 37, column: 9 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueArgumentNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueArgumentNamesRule-test.ts new file mode 100644 index 00000000000..a0928e8b51a --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueArgumentNamesRule-test.ts @@ -0,0 +1,152 @@ +import { UniqueArgumentNamesRule } from '../rules/UniqueArgumentNamesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(UniqueArgumentNamesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Unique argument names', () => { + it('no arguments on field', () => { + expectValid(` + { + field + } + `); + }); + + it('no arguments on directive', () => { + expectValid(` + { + field @directive + } + `); + }); + + it('argument on field', () => { + expectValid(` + { + field(arg: "value") + } + `); + }); + + it('argument on directive', () => { + expectValid(` + { + field @directive(arg: "value") + } + `); + }); + + it('same argument on two fields', () => { + expectValid(` + { + one: field(arg: "value") + two: field(arg: "value") + } + `); + }); + + it('same argument on field and directive', () => { + expectValid(` + { + field(arg: "value") @directive(arg: "value") + } + `); + }); + + it('same argument on two directives', () => { + expectValid(` + { + field @directive1(arg: "value") @directive2(arg: "value") + } + `); + }); + + it('multiple field arguments', () => { + expectValid(` + { + field(arg1: "value", arg2: "value", arg3: "value") + } + `); + }); + + it('multiple directive arguments', () => { + expectValid(` + { + field @directive(arg1: "value", arg2: "value", arg3: "value") + } + `); + }); + + it('duplicate field arguments', () => { + expectErrors(` + { + field(arg1: "value", arg1: "value") + } + `).toDeepEqual([ + { + message: 'There can be only one argument named "arg1".', + locations: [ + { line: 3, column: 15 }, + { line: 3, column: 30 }, + ], + }, + ]); + }); + + it('many duplicate field arguments', () => { + expectErrors(` + { + field(arg1: "value", arg1: "value", arg1: "value") + } + `).toDeepEqual([ + { + message: 'There can be only one argument named "arg1".', + locations: [ + { line: 3, column: 15 }, + { line: 3, column: 30 }, + { line: 3, column: 45 }, + ], + }, + ]); + }); + + it('duplicate directive arguments', () => { + expectErrors(` + { + field @directive(arg1: "value", arg1: "value") + } + `).toDeepEqual([ + { + message: 'There can be only one argument named "arg1".', + locations: [ + { line: 3, column: 26 }, + { line: 3, column: 41 }, + ], + }, + ]); + }); + + it('many duplicate directive arguments', () => { + expectErrors(` + { + field @directive(arg1: "value", arg1: "value", arg1: "value") + } + `).toDeepEqual([ + { + message: 'There can be only one argument named "arg1".', + locations: [ + { line: 3, column: 26 }, + { line: 3, column: 41 }, + { line: 3, column: 56 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueDirectiveNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueDirectiveNamesRule-test.ts new file mode 100644 index 00000000000..50e07bd881e --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueDirectiveNamesRule-test.ts @@ -0,0 +1,97 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { UniqueDirectiveNamesRule } from '../rules/UniqueDirectiveNamesRule.js'; + +import { expectSDLValidationErrors } from './harness.js'; + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, UniqueDirectiveNamesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +describe('Validate: Unique directive names', () => { + it('no directive', () => { + expectValidSDL(` + type Foo + `); + }); + + it('one directive', () => { + expectValidSDL(` + directive @foo on SCHEMA + `); + }); + + it('many directives', () => { + expectValidSDL(` + directive @foo on SCHEMA + directive @bar on SCHEMA + directive @baz on SCHEMA + `); + }); + + it('directive and non-directive definitions named the same', () => { + expectValidSDL(` + query foo { __typename } + fragment foo on foo { __typename } + type foo + + directive @foo on SCHEMA + `); + }); + + it('directives named the same', () => { + expectSDLErrors(` + directive @foo on SCHEMA + + directive @foo on SCHEMA + `).toDeepEqual([ + { + message: 'There can be only one directive named "@foo".', + locations: [ + { line: 2, column: 18 }, + { line: 4, column: 18 }, + ], + }, + ]); + }); + + it('adding new directive to existing schema', () => { + const schema = buildSchema('directive @foo on SCHEMA'); + + expectValidSDL('directive @bar on SCHEMA', schema); + }); + + it('adding new directive with standard name to existing schema', () => { + const schema = buildSchema('type foo'); + + expectSDLErrors('directive @skip on SCHEMA', schema).toDeepEqual([ + { + message: 'Directive "@skip" already exists in the schema. It cannot be redefined.', + locations: [{ line: 1, column: 12 }], + }, + ]); + }); + + it('adding new directive to existing schema with same-named type', () => { + const schema = buildSchema('type foo'); + + expectValidSDL('directive @foo on SCHEMA', schema); + }); + + it('adding conflicting directives to existing schema', () => { + const schema = buildSchema('directive @foo on SCHEMA'); + + expectSDLErrors('directive @foo on SCHEMA', schema).toDeepEqual([ + { + message: 'Directive "@foo" already exists in the schema. It cannot be redefined.', + locations: [{ line: 1, column: 12 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueDirectivesPerLocationRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueDirectivesPerLocationRule-test.ts new file mode 100644 index 00000000000..0007e85fc0d --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueDirectivesPerLocationRule-test.ts @@ -0,0 +1,356 @@ +import { parse } from '../../language/parser.js'; + +import type { GraphQLSchema } from '../../type/schema.js'; + +import { extendSchema } from '../../utilities/extendSchema.js'; + +import { UniqueDirectivesPerLocationRule } from '../rules/UniqueDirectivesPerLocationRule.js'; + +import { expectSDLValidationErrors, expectValidationErrorsWithSchema, testSchema } from './harness.js'; + +const extensionSDL = ` + directive @directive on FIELD | FRAGMENT_DEFINITION + directive @directiveA on FIELD | FRAGMENT_DEFINITION + directive @directiveB on FIELD | FRAGMENT_DEFINITION + directive @repeatable repeatable on FIELD | FRAGMENT_DEFINITION +`; +const schemaWithDirectives = extendSchema(testSchema, parse(extensionSDL)); + +function expectErrors(queryStr: string) { + return expectValidationErrorsWithSchema(schemaWithDirectives, UniqueDirectivesPerLocationRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, UniqueDirectivesPerLocationRule, sdlStr); +} + +describe('Validate: Directives Are Unique Per Location', () => { + it('no directives', () => { + expectValid(` + fragment Test on Type { + field + } + `); + }); + + it('unique directives in different locations', () => { + expectValid(` + fragment Test on Type @directiveA { + field @directiveB + } + `); + }); + + it('unique directives in same locations', () => { + expectValid(` + fragment Test on Type @directiveA @directiveB { + field @directiveA @directiveB + } + `); + }); + + it('same directives in different locations', () => { + expectValid(` + fragment Test on Type @directiveA { + field @directiveA + } + `); + }); + + it('same directives in similar locations', () => { + expectValid(` + fragment Test on Type { + field @directive + field @directive + } + `); + }); + + it('repeatable directives in same location', () => { + expectValid(` + fragment Test on Type @repeatable @repeatable { + field @repeatable @repeatable + } + `); + }); + + it('unknown directives must be ignored', () => { + expectValid(` + type Test @unknown @unknown { + field: String! @unknown @unknown + } + + extend type Test @unknown { + anotherField: String! + } + `); + }); + + it('duplicate directives in one location', () => { + expectErrors(` + fragment Test on Type { + field @directive @directive + } + `).toDeepEqual([ + { + message: 'The directive "@directive" can only be used once at this location.', + locations: [ + { line: 3, column: 15 }, + { line: 3, column: 26 }, + ], + }, + ]); + }); + + it('many duplicate directives in one location', () => { + expectErrors(` + fragment Test on Type { + field @directive @directive @directive + } + `).toDeepEqual([ + { + message: 'The directive "@directive" can only be used once at this location.', + locations: [ + { line: 3, column: 15 }, + { line: 3, column: 26 }, + ], + }, + { + message: 'The directive "@directive" can only be used once at this location.', + locations: [ + { line: 3, column: 15 }, + { line: 3, column: 37 }, + ], + }, + ]); + }); + + it('different duplicate directives in one location', () => { + expectErrors(` + fragment Test on Type { + field @directiveA @directiveB @directiveA @directiveB + } + `).toDeepEqual([ + { + message: 'The directive "@directiveA" can only be used once at this location.', + locations: [ + { line: 3, column: 15 }, + { line: 3, column: 39 }, + ], + }, + { + message: 'The directive "@directiveB" can only be used once at this location.', + locations: [ + { line: 3, column: 27 }, + { line: 3, column: 51 }, + ], + }, + ]); + }); + + it('duplicate directives in many locations', () => { + expectErrors(` + fragment Test on Type @directive @directive { + field @directive @directive + } + `).toDeepEqual([ + { + message: 'The directive "@directive" can only be used once at this location.', + locations: [ + { line: 2, column: 29 }, + { line: 2, column: 40 }, + ], + }, + { + message: 'The directive "@directive" can only be used once at this location.', + locations: [ + { line: 3, column: 15 }, + { line: 3, column: 26 }, + ], + }, + ]); + }); + + it('duplicate directives on SDL definitions', () => { + expectSDLErrors(` + directive @nonRepeatable on + SCHEMA | SCALAR | OBJECT | INTERFACE | UNION | INPUT_OBJECT + + schema @nonRepeatable @nonRepeatable { query: Dummy } + + scalar TestScalar @nonRepeatable @nonRepeatable + type TestObject @nonRepeatable @nonRepeatable + interface TestInterface @nonRepeatable @nonRepeatable + union TestUnion @nonRepeatable @nonRepeatable + input TestInput @nonRepeatable @nonRepeatable + `).toDeepEqual([ + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 5, column: 14 }, + { line: 5, column: 29 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 7, column: 25 }, + { line: 7, column: 40 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 8, column: 23 }, + { line: 8, column: 38 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 9, column: 31 }, + { line: 9, column: 46 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 10, column: 23 }, + { line: 10, column: 38 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 11, column: 23 }, + { line: 11, column: 38 }, + ], + }, + ]); + }); + + it('duplicate directives on SDL extensions', () => { + expectSDLErrors(` + directive @nonRepeatable on + SCHEMA | SCALAR | OBJECT | INTERFACE | UNION | INPUT_OBJECT + + extend schema @nonRepeatable @nonRepeatable + + extend scalar TestScalar @nonRepeatable @nonRepeatable + extend type TestObject @nonRepeatable @nonRepeatable + extend interface TestInterface @nonRepeatable @nonRepeatable + extend union TestUnion @nonRepeatable @nonRepeatable + extend input TestInput @nonRepeatable @nonRepeatable + `).toDeepEqual([ + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 5, column: 21 }, + { line: 5, column: 36 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 7, column: 32 }, + { line: 7, column: 47 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 8, column: 30 }, + { line: 8, column: 45 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 9, column: 38 }, + { line: 9, column: 53 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 10, column: 30 }, + { line: 10, column: 45 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 11, column: 30 }, + { line: 11, column: 45 }, + ], + }, + ]); + }); + + it('duplicate directives between SDL definitions and extensions', () => { + expectSDLErrors(` + directive @nonRepeatable on SCHEMA + + schema @nonRepeatable { query: Dummy } + extend schema @nonRepeatable + `).toDeepEqual([ + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 4, column: 14 }, + { line: 5, column: 21 }, + ], + }, + ]); + + expectSDLErrors(` + directive @nonRepeatable on SCALAR + + scalar TestScalar @nonRepeatable + extend scalar TestScalar @nonRepeatable + scalar TestScalar @nonRepeatable + `).toDeepEqual([ + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 4, column: 25 }, + { line: 5, column: 32 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 4, column: 25 }, + { line: 6, column: 25 }, + ], + }, + ]); + + expectSDLErrors(` + directive @nonRepeatable on OBJECT + + extend type TestObject @nonRepeatable + type TestObject @nonRepeatable + extend type TestObject @nonRepeatable + `).toDeepEqual([ + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 4, column: 30 }, + { line: 5, column: 23 }, + ], + }, + { + message: 'The directive "@nonRepeatable" can only be used once at this location.', + locations: [ + { line: 4, column: 30 }, + { line: 6, column: 30 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueEnumValueNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueEnumValueNamesRule-test.ts new file mode 100644 index 00000000000..6e55eb07e62 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueEnumValueNamesRule-test.ts @@ -0,0 +1,192 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { UniqueEnumValueNamesRule } from '../rules/UniqueEnumValueNamesRule.js'; + +import { expectSDLValidationErrors } from './harness.js'; + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, UniqueEnumValueNamesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +describe('Validate: Unique enum value names', () => { + it('no values', () => { + expectValidSDL(` + enum SomeEnum + `); + }); + + it('one value', () => { + expectValidSDL(` + enum SomeEnum { + FOO + } + `); + }); + + it('multiple values', () => { + expectValidSDL(` + enum SomeEnum { + FOO + BAR + } + `); + }); + + it('duplicate values inside the same enum definition', () => { + expectSDLErrors(` + enum SomeEnum { + FOO + BAR + FOO + } + `).toDeepEqual([ + { + message: 'Enum value "SomeEnum.FOO" can only be defined once.', + locations: [ + { line: 3, column: 9 }, + { line: 5, column: 9 }, + ], + }, + ]); + }); + + it('extend enum with new value', () => { + expectValidSDL(` + enum SomeEnum { + FOO + } + extend enum SomeEnum { + BAR + } + extend enum SomeEnum { + BAZ + } + `); + }); + + it('extend enum with duplicate value', () => { + expectSDLErrors(` + extend enum SomeEnum { + FOO + } + enum SomeEnum { + FOO + } + `).toDeepEqual([ + { + message: 'Enum value "SomeEnum.FOO" can only be defined once.', + locations: [ + { line: 3, column: 9 }, + { line: 6, column: 9 }, + ], + }, + ]); + }); + + it('duplicate value inside extension', () => { + expectSDLErrors(` + enum SomeEnum + extend enum SomeEnum { + FOO + BAR + FOO + } + `).toDeepEqual([ + { + message: 'Enum value "SomeEnum.FOO" can only be defined once.', + locations: [ + { line: 4, column: 9 }, + { line: 6, column: 9 }, + ], + }, + ]); + }); + + it('duplicate value inside different extensions', () => { + expectSDLErrors(` + enum SomeEnum + extend enum SomeEnum { + FOO + } + extend enum SomeEnum { + FOO + } + `).toDeepEqual([ + { + message: 'Enum value "SomeEnum.FOO" can only be defined once.', + locations: [ + { line: 4, column: 9 }, + { line: 7, column: 9 }, + ], + }, + ]); + }); + + it('adding new value to the type inside existing schema', () => { + const schema = buildSchema('enum SomeEnum'); + const sdl = ` + extend enum SomeEnum { + FOO + } + `; + + expectValidSDL(sdl, schema); + }); + + it('adding conflicting value to existing schema twice', () => { + const schema = buildSchema(` + enum SomeEnum { + FOO + } + `); + const sdl = ` + extend enum SomeEnum { + FOO + } + extend enum SomeEnum { + FOO + } + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: + 'Enum value "SomeEnum.FOO" already exists in the schema. It cannot also be defined in this type extension.', + locations: [{ line: 3, column: 9 }], + }, + { + message: + 'Enum value "SomeEnum.FOO" already exists in the schema. It cannot also be defined in this type extension.', + locations: [{ line: 6, column: 9 }], + }, + ]); + }); + + it('adding enum values to existing schema twice', () => { + const schema = buildSchema('enum SomeEnum'); + const sdl = ` + extend enum SomeEnum { + FOO + } + extend enum SomeEnum { + FOO + } + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: 'Enum value "SomeEnum.FOO" can only be defined once.', + locations: [ + { line: 3, column: 9 }, + { line: 6, column: 9 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueFieldDefinitionNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueFieldDefinitionNamesRule-test.ts new file mode 100644 index 00000000000..cb026639eaa --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueFieldDefinitionNamesRule-test.ts @@ -0,0 +1,429 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { UniqueFieldDefinitionNamesRule } from '../rules/UniqueFieldDefinitionNamesRule.js'; + +import { expectSDLValidationErrors } from './harness.js'; + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, UniqueFieldDefinitionNamesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +describe('Validate: Unique field definition names', () => { + it('no fields', () => { + expectValidSDL(` + type SomeObject + interface SomeInterface + input SomeInputObject + `); + }); + + it('one field', () => { + expectValidSDL(` + type SomeObject { + foo: String + } + + interface SomeInterface { + foo: String + } + + input SomeInputObject { + foo: String + } + `); + }); + + it('multiple fields', () => { + expectValidSDL(` + type SomeObject { + foo: String + bar: String + } + + interface SomeInterface { + foo: String + bar: String + } + + input SomeInputObject { + foo: String + bar: String + } + `); + }); + + it('duplicate fields inside the same type definition', () => { + expectSDLErrors(` + type SomeObject { + foo: String + bar: String + foo: String + } + + interface SomeInterface { + foo: String + bar: String + foo: String + } + + input SomeInputObject { + foo: String + bar: String + foo: String + } + `).toDeepEqual([ + { + message: 'Field "SomeObject.foo" can only be defined once.', + locations: [ + { line: 3, column: 9 }, + { line: 5, column: 9 }, + ], + }, + { + message: 'Field "SomeInterface.foo" can only be defined once.', + locations: [ + { line: 9, column: 9 }, + { line: 11, column: 9 }, + ], + }, + { + message: 'Field "SomeInputObject.foo" can only be defined once.', + locations: [ + { line: 15, column: 9 }, + { line: 17, column: 9 }, + ], + }, + ]); + }); + + it('extend type with new field', () => { + expectValidSDL(` + type SomeObject { + foo: String + } + extend type SomeObject { + bar: String + } + extend type SomeObject { + baz: String + } + + interface SomeInterface { + foo: String + } + extend interface SomeInterface { + bar: String + } + extend interface SomeInterface { + baz: String + } + + input SomeInputObject { + foo: String + } + extend input SomeInputObject { + bar: String + } + extend input SomeInputObject { + baz: String + } + `); + }); + + it('extend type with duplicate field', () => { + expectSDLErrors(` + extend type SomeObject { + foo: String + } + type SomeObject { + foo: String + } + + extend interface SomeInterface { + foo: String + } + interface SomeInterface { + foo: String + } + + extend input SomeInputObject { + foo: String + } + input SomeInputObject { + foo: String + } + `).toDeepEqual([ + { + message: 'Field "SomeObject.foo" can only be defined once.', + locations: [ + { line: 3, column: 9 }, + { line: 6, column: 9 }, + ], + }, + { + message: 'Field "SomeInterface.foo" can only be defined once.', + locations: [ + { line: 10, column: 9 }, + { line: 13, column: 9 }, + ], + }, + { + message: 'Field "SomeInputObject.foo" can only be defined once.', + locations: [ + { line: 17, column: 9 }, + { line: 20, column: 9 }, + ], + }, + ]); + }); + + it('duplicate field inside extension', () => { + expectSDLErrors(` + type SomeObject + extend type SomeObject { + foo: String + bar: String + foo: String + } + + interface SomeInterface + extend interface SomeInterface { + foo: String + bar: String + foo: String + } + + input SomeInputObject + extend input SomeInputObject { + foo: String + bar: String + foo: String + } + `).toDeepEqual([ + { + message: 'Field "SomeObject.foo" can only be defined once.', + locations: [ + { line: 4, column: 9 }, + { line: 6, column: 9 }, + ], + }, + { + message: 'Field "SomeInterface.foo" can only be defined once.', + locations: [ + { line: 11, column: 9 }, + { line: 13, column: 9 }, + ], + }, + { + message: 'Field "SomeInputObject.foo" can only be defined once.', + locations: [ + { line: 18, column: 9 }, + { line: 20, column: 9 }, + ], + }, + ]); + }); + + it('duplicate field inside different extensions', () => { + expectSDLErrors(` + type SomeObject + extend type SomeObject { + foo: String + } + extend type SomeObject { + foo: String + } + + interface SomeInterface + extend interface SomeInterface { + foo: String + } + extend interface SomeInterface { + foo: String + } + + input SomeInputObject + extend input SomeInputObject { + foo: String + } + extend input SomeInputObject { + foo: String + } + `).toDeepEqual([ + { + message: 'Field "SomeObject.foo" can only be defined once.', + locations: [ + { line: 4, column: 9 }, + { line: 7, column: 9 }, + ], + }, + { + message: 'Field "SomeInterface.foo" can only be defined once.', + locations: [ + { line: 12, column: 9 }, + { line: 15, column: 9 }, + ], + }, + { + message: 'Field "SomeInputObject.foo" can only be defined once.', + locations: [ + { line: 20, column: 9 }, + { line: 23, column: 9 }, + ], + }, + ]); + }); + + it('adding new field to the type inside existing schema', () => { + const schema = buildSchema(` + type SomeObject + interface SomeInterface + input SomeInputObject + `); + const sdl = ` + extend type SomeObject { + foo: String + } + + extend interface SomeInterface { + foo: String + } + + extend input SomeInputObject { + foo: String + } + `; + + expectValidSDL(sdl, schema); + }); + + it('adding conflicting fields to existing schema twice', () => { + const schema = buildSchema(` + type SomeObject { + foo: String + } + + interface SomeInterface { + foo: String + } + + input SomeInputObject { + foo: String + } + `); + const sdl = ` + extend type SomeObject { + foo: String + } + extend interface SomeInterface { + foo: String + } + extend input SomeInputObject { + foo: String + } + + extend type SomeObject { + foo: String + } + extend interface SomeInterface { + foo: String + } + extend input SomeInputObject { + foo: String + } + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: + 'Field "SomeObject.foo" already exists in the schema. It cannot also be defined in this type extension.', + locations: [{ line: 3, column: 9 }], + }, + { + message: + 'Field "SomeInterface.foo" already exists in the schema. It cannot also be defined in this type extension.', + locations: [{ line: 6, column: 9 }], + }, + { + message: + 'Field "SomeInputObject.foo" already exists in the schema. It cannot also be defined in this type extension.', + locations: [{ line: 9, column: 9 }], + }, + { + message: + 'Field "SomeObject.foo" already exists in the schema. It cannot also be defined in this type extension.', + locations: [{ line: 13, column: 9 }], + }, + { + message: + 'Field "SomeInterface.foo" already exists in the schema. It cannot also be defined in this type extension.', + locations: [{ line: 16, column: 9 }], + }, + { + message: + 'Field "SomeInputObject.foo" already exists in the schema. It cannot also be defined in this type extension.', + locations: [{ line: 19, column: 9 }], + }, + ]); + }); + + it('adding fields to existing schema twice', () => { + const schema = buildSchema(` + type SomeObject + interface SomeInterface + input SomeInputObject + `); + const sdl = ` + extend type SomeObject { + foo: String + } + extend type SomeObject { + foo: String + } + + extend interface SomeInterface { + foo: String + } + extend interface SomeInterface { + foo: String + } + + extend input SomeInputObject { + foo: String + } + extend input SomeInputObject { + foo: String + } + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: 'Field "SomeObject.foo" can only be defined once.', + locations: [ + { line: 3, column: 9 }, + { line: 6, column: 9 }, + ], + }, + { + message: 'Field "SomeInterface.foo" can only be defined once.', + locations: [ + { line: 10, column: 9 }, + { line: 13, column: 9 }, + ], + }, + { + message: 'Field "SomeInputObject.foo" can only be defined once.', + locations: [ + { line: 17, column: 9 }, + { line: 20, column: 9 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueFragmentNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueFragmentNamesRule-test.ts new file mode 100644 index 00000000000..e1dce2f3a40 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueFragmentNamesRule-test.ts @@ -0,0 +1,117 @@ +import { UniqueFragmentNamesRule } from '../rules/UniqueFragmentNamesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(UniqueFragmentNamesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Unique fragment names', () => { + it('no fragments', () => { + expectValid(` + { + field + } + `); + }); + + it('one fragment', () => { + expectValid(` + { + ...fragA + } + + fragment fragA on Type { + field + } + `); + }); + + it('many fragments', () => { + expectValid(` + { + ...fragA + ...fragB + ...fragC + } + fragment fragA on Type { + fieldA + } + fragment fragB on Type { + fieldB + } + fragment fragC on Type { + fieldC + } + `); + }); + + it('inline fragments are always unique', () => { + expectValid(` + { + ...on Type { + fieldA + } + ...on Type { + fieldB + } + } + `); + }); + + it('fragment and operation named the same', () => { + expectValid(` + query Foo { + ...Foo + } + fragment Foo on Type { + field + } + `); + }); + + it('fragments named the same', () => { + expectErrors(` + { + ...fragA + } + fragment fragA on Type { + fieldA + } + fragment fragA on Type { + fieldB + } + `).toDeepEqual([ + { + message: 'There can be only one fragment named "fragA".', + locations: [ + { line: 5, column: 16 }, + { line: 8, column: 16 }, + ], + }, + ]); + }); + + it('fragments named the same without being referenced', () => { + expectErrors(` + fragment fragA on Type { + fieldA + } + fragment fragA on Type { + fieldB + } + `).toDeepEqual([ + { + message: 'There can be only one fragment named "fragA".', + locations: [ + { line: 2, column: 16 }, + { line: 5, column: 16 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueInputFieldNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueInputFieldNamesRule-test.ts new file mode 100644 index 00000000000..fc0d6d7cb5b --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueInputFieldNamesRule-test.ts @@ -0,0 +1,108 @@ +import { UniqueInputFieldNamesRule } from '../rules/UniqueInputFieldNamesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(UniqueInputFieldNamesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Unique input field names', () => { + it('input object with fields', () => { + expectValid(` + { + field(arg: { f: true }) + } + `); + }); + + it('same input object within two args', () => { + expectValid(` + { + field(arg1: { f: true }, arg2: { f: true }) + } + `); + }); + + it('multiple input object fields', () => { + expectValid(` + { + field(arg: { f1: "value", f2: "value", f3: "value" }) + } + `); + }); + + it('allows for nested input objects with similar fields', () => { + expectValid(` + { + field(arg: { + deep: { + deep: { + id: 1 + } + id: 1 + } + id: 1 + }) + } + `); + }); + + it('duplicate input object fields', () => { + expectErrors(` + { + field(arg: { f1: "value", f1: "value" }) + } + `).toDeepEqual([ + { + message: 'There can be only one input field named "f1".', + locations: [ + { line: 3, column: 22 }, + { line: 3, column: 35 }, + ], + }, + ]); + }); + + it('many duplicate input object fields', () => { + expectErrors(` + { + field(arg: { f1: "value", f1: "value", f1: "value" }) + } + `).toDeepEqual([ + { + message: 'There can be only one input field named "f1".', + locations: [ + { line: 3, column: 22 }, + { line: 3, column: 35 }, + ], + }, + { + message: 'There can be only one input field named "f1".', + locations: [ + { line: 3, column: 22 }, + { line: 3, column: 48 }, + ], + }, + ]); + }); + + it('nested duplicate input object fields', () => { + expectErrors(` + { + field(arg: { f1: {f2: "value", f2: "value" }}) + } + `).toDeepEqual([ + { + message: 'There can be only one input field named "f2".', + locations: [ + { line: 3, column: 27 }, + { line: 3, column: 40 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueOperationNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueOperationNamesRule-test.ts new file mode 100644 index 00000000000..c4f9f46aa91 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueOperationNamesRule-test.ts @@ -0,0 +1,133 @@ +import { UniqueOperationNamesRule } from '../rules/UniqueOperationNamesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(UniqueOperationNamesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Unique operation names', () => { + it('no operations', () => { + expectValid(` + fragment fragA on Type { + field + } + `); + }); + + it('one anon operation', () => { + expectValid(` + { + field + } + `); + }); + + it('one named operation', () => { + expectValid(` + query Foo { + field + } + `); + }); + + it('multiple operations', () => { + expectValid(` + query Foo { + field + } + + query Bar { + field + } + `); + }); + + it('multiple operations of different types', () => { + expectValid(` + query Foo { + field + } + + mutation Bar { + field + } + + subscription Baz { + field + } + `); + }); + + it('fragment and operation named the same', () => { + expectValid(` + query Foo { + ...Foo + } + fragment Foo on Type { + field + } + `); + }); + + it('multiple operations of same name', () => { + expectErrors(` + query Foo { + fieldA + } + query Foo { + fieldB + } + `).toDeepEqual([ + { + message: 'There can be only one operation named "Foo".', + locations: [ + { line: 2, column: 13 }, + { line: 5, column: 13 }, + ], + }, + ]); + }); + + it('multiple ops of same name of different types (mutation)', () => { + expectErrors(` + query Foo { + fieldA + } + mutation Foo { + fieldB + } + `).toDeepEqual([ + { + message: 'There can be only one operation named "Foo".', + locations: [ + { line: 2, column: 13 }, + { line: 5, column: 16 }, + ], + }, + ]); + }); + + it('multiple ops of same name of different types (subscription)', () => { + expectErrors(` + query Foo { + fieldA + } + subscription Foo { + fieldB + } + `).toDeepEqual([ + { + message: 'There can be only one operation named "Foo".', + locations: [ + { line: 2, column: 13 }, + { line: 5, column: 20 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueOperationTypesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueOperationTypesRule-test.ts new file mode 100644 index 00000000000..47ef7d92002 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueOperationTypesRule-test.ts @@ -0,0 +1,373 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { UniqueOperationTypesRule } from '../rules/UniqueOperationTypesRule.js'; + +import { expectSDLValidationErrors } from './harness.js'; + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, UniqueOperationTypesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +describe('Validate: Unique operation types', () => { + it('no schema definition', () => { + expectValidSDL(` + type Foo + `); + }); + + it('schema definition with all types', () => { + expectValidSDL(` + type Foo + + schema { + query: Foo + mutation: Foo + subscription: Foo + } + `); + }); + + it('schema definition with single extension', () => { + expectValidSDL(` + type Foo + + schema { query: Foo } + + extend schema { + mutation: Foo + subscription: Foo + } + `); + }); + + it('schema definition with separate extensions', () => { + expectValidSDL(` + type Foo + + schema { query: Foo } + extend schema { mutation: Foo } + extend schema { subscription: Foo } + `); + }); + + it('extend schema before definition', () => { + expectValidSDL(` + type Foo + + extend schema { mutation: Foo } + extend schema { subscription: Foo } + + schema { query: Foo } + `); + }); + + it('duplicate operation types inside single schema definition', () => { + expectSDLErrors(` + type Foo + + schema { + query: Foo + mutation: Foo + subscription: Foo + + query: Foo + mutation: Foo + subscription: Foo + } + `).toDeepEqual([ + { + message: 'There can be only one query type in schema.', + locations: [ + { line: 5, column: 9 }, + { line: 9, column: 9 }, + ], + }, + { + message: 'There can be only one mutation type in schema.', + locations: [ + { line: 6, column: 9 }, + { line: 10, column: 9 }, + ], + }, + { + message: 'There can be only one subscription type in schema.', + locations: [ + { line: 7, column: 9 }, + { line: 11, column: 9 }, + ], + }, + ]); + }); + + it('duplicate operation types inside schema extension', () => { + expectSDLErrors(` + type Foo + + schema { + query: Foo + mutation: Foo + subscription: Foo + } + + extend schema { + query: Foo + mutation: Foo + subscription: Foo + } + `).toDeepEqual([ + { + message: 'There can be only one query type in schema.', + locations: [ + { line: 5, column: 9 }, + { line: 11, column: 9 }, + ], + }, + { + message: 'There can be only one mutation type in schema.', + locations: [ + { line: 6, column: 9 }, + { line: 12, column: 9 }, + ], + }, + { + message: 'There can be only one subscription type in schema.', + locations: [ + { line: 7, column: 9 }, + { line: 13, column: 9 }, + ], + }, + ]); + }); + + it('duplicate operation types inside schema extension twice', () => { + expectSDLErrors(` + type Foo + + schema { + query: Foo + mutation: Foo + subscription: Foo + } + + extend schema { + query: Foo + mutation: Foo + subscription: Foo + } + + extend schema { + query: Foo + mutation: Foo + subscription: Foo + } + `).toDeepEqual([ + { + message: 'There can be only one query type in schema.', + locations: [ + { line: 5, column: 9 }, + { line: 11, column: 9 }, + ], + }, + { + message: 'There can be only one mutation type in schema.', + locations: [ + { line: 6, column: 9 }, + { line: 12, column: 9 }, + ], + }, + { + message: 'There can be only one subscription type in schema.', + locations: [ + { line: 7, column: 9 }, + { line: 13, column: 9 }, + ], + }, + { + message: 'There can be only one query type in schema.', + locations: [ + { line: 5, column: 9 }, + { line: 17, column: 9 }, + ], + }, + { + message: 'There can be only one mutation type in schema.', + locations: [ + { line: 6, column: 9 }, + { line: 18, column: 9 }, + ], + }, + { + message: 'There can be only one subscription type in schema.', + locations: [ + { line: 7, column: 9 }, + { line: 19, column: 9 }, + ], + }, + ]); + }); + + it('duplicate operation types inside second schema extension', () => { + expectSDLErrors(` + type Foo + + schema { + query: Foo + } + + extend schema { + mutation: Foo + subscription: Foo + } + + extend schema { + query: Foo + mutation: Foo + subscription: Foo + } + `).toDeepEqual([ + { + message: 'There can be only one query type in schema.', + locations: [ + { line: 5, column: 9 }, + { line: 14, column: 9 }, + ], + }, + { + message: 'There can be only one mutation type in schema.', + locations: [ + { line: 9, column: 9 }, + { line: 15, column: 9 }, + ], + }, + { + message: 'There can be only one subscription type in schema.', + locations: [ + { line: 10, column: 9 }, + { line: 16, column: 9 }, + ], + }, + ]); + }); + + it('define schema inside extension SDL', () => { + const schema = buildSchema('type Foo'); + const sdl = ` + schema { + query: Foo + mutation: Foo + subscription: Foo + } + `; + + expectValidSDL(sdl, schema); + }); + + it('define and extend schema inside extension SDL', () => { + const schema = buildSchema('type Foo'); + const sdl = ` + schema { query: Foo } + extend schema { mutation: Foo } + extend schema { subscription: Foo } + `; + + expectValidSDL(sdl, schema); + }); + + it('adding new operation types to existing schema', () => { + const schema = buildSchema('type Query'); + const sdl = ` + extend schema { mutation: Foo } + extend schema { subscription: Foo } + `; + + expectValidSDL(sdl, schema); + }); + + it('adding conflicting operation types to existing schema', () => { + const schema = buildSchema(` + type Query + type Mutation + type Subscription + + type Foo + `); + + const sdl = ` + extend schema { + query: Foo + mutation: Foo + subscription: Foo + } + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: 'Type for query already defined in the schema. It cannot be redefined.', + locations: [{ line: 3, column: 9 }], + }, + { + message: 'Type for mutation already defined in the schema. It cannot be redefined.', + locations: [{ line: 4, column: 9 }], + }, + { + message: 'Type for subscription already defined in the schema. It cannot be redefined.', + locations: [{ line: 5, column: 9 }], + }, + ]); + }); + + it('adding conflicting operation types to existing schema twice', () => { + const schema = buildSchema(` + type Query + type Mutation + type Subscription + `); + + const sdl = ` + extend schema { + query: Foo + mutation: Foo + subscription: Foo + } + + extend schema { + query: Foo + mutation: Foo + subscription: Foo + } + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: 'Type for query already defined in the schema. It cannot be redefined.', + locations: [{ line: 3, column: 9 }], + }, + { + message: 'Type for mutation already defined in the schema. It cannot be redefined.', + locations: [{ line: 4, column: 9 }], + }, + { + message: 'Type for subscription already defined in the schema. It cannot be redefined.', + locations: [{ line: 5, column: 9 }], + }, + { + message: 'Type for query already defined in the schema. It cannot be redefined.', + locations: [{ line: 9, column: 9 }], + }, + { + message: 'Type for mutation already defined in the schema. It cannot be redefined.', + locations: [{ line: 10, column: 9 }], + }, + { + message: 'Type for subscription already defined in the schema. It cannot be redefined.', + locations: [{ line: 11, column: 9 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueTypeNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueTypeNamesRule-test.ts new file mode 100644 index 00000000000..e74c4a5626d --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueTypeNamesRule-test.ts @@ -0,0 +1,154 @@ +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { UniqueTypeNamesRule } from '../rules/UniqueTypeNamesRule.js'; + +import { expectSDLValidationErrors } from './harness.js'; + +function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) { + return expectSDLValidationErrors(schema, UniqueTypeNamesRule, sdlStr); +} + +function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) { + expectSDLErrors(sdlStr, schema).toDeepEqual([]); +} + +describe('Validate: Unique type names', () => { + it('no types', () => { + expectValidSDL(` + directive @test on SCHEMA + `); + }); + + it('one type', () => { + expectValidSDL(` + type Foo + `); + }); + + it('many types', () => { + expectValidSDL(` + type Foo + type Bar + type Baz + `); + }); + + it('type and non-type definitions named the same', () => { + expectValidSDL(` + query Foo { __typename } + fragment Foo on Query { __typename } + directive @Foo on SCHEMA + + type Foo + `); + }); + + it('types named the same', () => { + expectSDLErrors(` + type Foo + + scalar Foo + type Foo + interface Foo + union Foo + enum Foo + input Foo + `).toDeepEqual([ + { + message: 'There can be only one type named "Foo".', + locations: [ + { line: 2, column: 12 }, + { line: 4, column: 14 }, + ], + }, + { + message: 'There can be only one type named "Foo".', + locations: [ + { line: 2, column: 12 }, + { line: 5, column: 12 }, + ], + }, + { + message: 'There can be only one type named "Foo".', + locations: [ + { line: 2, column: 12 }, + { line: 6, column: 17 }, + ], + }, + { + message: 'There can be only one type named "Foo".', + locations: [ + { line: 2, column: 12 }, + { line: 7, column: 13 }, + ], + }, + { + message: 'There can be only one type named "Foo".', + locations: [ + { line: 2, column: 12 }, + { line: 8, column: 12 }, + ], + }, + { + message: 'There can be only one type named "Foo".', + locations: [ + { line: 2, column: 12 }, + { line: 9, column: 13 }, + ], + }, + ]); + }); + + it('adding new type to existing schema', () => { + const schema = buildSchema('type Foo'); + + expectValidSDL('type Bar', schema); + }); + + it('adding new type to existing schema with same-named directive', () => { + const schema = buildSchema('directive @Foo on SCHEMA'); + + expectValidSDL('type Foo', schema); + }); + + it('adding conflicting types to existing schema', () => { + const schema = buildSchema('type Foo'); + const sdl = ` + scalar Foo + type Foo + interface Foo + union Foo + enum Foo + input Foo + `; + + expectSDLErrors(sdl, schema).toDeepEqual([ + { + message: 'Type "Foo" already exists in the schema. It cannot also be defined in this type definition.', + locations: [{ line: 2, column: 14 }], + }, + { + message: 'Type "Foo" already exists in the schema. It cannot also be defined in this type definition.', + locations: [{ line: 3, column: 12 }], + }, + { + message: 'Type "Foo" already exists in the schema. It cannot also be defined in this type definition.', + locations: [{ line: 4, column: 17 }], + }, + { + message: 'Type "Foo" already exists in the schema. It cannot also be defined in this type definition.', + locations: [{ line: 5, column: 13 }], + }, + { + message: 'Type "Foo" already exists in the schema. It cannot also be defined in this type definition.', + locations: [{ line: 6, column: 12 }], + }, + { + message: 'Type "Foo" already exists in the schema. It cannot also be defined in this type definition.', + locations: [{ line: 7, column: 13 }], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/UniqueVariableNamesRule-test.ts b/packages/graphql/src/validation/__tests__/UniqueVariableNamesRule-test.ts new file mode 100644 index 00000000000..652c0154ca0 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/UniqueVariableNamesRule-test.ts @@ -0,0 +1,51 @@ +import { UniqueVariableNamesRule } from '../rules/UniqueVariableNamesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(UniqueVariableNamesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Unique variable names', () => { + it('unique variable names', () => { + expectValid(` + query A($x: Int, $y: String) { __typename } + query B($x: String, $y: Int) { __typename } + `); + }); + + it('duplicate variable names', () => { + expectErrors(` + query A($x: Int, $x: Int, $x: String) { __typename } + query B($x: String, $x: Int) { __typename } + query C($x: Int, $x: Int) { __typename } + `).toDeepEqual([ + { + message: 'There can be only one variable named "$x".', + locations: [ + { line: 2, column: 16 }, + { line: 2, column: 25 }, + { line: 2, column: 34 }, + ], + }, + { + message: 'There can be only one variable named "$x".', + locations: [ + { line: 3, column: 16 }, + { line: 3, column: 28 }, + ], + }, + { + message: 'There can be only one variable named "$x".', + locations: [ + { line: 4, column: 16 }, + { line: 4, column: 25 }, + ], + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/ValidationContext-test.ts b/packages/graphql/src/validation/__tests__/ValidationContext-test.ts new file mode 100644 index 00000000000..fd29219da86 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/ValidationContext-test.ts @@ -0,0 +1,27 @@ +import { identityFunc } from '../../jsutils/identityFunc.js'; + +import { parse } from '../../language/parser.js'; + +import { GraphQLSchema } from '../../type/schema.js'; + +import { TypeInfo } from '../../utilities/TypeInfo.js'; + +import { ASTValidationContext, SDLValidationContext, ValidationContext } from '../ValidationContext.js'; + +describe('ValidationContext', () => { + it('can be Object.toStringified', () => { + const schema = new GraphQLSchema({}); + const typeInfo = new TypeInfo(schema); + const ast = parse('{ foo }'); + const onError = identityFunc; + + const astContext = new ASTValidationContext(ast, onError); + expect(Object.prototype.toString.call(astContext)).toEqual('[object ASTValidationContext]'); + + const sdlContext = new SDLValidationContext(ast, schema, onError); + expect(Object.prototype.toString.call(sdlContext)).toEqual('[object SDLValidationContext]'); + + const context = new ValidationContext(schema, ast, typeInfo, onError); + expect(Object.prototype.toString.call(context)).toEqual('[object ValidationContext]'); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts b/packages/graphql/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts new file mode 100644 index 00000000000..3e3f3520bbd --- /dev/null +++ b/packages/graphql/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts @@ -0,0 +1,1175 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { inspect } from '../../jsutils/inspect.js'; + +import { parse } from '../../language/parser.js'; + +import { GraphQLObjectType, GraphQLScalarType } from '../../type/definition.js'; +import { GraphQLString } from '../../type/scalars.js'; +import { GraphQLSchema } from '../../type/schema.js'; + +import { ValuesOfCorrectTypeRule } from '../rules/ValuesOfCorrectTypeRule.js'; +import { validate } from '../validate.js'; + +import { expectValidationErrors, expectValidationErrorsWithSchema } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(ValuesOfCorrectTypeRule, queryStr); +} + +function expectErrorsWithSchema(schema: GraphQLSchema, queryStr: string) { + return expectValidationErrorsWithSchema(schema, ValuesOfCorrectTypeRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +function expectValidWithSchema(schema: GraphQLSchema, queryStr: string) { + expectErrorsWithSchema(schema, queryStr).toDeepEqual([]); +} + +describe('Validate: Values of correct type', () => { + describe('Valid values', () => { + it('Good int value', () => { + expectValid(` + { + complicatedArgs { + intArgField(intArg: 2) + } + } + `); + }); + + it('Good negative int value', () => { + expectValid(` + { + complicatedArgs { + intArgField(intArg: -2) + } + } + `); + }); + + it('Good boolean value', () => { + expectValid(` + { + complicatedArgs { + booleanArgField(booleanArg: true) + } + } + `); + }); + + it('Good string value', () => { + expectValid(` + { + complicatedArgs { + stringArgField(stringArg: "foo") + } + } + `); + }); + + it('Good float value', () => { + expectValid(` + { + complicatedArgs { + floatArgField(floatArg: 1.1) + } + } + `); + }); + + it('Good negative float value', () => { + expectValid(` + { + complicatedArgs { + floatArgField(floatArg: -1.1) + } + } + `); + }); + + it('Int into Float', () => { + expectValid(` + { + complicatedArgs { + floatArgField(floatArg: 1) + } + } + `); + }); + + it('Int into ID', () => { + expectValid(` + { + complicatedArgs { + idArgField(idArg: 1) + } + } + `); + }); + + it('String into ID', () => { + expectValid(` + { + complicatedArgs { + idArgField(idArg: "someIdString") + } + } + `); + }); + + it('Good enum value', () => { + expectValid(` + { + dog { + doesKnowCommand(dogCommand: SIT) + } + } + `); + }); + + it('Enum with undefined value', () => { + expectValid(` + { + complicatedArgs { + enumArgField(enumArg: UNKNOWN) + } + } + `); + }); + + it('Enum with null value', () => { + expectValid(` + { + complicatedArgs { + enumArgField(enumArg: NO_FUR) + } + } + `); + }); + + it('null into nullable type', () => { + expectValid(` + { + complicatedArgs { + intArgField(intArg: null) + } + } + `); + + expectValid(` + { + dog(a: null, b: null, c:{ requiredField: true, intField: null }) { + name + } + } + `); + }); + }); + + describe('Invalid String values', () => { + it('Int into String', () => { + expectErrors(` + { + complicatedArgs { + stringArgField(stringArg: 1) + } + } + `).toDeepEqual([ + { + message: 'String cannot represent a non string value: 1', + locations: [{ line: 4, column: 39 }], + }, + ]); + }); + + it('Float into String', () => { + expectErrors(` + { + complicatedArgs { + stringArgField(stringArg: 1.0) + } + } + `).toDeepEqual([ + { + message: 'String cannot represent a non string value: 1.0', + locations: [{ line: 4, column: 39 }], + }, + ]); + }); + + it('Boolean into String', () => { + expectErrors(` + { + complicatedArgs { + stringArgField(stringArg: true) + } + } + `).toDeepEqual([ + { + message: 'String cannot represent a non string value: true', + locations: [{ line: 4, column: 39 }], + }, + ]); + }); + + it('Unquoted String into String', () => { + expectErrors(` + { + complicatedArgs { + stringArgField(stringArg: BAR) + } + } + `).toDeepEqual([ + { + message: 'String cannot represent a non string value: BAR', + locations: [{ line: 4, column: 39 }], + }, + ]); + }); + }); + + describe('Invalid Int values', () => { + it('String into Int', () => { + expectErrors(` + { + complicatedArgs { + intArgField(intArg: "3") + } + } + `).toDeepEqual([ + { + message: 'Int cannot represent non-integer value: "3"', + locations: [{ line: 4, column: 33 }], + }, + ]); + }); + + it('Big Int into Int', () => { + expectErrors(` + { + complicatedArgs { + intArgField(intArg: 829384293849283498239482938) + } + } + `).toDeepEqual([ + { + message: 'Int cannot represent non 32-bit signed integer value: 829384293849283498239482938', + locations: [{ line: 4, column: 33 }], + }, + ]); + }); + + it('Unquoted String into Int', () => { + expectErrors(` + { + complicatedArgs { + intArgField(intArg: FOO) + } + } + `).toDeepEqual([ + { + message: 'Int cannot represent non-integer value: FOO', + locations: [{ line: 4, column: 33 }], + }, + ]); + }); + + it('Simple Float into Int', () => { + expectErrors(` + { + complicatedArgs { + intArgField(intArg: 3.0) + } + } + `).toDeepEqual([ + { + message: 'Int cannot represent non-integer value: 3.0', + locations: [{ line: 4, column: 33 }], + }, + ]); + }); + + it('Float into Int', () => { + expectErrors(` + { + complicatedArgs { + intArgField(intArg: 3.333) + } + } + `).toDeepEqual([ + { + message: 'Int cannot represent non-integer value: 3.333', + locations: [{ line: 4, column: 33 }], + }, + ]); + }); + }); + + describe('Invalid Float values', () => { + it('String into Float', () => { + expectErrors(` + { + complicatedArgs { + floatArgField(floatArg: "3.333") + } + } + `).toDeepEqual([ + { + message: 'Float cannot represent non numeric value: "3.333"', + locations: [{ line: 4, column: 37 }], + }, + ]); + }); + + it('Boolean into Float', () => { + expectErrors(` + { + complicatedArgs { + floatArgField(floatArg: true) + } + } + `).toDeepEqual([ + { + message: 'Float cannot represent non numeric value: true', + locations: [{ line: 4, column: 37 }], + }, + ]); + }); + + it('Unquoted into Float', () => { + expectErrors(` + { + complicatedArgs { + floatArgField(floatArg: FOO) + } + } + `).toDeepEqual([ + { + message: 'Float cannot represent non numeric value: FOO', + locations: [{ line: 4, column: 37 }], + }, + ]); + }); + }); + + describe('Invalid Boolean value', () => { + it('Int into Boolean', () => { + expectErrors(` + { + complicatedArgs { + booleanArgField(booleanArg: 2) + } + } + `).toDeepEqual([ + { + message: 'Boolean cannot represent a non boolean value: 2', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('Float into Boolean', () => { + expectErrors(` + { + complicatedArgs { + booleanArgField(booleanArg: 1.0) + } + } + `).toDeepEqual([ + { + message: 'Boolean cannot represent a non boolean value: 1.0', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('String into Boolean', () => { + expectErrors(` + { + complicatedArgs { + booleanArgField(booleanArg: "true") + } + } + `).toDeepEqual([ + { + message: 'Boolean cannot represent a non boolean value: "true"', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('Unquoted into Boolean', () => { + expectErrors(` + { + complicatedArgs { + booleanArgField(booleanArg: TRUE) + } + } + `).toDeepEqual([ + { + message: 'Boolean cannot represent a non boolean value: TRUE', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + }); + + describe('Invalid ID value', () => { + it('Float into ID', () => { + expectErrors(` + { + complicatedArgs { + idArgField(idArg: 1.0) + } + } + `).toDeepEqual([ + { + message: 'ID cannot represent a non-string and non-integer value: 1.0', + locations: [{ line: 4, column: 31 }], + }, + ]); + }); + + it('Boolean into ID', () => { + expectErrors(` + { + complicatedArgs { + idArgField(idArg: true) + } + } + `).toDeepEqual([ + { + message: 'ID cannot represent a non-string and non-integer value: true', + locations: [{ line: 4, column: 31 }], + }, + ]); + }); + + it('Unquoted into ID', () => { + expectErrors(` + { + complicatedArgs { + idArgField(idArg: SOMETHING) + } + } + `).toDeepEqual([ + { + message: 'ID cannot represent a non-string and non-integer value: SOMETHING', + locations: [{ line: 4, column: 31 }], + }, + ]); + }); + }); + + describe('Invalid Enum value', () => { + it('Int into Enum', () => { + expectErrors(` + { + dog { + doesKnowCommand(dogCommand: 2) + } + } + `).toDeepEqual([ + { + message: 'Enum "DogCommand" cannot represent non-enum value: 2.', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('Float into Enum', () => { + expectErrors(` + { + dog { + doesKnowCommand(dogCommand: 1.0) + } + } + `).toDeepEqual([ + { + message: 'Enum "DogCommand" cannot represent non-enum value: 1.0.', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('String into Enum', () => { + expectErrors(` + { + dog { + doesKnowCommand(dogCommand: "SIT") + } + } + `).toDeepEqual([ + { + message: 'Enum "DogCommand" cannot represent non-enum value: "SIT". Did you mean the enum value "SIT"?', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('Boolean into Enum', () => { + expectErrors(` + { + dog { + doesKnowCommand(dogCommand: true) + } + } + `).toDeepEqual([ + { + message: 'Enum "DogCommand" cannot represent non-enum value: true.', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('Unknown Enum Value into Enum', () => { + expectErrors(` + { + dog { + doesKnowCommand(dogCommand: JUGGLE) + } + } + `).toDeepEqual([ + { + message: 'Value "JUGGLE" does not exist in "DogCommand" enum.', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('Different case Enum Value into Enum', () => { + expectErrors(` + { + dog { + doesKnowCommand(dogCommand: sit) + } + } + `).toDeepEqual([ + { + message: 'Value "sit" does not exist in "DogCommand" enum. Did you mean the enum value "SIT"?', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + }); + + describe('Valid List value', () => { + it('Good list value', () => { + expectValid(` + { + complicatedArgs { + stringListArgField(stringListArg: ["one", null, "two"]) + } + } + `); + }); + + it('Empty list value', () => { + expectValid(` + { + complicatedArgs { + stringListArgField(stringListArg: []) + } + } + `); + }); + + it('Null value', () => { + expectValid(` + { + complicatedArgs { + stringListArgField(stringListArg: null) + } + } + `); + }); + + it('Single value into List', () => { + expectValid(` + { + complicatedArgs { + stringListArgField(stringListArg: "one") + } + } + `); + }); + }); + + describe('Invalid List value', () => { + it('Incorrect item type', () => { + expectErrors(` + { + complicatedArgs { + stringListArgField(stringListArg: ["one", 2]) + } + } + `).toDeepEqual([ + { + message: 'String cannot represent a non string value: 2', + locations: [{ line: 4, column: 55 }], + }, + ]); + }); + + it('Single value of incorrect type', () => { + expectErrors(` + { + complicatedArgs { + stringListArgField(stringListArg: 1) + } + } + `).toDeepEqual([ + { + message: 'String cannot represent a non string value: 1', + locations: [{ line: 4, column: 47 }], + }, + ]); + }); + }); + + describe('Valid non-nullable value', () => { + it('Arg on optional arg', () => { + expectValid(` + { + dog { + isHouseTrained(atOtherHomes: true) + } + } + `); + }); + + it('No Arg on optional arg', () => { + expectValid(` + { + dog { + isHouseTrained + } + } + `); + }); + + it('Multiple args', () => { + expectValid(` + { + complicatedArgs { + multipleReqs(req1: 1, req2: 2) + } + } + `); + }); + + it('Multiple args reverse order', () => { + expectValid(` + { + complicatedArgs { + multipleReqs(req2: 2, req1: 1) + } + } + `); + }); + + it('No args on multiple optional', () => { + expectValid(` + { + complicatedArgs { + multipleOpts + } + } + `); + }); + + it('One arg on multiple optional', () => { + expectValid(` + { + complicatedArgs { + multipleOpts(opt1: 1) + } + } + `); + }); + + it('Second arg on multiple optional', () => { + expectValid(` + { + complicatedArgs { + multipleOpts(opt2: 1) + } + } + `); + }); + + it('Multiple required args on mixedList', () => { + expectValid(` + { + complicatedArgs { + multipleOptAndReq(req1: 3, req2: 4) + } + } + `); + }); + + it('Multiple required and one optional arg on mixedList', () => { + expectValid(` + { + complicatedArgs { + multipleOptAndReq(req1: 3, req2: 4, opt1: 5) + } + } + `); + }); + + it('All required and optional args on mixedList', () => { + expectValid(` + { + complicatedArgs { + multipleOptAndReq(req1: 3, req2: 4, opt1: 5, opt2: 6) + } + } + `); + }); + }); + + describe('Invalid non-nullable value', () => { + it('Incorrect value type', () => { + expectErrors(` + { + complicatedArgs { + multipleReqs(req2: "two", req1: "one") + } + } + `).toDeepEqual([ + { + message: 'Int cannot represent non-integer value: "two"', + locations: [{ line: 4, column: 32 }], + }, + { + message: 'Int cannot represent non-integer value: "one"', + locations: [{ line: 4, column: 45 }], + }, + ]); + }); + + it('Incorrect value and missing argument (ProvidedRequiredArgumentsRule)', () => { + expectErrors(` + { + complicatedArgs { + multipleReqs(req1: "one") + } + } + `).toDeepEqual([ + { + message: 'Int cannot represent non-integer value: "one"', + locations: [{ line: 4, column: 32 }], + }, + ]); + }); + + it('Null value', () => { + expectErrors(` + { + complicatedArgs { + multipleReqs(req1: null) + } + } + `).toDeepEqual([ + { + message: 'Expected value of type "Int!", found null.', + locations: [{ line: 4, column: 32 }], + }, + ]); + }); + }); + + describe('Valid input object value', () => { + it('Optional arg, despite required field in type', () => { + expectValid(` + { + complicatedArgs { + complexArgField + } + } + `); + }); + + it('Partial object, only required', () => { + expectValid(` + { + complicatedArgs { + complexArgField(complexArg: { requiredField: true }) + } + } + `); + }); + + it('Partial object, required field can be falsy', () => { + expectValid(` + { + complicatedArgs { + complexArgField(complexArg: { requiredField: false }) + } + } + `); + }); + + it('Partial object, including required', () => { + expectValid(` + { + complicatedArgs { + complexArgField(complexArg: { requiredField: true, intField: 4 }) + } + } + `); + }); + + it('Full object', () => { + expectValid(` + { + complicatedArgs { + complexArgField(complexArg: { + requiredField: true, + intField: 4, + stringField: "foo", + booleanField: false, + stringListField: ["one", "two"] + }) + } + } + `); + }); + + it('Full object with fields in different order', () => { + expectValid(` + { + complicatedArgs { + complexArgField(complexArg: { + stringListField: ["one", "two"], + booleanField: false, + requiredField: true, + stringField: "foo", + intField: 4, + }) + } + } + `); + }); + }); + + describe('Invalid input object value', () => { + it('Partial object, missing required', () => { + expectErrors(` + { + complicatedArgs { + complexArgField(complexArg: { intField: 4 }) + } + } + `).toDeepEqual([ + { + message: 'Field "ComplexInput.requiredField" of required type "Boolean!" was not provided.', + locations: [{ line: 4, column: 41 }], + }, + ]); + }); + + it('Partial object, invalid field type', () => { + expectErrors(` + { + complicatedArgs { + complexArgField(complexArg: { + stringListField: ["one", 2], + requiredField: true, + }) + } + } + `).toDeepEqual([ + { + message: 'String cannot represent a non string value: 2', + locations: [{ line: 5, column: 40 }], + }, + ]); + }); + + it('Partial object, null to non-null field', () => { + expectErrors(` + { + complicatedArgs { + complexArgField(complexArg: { + requiredField: true, + nonNullField: null, + }) + } + } + `).toDeepEqual([ + { + message: 'Expected value of type "Boolean!", found null.', + locations: [{ line: 6, column: 29 }], + }, + ]); + }); + + it('Partial object, unknown field arg', () => { + expectErrors(` + { + complicatedArgs { + complexArgField(complexArg: { + requiredField: true, + invalidField: "value" + }) + } + } + `).toDeepEqual([ + { + message: 'Field "invalidField" is not defined by type "ComplexInput". Did you mean "intField"?', + locations: [{ line: 6, column: 15 }], + }, + ]); + }); + + it('reports original error for custom scalar which throws', () => { + const customScalar = new GraphQLScalarType({ + name: 'Invalid', + parseValue(value) { + throw new Error(`Invalid scalar is always invalid: ${inspect(value)}`); + }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + invalidArg: { + type: GraphQLString, + args: { arg: { type: customScalar } }, + }, + }, + }), + }); + + const doc = parse('{ invalidArg(arg: 123) }'); + const errors = validate(schema, doc, [ValuesOfCorrectTypeRule]); + + expectJSON(errors).toDeepEqual([ + { + message: 'Expected value of type "Invalid", found 123; Invalid scalar is always invalid: 123', + locations: [{ line: 1, column: 19 }], + }, + ]); + + expect(errors[0]).toHaveProperty('originalError.message', 'Invalid scalar is always invalid: 123'); + }); + + it('reports error for custom scalar that returns undefined', () => { + const customScalar = new GraphQLScalarType({ + name: 'CustomScalar', + parseValue() { + return undefined; + }, + }); + + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + invalidArg: { + type: GraphQLString, + args: { arg: { type: customScalar } }, + }, + }, + }), + }); + + expectErrorsWithSchema(schema, '{ invalidArg(arg: 123) }').toDeepEqual([ + { + message: 'Expected value of type "CustomScalar", found 123.', + locations: [{ line: 1, column: 19 }], + }, + ]); + }); + + it('allows custom scalar to accept complex literals', () => { + const customScalar = new GraphQLScalarType({ name: 'Any' }); + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + anyArg: { + type: GraphQLString, + args: { arg: { type: customScalar } }, + }, + }, + }), + }); + + expectValidWithSchema( + schema, + ` + { + test1: anyArg(arg: 123) + test2: anyArg(arg: "abc") + test3: anyArg(arg: [123, "abc"]) + test4: anyArg(arg: {deep: [123, "abc"]}) + } + ` + ); + }); + }); + + describe('Directive arguments', () => { + it('with directives of valid types', () => { + expectValid(` + { + dog @include(if: true) { + name + } + human @skip(if: false) { + name + } + } + `); + }); + + it('with directive with incorrect types', () => { + expectErrors(` + { + dog @include(if: "yes") { + name @skip(if: ENUM) + } + } + `).toDeepEqual([ + { + message: 'Boolean cannot represent a non boolean value: "yes"', + locations: [{ line: 3, column: 28 }], + }, + { + message: 'Boolean cannot represent a non boolean value: ENUM', + locations: [{ line: 4, column: 28 }], + }, + ]); + }); + }); + + describe('Variable default values', () => { + it('variables with valid default values', () => { + expectValid(` + query WithDefaultValues( + $a: Int = 1, + $b: String = "ok", + $c: ComplexInput = { requiredField: true, intField: 3 } + $d: Int! = 123 + ) { + dog { name } + } + `); + }); + + it('variables with valid default null values', () => { + expectValid(` + query WithDefaultValues( + $a: Int = null, + $b: String = null, + $c: ComplexInput = { requiredField: true, intField: null } + ) { + dog { name } + } + `); + }); + + it('variables with invalid default null values', () => { + expectErrors(` + query WithDefaultValues( + $a: Int! = null, + $b: String! = null, + $c: ComplexInput = { requiredField: null, intField: null } + ) { + dog { name } + } + `).toDeepEqual([ + { + message: 'Expected value of type "Int!", found null.', + locations: [{ line: 3, column: 22 }], + }, + { + message: 'Expected value of type "String!", found null.', + locations: [{ line: 4, column: 25 }], + }, + { + message: 'Expected value of type "Boolean!", found null.', + locations: [{ line: 5, column: 47 }], + }, + ]); + }); + + it('variables with invalid default values', () => { + expectErrors(` + query InvalidDefaultValues( + $a: Int = "one", + $b: String = 4, + $c: ComplexInput = "NotVeryComplex" + ) { + dog { name } + } + `).toDeepEqual([ + { + message: 'Int cannot represent non-integer value: "one"', + locations: [{ line: 3, column: 21 }], + }, + { + message: 'String cannot represent a non string value: 4', + locations: [{ line: 4, column: 24 }], + }, + { + message: 'Expected value of type "ComplexInput", found "NotVeryComplex".', + locations: [{ line: 5, column: 30 }], + }, + ]); + }); + + it('variables with complex invalid default values', () => { + expectErrors(` + query WithDefaultValues( + $a: ComplexInput = { requiredField: 123, intField: "abc" } + ) { + dog { name } + } + `).toDeepEqual([ + { + message: 'Boolean cannot represent a non boolean value: 123', + locations: [{ line: 3, column: 47 }], + }, + { + message: 'Int cannot represent non-integer value: "abc"', + locations: [{ line: 3, column: 62 }], + }, + ]); + }); + + it('complex variables missing required field', () => { + expectErrors(` + query MissingRequiredField($a: ComplexInput = {intField: 3}) { + dog { name } + } + `).toDeepEqual([ + { + message: 'Field "ComplexInput.requiredField" of required type "Boolean!" was not provided.', + locations: [{ line: 2, column: 55 }], + }, + ]); + }); + + it('list variables with invalid item', () => { + expectErrors(` + query InvalidItem($a: [String] = ["one", 2]) { + dog { name } + } + `).toDeepEqual([ + { + message: 'String cannot represent a non string value: 2', + locations: [{ line: 2, column: 50 }], + }, + ]); + }); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/VariablesAreInputTypesRule-test.ts b/packages/graphql/src/validation/__tests__/VariablesAreInputTypesRule-test.ts new file mode 100644 index 00000000000..2834324b4a4 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/VariablesAreInputTypesRule-test.ts @@ -0,0 +1,50 @@ +import { VariablesAreInputTypesRule } from '../rules/VariablesAreInputTypesRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(VariablesAreInputTypesRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Variables are input types', () => { + it('unknown types are ignored', () => { + expectValid(` + query Foo($a: Unknown, $b: [[Unknown!]]!) { + field(a: $a, b: $b) + } + `); + }); + + it('input types are valid', () => { + expectValid(` + query Foo($a: String, $b: [Boolean!]!, $c: ComplexInput) { + field(a: $a, b: $b, c: $c) + } + `); + }); + + it('output types are invalid', () => { + expectErrors(` + query Foo($a: Dog, $b: [[CatOrDog!]]!, $c: Pet) { + field(a: $a, b: $b, c: $c) + } + `).toDeepEqual([ + { + locations: [{ line: 2, column: 21 }], + message: 'Variable "$a" cannot be non-input type "Dog".', + }, + { + locations: [{ line: 2, column: 30 }], + message: 'Variable "$b" cannot be non-input type "[[CatOrDog!]]!".', + }, + { + locations: [{ line: 2, column: 50 }], + message: 'Variable "$c" cannot be non-input type "Pet".', + }, + ]); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/VariablesInAllowedPositionRule-test.ts b/packages/graphql/src/validation/__tests__/VariablesInAllowedPositionRule-test.ts new file mode 100644 index 00000000000..5b3d320f5ea --- /dev/null +++ b/packages/graphql/src/validation/__tests__/VariablesInAllowedPositionRule-test.ts @@ -0,0 +1,349 @@ +import { VariablesInAllowedPositionRule } from '../rules/VariablesInAllowedPositionRule.js'; + +import { expectValidationErrors } from './harness.js'; + +function expectErrors(queryStr: string) { + return expectValidationErrors(VariablesInAllowedPositionRule, queryStr); +} + +function expectValid(queryStr: string) { + expectErrors(queryStr).toDeepEqual([]); +} + +describe('Validate: Variables are in allowed positions', () => { + it('Boolean => Boolean', () => { + expectValid(` + query Query($booleanArg: Boolean) + { + complicatedArgs { + booleanArgField(booleanArg: $booleanArg) + } + } + `); + }); + + it('Boolean => Boolean within fragment', () => { + expectValid(` + fragment booleanArgFrag on ComplicatedArgs { + booleanArgField(booleanArg: $booleanArg) + } + query Query($booleanArg: Boolean) + { + complicatedArgs { + ...booleanArgFrag + } + } + `); + + expectValid(` + query Query($booleanArg: Boolean) + { + complicatedArgs { + ...booleanArgFrag + } + } + fragment booleanArgFrag on ComplicatedArgs { + booleanArgField(booleanArg: $booleanArg) + } + `); + }); + + it('Boolean! => Boolean', () => { + expectValid(` + query Query($nonNullBooleanArg: Boolean!) + { + complicatedArgs { + booleanArgField(booleanArg: $nonNullBooleanArg) + } + } + `); + }); + + it('Boolean! => Boolean within fragment', () => { + expectValid(` + fragment booleanArgFrag on ComplicatedArgs { + booleanArgField(booleanArg: $nonNullBooleanArg) + } + + query Query($nonNullBooleanArg: Boolean!) + { + complicatedArgs { + ...booleanArgFrag + } + } + `); + }); + + it('[String] => [String]', () => { + expectValid(` + query Query($stringListVar: [String]) + { + complicatedArgs { + stringListArgField(stringListArg: $stringListVar) + } + } + `); + }); + + it('[String!] => [String]', () => { + expectValid(` + query Query($stringListVar: [String!]) + { + complicatedArgs { + stringListArgField(stringListArg: $stringListVar) + } + } + `); + }); + + it('String => [String] in item position', () => { + expectValid(` + query Query($stringVar: String) + { + complicatedArgs { + stringListArgField(stringListArg: [$stringVar]) + } + } + `); + }); + + it('String! => [String] in item position', () => { + expectValid(` + query Query($stringVar: String!) + { + complicatedArgs { + stringListArgField(stringListArg: [$stringVar]) + } + } + `); + }); + + it('ComplexInput => ComplexInput', () => { + expectValid(` + query Query($complexVar: ComplexInput) + { + complicatedArgs { + complexArgField(complexArg: $complexVar) + } + } + `); + }); + + it('ComplexInput => ComplexInput in field position', () => { + expectValid(` + query Query($boolVar: Boolean = false) + { + complicatedArgs { + complexArgField(complexArg: {requiredArg: $boolVar}) + } + } + `); + }); + + it('Boolean! => Boolean! in directive', () => { + expectValid(` + query Query($boolVar: Boolean!) + { + dog @include(if: $boolVar) + } + `); + }); + + it('Int => Int!', () => { + expectErrors(` + query Query($intArg: Int) { + complicatedArgs { + nonNullIntArgField(nonNullIntArg: $intArg) + } + } + `).toDeepEqual([ + { + message: 'Variable "$intArg" of type "Int" used in position expecting type "Int!".', + locations: [ + { line: 2, column: 19 }, + { line: 4, column: 45 }, + ], + }, + ]); + }); + + it('Int => Int! within fragment', () => { + expectErrors(` + fragment nonNullIntArgFieldFrag on ComplicatedArgs { + nonNullIntArgField(nonNullIntArg: $intArg) + } + + query Query($intArg: Int) { + complicatedArgs { + ...nonNullIntArgFieldFrag + } + } + `).toDeepEqual([ + { + message: 'Variable "$intArg" of type "Int" used in position expecting type "Int!".', + locations: [ + { line: 6, column: 19 }, + { line: 3, column: 43 }, + ], + }, + ]); + }); + + it('Int => Int! within nested fragment', () => { + expectErrors(` + fragment outerFrag on ComplicatedArgs { + ...nonNullIntArgFieldFrag + } + + fragment nonNullIntArgFieldFrag on ComplicatedArgs { + nonNullIntArgField(nonNullIntArg: $intArg) + } + + query Query($intArg: Int) { + complicatedArgs { + ...outerFrag + } + } + `).toDeepEqual([ + { + message: 'Variable "$intArg" of type "Int" used in position expecting type "Int!".', + locations: [ + { line: 10, column: 19 }, + { line: 7, column: 43 }, + ], + }, + ]); + }); + + it('String over Boolean', () => { + expectErrors(` + query Query($stringVar: String) { + complicatedArgs { + booleanArgField(booleanArg: $stringVar) + } + } + `).toDeepEqual([ + { + message: 'Variable "$stringVar" of type "String" used in position expecting type "Boolean".', + locations: [ + { line: 2, column: 19 }, + { line: 4, column: 39 }, + ], + }, + ]); + }); + + it('String => [String]', () => { + expectErrors(` + query Query($stringVar: String) { + complicatedArgs { + stringListArgField(stringListArg: $stringVar) + } + } + `).toDeepEqual([ + { + message: 'Variable "$stringVar" of type "String" used in position expecting type "[String]".', + locations: [ + { line: 2, column: 19 }, + { line: 4, column: 45 }, + ], + }, + ]); + }); + + it('Boolean => Boolean! in directive', () => { + expectErrors(` + query Query($boolVar: Boolean) { + dog @include(if: $boolVar) + } + `).toDeepEqual([ + { + message: 'Variable "$boolVar" of type "Boolean" used in position expecting type "Boolean!".', + locations: [ + { line: 2, column: 19 }, + { line: 3, column: 26 }, + ], + }, + ]); + }); + + it('String => Boolean! in directive', () => { + expectErrors(` + query Query($stringVar: String) { + dog @include(if: $stringVar) + } + `).toDeepEqual([ + { + message: 'Variable "$stringVar" of type "String" used in position expecting type "Boolean!".', + locations: [ + { line: 2, column: 19 }, + { line: 3, column: 26 }, + ], + }, + ]); + }); + + it('[String] => [String!]', () => { + expectErrors(` + query Query($stringListVar: [String]) + { + complicatedArgs { + stringListNonNullArgField(stringListNonNullArg: $stringListVar) + } + } + `).toDeepEqual([ + { + message: 'Variable "$stringListVar" of type "[String]" used in position expecting type "[String!]".', + locations: [ + { line: 2, column: 19 }, + { line: 5, column: 59 }, + ], + }, + ]); + }); + + describe('Allows optional (nullable) variables with default values', () => { + it('Int => Int! fails when variable provides null default value', () => { + expectErrors(` + query Query($intVar: Int = null) { + complicatedArgs { + nonNullIntArgField(nonNullIntArg: $intVar) + } + } + `).toDeepEqual([ + { + message: 'Variable "$intVar" of type "Int" used in position expecting type "Int!".', + locations: [ + { line: 2, column: 21 }, + { line: 4, column: 47 }, + ], + }, + ]); + }); + + it('Int => Int! when variable provides non-null default value', () => { + expectValid(` + query Query($intVar: Int = 1) { + complicatedArgs { + nonNullIntArgField(nonNullIntArg: $intVar) + } + }`); + }); + + it('Int => Int! when optional argument provides default value', () => { + expectValid(` + query Query($intVar: Int) { + complicatedArgs { + nonNullFieldWithDefault(nonNullIntArg: $intVar) + } + }`); + }); + + it('Boolean => Boolean! in directive with default value with option', () => { + expectValid(` + query Query($boolVar: Boolean = false) { + dog @include(if: $boolVar) + }`); + }); + }); +}); diff --git a/packages/graphql/src/validation/__tests__/harness.ts b/packages/graphql/src/validation/__tests__/harness.ts new file mode 100644 index 00000000000..926a4120c25 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/harness.ts @@ -0,0 +1,136 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import type { Maybe } from '../../jsutils/Maybe.js'; + +import { parse } from '../../language/parser.js'; + +import type { GraphQLSchema } from '../../type/schema.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; + +import { validate, validateSDL } from '../validate.js'; +import type { SDLValidationRule, ValidationRule } from '../ValidationContext.js'; + +export const testSchema: GraphQLSchema = buildSchema(` + interface Mammal { + mother: Mammal + father: Mammal + } + + interface Pet { + name(surname: Boolean): String + } + + interface Canine implements Mammal { + name(surname: Boolean): String + mother: Canine + father: Canine + } + + enum DogCommand { + SIT + HEEL + DOWN + } + + type Dog implements Pet & Mammal & Canine { + name(surname: Boolean): String + nickname: String + barkVolume: Int + barks: Boolean + doesKnowCommand(dogCommand: DogCommand): Boolean + isHouseTrained(atOtherHomes: Boolean = true): Boolean + isAtLocation(x: Int, y: Int): Boolean + mother: Dog + father: Dog + } + + type Cat implements Pet { + name(surname: Boolean): String + nickname: String + meows: Boolean + meowsVolume: Int + furColor: FurColor + } + + union CatOrDog = Cat | Dog + + type Human { + name(surname: Boolean): String + pets: [Pet] + relatives: [Human] + } + + enum FurColor { + BROWN + BLACK + TAN + SPOTTED + NO_FUR + UNKNOWN + } + + input ComplexInput { + requiredField: Boolean! + nonNullField: Boolean! = false + intField: Int + stringField: String + booleanField: Boolean + stringListField: [String] + } + + type ComplicatedArgs { + # TODO List + # TODO Coercion + # TODO NotNulls + intArgField(intArg: Int): String + nonNullIntArgField(nonNullIntArg: Int!): String + stringArgField(stringArg: String): String + booleanArgField(booleanArg: Boolean): String + enumArgField(enumArg: FurColor): String + floatArgField(floatArg: Float): String + idArgField(idArg: ID): String + stringListArgField(stringListArg: [String]): String + stringListNonNullArgField(stringListNonNullArg: [String!]): String + complexArgField(complexArg: ComplexInput): String + multipleReqs(req1: Int!, req2: Int!): String + nonNullFieldWithDefault(arg: Int! = 0): String + multipleOpts(opt1: Int = 0, opt2: Int = 0): String + multipleOptAndReq(req1: Int!, req2: Int!, opt1: Int = 0, opt2: Int = 0): String + } + + type QueryRoot { + human(id: ID): Human + dog: Dog + cat: Cat + pet: Pet + catOrDog: CatOrDog + complicatedArgs: ComplicatedArgs + } + + schema { + query: QueryRoot + } + + directive @onField on FIELD +`); + +export function expectValidationErrorsWithSchema(schema: GraphQLSchema, rule: ValidationRule, queryStr: string): any { + const doc = parse(queryStr); + const errors = validate(schema, doc, [rule]); + return expectJSON(errors); +} + +export function expectValidationErrors(rule: ValidationRule, queryStr: string): any { + return expectValidationErrorsWithSchema(testSchema, rule, queryStr); +} + +export function expectSDLValidationErrors(schema: Maybe, rule: SDLValidationRule, sdlStr: string): any { + const doc = parse(sdlStr); + const errors = validateSDL(doc, schema, [rule]); + return expectJSON(errors); +} + +describe.skip('no harness tests', () => { + it.todo('nothing to test'); +}); diff --git a/packages/graphql/src/validation/__tests__/validation-test.ts b/packages/graphql/src/validation/__tests__/validation-test.ts new file mode 100644 index 00000000000..f62f3cf91e5 --- /dev/null +++ b/packages/graphql/src/validation/__tests__/validation-test.ts @@ -0,0 +1,167 @@ +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { DirectiveNode } from '../../language/ast.js'; +import { parse } from '../../language/parser.js'; + +import { buildSchema } from '../../utilities/buildASTSchema.js'; +import { TypeInfo } from '../../utilities/TypeInfo.js'; + +import { validate } from '../validate.js'; +import type { ValidationContext } from '../ValidationContext.js'; + +import { testSchema } from './harness.js'; + +describe('Validate: Supports full validation', () => { + it('validates queries', () => { + const doc = parse(` + query { + human { + pets { + ... on Cat { + meowsVolume + } + ... on Dog { + barkVolume + } + } + } + } + `); + + const errors = validate(testSchema, doc); + expectJSON(errors).toDeepEqual([]); + }); + + it('detects unknown fields', () => { + const doc = parse(` + { + unknown + } + `); + + const errors = validate(testSchema, doc); + expectJSON(errors).toDeepEqual([ + { + locations: [{ line: 3, column: 9 }], + message: 'Cannot query field "unknown" on type "QueryRoot".', + }, + ]); + }); + + it('Deprecated: validates using a custom TypeInfo', () => { + // This TypeInfo will never return a valid field. + const typeInfo = new TypeInfo(testSchema, null, () => null); + + const doc = parse(` + query { + human { + pets { + ... on Cat { + meowsVolume + } + ... on Dog { + barkVolume + } + } + } + } + `); + + const errors = validate(testSchema, doc, undefined, undefined, typeInfo); + const errorMessages = errors.map(error => error.message); + + expect(errorMessages).toEqual([ + 'Cannot query field "human" on type "QueryRoot". Did you mean "human"?', + 'Cannot query field "meowsVolume" on type "Cat". Did you mean "meowsVolume"?', + 'Cannot query field "barkVolume" on type "Dog". Did you mean "barkVolume"?', + ]); + }); + + it('validates using a custom rule', () => { + const schema = buildSchema(` + directive @custom(arg: String) on FIELD + + type Query { + foo: String + } + `); + + const doc = parse(` + query { + name @custom + } + `); + + function customRule(context: ValidationContext) { + return { + Directive(node: DirectiveNode) { + const directiveDef = context.getDirective(); + const error = new GraphQLError('Reporting directive: ' + String(directiveDef), { nodes: node }); + context.reportError(error); + }, + }; + } + + const errors = validate(schema, doc, [customRule]); + expectJSON(errors).toDeepEqual([ + { + message: 'Reporting directive: @custom', + locations: [{ line: 3, column: 14 }], + }, + ]); + }); +}); + +describe('Validate: Limit maximum number of validation errors', () => { + const query = ` + { + firstUnknownField + secondUnknownField + thirdUnknownField + } + `; + const doc = parse(query, { noLocation: true }); + + function validateDocument(options: { maxErrors?: number }) { + return validate(testSchema, doc, undefined, options); + } + + function invalidFieldError(fieldName: string) { + return { + message: `Cannot query field "${fieldName}" on type "QueryRoot".`, + }; + } + + it('when maxErrors is equal to number of errors', () => { + const errors = validateDocument({ maxErrors: 3 }); + expectJSON(errors).toDeepEqual([ + invalidFieldError('firstUnknownField'), + invalidFieldError('secondUnknownField'), + invalidFieldError('thirdUnknownField'), + ]); + }); + + it('when maxErrors is less than number of errors', () => { + const errors = validateDocument({ maxErrors: 2 }); + expectJSON(errors).toDeepEqual([ + invalidFieldError('firstUnknownField'), + invalidFieldError('secondUnknownField'), + { + message: 'Too many validation errors, error limit reached. Validation aborted.', + }, + ]); + }); + + it('passthrough exceptions from rules', () => { + function customRule() { + return { + Field() { + throw new Error('Error from custom rule!'); + }, + }; + } + expect(() => validate(testSchema, doc, [customRule], { maxErrors: 1 })).toThrow(/^Error from custom rule!$/); + }); +}); diff --git a/packages/graphql/src/validation/index.ts b/packages/graphql/src/validation/index.ts new file mode 100644 index 00000000000..c221f4e306b --- /dev/null +++ b/packages/graphql/src/validation/index.ts @@ -0,0 +1,4 @@ +export * from './ValidationContext.js'; +export * from './specifiedRules.js'; +export * from './validate.js'; +export * from './rules/index.js'; diff --git a/packages/graphql/src/validation/rules/ExecutableDefinitionsRule.ts b/packages/graphql/src/validation/rules/ExecutableDefinitionsRule.ts new file mode 100644 index 00000000000..4f30187a80c --- /dev/null +++ b/packages/graphql/src/validation/rules/ExecutableDefinitionsRule.ts @@ -0,0 +1,36 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import { Kind } from '../../language/kinds.js'; +import { isExecutableDefinitionNode } from '../../language/predicates.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * Executable definitions + * + * A GraphQL document is only valid for execution if all definitions are either + * operation or fragment definitions. + * + * See https://spec.graphql.org/draft/#sec-Executable-Definitions + */ +export function ExecutableDefinitionsRule(context: ASTValidationContext): ASTVisitor { + return { + Document(node) { + for (const definition of node.definitions) { + if (!isExecutableDefinitionNode(definition)) { + const defName = + definition.kind === Kind.SCHEMA_DEFINITION || definition.kind === Kind.SCHEMA_EXTENSION + ? 'schema' + : '"' + definition.name.value + '"'; + context.reportError( + new GraphQLError(`The ${defName} definition is not executable.`, { + nodes: definition, + }) + ); + } + } + return false; + }, + }; +} diff --git a/packages/graphql/src/validation/rules/FieldsOnCorrectTypeRule.ts b/packages/graphql/src/validation/rules/FieldsOnCorrectTypeRule.ts new file mode 100644 index 00000000000..0b52b7a780f --- /dev/null +++ b/packages/graphql/src/validation/rules/FieldsOnCorrectTypeRule.ts @@ -0,0 +1,118 @@ +import { didYouMean } from '../../jsutils/didYouMean.js'; +import { naturalCompare } from '../../jsutils/naturalCompare.js'; +import { suggestionList } from '../../jsutils/suggestionList.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { FieldNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { GraphQLInterfaceType, GraphQLObjectType, GraphQLOutputType } from '../../type/definition.js'; +import { isAbstractType, isInterfaceType, isObjectType } from '../../type/definition.js'; +import type { GraphQLSchema } from '../../type/schema.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Fields on correct type + * + * A GraphQL document is only valid if all fields selected are defined by the + * parent type, or are an allowed meta field such as __typename. + * + * See https://spec.graphql.org/draft/#sec-Field-Selections + */ +export function FieldsOnCorrectTypeRule(context: ValidationContext): ASTVisitor { + return { + Field(node: FieldNode) { + const type = context.getParentType(); + if (type) { + const fieldDef = context.getFieldDef(); + if (!fieldDef) { + // This field doesn't exist, lets look for suggestions. + const schema = context.getSchema(); + const fieldName = node.name.value; + + // First determine if there are any suggested types to condition on. + let suggestion = didYouMean('to use an inline fragment on', getSuggestedTypeNames(schema, type, fieldName)); + + // If there are no suggested types, then perhaps this was a typo? + if (suggestion === '') { + suggestion = didYouMean(getSuggestedFieldNames(type, fieldName)); + } + + // Report an error, including helpful suggestions. + context.reportError( + new GraphQLError(`Cannot query field "${fieldName}" on type "${type.name}".` + suggestion, { nodes: node }) + ); + } + } + }, + }; +} + +/** + * Go through all of the implementations of type, as well as the interfaces that + * they implement. If any of those types include the provided field, suggest them, + * sorted by how often the type is referenced. + */ +function getSuggestedTypeNames(schema: GraphQLSchema, type: GraphQLOutputType, fieldName: string): Array { + if (!isAbstractType(type)) { + // Must be an Object type, which does not have possible fields. + return []; + } + + const suggestedTypes = new Set(); + const usageCount = Object.create(null); + for (const possibleType of schema.getPossibleTypes(type)) { + if (!possibleType.getFields()[fieldName]) { + continue; + } + + // This object type defines this field. + suggestedTypes.add(possibleType); + usageCount[possibleType.name] = 1; + + for (const possibleInterface of possibleType.getInterfaces()) { + if (!possibleInterface.getFields()[fieldName]) { + continue; + } + + // This interface type defines this field. + suggestedTypes.add(possibleInterface); + usageCount[possibleInterface.name] = (usageCount[possibleInterface.name] ?? 0) + 1; + } + } + + return [...suggestedTypes] + .sort((typeA, typeB) => { + // Suggest both interface and object types based on how common they are. + const usageCountDiff = usageCount[typeB.name] - usageCount[typeA.name]; + if (usageCountDiff !== 0) { + return usageCountDiff; + } + + // Suggest super types first followed by subtypes + if (isInterfaceType(typeA) && schema.isSubType(typeA, typeB)) { + return -1; + } + if (isInterfaceType(typeB) && schema.isSubType(typeB, typeA)) { + return 1; + } + + return naturalCompare(typeA.name, typeB.name); + }) + .map(x => x.name); +} + +/** + * For the field name provided, determine if there are any similar field names + * that may be the result of a typo. + */ +function getSuggestedFieldNames(type: GraphQLOutputType, fieldName: string): Array { + if (isObjectType(type) || isInterfaceType(type)) { + const possibleFieldNames = Object.keys(type.getFields()); + return suggestionList(fieldName, possibleFieldNames); + } + // Otherwise, must be a Union type, which does not define fields. + return []; +} diff --git a/packages/graphql/src/validation/rules/FragmentsOnCompositeTypesRule.ts b/packages/graphql/src/validation/rules/FragmentsOnCompositeTypesRule.ts new file mode 100644 index 00000000000..5c5d0f55cc9 --- /dev/null +++ b/packages/graphql/src/validation/rules/FragmentsOnCompositeTypesRule.ts @@ -0,0 +1,47 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import { print } from '../../language/printer.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { isCompositeType } from '../../type/definition.js'; + +import { typeFromAST } from '../../utilities/typeFromAST.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Fragments on composite type + * + * Fragments use a type condition to determine if they apply, since fragments + * can only be spread into a composite type (object, interface, or union), the + * type condition must also be a composite type. + * + * See https://spec.graphql.org/draft/#sec-Fragments-On-Composite-Types + */ +export function FragmentsOnCompositeTypesRule(context: ValidationContext): ASTVisitor { + return { + InlineFragment(node) { + const typeCondition = node.typeCondition; + if (typeCondition) { + const type = typeFromAST(context.getSchema(), typeCondition); + if (type && !isCompositeType(type)) { + const typeStr = print(typeCondition); + context.reportError( + new GraphQLError(`Fragment cannot condition on non composite type "${typeStr}".`, { nodes: typeCondition }) + ); + } + } + }, + FragmentDefinition(node) { + const type = typeFromAST(context.getSchema(), node.typeCondition); + if (type && !isCompositeType(type)) { + const typeStr = print(node.typeCondition); + context.reportError( + new GraphQLError(`Fragment "${node.name.value}" cannot condition on non composite type "${typeStr}".`, { + nodes: node.typeCondition, + }) + ); + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/KnownArgumentNamesRule.ts b/packages/graphql/src/validation/rules/KnownArgumentNamesRule.ts new file mode 100644 index 00000000000..bceaac66033 --- /dev/null +++ b/packages/graphql/src/validation/rules/KnownArgumentNamesRule.ts @@ -0,0 +1,91 @@ +import { didYouMean } from '../../jsutils/didYouMean.js'; +import { suggestionList } from '../../jsutils/suggestionList.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import { Kind } from '../../language/kinds.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { specifiedDirectives } from '../../type/directives.js'; + +import type { SDLValidationContext, ValidationContext } from '../ValidationContext.js'; + +/** + * Known argument names + * + * A GraphQL field is only valid if all supplied arguments are defined by + * that field. + * + * See https://spec.graphql.org/draft/#sec-Argument-Names + * See https://spec.graphql.org/draft/#sec-Directives-Are-In-Valid-Locations + */ +export function KnownArgumentNamesRule(context: ValidationContext): ASTVisitor { + return { + ...KnownArgumentNamesOnDirectivesRule(context), + Argument(argNode) { + const argDef = context.getArgument(); + const fieldDef = context.getFieldDef(); + const parentType = context.getParentType(); + + if (!argDef && fieldDef && parentType) { + const argName = argNode.name.value; + const knownArgsNames = fieldDef.args.map(arg => arg.name); + const suggestions = suggestionList(argName, knownArgsNames); + context.reportError( + new GraphQLError( + `Unknown argument "${argName}" on field "${parentType.name}.${fieldDef.name}".` + didYouMean(suggestions), + { nodes: argNode } + ) + ); + } + }, + }; +} + +/** + * @internal + */ +export function KnownArgumentNamesOnDirectivesRule(context: ValidationContext | SDLValidationContext): ASTVisitor { + const directiveArgs = Object.create(null); + + const schema = context.getSchema(); + const definedDirectives = schema ? schema.getDirectives() : specifiedDirectives; + for (const directive of definedDirectives) { + directiveArgs[directive.name] = directive.args.map(arg => arg.name); + } + + const astDefinitions = context.getDocument().definitions; + for (const def of astDefinitions) { + if (def.kind === Kind.DIRECTIVE_DEFINITION) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const argsNodes = def.arguments ?? []; + + directiveArgs[def.name.value] = argsNodes.map(arg => arg.name.value); + } + } + + return { + Directive(directiveNode) { + const directiveName = directiveNode.name.value; + const knownArgs = directiveArgs[directiveName]; + + if (directiveNode.arguments && knownArgs) { + for (const argNode of directiveNode.arguments) { + const argName = argNode.name.value; + if (!knownArgs.includes(argName)) { + const suggestions = suggestionList(argName, knownArgs); + context.reportError( + new GraphQLError( + `Unknown argument "${argName}" on directive "@${directiveName}".` + didYouMean(suggestions), + { nodes: argNode } + ) + ); + } + } + } + + return false; + }, + }; +} diff --git a/packages/graphql/src/validation/rules/KnownDirectivesRule.ts b/packages/graphql/src/validation/rules/KnownDirectivesRule.ts new file mode 100644 index 00000000000..81d8566dc84 --- /dev/null +++ b/packages/graphql/src/validation/rules/KnownDirectivesRule.ts @@ -0,0 +1,127 @@ +import { inspect } from '../../jsutils/inspect.js'; +import { invariant } from '../../jsutils/invariant.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTNode } from '../../language/ast.js'; +import { OperationTypeNode } from '../../language/ast.js'; +import { DirectiveLocation } from '../../language/directiveLocation.js'; +import { Kind } from '../../language/kinds.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { specifiedDirectives } from '../../type/directives.js'; + +import type { SDLValidationContext, ValidationContext } from '../ValidationContext.js'; + +/** + * Known directives + * + * A GraphQL document is only valid if all `@directives` are known by the + * schema and legally positioned. + * + * See https://spec.graphql.org/draft/#sec-Directives-Are-Defined + */ +export function KnownDirectivesRule(context: ValidationContext | SDLValidationContext): ASTVisitor { + const locationsMap = Object.create(null); + + const schema = context.getSchema(); + const definedDirectives = schema ? schema.getDirectives() : specifiedDirectives; + for (const directive of definedDirectives) { + locationsMap[directive.name] = directive.locations; + } + + const astDefinitions = context.getDocument().definitions; + for (const def of astDefinitions) { + if (def.kind === Kind.DIRECTIVE_DEFINITION) { + locationsMap[def.name.value] = def.locations.map(name => name.value); + } + } + + return { + Directive(node, _key, _parent, _path, ancestors) { + const name = node.name.value; + const locations = locationsMap[name]; + + if (!locations) { + context.reportError(new GraphQLError(`Unknown directive "@${name}".`, { nodes: node })); + return; + } + + const candidateLocation = getDirectiveLocationForASTPath(ancestors); + if (candidateLocation && !locations.includes(candidateLocation)) { + context.reportError( + new GraphQLError(`Directive "@${name}" may not be used on ${candidateLocation}.`, { nodes: node }) + ); + } + }, + }; +} + +function getDirectiveLocationForASTPath( + ancestors: ReadonlyArray> +): DirectiveLocation | undefined { + const appliedTo = ancestors[ancestors.length - 1]; + invariant('kind' in appliedTo); + + switch (appliedTo.kind) { + case Kind.OPERATION_DEFINITION: + return getDirectiveLocationForOperation(appliedTo.operation); + case Kind.FIELD: + return DirectiveLocation.FIELD; + case Kind.FRAGMENT_SPREAD: + return DirectiveLocation.FRAGMENT_SPREAD; + case Kind.INLINE_FRAGMENT: + return DirectiveLocation.INLINE_FRAGMENT; + case Kind.FRAGMENT_DEFINITION: + return DirectiveLocation.FRAGMENT_DEFINITION; + case Kind.VARIABLE_DEFINITION: + return DirectiveLocation.VARIABLE_DEFINITION; + case Kind.SCHEMA_DEFINITION: + case Kind.SCHEMA_EXTENSION: + return DirectiveLocation.SCHEMA; + case Kind.SCALAR_TYPE_DEFINITION: + case Kind.SCALAR_TYPE_EXTENSION: + return DirectiveLocation.SCALAR; + case Kind.OBJECT_TYPE_DEFINITION: + case Kind.OBJECT_TYPE_EXTENSION: + return DirectiveLocation.OBJECT; + case Kind.FIELD_DEFINITION: + return DirectiveLocation.FIELD_DEFINITION; + case Kind.INTERFACE_TYPE_DEFINITION: + case Kind.INTERFACE_TYPE_EXTENSION: + return DirectiveLocation.INTERFACE; + case Kind.UNION_TYPE_DEFINITION: + case Kind.UNION_TYPE_EXTENSION: + return DirectiveLocation.UNION; + case Kind.ENUM_TYPE_DEFINITION: + case Kind.ENUM_TYPE_EXTENSION: + return DirectiveLocation.ENUM; + case Kind.ENUM_VALUE_DEFINITION: + return DirectiveLocation.ENUM_VALUE; + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + case Kind.INPUT_OBJECT_TYPE_EXTENSION: + return DirectiveLocation.INPUT_OBJECT; + case Kind.INPUT_VALUE_DEFINITION: { + const parentNode = ancestors[ancestors.length - 3]; + invariant('kind' in parentNode); + return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION + ? DirectiveLocation.INPUT_FIELD_DEFINITION + : DirectiveLocation.ARGUMENT_DEFINITION; + } + // Not reachable, all possible types have been considered. + /* c8 ignore next 2 */ + default: + invariant(false, 'Unexpected kind: ' + inspect(appliedTo.kind)); + } +} + +function getDirectiveLocationForOperation(operation: OperationTypeNode): DirectiveLocation { + switch (operation) { + case OperationTypeNode.QUERY: + return DirectiveLocation.QUERY; + case OperationTypeNode.MUTATION: + return DirectiveLocation.MUTATION; + case OperationTypeNode.SUBSCRIPTION: + return DirectiveLocation.SUBSCRIPTION; + } +} diff --git a/packages/graphql/src/validation/rules/KnownFragmentNamesRule.ts b/packages/graphql/src/validation/rules/KnownFragmentNamesRule.ts new file mode 100644 index 00000000000..c87f2ac1ce9 --- /dev/null +++ b/packages/graphql/src/validation/rules/KnownFragmentNamesRule.ts @@ -0,0 +1,29 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Known fragment names + * + * A GraphQL document is only valid if all `...Fragment` fragment spreads refer + * to fragments defined in the same document. + * + * See https://spec.graphql.org/draft/#sec-Fragment-spread-target-defined + */ +export function KnownFragmentNamesRule(context: ValidationContext): ASTVisitor { + return { + FragmentSpread(node) { + const fragmentName = node.name.value; + const fragment = context.getFragment(fragmentName); + if (!fragment) { + context.reportError( + new GraphQLError(`Unknown fragment "${fragmentName}".`, { + nodes: node.name, + }) + ); + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/KnownTypeNamesRule.ts b/packages/graphql/src/validation/rules/KnownTypeNamesRule.ts new file mode 100644 index 00000000000..764392eb377 --- /dev/null +++ b/packages/graphql/src/validation/rules/KnownTypeNamesRule.ts @@ -0,0 +1,63 @@ +import { didYouMean } from '../../jsutils/didYouMean.js'; +import { suggestionList } from '../../jsutils/suggestionList.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTNode } from '../../language/ast.js'; +import { + isTypeDefinitionNode, + isTypeSystemDefinitionNode, + isTypeSystemExtensionNode, +} from '../../language/predicates.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { introspectionTypes } from '../../type/introspection.js'; +import { specifiedScalarTypes } from '../../type/scalars.js'; + +import type { SDLValidationContext, ValidationContext } from '../ValidationContext.js'; + +/** + * Known type names + * + * A GraphQL document is only valid if referenced types (specifically + * variable definitions and fragment conditions) are defined by the type schema. + * + * See https://spec.graphql.org/draft/#sec-Fragment-Spread-Type-Existence + */ +export function KnownTypeNamesRule(context: ValidationContext | SDLValidationContext): ASTVisitor { + const schema = context.getSchema(); + const existingTypesMap = schema ? schema.getTypeMap() : Object.create(null); + + const definedTypes = Object.create(null); + for (const def of context.getDocument().definitions) { + if (isTypeDefinitionNode(def)) { + definedTypes[def.name.value] = true; + } + } + + const typeNames = [...Object.keys(existingTypesMap), ...Object.keys(definedTypes)]; + + return { + NamedType(node, _1, parent, _2, ancestors) { + const typeName = node.name.value; + if (!existingTypesMap[typeName] && !definedTypes[typeName]) { + const definitionNode = ancestors[2] ?? parent; + const isSDL = definitionNode != null && isSDLNode(definitionNode); + if (isSDL && standardTypeNames.includes(typeName)) { + return; + } + + const suggestedTypes = suggestionList(typeName, isSDL ? standardTypeNames.concat(typeNames) : typeNames); + context.reportError( + new GraphQLError(`Unknown type "${typeName}".` + didYouMean(suggestedTypes), { nodes: node }) + ); + } + }, + }; +} + +const standardTypeNames = [...specifiedScalarTypes, ...introspectionTypes].map(type => type.name); + +function isSDLNode(value: ASTNode | ReadonlyArray): boolean { + return 'kind' in value && (isTypeSystemDefinitionNode(value) || isTypeSystemExtensionNode(value)); +} diff --git a/packages/graphql/src/validation/rules/LoneAnonymousOperationRule.ts b/packages/graphql/src/validation/rules/LoneAnonymousOperationRule.ts new file mode 100644 index 00000000000..90d52099093 --- /dev/null +++ b/packages/graphql/src/validation/rules/LoneAnonymousOperationRule.ts @@ -0,0 +1,30 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import { Kind } from '../../language/kinds.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * Lone anonymous operation + * + * A GraphQL document is only valid if when it contains an anonymous operation + * (the query short-hand) that it contains only that one operation definition. + * + * See https://spec.graphql.org/draft/#sec-Lone-Anonymous-Operation + */ +export function LoneAnonymousOperationRule(context: ASTValidationContext): ASTVisitor { + let operationCount = 0; + return { + Document(node) { + operationCount = node.definitions.filter(definition => definition.kind === Kind.OPERATION_DEFINITION).length; + }, + OperationDefinition(node) { + if (!node.name && operationCount > 1) { + context.reportError( + new GraphQLError('This anonymous operation must be the only defined operation.', { nodes: node }) + ); + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/LoneSchemaDefinitionRule.ts b/packages/graphql/src/validation/rules/LoneSchemaDefinitionRule.ts new file mode 100644 index 00000000000..ed6beeaf4fa --- /dev/null +++ b/packages/graphql/src/validation/rules/LoneSchemaDefinitionRule.ts @@ -0,0 +1,35 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { SDLValidationContext } from '../ValidationContext.js'; + +/** + * Lone Schema definition + * + * A GraphQL document is only valid if it contains only one schema definition. + */ +export function LoneSchemaDefinitionRule(context: SDLValidationContext): ASTVisitor { + const oldSchema = context.getSchema(); + const alreadyDefined = + oldSchema?.astNode ?? oldSchema?.getQueryType() ?? oldSchema?.getMutationType() ?? oldSchema?.getSubscriptionType(); + + let schemaDefinitionsCount = 0; + return { + SchemaDefinition(node) { + if (alreadyDefined) { + context.reportError(new GraphQLError('Cannot define a new schema within a schema extension.', { nodes: node })); + return; + } + + if (schemaDefinitionsCount > 0) { + context.reportError( + new GraphQLError('Must provide only one schema definition.', { + nodes: node, + }) + ); + } + ++schemaDefinitionsCount; + }, + }; +} diff --git a/packages/graphql/src/validation/rules/NoFragmentCyclesRule.ts b/packages/graphql/src/validation/rules/NoFragmentCyclesRule.ts new file mode 100644 index 00000000000..502bc0ff58d --- /dev/null +++ b/packages/graphql/src/validation/rules/NoFragmentCyclesRule.ts @@ -0,0 +1,84 @@ +import type { ObjMap } from '../../jsutils/ObjMap.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { FragmentDefinitionNode, FragmentSpreadNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * No fragment cycles + * + * The graph of fragment spreads must not form any cycles including spreading itself. + * Otherwise an operation could infinitely spread or infinitely execute on cycles in the underlying data. + * + * See https://spec.graphql.org/draft/#sec-Fragment-spreads-must-not-form-cycles + */ +export function NoFragmentCyclesRule(context: ASTValidationContext): ASTVisitor { + // Tracks already visited fragments to maintain O(N) and to ensure that cycles + // are not redundantly reported. + const visitedFrags: ObjMap = Object.create(null); + + // Array of AST nodes used to produce meaningful errors + const spreadPath: Array = []; + + // Position in the spread path + const spreadPathIndexByName: ObjMap = Object.create(null); + + return { + OperationDefinition: () => false, + FragmentDefinition(node) { + detectCycleRecursive(node); + return false; + }, + }; + + // This does a straight-forward DFS to find cycles. + // It does not terminate when a cycle was found but continues to explore + // the graph to find all possible cycles. + function detectCycleRecursive(fragment: FragmentDefinitionNode): void { + if (visitedFrags[fragment.name.value]) { + return; + } + + const fragmentName = fragment.name.value; + visitedFrags[fragmentName] = true; + + const spreadNodes = context.getFragmentSpreads(fragment.selectionSet); + if (spreadNodes.length === 0) { + return; + } + + spreadPathIndexByName[fragmentName] = spreadPath.length; + + for (const spreadNode of spreadNodes) { + const spreadName = spreadNode.name.value; + const cycleIndex = spreadPathIndexByName[spreadName]; + + spreadPath.push(spreadNode); + if (cycleIndex === undefined) { + const spreadFragment = context.getFragment(spreadName); + if (spreadFragment) { + detectCycleRecursive(spreadFragment); + } + } else { + const cyclePath = spreadPath.slice(cycleIndex); + const viaPath = cyclePath + .slice(0, -1) + .map(s => '"' + s.name.value + '"') + .join(', '); + + context.reportError( + new GraphQLError( + `Cannot spread fragment "${spreadName}" within itself` + (viaPath !== '' ? ` via ${viaPath}.` : '.'), + { nodes: cyclePath } + ) + ); + } + spreadPath.pop(); + } + + spreadPathIndexByName[fragmentName] = undefined; + } +} diff --git a/packages/graphql/src/validation/rules/NoUndefinedVariablesRule.ts b/packages/graphql/src/validation/rules/NoUndefinedVariablesRule.ts new file mode 100644 index 00000000000..43a00aba8ab --- /dev/null +++ b/packages/graphql/src/validation/rules/NoUndefinedVariablesRule.ts @@ -0,0 +1,45 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * No undefined variables + * + * A GraphQL operation is only valid if all variables encountered, both directly + * and via fragment spreads, are defined by that operation. + * + * See https://spec.graphql.org/draft/#sec-All-Variable-Uses-Defined + */ +export function NoUndefinedVariablesRule(context: ValidationContext): ASTVisitor { + let variableNameDefined = Object.create(null); + + return { + OperationDefinition: { + enter() { + variableNameDefined = Object.create(null); + }, + leave(operation) { + const usages = context.getRecursiveVariableUsages(operation); + + for (const { node } of usages) { + const varName = node.name.value; + if (variableNameDefined[varName] !== true) { + context.reportError( + new GraphQLError( + operation.name + ? `Variable "$${varName}" is not defined by operation "${operation.name.value}".` + : `Variable "$${varName}" is not defined.`, + { nodes: [node, operation] } + ) + ); + } + } + }, + }, + VariableDefinition(node) { + variableNameDefined[node.variable.name.value] = true; + }, + }; +} diff --git a/packages/graphql/src/validation/rules/NoUnusedFragmentsRule.ts b/packages/graphql/src/validation/rules/NoUnusedFragmentsRule.ts new file mode 100644 index 00000000000..425db573461 --- /dev/null +++ b/packages/graphql/src/validation/rules/NoUnusedFragmentsRule.ts @@ -0,0 +1,51 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { FragmentDefinitionNode, OperationDefinitionNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * No unused fragments + * + * A GraphQL document is only valid if all fragment definitions are spread + * within operations, or spread within other fragments spread within operations. + * + * See https://spec.graphql.org/draft/#sec-Fragments-Must-Be-Used + */ +export function NoUnusedFragmentsRule(context: ASTValidationContext): ASTVisitor { + const operationDefs: Array = []; + const fragmentDefs: Array = []; + + return { + OperationDefinition(node) { + operationDefs.push(node); + return false; + }, + FragmentDefinition(node) { + fragmentDefs.push(node); + return false; + }, + Document: { + leave() { + const fragmentNameUsed = Object.create(null); + for (const operation of operationDefs) { + for (const fragment of context.getRecursivelyReferencedFragments(operation)) { + fragmentNameUsed[fragment.name.value] = true; + } + } + + for (const fragmentDef of fragmentDefs) { + const fragName = fragmentDef.name.value; + if (fragmentNameUsed[fragName] !== true) { + context.reportError( + new GraphQLError(`Fragment "${fragName}" is never used.`, { + nodes: fragmentDef, + }) + ); + } + } + }, + }, + }; +} diff --git a/packages/graphql/src/validation/rules/NoUnusedVariablesRule.ts b/packages/graphql/src/validation/rules/NoUnusedVariablesRule.ts new file mode 100644 index 00000000000..9cc5fc1c310 --- /dev/null +++ b/packages/graphql/src/validation/rules/NoUnusedVariablesRule.ts @@ -0,0 +1,51 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { VariableDefinitionNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * No unused variables + * + * A GraphQL operation is only valid if all variables defined by an operation + * are used, either directly or within a spread fragment. + * + * See https://spec.graphql.org/draft/#sec-All-Variables-Used + */ +export function NoUnusedVariablesRule(context: ValidationContext): ASTVisitor { + let variableDefs: Array = []; + + return { + OperationDefinition: { + enter() { + variableDefs = []; + }, + leave(operation) { + const variableNameUsed = Object.create(null); + const usages = context.getRecursiveVariableUsages(operation); + + for (const { node } of usages) { + variableNameUsed[node.name.value] = true; + } + + for (const variableDef of variableDefs) { + const variableName = variableDef.variable.name.value; + if (variableNameUsed[variableName] !== true) { + context.reportError( + new GraphQLError( + operation.name + ? `Variable "$${variableName}" is never used in operation "${operation.name.value}".` + : `Variable "$${variableName}" is never used.`, + { nodes: variableDef } + ) + ); + } + } + }, + }, + VariableDefinition(def) { + variableDefs.push(def); + }, + }; +} diff --git a/packages/graphql/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts b/packages/graphql/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts new file mode 100644 index 00000000000..8479b6903db --- /dev/null +++ b/packages/graphql/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts @@ -0,0 +1,745 @@ +import { inspect } from '../../jsutils/inspect.js'; +import type { Maybe } from '../../jsutils/Maybe.js'; +import type { ObjMap } from '../../jsutils/ObjMap.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { FieldNode, FragmentDefinitionNode, ObjectValueNode, SelectionSetNode } from '../../language/ast.js'; +import { Kind } from '../../language/kinds.js'; +import { print } from '../../language/printer.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { GraphQLField, GraphQLNamedType, GraphQLOutputType } from '../../type/definition.js'; +import { + getNamedType, + isInterfaceType, + isLeafType, + isListType, + isNonNullType, + isObjectType, +} from '../../type/definition.js'; + +import { sortValueNode } from '../../utilities/sortValueNode.js'; +import { typeFromAST } from '../../utilities/typeFromAST.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +// This file contains a lot of such errors but we plan to refactor it anyway +// so just disable it for entire file. + +function reasonMessage(reason: ConflictReasonMessage): string { + if (Array.isArray(reason)) { + return reason + .map(([responseName, subReason]) => `subfields "${responseName}" conflict because ` + reasonMessage(subReason)) + .join(' and '); + } + return reason; +} + +/** + * Overlapping fields can be merged + * + * A selection set is only valid if all fields (including spreading any + * fragments) either correspond to distinct response names or can be merged + * without ambiguity. + * + * See https://spec.graphql.org/draft/#sec-Field-Selection-Merging + */ +export function OverlappingFieldsCanBeMergedRule(context: ValidationContext): ASTVisitor { + // A memoization for when two fragments are compared "between" each other for + // conflicts. Two fragments may be compared many times, so memoizing this can + // dramatically improve the performance of this validator. + const comparedFragmentPairs = new PairSet(); + + // A cache for the "field map" and list of fragment names found in any given + // selection set. Selection sets may be asked for this information multiple + // times, so this improves the performance of this validator. + const cachedFieldsAndFragmentNames = new Map(); + + return { + SelectionSet(selectionSet) { + const conflicts = findConflictsWithinSelectionSet( + context, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + context.getParentType(), + selectionSet + ); + for (const [[responseName, reason], fields1, fields2] of conflicts) { + const reasonMsg = reasonMessage(reason); + context.reportError( + new GraphQLError( + `Fields "${responseName}" conflict because ${reasonMsg}. Use different aliases on the fields to fetch both if this was intentional.`, + { nodes: fields1.concat(fields2) } + ) + ); + } + }, + }; +} + +type Conflict = [ConflictReason, Array, Array]; +// Field name and reason. +type ConflictReason = [string, ConflictReasonMessage]; +// Reason is a string, or a nested list of conflicts. +type ConflictReasonMessage = string | Array; +// Tuple defining a field node in a context. +type NodeAndDef = [Maybe, FieldNode, Maybe>]; +// Map of array of those. +type NodeAndDefCollection = ObjMap>; +type FragmentNames = Array; +type FieldsAndFragmentNames = readonly [NodeAndDefCollection, FragmentNames]; + +/** + * Algorithm: + * + * Conflicts occur when two fields exist in a query which will produce the same + * response name, but represent differing values, thus creating a conflict. + * The algorithm below finds all conflicts via making a series of comparisons + * between fields. In order to compare as few fields as possible, this makes + * a series of comparisons "within" sets of fields and "between" sets of fields. + * + * Given any selection set, a collection produces both a set of fields by + * also including all inline fragments, as well as a list of fragments + * referenced by fragment spreads. + * + * A) Each selection set represented in the document first compares "within" its + * collected set of fields, finding any conflicts between every pair of + * overlapping fields. + * Note: This is the *only time* that a the fields "within" a set are compared + * to each other. After this only fields "between" sets are compared. + * + * B) Also, if any fragment is referenced in a selection set, then a + * comparison is made "between" the original set of fields and the + * referenced fragment. + * + * C) Also, if multiple fragments are referenced, then comparisons + * are made "between" each referenced fragment. + * + * D) When comparing "between" a set of fields and a referenced fragment, first + * a comparison is made between each field in the original set of fields and + * each field in the the referenced set of fields. + * + * E) Also, if any fragment is referenced in the referenced selection set, + * then a comparison is made "between" the original set of fields and the + * referenced fragment (recursively referring to step D). + * + * F) When comparing "between" two fragments, first a comparison is made between + * each field in the first referenced set of fields and each field in the the + * second referenced set of fields. + * + * G) Also, any fragments referenced by the first must be compared to the + * second, and any fragments referenced by the second must be compared to the + * first (recursively referring to step F). + * + * H) When comparing two fields, if both have selection sets, then a comparison + * is made "between" both selection sets, first comparing the set of fields in + * the first selection set with the set of fields in the second. + * + * I) Also, if any fragment is referenced in either selection set, then a + * comparison is made "between" the other set of fields and the + * referenced fragment. + * + * J) Also, if two fragments are referenced in both selection sets, then a + * comparison is made "between" the two fragments. + * + */ + +// Find all conflicts found "within" a selection set, including those found +// via spreading in fragments. Called when visiting each SelectionSet in the +// GraphQL Document. +function findConflictsWithinSelectionSet( + context: ValidationContext, + cachedFieldsAndFragmentNames: Map, + comparedFragmentPairs: PairSet, + parentType: Maybe, + selectionSet: SelectionSetNode +): Array { + const conflicts: Array = []; + + const [fieldMap, fragmentNames] = getFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + parentType, + selectionSet + ); + + // (A) Find find all conflicts "within" the fields of this selection set. + // Note: this is the *only place* `collectConflictsWithin` is called. + collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap); + + if (fragmentNames.length !== 0) { + // (B) Then collect conflicts between these fields and those represented by + // each spread fragment name found. + for (let i = 0; i < fragmentNames.length; i++) { + collectConflictsBetweenFieldsAndFragment( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + false, + fieldMap, + fragmentNames[i] + ); + // (C) Then compare this fragment with all other fragments found in this + // selection set to collect conflicts between fragments spread together. + // This compares each item in the list of fragment names to every other + // item in that same list (except for itself). + for (let j = i + 1; j < fragmentNames.length; j++) { + collectConflictsBetweenFragments( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + false, + fragmentNames[i], + fragmentNames[j] + ); + } + } + } + return conflicts; +} + +// Collect all conflicts found between a set of fields and a fragment reference +// including via spreading in any nested fragments. +function collectConflictsBetweenFieldsAndFragment( + context: ValidationContext, + conflicts: Array, + cachedFieldsAndFragmentNames: Map, + comparedFragmentPairs: PairSet, + areMutuallyExclusive: boolean, + fieldMap: NodeAndDefCollection, + fragmentName: string +): void { + const fragment = context.getFragment(fragmentName); + if (!fragment) { + return; + } + + const [fieldMap2, referencedFragmentNames] = getReferencedFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + fragment + ); + + // Do not compare a fragment's fieldMap to itself. + if (fieldMap === fieldMap2) { + return; + } + + // (D) First collect any conflicts between the provided collection of fields + // and the collection of fields represented by the given fragment. + collectConflictsBetween( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap, + fieldMap2 + ); + + // (E) Then collect any conflicts between the provided collection of fields + // and any fragment names found in the given fragment. + for (const referencedFragmentName of referencedFragmentNames) { + // Memoize so two fragments are not compared for conflicts more than once. + if (comparedFragmentPairs.has(referencedFragmentName, fragmentName, areMutuallyExclusive)) { + continue; + } + comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive); + + collectConflictsBetweenFieldsAndFragment( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap, + referencedFragmentName + ); + } +} + +// Collect all conflicts found between two fragments, including via spreading in +// any nested fragments. +function collectConflictsBetweenFragments( + context: ValidationContext, + conflicts: Array, + cachedFieldsAndFragmentNames: Map, + comparedFragmentPairs: PairSet, + areMutuallyExclusive: boolean, + fragmentName1: string, + fragmentName2: string +): void { + // No need to compare a fragment to itself. + if (fragmentName1 === fragmentName2) { + return; + } + + // Memoize so two fragments are not compared for conflicts more than once. + if (comparedFragmentPairs.has(fragmentName1, fragmentName2, areMutuallyExclusive)) { + return; + } + comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive); + + const fragment1 = context.getFragment(fragmentName1); + const fragment2 = context.getFragment(fragmentName2); + if (!fragment1 || !fragment2) { + return; + } + + const [fieldMap1, referencedFragmentNames1] = getReferencedFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + fragment1 + ); + const [fieldMap2, referencedFragmentNames2] = getReferencedFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + fragment2 + ); + + // (F) First, collect all conflicts between these two collections of fields + // (not including any nested fragments). + collectConflictsBetween( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap1, + fieldMap2 + ); + + // (G) Then collect conflicts between the first fragment and any nested + // fragments spread in the second fragment. + for (const referencedFragmentName2 of referencedFragmentNames2) { + collectConflictsBetweenFragments( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fragmentName1, + referencedFragmentName2 + ); + } + + // (G) Then collect conflicts between the second fragment and any nested + // fragments spread in the first fragment. + for (const referencedFragmentName1 of referencedFragmentNames1) { + collectConflictsBetweenFragments( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + referencedFragmentName1, + fragmentName2 + ); + } +} + +// Find all conflicts found between two selection sets, including those found +// via spreading in fragments. Called when determining if conflicts exist +// between the sub-fields of two overlapping fields. +function findConflictsBetweenSubSelectionSets( + context: ValidationContext, + cachedFieldsAndFragmentNames: Map, + comparedFragmentPairs: PairSet, + areMutuallyExclusive: boolean, + parentType1: Maybe, + selectionSet1: SelectionSetNode, + parentType2: Maybe, + selectionSet2: SelectionSetNode +): Array { + const conflicts: Array = []; + + const [fieldMap1, fragmentNames1] = getFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + parentType1, + selectionSet1 + ); + const [fieldMap2, fragmentNames2] = getFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + parentType2, + selectionSet2 + ); + + // (H) First, collect all conflicts between these two collections of field. + collectConflictsBetween( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap1, + fieldMap2 + ); + + // (I) Then collect conflicts between the first collection of fields and + // those referenced by each fragment name associated with the second. + for (const fragmentName2 of fragmentNames2) { + collectConflictsBetweenFieldsAndFragment( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap1, + fragmentName2 + ); + } + + // (I) Then collect conflicts between the second collection of fields and + // those referenced by each fragment name associated with the first. + for (const fragmentName1 of fragmentNames1) { + collectConflictsBetweenFieldsAndFragment( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap2, + fragmentName1 + ); + } + + // (J) Also collect conflicts between any fragment names by the first and + // fragment names by the second. This compares each item in the first set of + // names to each item in the second set of names. + for (const fragmentName1 of fragmentNames1) { + for (const fragmentName2 of fragmentNames2) { + collectConflictsBetweenFragments( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fragmentName1, + fragmentName2 + ); + } + } + return conflicts; +} + +// Collect all Conflicts "within" one collection of fields. +function collectConflictsWithin( + context: ValidationContext, + conflicts: Array, + cachedFieldsAndFragmentNames: Map, + comparedFragmentPairs: PairSet, + fieldMap: NodeAndDefCollection +): void { + // A field map is a keyed collection, where each key represents a response + // name and the value at that key is a list of all fields which provide that + // response name. For every response name, if there are multiple fields, they + // must be compared to find a potential conflict. + for (const [responseName, fields] of Object.entries(fieldMap)) { + // This compares every field in the list to every other field in this list + // (except to itself). If the list only has one item, nothing needs to + // be compared. + if (fields.length > 1) { + for (let i = 0; i < fields.length; i++) { + for (let j = i + 1; j < fields.length; j++) { + const conflict = findConflict( + context, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + false, // within one collection is never mutually exclusive + responseName, + fields[i], + fields[j] + ); + if (conflict) { + conflicts.push(conflict); + } + } + } + } + } +} + +// Collect all Conflicts between two collections of fields. This is similar to, +// but different from the `collectConflictsWithin` function above. This check +// assumes that `collectConflictsWithin` has already been called on each +// provided collection of fields. This is true because this validator traverses +// each individual selection set. +function collectConflictsBetween( + context: ValidationContext, + conflicts: Array, + cachedFieldsAndFragmentNames: Map, + comparedFragmentPairs: PairSet, + parentFieldsAreMutuallyExclusive: boolean, + fieldMap1: NodeAndDefCollection, + fieldMap2: NodeAndDefCollection +): void { + // A field map is a keyed collection, where each key represents a response + // name and the value at that key is a list of all fields which provide that + // response name. For any response name which appears in both provided field + // maps, each field from the first field map must be compared to every field + // in the second field map to find potential conflicts. + for (const [responseName, fields1] of Object.entries(fieldMap1)) { + const fields2 = fieldMap2[responseName]; + if (fields2) { + for (const field1 of fields1) { + for (const field2 of fields2) { + const conflict = findConflict( + context, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + parentFieldsAreMutuallyExclusive, + responseName, + field1, + field2 + ); + if (conflict) { + conflicts.push(conflict); + } + } + } + } + } +} + +// Determines if there is a conflict between two particular fields, including +// comparing their sub-fields. +function findConflict( + context: ValidationContext, + cachedFieldsAndFragmentNames: Map, + comparedFragmentPairs: PairSet, + parentFieldsAreMutuallyExclusive: boolean, + responseName: string, + field1: NodeAndDef, + field2: NodeAndDef +): Maybe { + const [parentType1, node1, def1] = field1; + const [parentType2, node2, def2] = field2; + + // If it is known that two fields could not possibly apply at the same + // time, due to the parent types, then it is safe to permit them to diverge + // in aliased field or arguments used as they will not present any ambiguity + // by differing. + // It is known that two parent types could never overlap if they are + // different Object types. Interface or Union types might overlap - if not + // in the current state of the schema, then perhaps in some future version, + // thus may not safely diverge. + const areMutuallyExclusive = + parentFieldsAreMutuallyExclusive || + (parentType1 !== parentType2 && isObjectType(parentType1) && isObjectType(parentType2)); + + if (!areMutuallyExclusive) { + // Two aliases must refer to the same field. + const name1 = node1.name.value; + const name2 = node2.name.value; + if (name1 !== name2) { + return [[responseName, `"${name1}" and "${name2}" are different fields`], [node1], [node2]]; + } + + // Two field calls must have the same arguments. + if (stringifyArguments(node1) !== stringifyArguments(node2)) { + return [[responseName, 'they have differing arguments'], [node1], [node2]]; + } + } + + // The return type for each field. + const type1 = def1?.type; + const type2 = def2?.type; + + if (type1 && type2 && doTypesConflict(type1, type2)) { + return [ + [responseName, `they return conflicting types "${inspect(type1)}" and "${inspect(type2)}"`], + [node1], + [node2], + ]; + } + + // Collect and compare sub-fields. Use the same "visited fragment names" list + // for both collections so fields in a fragment reference are never + // compared to themselves. + const selectionSet1 = node1.selectionSet; + const selectionSet2 = node2.selectionSet; + if (selectionSet1 && selectionSet2) { + const conflicts = findConflictsBetweenSubSelectionSets( + context, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + getNamedType(type1), + selectionSet1, + getNamedType(type2), + selectionSet2 + ); + return subfieldConflicts(conflicts, responseName, node1, node2); + } + + return undefined; +} + +function stringifyArguments(fieldNode: FieldNode): string { + // FIXME https://github.com/graphql/graphql-js/issues/2203 + const args = /* c8 ignore next */ fieldNode.arguments ?? []; + + const inputObjectWithArgs: ObjectValueNode = { + kind: Kind.OBJECT, + fields: args.map(argNode => ({ + kind: Kind.OBJECT_FIELD, + name: argNode.name, + value: argNode.value, + })), + }; + return print(sortValueNode(inputObjectWithArgs)); +} + +// Two types conflict if both types could not apply to a value simultaneously. +// Composite types are ignored as their individual field types will be compared +// later recursively. However List and Non-Null types must match. +function doTypesConflict(type1: GraphQLOutputType, type2: GraphQLOutputType): boolean { + if (isListType(type1)) { + return isListType(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true; + } + if (isListType(type2)) { + return true; + } + if (isNonNullType(type1)) { + return isNonNullType(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true; + } + if (isNonNullType(type2)) { + return true; + } + if (isLeafType(type1) || isLeafType(type2)) { + return type1 !== type2; + } + return false; +} + +// Given a selection set, return the collection of fields (a mapping of response +// name to field nodes and definitions) as well as a list of fragment names +// referenced via fragment spreads. +function getFieldsAndFragmentNames( + context: ValidationContext, + cachedFieldsAndFragmentNames: Map, + parentType: Maybe, + selectionSet: SelectionSetNode +): FieldsAndFragmentNames { + const cached = cachedFieldsAndFragmentNames.get(selectionSet); + if (cached) { + return cached; + } + const nodeAndDefs: NodeAndDefCollection = Object.create(null); + const fragmentNames: ObjMap = Object.create(null); + _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames); + const result = [nodeAndDefs, Object.keys(fragmentNames)] as const; + cachedFieldsAndFragmentNames.set(selectionSet, result); + return result; +} + +// Given a reference to a fragment, return the represented collection of fields +// as well as a list of nested fragment names referenced via fragment spreads. +function getReferencedFieldsAndFragmentNames( + context: ValidationContext, + cachedFieldsAndFragmentNames: Map, + fragment: FragmentDefinitionNode +) { + // Short-circuit building a type from the node if possible. + const cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet); + if (cached) { + return cached; + } + + const fragmentType = typeFromAST(context.getSchema(), fragment.typeCondition); + return getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragmentType, fragment.selectionSet); +} + +function _collectFieldsAndFragmentNames( + context: ValidationContext, + parentType: Maybe, + selectionSet: SelectionSetNode, + nodeAndDefs: NodeAndDefCollection, + fragmentNames: ObjMap +): void { + for (const selection of selectionSet.selections) { + switch (selection.kind) { + case Kind.FIELD: { + const fieldName = selection.name.value; + let fieldDef; + if (isObjectType(parentType) || isInterfaceType(parentType)) { + fieldDef = parentType.getFields()[fieldName]; + } + const responseName = selection.alias ? selection.alias.value : fieldName; + if (!nodeAndDefs[responseName]) { + nodeAndDefs[responseName] = []; + } + nodeAndDefs[responseName].push([parentType, selection, fieldDef]); + break; + } + case Kind.FRAGMENT_SPREAD: + fragmentNames[selection.name.value] = true; + break; + case Kind.INLINE_FRAGMENT: { + const typeCondition = selection.typeCondition; + const inlineFragmentType = typeCondition ? typeFromAST(context.getSchema(), typeCondition) : parentType; + _collectFieldsAndFragmentNames(context, inlineFragmentType, selection.selectionSet, nodeAndDefs, fragmentNames); + break; + } + } + } +} + +// Given a series of Conflicts which occurred between two sub-fields, generate +// a single Conflict. +function subfieldConflicts( + conflicts: ReadonlyArray, + responseName: string, + node1: FieldNode, + node2: FieldNode +): Maybe { + if (conflicts.length > 0) { + return [ + [responseName, conflicts.map(([reason]) => reason)], + [node1, ...conflicts.map(([, fields1]) => fields1).flat()], + [node2, ...conflicts.map(([, , fields2]) => fields2).flat()], + ]; + } + return undefined; +} + +/** + * A way to keep track of pairs of things when the ordering of the pair does not matter. + */ +class PairSet { + _data: Map>; + + constructor() { + this._data = new Map(); + } + + has(a: string, b: string, areMutuallyExclusive: boolean): boolean { + const [key1, key2] = a < b ? [a, b] : [b, a]; + + const result = this._data.get(key1)?.get(key2); + if (result === undefined) { + return false; + } + + // areMutuallyExclusive being false is a superset of being true, hence if + // we want to know if this PairSet "has" these two with no exclusivity, + // we have to ensure it was added as such. + return areMutuallyExclusive ? true : areMutuallyExclusive === result; + } + + add(a: string, b: string, areMutuallyExclusive: boolean): void { + const [key1, key2] = a < b ? [a, b] : [b, a]; + + const map = this._data.get(key1); + if (map === undefined) { + this._data.set(key1, new Map([[key2, areMutuallyExclusive]])); + } else { + map.set(key2, areMutuallyExclusive); + } + } +} diff --git a/packages/graphql/src/validation/rules/PossibleFragmentSpreadsRule.ts b/packages/graphql/src/validation/rules/PossibleFragmentSpreadsRule.ts new file mode 100644 index 00000000000..c2fb857bb76 --- /dev/null +++ b/packages/graphql/src/validation/rules/PossibleFragmentSpreadsRule.ts @@ -0,0 +1,70 @@ +import { inspect } from '../../jsutils/inspect.js'; +import type { Maybe } from '../../jsutils/Maybe.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { GraphQLCompositeType } from '../../type/definition.js'; +import { isCompositeType } from '../../type/definition.js'; + +import { doTypesOverlap } from '../../utilities/typeComparators.js'; +import { typeFromAST } from '../../utilities/typeFromAST.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Possible fragment spread + * + * A fragment spread is only valid if the type condition could ever possibly + * be true: if there is a non-empty intersection of the possible parent types, + * and possible types which pass the type condition. + */ +export function PossibleFragmentSpreadsRule(context: ValidationContext): ASTVisitor { + return { + InlineFragment(node) { + const fragType = context.getType(); + const parentType = context.getParentType(); + if ( + isCompositeType(fragType) && + isCompositeType(parentType) && + !doTypesOverlap(context.getSchema(), fragType, parentType) + ) { + const parentTypeStr = inspect(parentType); + const fragTypeStr = inspect(fragType); + context.reportError( + new GraphQLError( + `Fragment cannot be spread here as objects of type "${parentTypeStr}" can never be of type "${fragTypeStr}".`, + { nodes: node } + ) + ); + } + }, + FragmentSpread(node) { + const fragName = node.name.value; + const fragType = getFragmentType(context, fragName); + const parentType = context.getParentType(); + if (fragType && parentType && !doTypesOverlap(context.getSchema(), fragType, parentType)) { + const parentTypeStr = inspect(parentType); + const fragTypeStr = inspect(fragType); + context.reportError( + new GraphQLError( + `Fragment "${fragName}" cannot be spread here as objects of type "${parentTypeStr}" can never be of type "${fragTypeStr}".`, + { nodes: node } + ) + ); + } + }, + }; +} + +function getFragmentType(context: ValidationContext, name: string): Maybe { + const frag = context.getFragment(name); + if (frag) { + const type = typeFromAST(context.getSchema(), frag.typeCondition); + if (isCompositeType(type)) { + return type; + } + } + return undefined; +} diff --git a/packages/graphql/src/validation/rules/PossibleTypeExtensionsRule.ts b/packages/graphql/src/validation/rules/PossibleTypeExtensionsRule.ts new file mode 100644 index 00000000000..28f9142845f --- /dev/null +++ b/packages/graphql/src/validation/rules/PossibleTypeExtensionsRule.ts @@ -0,0 +1,139 @@ +import { didYouMean } from '../../jsutils/didYouMean.js'; +import { inspect } from '../../jsutils/inspect.js'; +import { invariant } from '../../jsutils/invariant.js'; +import type { ObjMap } from '../../jsutils/ObjMap.js'; +import { suggestionList } from '../../jsutils/suggestionList.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { DefinitionNode, TypeExtensionNode } from '../../language/ast.js'; +import { Kind } from '../../language/kinds.js'; +import { isTypeDefinitionNode } from '../../language/predicates.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { GraphQLNamedType } from '../../type/definition.js'; +import { + isEnumType, + isInputObjectType, + isInterfaceType, + isObjectType, + isScalarType, + isUnionType, +} from '../../type/definition.js'; + +import type { SDLValidationContext } from '../ValidationContext.js'; + +/** + * Possible type extension + * + * A type extension is only valid if the type is defined and has the same kind. + */ +export function PossibleTypeExtensionsRule(context: SDLValidationContext): ASTVisitor { + const schema = context.getSchema(); + const definedTypes: ObjMap = Object.create(null); + + for (const def of context.getDocument().definitions) { + if (isTypeDefinitionNode(def)) { + definedTypes[def.name.value] = def; + } + } + + return { + ScalarTypeExtension: checkExtension, + ObjectTypeExtension: checkExtension, + InterfaceTypeExtension: checkExtension, + UnionTypeExtension: checkExtension, + EnumTypeExtension: checkExtension, + InputObjectTypeExtension: checkExtension, + }; + + function checkExtension(node: TypeExtensionNode): void { + const typeName = node.name.value; + const defNode = definedTypes[typeName]; + const existingType = schema?.getType(typeName); + + let expectedKind: Kind | undefined; + if (defNode) { + expectedKind = defKindToExtKind[defNode.kind]; + } else if (existingType) { + expectedKind = typeToExtKind(existingType); + } + + if (expectedKind) { + if (expectedKind !== node.kind) { + const kindStr = extensionKindToTypeName(node.kind); + context.reportError( + new GraphQLError(`Cannot extend non-${kindStr} type "${typeName}".`, { + nodes: defNode ? [defNode, node] : node, + }) + ); + } + } else { + const allTypeNames = Object.keys({ + ...definedTypes, + ...schema?.getTypeMap(), + }); + + const suggestedTypes = suggestionList(typeName, allTypeNames); + context.reportError( + new GraphQLError(`Cannot extend type "${typeName}" because it is not defined.` + didYouMean(suggestedTypes), { + nodes: node.name, + }) + ); + } + } +} + +const defKindToExtKind: ObjMap = { + [Kind.SCALAR_TYPE_DEFINITION]: Kind.SCALAR_TYPE_EXTENSION, + [Kind.OBJECT_TYPE_DEFINITION]: Kind.OBJECT_TYPE_EXTENSION, + [Kind.INTERFACE_TYPE_DEFINITION]: Kind.INTERFACE_TYPE_EXTENSION, + [Kind.UNION_TYPE_DEFINITION]: Kind.UNION_TYPE_EXTENSION, + [Kind.ENUM_TYPE_DEFINITION]: Kind.ENUM_TYPE_EXTENSION, + [Kind.INPUT_OBJECT_TYPE_DEFINITION]: Kind.INPUT_OBJECT_TYPE_EXTENSION, +}; + +function typeToExtKind(type: GraphQLNamedType): Kind { + if (isScalarType(type)) { + return Kind.SCALAR_TYPE_EXTENSION; + } + if (isObjectType(type)) { + return Kind.OBJECT_TYPE_EXTENSION; + } + if (isInterfaceType(type)) { + return Kind.INTERFACE_TYPE_EXTENSION; + } + if (isUnionType(type)) { + return Kind.UNION_TYPE_EXTENSION; + } + if (isEnumType(type)) { + return Kind.ENUM_TYPE_EXTENSION; + } + if (isInputObjectType(type)) { + return Kind.INPUT_OBJECT_TYPE_EXTENSION; + } + /* c8 ignore next 3 */ + // Not reachable. All possible types have been considered + invariant(false, 'Unexpected type: ' + inspect(type)); +} + +function extensionKindToTypeName(kind: Kind): string { + switch (kind) { + case Kind.SCALAR_TYPE_EXTENSION: + return 'scalar'; + case Kind.OBJECT_TYPE_EXTENSION: + return 'object'; + case Kind.INTERFACE_TYPE_EXTENSION: + return 'interface'; + case Kind.UNION_TYPE_EXTENSION: + return 'union'; + case Kind.ENUM_TYPE_EXTENSION: + return 'enum'; + case Kind.INPUT_OBJECT_TYPE_EXTENSION: + return 'input object'; + // Not reachable. All possible types have been considered + /* c8 ignore next 2 */ + default: + invariant(false, 'Unexpected kind: ' + inspect(kind)); + } +} diff --git a/packages/graphql/src/validation/rules/ProvidedRequiredArgumentsRule.ts b/packages/graphql/src/validation/rules/ProvidedRequiredArgumentsRule.ts new file mode 100644 index 00000000000..3ece9120f3f --- /dev/null +++ b/packages/graphql/src/validation/rules/ProvidedRequiredArgumentsRule.ts @@ -0,0 +1,113 @@ +import { inspect } from '../../jsutils/inspect.js'; +import { keyMap } from '../../jsutils/keyMap.js'; +import type { ObjMap } from '../../jsutils/ObjMap.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { InputValueDefinitionNode } from '../../language/ast.js'; +import { Kind } from '../../language/kinds.js'; +import { print } from '../../language/printer.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { GraphQLArgument } from '../../type/definition.js'; +import { isRequiredArgument, isType } from '../../type/definition.js'; +import { specifiedDirectives } from '../../type/directives.js'; + +import type { SDLValidationContext, ValidationContext } from '../ValidationContext.js'; + +/** + * Provided required arguments + * + * A field or directive is only valid if all required (non-null without a + * default value) field arguments have been provided. + */ +export function ProvidedRequiredArgumentsRule(context: ValidationContext): ASTVisitor { + return { + ...ProvidedRequiredArgumentsOnDirectivesRule(context), + Field: { + // Validate on leave to allow for deeper errors to appear first. + leave(fieldNode) { + const fieldDef = context.getFieldDef(); + if (!fieldDef) { + return false; + } + + const providedArgs = new Set( + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + fieldNode.arguments?.map(arg => arg.name.value) + ); + for (const argDef of fieldDef.args) { + if (!providedArgs.has(argDef.name) && isRequiredArgument(argDef)) { + const argTypeStr = inspect(argDef.type); + context.reportError( + new GraphQLError( + `Field "${fieldDef.name}" argument "${argDef.name}" of type "${argTypeStr}" is required, but it was not provided.`, + { nodes: fieldNode } + ) + ); + } + } + + return undefined; + }, + }, + }; +} + +/** + * @internal + */ +export function ProvidedRequiredArgumentsOnDirectivesRule( + context: ValidationContext | SDLValidationContext +): ASTVisitor { + const requiredArgsMap: ObjMap> = Object.create(null); + + const schema = context.getSchema(); + const definedDirectives = schema?.getDirectives() ?? specifiedDirectives; + for (const directive of definedDirectives) { + requiredArgsMap[directive.name] = keyMap(directive.args.filter(isRequiredArgument), arg => arg.name); + } + + const astDefinitions = context.getDocument().definitions; + for (const def of astDefinitions) { + if (def.kind === Kind.DIRECTIVE_DEFINITION) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const argNodes = def.arguments ?? []; + + requiredArgsMap[def.name.value] = keyMap(argNodes.filter(isRequiredArgumentNode), arg => arg.name.value); + } + } + + return { + Directive: { + // Validate on leave to allow for deeper errors to appear first. + leave(directiveNode) { + const directiveName = directiveNode.name.value; + const requiredArgs = requiredArgsMap[directiveName]; + if (requiredArgs) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const argNodes = directiveNode.arguments ?? []; + const argNodeMap = new Set(argNodes.map(arg => arg.name.value)); + for (const [argName, argDef] of Object.entries(requiredArgs)) { + if (!argNodeMap.has(argName)) { + const argType = isType(argDef.type) ? inspect(argDef.type) : print(argDef.type); + context.reportError( + new GraphQLError( + `Directive "@${directiveName}" argument "${argName}" of type "${argType}" is required, but it was not provided.`, + { nodes: directiveNode } + ) + ); + } + } + } + }, + }, + }; +} + +function isRequiredArgumentNode(arg: InputValueDefinitionNode): boolean { + return arg.type.kind === Kind.NON_NULL_TYPE && arg.defaultValue == null; +} diff --git a/packages/graphql/src/validation/rules/ScalarLeafsRule.ts b/packages/graphql/src/validation/rules/ScalarLeafsRule.ts new file mode 100644 index 00000000000..bac9432c235 --- /dev/null +++ b/packages/graphql/src/validation/rules/ScalarLeafsRule.ts @@ -0,0 +1,48 @@ +import { inspect } from '../../jsutils/inspect.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { FieldNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { getNamedType, isLeafType } from '../../type/definition.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Scalar leafs + * + * A GraphQL document is valid only if all leaf fields (fields without + * sub selections) are of scalar or enum types. + */ +export function ScalarLeafsRule(context: ValidationContext): ASTVisitor { + return { + Field(node: FieldNode) { + const type = context.getType(); + const selectionSet = node.selectionSet; + if (type) { + if (isLeafType(getNamedType(type))) { + if (selectionSet) { + const fieldName = node.name.value; + const typeStr = inspect(type); + context.reportError( + new GraphQLError( + `Field "${fieldName}" must not have a selection since type "${typeStr}" has no subfields.`, + { nodes: selectionSet } + ) + ); + } + } else if (!selectionSet) { + const fieldName = node.name.value; + const typeStr = inspect(type); + context.reportError( + new GraphQLError( + `Field "${fieldName}" of type "${typeStr}" must have a selection of subfields. Did you mean "${fieldName} { ... }"?`, + { nodes: node } + ) + ); + } + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/SingleFieldSubscriptionsRule.ts b/packages/graphql/src/validation/rules/SingleFieldSubscriptionsRule.ts new file mode 100644 index 00000000000..11fda5748df --- /dev/null +++ b/packages/graphql/src/validation/rules/SingleFieldSubscriptionsRule.ts @@ -0,0 +1,71 @@ +import type { ObjMap } from '../../jsutils/ObjMap.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { FragmentDefinitionNode, OperationDefinitionNode } from '../../language/ast.js'; +import { Kind } from '../../language/kinds.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { collectFields } from '../../execution/collectFields.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Subscriptions must only include a non-introspection field. + * + * A GraphQL subscription is valid only if it contains a single root field and + * that root field is not an introspection field. + * + * See https://spec.graphql.org/draft/#sec-Single-root-field + */ +export function SingleFieldSubscriptionsRule(context: ValidationContext): ASTVisitor { + return { + OperationDefinition(node: OperationDefinitionNode) { + if (node.operation === 'subscription') { + const schema = context.getSchema(); + const subscriptionType = schema.getSubscriptionType(); + if (subscriptionType) { + const operationName = node.name ? node.name.value : null; + const variableValues: { + [variable: string]: any; + } = Object.create(null); + const document = context.getDocument(); + const fragments: ObjMap = Object.create(null); + for (const definition of document.definitions) { + if (definition.kind === Kind.FRAGMENT_DEFINITION) { + fragments[definition.name.value] = definition; + } + } + const fields = collectFields(schema, fragments, variableValues, subscriptionType, node.selectionSet); + if (fields.size > 1) { + const fieldSelectionLists = [...fields.values()]; + const extraFieldSelectionLists = fieldSelectionLists.slice(1); + const extraFieldSelections = extraFieldSelectionLists.flat(); + context.reportError( + new GraphQLError( + operationName != null + ? `Subscription "${operationName}" must select only one top level field.` + : 'Anonymous Subscription must select only one top level field.', + { nodes: extraFieldSelections } + ) + ); + } + for (const fieldNodes of fields.values()) { + const field = fieldNodes[0]; + const fieldName = field.name.value; + if (fieldName.startsWith('__')) { + context.reportError( + new GraphQLError( + operationName != null + ? `Subscription "${operationName}" must not select an introspection top level field.` + : 'Anonymous Subscription must not select an introspection top level field.', + { nodes: fieldNodes } + ) + ); + } + } + } + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/UniqueArgumentDefinitionNamesRule.ts b/packages/graphql/src/validation/rules/UniqueArgumentDefinitionNamesRule.ts new file mode 100644 index 00000000000..9e6aad0d7b6 --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueArgumentDefinitionNamesRule.ts @@ -0,0 +1,69 @@ +import { groupBy } from '../../jsutils/groupBy.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { FieldDefinitionNode, InputValueDefinitionNode, NameNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { SDLValidationContext } from '../ValidationContext.js'; + +/** + * Unique argument definition names + * + * A GraphQL Object or Interface type is only valid if all its fields have uniquely named arguments. + * A GraphQL Directive is only valid if all its arguments are uniquely named. + */ +export function UniqueArgumentDefinitionNamesRule(context: SDLValidationContext): ASTVisitor { + return { + DirectiveDefinition(directiveNode) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const argumentNodes = directiveNode.arguments ?? []; + + return checkArgUniqueness(`@${directiveNode.name.value}`, argumentNodes); + }, + InterfaceTypeDefinition: checkArgUniquenessPerField, + InterfaceTypeExtension: checkArgUniquenessPerField, + ObjectTypeDefinition: checkArgUniquenessPerField, + ObjectTypeExtension: checkArgUniquenessPerField, + }; + + function checkArgUniquenessPerField(typeNode: { + readonly name: NameNode; + readonly fields?: ReadonlyArray; + }) { + const typeName = typeNode.name.value; + + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const fieldNodes = typeNode.fields ?? []; + + for (const fieldDef of fieldNodes) { + const fieldName = fieldDef.name.value; + + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const argumentNodes = fieldDef.arguments ?? []; + + checkArgUniqueness(`${typeName}.${fieldName}`, argumentNodes); + } + + return false; + } + + function checkArgUniqueness(parentName: string, argumentNodes: ReadonlyArray) { + const seenArgs = groupBy(argumentNodes, arg => arg.name.value); + + for (const [argName, argNodes] of seenArgs) { + if (argNodes.length > 1) { + context.reportError( + new GraphQLError(`Argument "${parentName}(${argName}:)" can only be defined once.`, { + nodes: argNodes.map(node => node.name), + }) + ); + } + } + + return false; + } +} diff --git a/packages/graphql/src/validation/rules/UniqueArgumentNamesRule.ts b/packages/graphql/src/validation/rules/UniqueArgumentNamesRule.ts new file mode 100644 index 00000000000..e149b367cee --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueArgumentNamesRule.ts @@ -0,0 +1,41 @@ +import { groupBy } from '../../jsutils/groupBy.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ArgumentNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * Unique argument names + * + * A GraphQL field or directive is only valid if all supplied arguments are + * uniquely named. + * + * See https://spec.graphql.org/draft/#sec-Argument-Names + */ +export function UniqueArgumentNamesRule(context: ASTValidationContext): ASTVisitor { + return { + Field: checkArgUniqueness, + Directive: checkArgUniqueness, + }; + + function checkArgUniqueness(parentNode: { arguments?: ReadonlyArray }) { + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const argumentNodes = parentNode.arguments ?? []; + + const seenArgs = groupBy(argumentNodes, arg => arg.name.value); + + for (const [argName, argNodes] of seenArgs) { + if (argNodes.length > 1) { + context.reportError( + new GraphQLError(`There can be only one argument named "${argName}".`, { + nodes: argNodes.map(node => node.name), + }) + ); + } + } + } +} diff --git a/packages/graphql/src/validation/rules/UniqueDirectiveNamesRule.ts b/packages/graphql/src/validation/rules/UniqueDirectiveNamesRule.ts new file mode 100644 index 00000000000..70447d3ebbf --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueDirectiveNamesRule.ts @@ -0,0 +1,42 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { SDLValidationContext } from '../ValidationContext.js'; + +/** + * Unique directive names + * + * A GraphQL document is only valid if all defined directives have unique names. + */ +export function UniqueDirectiveNamesRule(context: SDLValidationContext): ASTVisitor { + const knownDirectiveNames = Object.create(null); + const schema = context.getSchema(); + + return { + DirectiveDefinition(node) { + const directiveName = node.name.value; + + if (schema?.getDirective(directiveName)) { + context.reportError( + new GraphQLError(`Directive "@${directiveName}" already exists in the schema. It cannot be redefined.`, { + nodes: node.name, + }) + ); + return; + } + + if (knownDirectiveNames[directiveName]) { + context.reportError( + new GraphQLError(`There can be only one directive named "@${directiveName}".`, { + nodes: [knownDirectiveNames[directiveName], node.name], + }) + ); + } else { + knownDirectiveNames[directiveName] = node.name; + } + + return false; + }, + }; +} diff --git a/packages/graphql/src/validation/rules/UniqueDirectivesPerLocationRule.ts b/packages/graphql/src/validation/rules/UniqueDirectivesPerLocationRule.ts new file mode 100644 index 00000000000..2fba9b695ab --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueDirectivesPerLocationRule.ts @@ -0,0 +1,77 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import { Kind } from '../../language/kinds.js'; +import { isTypeDefinitionNode, isTypeExtensionNode } from '../../language/predicates.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { specifiedDirectives } from '../../type/directives.js'; + +import type { SDLValidationContext, ValidationContext } from '../ValidationContext.js'; + +/** + * Unique directive names per location + * + * A GraphQL document is only valid if all non-repeatable directives at + * a given location are uniquely named. + * + * See https://spec.graphql.org/draft/#sec-Directives-Are-Unique-Per-Location + */ +export function UniqueDirectivesPerLocationRule(context: ValidationContext | SDLValidationContext): ASTVisitor { + const uniqueDirectiveMap = Object.create(null); + + const schema = context.getSchema(); + const definedDirectives = schema ? schema.getDirectives() : specifiedDirectives; + for (const directive of definedDirectives) { + uniqueDirectiveMap[directive.name] = !directive.isRepeatable; + } + + const astDefinitions = context.getDocument().definitions; + for (const def of astDefinitions) { + if (def.kind === Kind.DIRECTIVE_DEFINITION) { + uniqueDirectiveMap[def.name.value] = !def.repeatable; + } + } + + const schemaDirectives = Object.create(null); + const typeDirectivesMap = Object.create(null); + + return { + // Many different AST nodes may contain directives. Rather than listing + // them all, just listen for entering any node, and check to see if it + // defines any directives. + enter(node) { + if (!('directives' in node) || !node.directives) { + return; + } + + let seenDirectives; + if (node.kind === Kind.SCHEMA_DEFINITION || node.kind === Kind.SCHEMA_EXTENSION) { + seenDirectives = schemaDirectives; + } else if (isTypeDefinitionNode(node) || isTypeExtensionNode(node)) { + const typeName = node.name.value; + seenDirectives = typeDirectivesMap[typeName]; + if (seenDirectives === undefined) { + typeDirectivesMap[typeName] = seenDirectives = Object.create(null); + } + } else { + seenDirectives = Object.create(null); + } + + for (const directive of node.directives) { + const directiveName = directive.name.value; + + if (uniqueDirectiveMap[directiveName]) { + if (seenDirectives[directiveName]) { + context.reportError( + new GraphQLError(`The directive "@${directiveName}" can only be used once at this location.`, { + nodes: [seenDirectives[directiveName], directive], + }) + ); + } else { + seenDirectives[directiveName] = directive; + } + } + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/UniqueEnumValueNamesRule.ts b/packages/graphql/src/validation/rules/UniqueEnumValueNamesRule.ts new file mode 100644 index 00000000000..7369b73a8e3 --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueEnumValueNamesRule.ts @@ -0,0 +1,61 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { EnumTypeDefinitionNode, EnumTypeExtensionNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { isEnumType } from '../../type/definition.js'; + +import type { SDLValidationContext } from '../ValidationContext.js'; + +/** + * Unique enum value names + * + * A GraphQL enum type is only valid if all its values are uniquely named. + */ +export function UniqueEnumValueNamesRule(context: SDLValidationContext): ASTVisitor { + const schema = context.getSchema(); + const existingTypeMap = schema ? schema.getTypeMap() : Object.create(null); + const knownValueNames = Object.create(null); + + return { + EnumTypeDefinition: checkValueUniqueness, + EnumTypeExtension: checkValueUniqueness, + }; + + function checkValueUniqueness(node: EnumTypeDefinitionNode | EnumTypeExtensionNode) { + const typeName = node.name.value; + + if (!knownValueNames[typeName]) { + knownValueNames[typeName] = Object.create(null); + } + + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const valueNodes = node.values ?? []; + const valueNames = knownValueNames[typeName]; + + for (const valueDef of valueNodes) { + const valueName = valueDef.name.value; + + const existingType = existingTypeMap[typeName]; + if (isEnumType(existingType) && existingType.getValue(valueName)) { + context.reportError( + new GraphQLError( + `Enum value "${typeName}.${valueName}" already exists in the schema. It cannot also be defined in this type extension.`, + { nodes: valueDef.name } + ) + ); + } else if (valueNames[valueName]) { + context.reportError( + new GraphQLError(`Enum value "${typeName}.${valueName}" can only be defined once.`, { + nodes: [valueNames[valueName], valueDef.name], + }) + ); + } else { + valueNames[valueName] = valueDef.name; + } + } + + return false; + } +} diff --git a/packages/graphql/src/validation/rules/UniqueFieldDefinitionNamesRule.ts b/packages/graphql/src/validation/rules/UniqueFieldDefinitionNamesRule.ts new file mode 100644 index 00000000000..f0eb42d9766 --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueFieldDefinitionNamesRule.ts @@ -0,0 +1,75 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { FieldDefinitionNode, InputValueDefinitionNode, NameNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { GraphQLNamedType } from '../../type/definition.js'; +import { isInputObjectType, isInterfaceType, isObjectType } from '../../type/definition.js'; + +import type { SDLValidationContext } from '../ValidationContext.js'; + +/** + * Unique field definition names + * + * A GraphQL complex type is only valid if all its fields are uniquely named. + */ +export function UniqueFieldDefinitionNamesRule(context: SDLValidationContext): ASTVisitor { + const schema = context.getSchema(); + const existingTypeMap = schema ? schema.getTypeMap() : Object.create(null); + const knownFieldNames = Object.create(null); + + return { + InputObjectTypeDefinition: checkFieldUniqueness, + InputObjectTypeExtension: checkFieldUniqueness, + InterfaceTypeDefinition: checkFieldUniqueness, + InterfaceTypeExtension: checkFieldUniqueness, + ObjectTypeDefinition: checkFieldUniqueness, + ObjectTypeExtension: checkFieldUniqueness, + }; + + function checkFieldUniqueness(node: { + readonly name: NameNode; + readonly fields?: ReadonlyArray; + }) { + const typeName = node.name.value; + + if (!knownFieldNames[typeName]) { + knownFieldNames[typeName] = Object.create(null); + } + + // FIXME: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const fieldNodes = node.fields ?? []; + const fieldNames = knownFieldNames[typeName]; + + for (const fieldDef of fieldNodes) { + const fieldName = fieldDef.name.value; + + if (hasField(existingTypeMap[typeName], fieldName)) { + context.reportError( + new GraphQLError( + `Field "${typeName}.${fieldName}" already exists in the schema. It cannot also be defined in this type extension.`, + { nodes: fieldDef.name } + ) + ); + } else if (fieldNames[fieldName]) { + context.reportError( + new GraphQLError(`Field "${typeName}.${fieldName}" can only be defined once.`, { + nodes: [fieldNames[fieldName], fieldDef.name], + }) + ); + } else { + fieldNames[fieldName] = fieldDef.name; + } + } + + return false; + } +} + +function hasField(type: GraphQLNamedType, fieldName: string): boolean { + if (isObjectType(type) || isInterfaceType(type) || isInputObjectType(type)) { + return type.getFields()[fieldName] != null; + } + return false; +} diff --git a/packages/graphql/src/validation/rules/UniqueFragmentNamesRule.ts b/packages/graphql/src/validation/rules/UniqueFragmentNamesRule.ts new file mode 100644 index 00000000000..072cc1b13ad --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueFragmentNamesRule.ts @@ -0,0 +1,32 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * Unique fragment names + * + * A GraphQL document is only valid if all defined fragments have unique names. + * + * See https://spec.graphql.org/draft/#sec-Fragment-Name-Uniqueness + */ +export function UniqueFragmentNamesRule(context: ASTValidationContext): ASTVisitor { + const knownFragmentNames = Object.create(null); + return { + OperationDefinition: () => false, + FragmentDefinition(node) { + const fragmentName = node.name.value; + if (knownFragmentNames[fragmentName]) { + context.reportError( + new GraphQLError(`There can be only one fragment named "${fragmentName}".`, { + nodes: [knownFragmentNames[fragmentName], node.name], + }) + ); + } else { + knownFragmentNames[fragmentName] = node.name; + } + return false; + }, + }; +} diff --git a/packages/graphql/src/validation/rules/UniqueInputFieldNamesRule.ts b/packages/graphql/src/validation/rules/UniqueInputFieldNamesRule.ts new file mode 100644 index 00000000000..a71a352dfd8 --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueInputFieldNamesRule.ts @@ -0,0 +1,48 @@ +import { invariant } from '../../jsutils/invariant.js'; +import type { ObjMap } from '../../jsutils/ObjMap.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { NameNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * Unique input field names + * + * A GraphQL input object value is only valid if all supplied fields are + * uniquely named. + * + * See https://spec.graphql.org/draft/#sec-Input-Object-Field-Uniqueness + */ +export function UniqueInputFieldNamesRule(context: ASTValidationContext): ASTVisitor { + const knownNameStack: Array> = []; + let knownNames: ObjMap = Object.create(null); + + return { + ObjectValue: { + enter() { + knownNameStack.push(knownNames); + knownNames = Object.create(null); + }, + leave() { + const prevKnownNames = knownNameStack.pop(); + invariant(prevKnownNames != null); + knownNames = prevKnownNames; + }, + }, + ObjectField(node) { + const fieldName = node.name.value; + if (knownNames[fieldName]) { + context.reportError( + new GraphQLError(`There can be only one input field named "${fieldName}".`, { + nodes: [knownNames[fieldName], node.name], + }) + ); + } else { + knownNames[fieldName] = node.name; + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/UniqueOperationNamesRule.ts b/packages/graphql/src/validation/rules/UniqueOperationNamesRule.ts new file mode 100644 index 00000000000..f7747e9ad88 --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueOperationNamesRule.ts @@ -0,0 +1,34 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * Unique operation names + * + * A GraphQL document is only valid if all defined operations have unique names. + * + * See https://spec.graphql.org/draft/#sec-Operation-Name-Uniqueness + */ +export function UniqueOperationNamesRule(context: ASTValidationContext): ASTVisitor { + const knownOperationNames = Object.create(null); + return { + OperationDefinition(node) { + const operationName = node.name; + if (operationName) { + if (knownOperationNames[operationName.value]) { + context.reportError( + new GraphQLError(`There can be only one operation named "${operationName.value}".`, { + nodes: [knownOperationNames[operationName.value], operationName], + }) + ); + } else { + knownOperationNames[operationName.value] = operationName; + } + } + return false; + }, + FragmentDefinition: () => false, + }; +} diff --git a/packages/graphql/src/validation/rules/UniqueOperationTypesRule.ts b/packages/graphql/src/validation/rules/UniqueOperationTypesRule.ts new file mode 100644 index 00000000000..2afd52bacda --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueOperationTypesRule.ts @@ -0,0 +1,57 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { SchemaDefinitionNode, SchemaExtensionNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { SDLValidationContext } from '../ValidationContext.js'; + +/** + * Unique operation types + * + * A GraphQL document is only valid if it has only one type per operation. + */ +export function UniqueOperationTypesRule(context: SDLValidationContext): ASTVisitor { + const schema = context.getSchema(); + const definedOperationTypes = Object.create(null); + const existingOperationTypes = schema + ? { + query: schema.getQueryType(), + mutation: schema.getMutationType(), + subscription: schema.getSubscriptionType(), + } + : {}; + + return { + SchemaDefinition: checkOperationTypes, + SchemaExtension: checkOperationTypes, + }; + + function checkOperationTypes(node: SchemaDefinitionNode | SchemaExtensionNode) { + // See: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const operationTypesNodes = node.operationTypes ?? []; + + for (const operationType of operationTypesNodes) { + const operation = operationType.operation; + const alreadyDefinedOperationType = definedOperationTypes[operation]; + + if (existingOperationTypes[operation]) { + context.reportError( + new GraphQLError(`Type for ${operation} already defined in the schema. It cannot be redefined.`, { + nodes: operationType, + }) + ); + } else if (alreadyDefinedOperationType) { + context.reportError( + new GraphQLError(`There can be only one ${operation} type in schema.`, { + nodes: [alreadyDefinedOperationType, operationType], + }) + ); + } else { + definedOperationTypes[operation] = operationType; + } + } + + return false; + } +} diff --git a/packages/graphql/src/validation/rules/UniqueTypeNamesRule.ts b/packages/graphql/src/validation/rules/UniqueTypeNamesRule.ts new file mode 100644 index 00000000000..96c1e26a5e3 --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueTypeNamesRule.ts @@ -0,0 +1,51 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { TypeDefinitionNode } from '../../language/ast.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { SDLValidationContext } from '../ValidationContext.js'; + +/** + * Unique type names + * + * A GraphQL document is only valid if all defined types have unique names. + */ +export function UniqueTypeNamesRule(context: SDLValidationContext): ASTVisitor { + const knownTypeNames = Object.create(null); + const schema = context.getSchema(); + + return { + ScalarTypeDefinition: checkTypeName, + ObjectTypeDefinition: checkTypeName, + InterfaceTypeDefinition: checkTypeName, + UnionTypeDefinition: checkTypeName, + EnumTypeDefinition: checkTypeName, + InputObjectTypeDefinition: checkTypeName, + }; + + function checkTypeName(node: TypeDefinitionNode) { + const typeName = node.name.value; + + if (schema?.getType(typeName)) { + context.reportError( + new GraphQLError( + `Type "${typeName}" already exists in the schema. It cannot also be defined in this type definition.`, + { nodes: node.name } + ) + ); + return; + } + + if (knownTypeNames[typeName]) { + context.reportError( + new GraphQLError(`There can be only one type named "${typeName}".`, { + nodes: [knownTypeNames[typeName], node.name], + }) + ); + } else { + knownTypeNames[typeName] = node.name; + } + + return false; + } +} diff --git a/packages/graphql/src/validation/rules/UniqueVariableNamesRule.ts b/packages/graphql/src/validation/rules/UniqueVariableNamesRule.ts new file mode 100644 index 00000000000..7ae944cffd8 --- /dev/null +++ b/packages/graphql/src/validation/rules/UniqueVariableNamesRule.ts @@ -0,0 +1,34 @@ +import { groupBy } from '../../jsutils/groupBy.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { ASTValidationContext } from '../ValidationContext.js'; + +/** + * Unique variable names + * + * A GraphQL operation is only valid if all its variables are uniquely named. + */ +export function UniqueVariableNamesRule(context: ASTValidationContext): ASTVisitor { + return { + OperationDefinition(operationNode) { + // See: https://github.com/graphql/graphql-js/issues/2203 + /* c8 ignore next */ + const variableDefinitions = operationNode.variableDefinitions ?? []; + + const seenVariableDefinitions = groupBy(variableDefinitions, node => node.variable.name.value); + + for (const [variableName, variableNodes] of seenVariableDefinitions) { + if (variableNodes.length > 1) { + context.reportError( + new GraphQLError(`There can be only one variable named "$${variableName}".`, { + nodes: variableNodes.map(node => node.variable.name), + }) + ); + } + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/ValuesOfCorrectTypeRule.ts b/packages/graphql/src/validation/rules/ValuesOfCorrectTypeRule.ts new file mode 100644 index 00000000000..d95703f64a5 --- /dev/null +++ b/packages/graphql/src/validation/rules/ValuesOfCorrectTypeRule.ts @@ -0,0 +1,138 @@ +import { didYouMean } from '../../jsutils/didYouMean.js'; +import { inspect } from '../../jsutils/inspect.js'; +import { keyMap } from '../../jsutils/keyMap.js'; +import { suggestionList } from '../../jsutils/suggestionList.js'; + +import { GraphQLError, isGraphQLError } from '../../error/GraphQLError.js'; + +import type { ValueNode } from '../../language/ast.js'; +import { print } from '../../language/printer.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { + getNamedType, + getNullableType, + isInputObjectType, + isLeafType, + isListType, + isNonNullType, + isRequiredInputField, +} from '../../type/definition.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Value literals of correct type + * + * A GraphQL document is only valid if all value literals are of the type + * expected at their position. + * + * See https://spec.graphql.org/draft/#sec-Values-of-Correct-Type + */ +export function ValuesOfCorrectTypeRule(context: ValidationContext): ASTVisitor { + return { + ListValue(node) { + // Note: TypeInfo will traverse into a list's item type, so look to the + // parent input type to check if it is a list. + const type = getNullableType(context.getParentInputType()); + if (!isListType(type)) { + isValidValueNode(context, node); + return false; // Don't traverse further. + } + return undefined; + }, + ObjectValue(node) { + const type = getNamedType(context.getInputType()); + if (!isInputObjectType(type)) { + isValidValueNode(context, node); + return false; // Don't traverse further. + } + // Ensure every required field exists. + const fieldNodeMap = keyMap(node.fields, field => field.name.value); + for (const fieldDef of Object.values(type.getFields())) { + const fieldNode = fieldNodeMap[fieldDef.name]; + if (!fieldNode && isRequiredInputField(fieldDef)) { + const typeStr = inspect(fieldDef.type); + context.reportError( + new GraphQLError(`Field "${type.name}.${fieldDef.name}" of required type "${typeStr}" was not provided.`, { + nodes: node, + }) + ); + } + } + return undefined; + }, + ObjectField(node) { + const parentType = getNamedType(context.getParentInputType()); + const fieldType = context.getInputType(); + if (!fieldType && isInputObjectType(parentType)) { + const suggestions = suggestionList(node.name.value, Object.keys(parentType.getFields())); + context.reportError( + new GraphQLError( + `Field "${node.name.value}" is not defined by type "${parentType.name}".` + didYouMean(suggestions), + { nodes: node } + ) + ); + } + }, + NullValue(node) { + const type = context.getInputType(); + if (isNonNullType(type)) { + context.reportError( + new GraphQLError(`Expected value of type "${inspect(type)}", found ${print(node)}.`, { nodes: node }) + ); + } + }, + EnumValue: node => isValidValueNode(context, node), + IntValue: node => isValidValueNode(context, node), + FloatValue: node => isValidValueNode(context, node), + StringValue: node => isValidValueNode(context, node), + BooleanValue: node => isValidValueNode(context, node), + }; +} + +/** + * Any value literal may be a valid representation of a Scalar, depending on + * that scalar type. + */ +function isValidValueNode(context: ValidationContext, node: ValueNode): void { + // Report any error at the full type expected by the location. + const locationType = context.getInputType(); + if (!locationType) { + return; + } + + const type = getNamedType(locationType); + + if (!isLeafType(type)) { + const typeStr = inspect(locationType); + context.reportError( + new GraphQLError(`Expected value of type "${typeStr}", found ${print(node)}.`, { nodes: node }) + ); + return; + } + + // Scalars and Enums determine if a literal value is valid via parseLiteral(), + // which may throw or return an invalid value to indicate failure. + try { + const parseResult = type.parseLiteral(node, undefined /* variables */); + if (parseResult === undefined) { + const typeStr = inspect(locationType); + context.reportError( + new GraphQLError(`Expected value of type "${typeStr}", found ${print(node)}.`, { nodes: node }) + ); + } + } catch (error) { + const typeStr = inspect(locationType); + if (isGraphQLError(error)) { + context.reportError(error); + } else { + context.reportError( + new GraphQLError(`Expected value of type "${typeStr}", found ${print(node)}; ` + (error as Error).message, { + nodes: node, + originalError: error as Error, + }) + ); + } + } +} diff --git a/packages/graphql/src/validation/rules/VariablesAreInputTypesRule.ts b/packages/graphql/src/validation/rules/VariablesAreInputTypesRule.ts new file mode 100644 index 00000000000..de6fe3f4124 --- /dev/null +++ b/packages/graphql/src/validation/rules/VariablesAreInputTypesRule.ts @@ -0,0 +1,36 @@ +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { VariableDefinitionNode } from '../../language/ast.js'; +import { print } from '../../language/printer.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import { isInputType } from '../../type/definition.js'; + +import { typeFromAST } from '../../utilities/typeFromAST.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Variables are input types + * + * A GraphQL operation is only valid if all the variables it defines are of + * input types (scalar, enum, or input object). + * + * See https://spec.graphql.org/draft/#sec-Variables-Are-Input-Types + */ +export function VariablesAreInputTypesRule(context: ValidationContext): ASTVisitor { + return { + VariableDefinition(node: VariableDefinitionNode) { + const type = typeFromAST(context.getSchema(), node.type); + + if (type !== undefined && !isInputType(type)) { + const variableName = node.variable.name.value; + const typeName = print(node.type); + + context.reportError( + new GraphQLError(`Variable "$${variableName}" cannot be non-input type "${typeName}".`, { nodes: node.type }) + ); + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/VariablesInAllowedPositionRule.ts b/packages/graphql/src/validation/rules/VariablesInAllowedPositionRule.ts new file mode 100644 index 00000000000..23459b89cac --- /dev/null +++ b/packages/graphql/src/validation/rules/VariablesInAllowedPositionRule.ts @@ -0,0 +1,90 @@ +import { inspect } from '../../jsutils/inspect.js'; +import type { Maybe } from '../../jsutils/Maybe.js'; + +import { GraphQLError } from '../../error/GraphQLError.js'; + +import type { ValueNode } from '../../language/ast.js'; +import { Kind } from '../../language/kinds.js'; +import type { ASTVisitor } from '../../language/visitor.js'; + +import type { GraphQLType } from '../../type/definition.js'; +import { isNonNullType } from '../../type/definition.js'; +import type { GraphQLSchema } from '../../type/schema.js'; + +import { isTypeSubTypeOf } from '../../utilities/typeComparators.js'; +import { typeFromAST } from '../../utilities/typeFromAST.js'; + +import type { ValidationContext } from '../ValidationContext.js'; + +/** + * Variables in allowed position + * + * Variable usages must be compatible with the arguments they are passed to. + * + * See https://spec.graphql.org/draft/#sec-All-Variable-Usages-are-Allowed + */ +export function VariablesInAllowedPositionRule(context: ValidationContext): ASTVisitor { + let varDefMap = Object.create(null); + + return { + OperationDefinition: { + enter() { + varDefMap = Object.create(null); + }, + leave(operation) { + const usages = context.getRecursiveVariableUsages(operation); + + for (const { node, type, defaultValue } of usages) { + const varName = node.name.value; + const varDef = varDefMap[varName]; + if (varDef && type) { + // A var type is allowed if it is the same or more strict (e.g. is + // a subtype of) than the expected type. It can be more strict if + // the variable type is non-null when the expected type is nullable. + // If both are list types, the variable item type can be more strict + // than the expected item type (contravariant). + const schema = context.getSchema(); + const varType = typeFromAST(schema, varDef.type); + if (varType && !allowedVariableUsage(schema, varType, varDef.defaultValue, type, defaultValue)) { + const varTypeStr = inspect(varType); + const typeStr = inspect(type); + context.reportError( + new GraphQLError( + `Variable "$${varName}" of type "${varTypeStr}" used in position expecting type "${typeStr}".`, + { nodes: [varDef, node] } + ) + ); + } + } + } + }, + }, + VariableDefinition(node) { + varDefMap[node.variable.name.value] = node; + }, + }; +} + +/** + * Returns true if the variable is allowed in the location it was found, + * which includes considering if default values exist for either the variable + * or the location at which it is located. + */ +function allowedVariableUsage( + schema: GraphQLSchema, + varType: GraphQLType, + varDefaultValue: Maybe, + locationType: GraphQLType, + locationDefaultValue: Maybe +): boolean { + if (isNonNullType(locationType) && !isNonNullType(varType)) { + const hasNonNullVariableDefaultValue = varDefaultValue != null && varDefaultValue.kind !== Kind.NULL; + const hasLocationDefaultValue = locationDefaultValue !== undefined; + if (!hasNonNullVariableDefaultValue && !hasLocationDefaultValue) { + return false; + } + const nullableLocationType = locationType.ofType; + return isTypeSubTypeOf(schema, varType, nullableLocationType); + } + return isTypeSubTypeOf(schema, varType, locationType); +} diff --git a/packages/graphql/src/validation/rules/custom/NoDeprecatedCustomRule.ts b/packages/graphql/src/validation/rules/custom/NoDeprecatedCustomRule.ts new file mode 100644 index 00000000000..2155d8f5194 --- /dev/null +++ b/packages/graphql/src/validation/rules/custom/NoDeprecatedCustomRule.ts @@ -0,0 +1,91 @@ +import { invariant } from '../../../jsutils/invariant.js'; + +import { GraphQLError } from '../../../error/GraphQLError.js'; + +import type { ASTVisitor } from '../../../language/visitor.js'; + +import { getNamedType, isInputObjectType } from '../../../type/definition.js'; + +import type { ValidationContext } from '../../ValidationContext.js'; + +/** + * No deprecated + * + * A GraphQL document is only valid if all selected fields and all used enum values have not been + * deprecated. + * + * Note: This rule is optional and is not part of the Validation section of the GraphQL + * Specification. The main purpose of this rule is detection of deprecated usages and not + * necessarily to forbid their use when querying a service. + */ +export function NoDeprecatedCustomRule(context: ValidationContext): ASTVisitor { + return { + Field(node) { + const fieldDef = context.getFieldDef(); + const deprecationReason = fieldDef?.deprecationReason; + if (fieldDef && deprecationReason != null) { + const parentType = context.getParentType(); + invariant(parentType != null); + context.reportError( + new GraphQLError(`The field ${parentType.name}.${fieldDef.name} is deprecated. ${deprecationReason}`, { + nodes: node, + }) + ); + } + }, + Argument(node) { + const argDef = context.getArgument(); + const deprecationReason = argDef?.deprecationReason; + if (argDef && deprecationReason != null) { + const directiveDef = context.getDirective(); + if (directiveDef != null) { + context.reportError( + new GraphQLError( + `Directive "@${directiveDef.name}" argument "${argDef.name}" is deprecated. ${deprecationReason}`, + { nodes: node } + ) + ); + } else { + const parentType = context.getParentType(); + const fieldDef = context.getFieldDef(); + invariant(parentType != null && fieldDef != null); + context.reportError( + new GraphQLError( + `Field "${parentType.name}.${fieldDef.name}" argument "${argDef.name}" is deprecated. ${deprecationReason}`, + { nodes: node } + ) + ); + } + } + }, + ObjectField(node) { + const inputObjectDef = getNamedType(context.getParentInputType()); + if (isInputObjectType(inputObjectDef)) { + const inputFieldDef = inputObjectDef.getFields()[node.name.value]; + const deprecationReason = inputFieldDef?.deprecationReason; + if (deprecationReason != null) { + context.reportError( + new GraphQLError( + `The input field ${inputObjectDef.name}.${inputFieldDef.name} is deprecated. ${deprecationReason}`, + { nodes: node } + ) + ); + } + } + }, + EnumValue(node) { + const enumValueDef = context.getEnumValue(); + const deprecationReason = enumValueDef?.deprecationReason; + if (enumValueDef && deprecationReason != null) { + const enumTypeDef = getNamedType(context.getInputType()); + invariant(enumTypeDef != null); + context.reportError( + new GraphQLError( + `The enum value "${enumTypeDef.name}.${enumValueDef.name}" is deprecated. ${deprecationReason}`, + { nodes: node } + ) + ); + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/custom/NoSchemaIntrospectionCustomRule.ts b/packages/graphql/src/validation/rules/custom/NoSchemaIntrospectionCustomRule.ts new file mode 100644 index 00000000000..cca5fdd0f8f --- /dev/null +++ b/packages/graphql/src/validation/rules/custom/NoSchemaIntrospectionCustomRule.ts @@ -0,0 +1,35 @@ +import { GraphQLError } from '../../../error/GraphQLError.js'; + +import type { FieldNode } from '../../../language/ast.js'; +import type { ASTVisitor } from '../../../language/visitor.js'; + +import { getNamedType } from '../../../type/definition.js'; +import { isIntrospectionType } from '../../../type/introspection.js'; + +import type { ValidationContext } from '../../ValidationContext.js'; + +/** + * Prohibit introspection queries + * + * A GraphQL document is only valid if all fields selected are not fields that + * return an introspection type. + * + * Note: This rule is optional and is not part of the Validation section of the + * GraphQL Specification. This rule effectively disables introspection, which + * does not reflect best practices and should only be done if absolutely necessary. + */ +export function NoSchemaIntrospectionCustomRule(context: ValidationContext): ASTVisitor { + return { + Field(node: FieldNode) { + const type = getNamedType(context.getType()); + if (type && isIntrospectionType(type)) { + context.reportError( + new GraphQLError( + `GraphQL introspection has been disabled, but the requested query contained the field "${node.name.value}".`, + { nodes: node } + ) + ); + } + }, + }; +} diff --git a/packages/graphql/src/validation/rules/custom/index.ts b/packages/graphql/src/validation/rules/custom/index.ts new file mode 100644 index 00000000000..d012099ff8c --- /dev/null +++ b/packages/graphql/src/validation/rules/custom/index.ts @@ -0,0 +1,2 @@ +export * from './NoDeprecatedCustomRule.js'; +export * from './NoSchemaIntrospectionCustomRule.js'; diff --git a/packages/graphql/src/validation/rules/index.ts b/packages/graphql/src/validation/rules/index.ts new file mode 100644 index 00000000000..9564e2f5596 --- /dev/null +++ b/packages/graphql/src/validation/rules/index.ts @@ -0,0 +1,35 @@ +export * from './ExecutableDefinitionsRule.js'; +export * from './FieldsOnCorrectTypeRule.js'; +export * from './FragmentsOnCompositeTypesRule.js'; +export * from './KnownArgumentNamesRule.js'; +export * from './KnownDirectivesRule.js'; +export * from './KnownFragmentNamesRule.js'; +export * from './KnownTypeNamesRule.js'; +export * from './LoneAnonymousOperationRule.js'; +export * from './LoneSchemaDefinitionRule.js'; +export * from './NoFragmentCyclesRule.js'; +export * from './NoUndefinedVariablesRule.js'; +export * from './NoUnusedFragmentsRule.js'; +export * from './NoUnusedVariablesRule.js'; +export * from './OverlappingFieldsCanBeMergedRule.js'; +export * from './PossibleFragmentSpreadsRule.js'; +export * from './PossibleTypeExtensionsRule.js'; +export * from './ProvidedRequiredArgumentsRule.js'; +export * from './ScalarLeafsRule.js'; +export * from './SingleFieldSubscriptionsRule.js'; +export * from './UniqueArgumentDefinitionNamesRule.js'; +export * from './UniqueArgumentNamesRule.js'; +export * from './UniqueDirectivesPerLocationRule.js'; +export * from './UniqueEnumValueNamesRule.js'; +export * from './UniqueFieldDefinitionNamesRule.js'; +export * from './UniqueFragmentNamesRule.js'; +export * from './UniqueInputFieldNamesRule.js'; +export * from './UniqueOperationNamesRule.js'; +export * from './UniqueOperationTypesRule.js'; +export * from './UniqueDirectiveNamesRule.js'; +export * from './UniqueTypeNamesRule.js'; +export * from './UniqueVariableNamesRule.js'; +export * from './ValuesOfCorrectTypeRule.js'; +export * from './VariablesAreInputTypesRule.js'; +export * from './VariablesInAllowedPositionRule.js'; +export * from './custom/index.js'; diff --git a/packages/graphql/src/validation/specifiedRules.ts b/packages/graphql/src/validation/specifiedRules.ts new file mode 100644 index 00000000000..c4ac1aeb805 --- /dev/null +++ b/packages/graphql/src/validation/specifiedRules.ts @@ -0,0 +1,121 @@ +// Spec Section: "Executable Definitions" +import { ExecutableDefinitionsRule } from './rules/ExecutableDefinitionsRule.js'; +// Spec Section: "Field Selections on Objects, Interfaces, and Unions Types" +import { FieldsOnCorrectTypeRule } from './rules/FieldsOnCorrectTypeRule.js'; +// Spec Section: "Fragments on Composite Types" +import { FragmentsOnCompositeTypesRule } from './rules/FragmentsOnCompositeTypesRule.js'; +// Spec Section: "Argument Names" +import { KnownArgumentNamesOnDirectivesRule, KnownArgumentNamesRule } from './rules/KnownArgumentNamesRule.js'; +// Spec Section: "Directives Are Defined" +import { KnownDirectivesRule } from './rules/KnownDirectivesRule.js'; +// Spec Section: "Fragment spread target defined" +import { KnownFragmentNamesRule } from './rules/KnownFragmentNamesRule.js'; +// Spec Section: "Fragment Spread Type Existence" +import { KnownTypeNamesRule } from './rules/KnownTypeNamesRule.js'; +// Spec Section: "Lone Anonymous Operation" +import { LoneAnonymousOperationRule } from './rules/LoneAnonymousOperationRule.js'; +// SDL-specific validation rules +import { LoneSchemaDefinitionRule } from './rules/LoneSchemaDefinitionRule.js'; +// Spec Section: "Fragments must not form cycles" +import { NoFragmentCyclesRule } from './rules/NoFragmentCyclesRule.js'; +// Spec Section: "All Variable Used Defined" +import { NoUndefinedVariablesRule } from './rules/NoUndefinedVariablesRule.js'; +// Spec Section: "Fragments must be used" +import { NoUnusedFragmentsRule } from './rules/NoUnusedFragmentsRule.js'; +// Spec Section: "All Variables Used" +import { NoUnusedVariablesRule } from './rules/NoUnusedVariablesRule.js'; +// Spec Section: "Field Selection Merging" +import { OverlappingFieldsCanBeMergedRule } from './rules/OverlappingFieldsCanBeMergedRule.js'; +// Spec Section: "Fragment spread is possible" +import { PossibleFragmentSpreadsRule } from './rules/PossibleFragmentSpreadsRule.js'; +import { PossibleTypeExtensionsRule } from './rules/PossibleTypeExtensionsRule.js'; +// Spec Section: "Argument Optionality" +import { + ProvidedRequiredArgumentsOnDirectivesRule, + ProvidedRequiredArgumentsRule, +} from './rules/ProvidedRequiredArgumentsRule.js'; +// Spec Section: "Leaf Field Selections" +import { ScalarLeafsRule } from './rules/ScalarLeafsRule.js'; +// Spec Section: "Subscriptions with Single Root Field" +import { SingleFieldSubscriptionsRule } from './rules/SingleFieldSubscriptionsRule.js'; +import { UniqueArgumentDefinitionNamesRule } from './rules/UniqueArgumentDefinitionNamesRule.js'; +// Spec Section: "Argument Uniqueness" +import { UniqueArgumentNamesRule } from './rules/UniqueArgumentNamesRule.js'; +import { UniqueDirectiveNamesRule } from './rules/UniqueDirectiveNamesRule.js'; +// Spec Section: "Directives Are Unique Per Location" +import { UniqueDirectivesPerLocationRule } from './rules/UniqueDirectivesPerLocationRule.js'; +import { UniqueEnumValueNamesRule } from './rules/UniqueEnumValueNamesRule.js'; +import { UniqueFieldDefinitionNamesRule } from './rules/UniqueFieldDefinitionNamesRule.js'; +// Spec Section: "Fragment Name Uniqueness" +import { UniqueFragmentNamesRule } from './rules/UniqueFragmentNamesRule.js'; +// Spec Section: "Input Object Field Uniqueness" +import { UniqueInputFieldNamesRule } from './rules/UniqueInputFieldNamesRule.js'; +// Spec Section: "Operation Name Uniqueness" +import { UniqueOperationNamesRule } from './rules/UniqueOperationNamesRule.js'; +import { UniqueOperationTypesRule } from './rules/UniqueOperationTypesRule.js'; +import { UniqueTypeNamesRule } from './rules/UniqueTypeNamesRule.js'; +// Spec Section: "Variable Uniqueness" +import { UniqueVariableNamesRule } from './rules/UniqueVariableNamesRule.js'; +// Spec Section: "Value Type Correctness" +import { ValuesOfCorrectTypeRule } from './rules/ValuesOfCorrectTypeRule.js'; +// Spec Section: "Variables are Input Types" +import { VariablesAreInputTypesRule } from './rules/VariablesAreInputTypesRule.js'; +// Spec Section: "All Variable Usages Are Allowed" +import { VariablesInAllowedPositionRule } from './rules/VariablesInAllowedPositionRule.js'; +import type { SDLValidationRule, ValidationRule } from './ValidationContext.js'; + +/** + * This set includes all validation rules defined by the GraphQL spec. + * + * The order of the rules in this list has been adjusted to lead to the + * most clear output when encountering multiple validation errors. + */ +export const specifiedRules: ReadonlyArray = Object.freeze([ + ExecutableDefinitionsRule, + UniqueOperationNamesRule, + LoneAnonymousOperationRule, + SingleFieldSubscriptionsRule, + KnownTypeNamesRule, + FragmentsOnCompositeTypesRule, + VariablesAreInputTypesRule, + ScalarLeafsRule, + FieldsOnCorrectTypeRule, + UniqueFragmentNamesRule, + KnownFragmentNamesRule, + NoUnusedFragmentsRule, + PossibleFragmentSpreadsRule, + NoFragmentCyclesRule, + UniqueVariableNamesRule, + NoUndefinedVariablesRule, + NoUnusedVariablesRule, + KnownDirectivesRule, + UniqueDirectivesPerLocationRule, + KnownArgumentNamesRule, + UniqueArgumentNamesRule, + ValuesOfCorrectTypeRule, + ProvidedRequiredArgumentsRule, + VariablesInAllowedPositionRule, + OverlappingFieldsCanBeMergedRule, + UniqueInputFieldNamesRule, +]); + +/** + * @internal + */ +export const specifiedSDLRules: ReadonlyArray = Object.freeze([ + LoneSchemaDefinitionRule, + UniqueOperationTypesRule, + UniqueTypeNamesRule, + UniqueEnumValueNamesRule, + UniqueFieldDefinitionNamesRule, + UniqueArgumentDefinitionNamesRule, + UniqueDirectiveNamesRule, + KnownTypeNamesRule, + KnownDirectivesRule, + UniqueDirectivesPerLocationRule, + PossibleTypeExtensionsRule, + KnownArgumentNamesOnDirectivesRule, + UniqueArgumentNamesRule, + UniqueInputFieldNamesRule, + ProvidedRequiredArgumentsOnDirectivesRule, +]); diff --git a/packages/graphql/src/validation/validate.ts b/packages/graphql/src/validation/validate.ts new file mode 100644 index 00000000000..addc169dc3a --- /dev/null +++ b/packages/graphql/src/validation/validate.ts @@ -0,0 +1,119 @@ +import type { Maybe } from '../jsutils/Maybe.js'; + +import { GraphQLError } from '../error/GraphQLError.js'; + +import type { DocumentNode } from '../language/ast.js'; +import { visit, visitInParallel } from '../language/visitor.js'; + +import type { GraphQLSchema } from '../type/schema.js'; +import { assertValidSchema } from '../type/validate.js'; + +import { TypeInfo, visitWithTypeInfo } from '../utilities/TypeInfo.js'; + +import { specifiedRules, specifiedSDLRules } from './specifiedRules.js'; +import type { SDLValidationRule, ValidationRule } from './ValidationContext.js'; +import { SDLValidationContext, ValidationContext } from './ValidationContext.js'; + +/** + * Implements the "Validation" section of the spec. + * + * Validation runs synchronously, returning an array of encountered errors, or + * an empty array if no errors were encountered and the document is valid. + * + * A list of specific validation rules may be provided. If not provided, the + * default list of rules defined by the GraphQL specification will be used. + * + * Each validation rules is a function which returns a visitor + * (see the language/visitor API). Visitor methods are expected to return + * GraphQLErrors, or Arrays of GraphQLErrors when invalid. + * + * Validate will stop validation after a `maxErrors` limit has been reached. + * Attackers can send pathologically invalid queries to induce a DoS attack, + * so by default `maxErrors` set to 100 errors. + * + * Optionally a custom TypeInfo instance may be provided. If not provided, one + * will be created from the provided schema. + */ +export function validate( + schema: GraphQLSchema, + documentAST: DocumentNode, + rules: ReadonlyArray = specifiedRules, + options?: { maxErrors?: number }, + + /** @deprecated will be removed in 17.0.0 */ + typeInfo: TypeInfo = new TypeInfo(schema) +): ReadonlyArray { + const maxErrors = options?.maxErrors ?? 100; + + // If the schema used for validation is invalid, throw an error. + assertValidSchema(schema); + + const abortObj = Object.freeze({}); + const errors: Array = []; + const context = new ValidationContext(schema, documentAST, typeInfo, error => { + if (errors.length >= maxErrors) { + errors.push(new GraphQLError('Too many validation errors, error limit reached. Validation aborted.')); + + throw abortObj; + } + errors.push(error); + }); + + // This uses a specialized visitor which runs multiple visitors in parallel, + // while maintaining the visitor skip and break API. + const visitor = visitInParallel(rules.map(rule => rule(context))); + + // Visit the whole document with each instance of all provided rules. + try { + visit(documentAST, visitWithTypeInfo(typeInfo, visitor)); + } catch (e) { + if (e !== abortObj) { + throw e; + } + } + return errors; +} + +/** + * @internal + */ +export function validateSDL( + documentAST: DocumentNode, + schemaToExtend?: Maybe, + rules: ReadonlyArray = specifiedSDLRules +): ReadonlyArray { + const errors: Array = []; + const context = new SDLValidationContext(documentAST, schemaToExtend, error => { + errors.push(error); + }); + + const visitors = rules.map(rule => rule(context)); + visit(documentAST, visitInParallel(visitors)); + return errors; +} + +/** + * Utility function which asserts a SDL document is valid by throwing an error + * if it is invalid. + * + * @internal + */ +export function assertValidSDL(documentAST: DocumentNode): void { + const errors = validateSDL(documentAST); + if (errors.length !== 0) { + throw new Error(errors.map(error => error.message).join('\n\n')); + } +} + +/** + * Utility function which asserts a SDL document is valid by throwing an error + * if it is invalid. + * + * @internal + */ +export function assertValidSDLExtension(documentAST: DocumentNode, schema: GraphQLSchema): void { + const errors = validateSDL(documentAST, schema); + if (errors.length !== 0) { + throw new Error(errors.map(error => error.message).join('\n\n')); + } +} diff --git a/packages/graphql/src/version.ts b/packages/graphql/src/version.ts new file mode 100644 index 00000000000..2ccaf094d9f --- /dev/null +++ b/packages/graphql/src/version.ts @@ -0,0 +1,17 @@ +// Note: This file is autogenerated using "resources/gen-version.js" script and +// automatically updated by "npm version" command. + +/** + * A string containing the version of the GraphQL.js library + */ +export const version = '16.5.0' as string; + +/** + * An object containing the components of the GraphQL.js version string + */ +export const versionInfo = Object.freeze({ + major: 16 as number, + minor: 5 as number, + patch: 0 as number, + preReleaseTag: null as string | null, +}); diff --git a/scripts/build-api-docs.ts b/scripts/build-api-docs.ts index ff353720781..fb558036c93 100644 --- a/scripts/build-api-docs.ts +++ b/scripts/build-api-docs.ts @@ -24,6 +24,8 @@ async function buildApiDocs() { !packageJsonPath.includes('./website/') && !packageJsonContent.private && packageJsonContent.name !== MONOREPO && + // Skipping the fork for now + !packageJsonContent.name.endsWith('/graphql') && !packageJsonContent.name.endsWith('/container') ) { modules.push([ diff --git a/yarn.lock b/yarn.lock index e2cf1ac6fde..b0e9905f742 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13597,6 +13597,11 @@ typedoc@0.22.15: minimatch "^5.0.1" shiki "^0.10.1" +typescript@4.7.4: + version "4.7.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235" + integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ== + typescript@4.8.2: version "4.8.2" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.2.tgz#e3b33d5ccfb5914e4eeab6699cf208adee3fd790"