Skip to content

Support property names for directive arg defaults #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1753,12 +1753,13 @@ class Extractor {
collectArgDefaults(node: ts.ObjectBindingPattern): ArgDefaults {
const defaults = new Map();
for (const element of node.elements) {
const name = element.propertyName ?? element.name;
if (
ts.isBindingElement(element) &&
element.initializer &&
ts.isIdentifier(element.name)
ts.isIdentifier(name)
) {
defaults.set(element.name.text, element.initializer);
defaults.set(name.text, element.initializer);
}
}
return defaults;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @gqlType */
export default class SomeType {
/** @gqlField */
hello({ if: x = false }: { if: boolean }): string {
return x ? "hello" : "world";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-----------------
INPUT
-----------------
/** @gqlType */
export default class SomeType {
/** @gqlField */
hello({ if: x = false }: { if: boolean }): string {
return x ? "hello" : "world";
}
}

-----------------
OUTPUT
-----------------
-- SDL --
type SomeType {
hello(if: Boolean! = false): String
}
-- TypeScript --
import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLNonNull, GraphQLBoolean } from "graphql";
export function getSchema(): GraphQLSchema {
const SomeTypeType: GraphQLObjectType = new GraphQLObjectType({
name: "SomeType",
fields() {
return {
hello: {
name: "hello",
type: GraphQLString,
args: {
if: {
type: new GraphQLNonNull(GraphQLBoolean),
defaultValue: false
}
}
}
};
}
});
return new GraphQLSchema({
types: [SomeTypeType]
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://github.com/captbaritone/grats/issues/166#issuecomment-2753130827

/**
* @gqlDirective on FRAGMENT_SPREAD | INLINE_FRAGMENT
*/
function defer({
label,
if: _ = true, // anonymous alias
}: {
label: string;
if?: boolean | null;
}): void {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-----------------
INPUT
-----------------
// https://github.com/captbaritone/grats/issues/166#issuecomment-2753130827

/**
* @gqlDirective on FRAGMENT_SPREAD | INLINE_FRAGMENT
*/
function defer({
label,
if: _ = true, // anonymous alias
}: {
label: string;
if?: boolean | null;
}): void {}

-----------------
OUTPUT
-----------------
-- SDL --
directive @defer(label: String!, if: Boolean = true) on FRAGMENT_SPREAD | INLINE_FRAGMENT
-- TypeScript --
import { GraphQLSchema, GraphQLDirective, DirectiveLocation, GraphQLNonNull, GraphQLString, GraphQLBoolean } from "graphql";
export function getSchema(): GraphQLSchema {
return new GraphQLSchema({
directives: [new GraphQLDirective({
name: "defer",
locations: [DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT],
args: {
label: {
type: new GraphQLNonNull(GraphQLString)
},
if: {
type: GraphQLBoolean,
defaultValue: true
}
}
})],
types: []
});
}