diff --git a/src/Extractor.ts b/src/Extractor.ts index 84a91534..b60b3993 100644 --- a/src/Extractor.ts +++ b/src/Extractor.ts @@ -1442,6 +1442,8 @@ class Extractor { collectConstValue(node: ts.Expression): ConstValueNode | null { if (ts.isStringLiteral(node)) { return this.gql.string(node, node.text); + } else if (ts.isNoSubstitutionTemplateLiteral(node)) { + return this.gql.string(node, node.text); } else if (ts.isNumericLiteral(node)) { return node.text.includes(".") ? this.gql.float(node, node.text) diff --git a/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticks.ts b/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticks.ts new file mode 100644 index 00000000..2fbecd13 --- /dev/null +++ b/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticks.ts @@ -0,0 +1,7 @@ +/** @gqlType */ +export default class SomeType { + /** @gqlField */ + hello({ greeting = `hello` }: { greeting: string }): string { + return `${greeting} world!`; + } +} diff --git a/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticks.ts.expected b/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticks.ts.expected new file mode 100644 index 00000000..b44b8344 --- /dev/null +++ b/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticks.ts.expected @@ -0,0 +1,43 @@ +----------------- +INPUT +----------------- +/** @gqlType */ +export default class SomeType { + /** @gqlField */ + hello({ greeting = `hello` }: { greeting: string }): string { + return `${greeting} world!`; + } +} + +----------------- +OUTPUT +----------------- +-- SDL -- +type SomeType { + hello(greeting: String! = "hello"): String @metadata +} +-- TypeScript -- +import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLNonNull } from "graphql"; +export function getSchema(): GraphQLSchema { + const SomeTypeType: GraphQLObjectType = new GraphQLObjectType({ + name: "SomeType", + fields() { + return { + hello: { + name: "hello", + type: GraphQLString, + args: { + greeting: { + name: "greeting", + type: new GraphQLNonNull(GraphQLString), + defaultValue: "hello" + } + } + } + }; + } + }); + return new GraphQLSchema({ + types: [SomeTypeType] + }); +} diff --git a/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticksInterpolated.invalid.ts b/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticksInterpolated.invalid.ts new file mode 100644 index 00000000..5cf9f4f7 --- /dev/null +++ b/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticksInterpolated.invalid.ts @@ -0,0 +1,9 @@ +const CONSTANT = "constant"; + +/** @gqlType */ +export default class SomeType { + /** @gqlField */ + hello({ greeting = `hello ${CONSTANT}` }: { greeting: string }): string { + return `${greeting} world!`; + } +} diff --git a/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticksInterpolated.invalid.ts.expected b/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticksInterpolated.invalid.ts.expected new file mode 100644 index 00000000..ddc85853 --- /dev/null +++ b/src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticksInterpolated.invalid.ts.expected @@ -0,0 +1,22 @@ +----------------- +INPUT +----------------- +const CONSTANT = "constant"; + +/** @gqlType */ +export default class SomeType { + /** @gqlField */ + hello({ greeting = `hello ${CONSTANT}` }: { greeting: string }): string { + return `${greeting} world!`; + } +} + +----------------- +OUTPUT +----------------- +src/tests/fixtures/default_values/DefaultArgumentStringLiteralBackticksInterpolated.invalid.ts:6:22 - error: Expected GraphQL field argument default values to be a literal. Grats interprets argument defaults as GraphQL default values, which must be literals. For example: `10` or `"foo"`. + +If you think Grats should be able to infer this constant value, please report an issue at https://github.com/captbaritone/grats/issues. + +6 hello({ greeting = `hello ${CONSTANT}` }: { greeting: string }): string { + ~~~~~~~~~~~~~~~~~~~