Skip to content

Commit

Permalink
Support backtick strings as constants
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Aug 14, 2024
1 parent bc5527c commit 6233439
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @gqlType */
export default class SomeType {
/** @gqlField */
hello({ greeting = `hello` }: { greeting: string }): string {
return `${greeting} world!`;
}
}
Original file line number Diff line number Diff line change
@@ -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]
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const CONSTANT = "constant";

/** @gqlType */
export default class SomeType {
/** @gqlField */
hello({ greeting = `hello ${CONSTANT}` }: { greeting: string }): string {
return `${greeting} world!`;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
~~~~~~~~~~~~~~~~~~~

0 comments on commit 6233439

Please sign in to comment.