Skip to content
Open
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
28 changes: 28 additions & 0 deletions packages/core/src/api/config/generate-list-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,32 @@ describe('generateListOptions()', () => {
}`),
);
});

it('propagates source field descriptions to the generated sort and filter parameters', () => {
const input = `
${COMMON_TYPES}
type Query {
people: PersonList
}

type Person {
"""The person's full name"""
name: String!
"""Age in years"""
age: Int!
}
`;

const result = generateListOptions(buildSchema(input));

const sortParameter = result.getType('PersonSortParameter') as any;
expect(sortParameter.getFields().name.description).toBe("The person's full name");
expect(sortParameter.getFields().age.description).toBe('Age in years');

const filterParameter = result.getType('PersonFilterParameter') as any;
expect(filterParameter.getFields().name.description).toBe("The person's full name");
expect(filterParameter.getFields().age.description).toBe('Age in years');
// synthetic fields without a source description remain undescribed
expect(filterParameter.getFields()._and.description).toBeUndefined();
});
Comment on lines +369 to +380

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The production change is correct — I reverted both description: lines locally and confirmed this test genuinely fails without them. Two things on the test itself.

Use printType against exact SDL, like every other test in this file. All 350 lines above assert via printType(...) + removeLeadingWhitespace(...); this one reaches into getFields().name.description behind as any. printType renders the descriptions as docstrings, so a single comparison per generated type asserts both fields, both types, and that _and/_or carry no docstring — in the form the rest of the suite uses, with no as any.

It also drops expect(..._and.description).toBeUndefined(), which pins an implementation detail: graphql-js or stitchSchemas normalising an absent description to null would fail that assertion for reasons unrelated to the behaviour. With printType the question doesn't arise — either a docstring prints or it doesn't. The // synthetic fields ... remain undescribed comment goes away with it.

Cover the pre-existing-input merge path. createSortParameter / createFilterParameter fold in fields from a hand-written {Type}SortParameter / {Type}FilterParameter when one exists (generate-list-options.ts:126-128, :167-169), and Vendure ships several — OrderFilterParameter, OrderSortParameter, ProductFilterParameter. Those fields flow through the same new field.description line, so documented hand-written SDL fields start emitting descriptions too. Currently untested. Second test below covers it (note the merged fields print before the source-type fields — that's the existing merge order, asserted as-is).

Verified locally:

  • GREEN with the fix: Tests 9 passed (9)
  • RED without it (both description: lines deleted from generate-list-options.ts): Tests 2 failed | 7 passed — both new tests fail
Suggested change
const result = generateListOptions(buildSchema(input));
const sortParameter = result.getType('PersonSortParameter') as any;
expect(sortParameter.getFields().name.description).toBe("The person's full name");
expect(sortParameter.getFields().age.description).toBe('Age in years');
const filterParameter = result.getType('PersonFilterParameter') as any;
expect(filterParameter.getFields().name.description).toBe("The person's full name");
expect(filterParameter.getFields().age.description).toBe('Age in years');
// synthetic fields without a source description remain undescribed
expect(filterParameter.getFields()._and.description).toBeUndefined();
});
const result = generateListOptions(buildSchema(input));
expect(printType(result.getType('PersonSortParameter')!)).toBe(
removeLeadingWhitespace(`
input PersonSortParameter {
"""The person's full name"""
name: SortOrder
"""Age in years"""
age: SortOrder
}`),
);
expect(printType(result.getType('PersonFilterParameter')!)).toBe(
removeLeadingWhitespace(`
input PersonFilterParameter {
"""The person's full name"""
name: StringOperators
"""Age in years"""
age: NumberOperators
_and: [PersonFilterParameter!]
_or: [PersonFilterParameter!]
}`),
);
});
it('propagates descriptions from a pre-existing sort and filter parameter', () => {
const input = `
${COMMON_TYPES}
type Query {
people: PersonList
}
type Person {
name: String!
}
input PersonSortParameter {
"""Sort by relevance score"""
score: SortOrder
}
input PersonFilterParameter {
"""Filter by nickname"""
nickname: StringOperators
}
`;
const result = generateListOptions(buildSchema(input));
expect(printType(result.getType('PersonSortParameter')!)).toBe(
removeLeadingWhitespace(`
input PersonSortParameter {
"""Sort by relevance score"""
score: SortOrder
name: SortOrder
}`),
);
expect(printType(result.getType('PersonFilterParameter')!)).toBe(
removeLeadingWhitespace(`
input PersonFilterParameter {
"""Filter by nickname"""
nickname: StringOperators
name: StringOperators
_and: [PersonFilterParameter!]
_or: [PersonFilterParameter!]
}`),
);
});

});
2 changes: 2 additions & 0 deletions packages/core/src/api/config/generate-list-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function createSortParameter(schema: GraphQLSchema, targetType: GraphQLObjectTyp
.reduce((result, field) => {
const fieldConfig: GraphQLInputFieldConfig = {
type: SortOrder,
description: field.description ?? undefined,
};
return {
...result,
Expand Down Expand Up @@ -204,6 +205,7 @@ function createFilterParameter(schema: GraphQLSchema, targetType: GraphQLObjectT
}
const fieldConfig: GraphQLInputFieldConfig = {
type: filterType,
description: field.description ?? undefined,
};
return {
...result,
Expand Down
Loading