Skip to content

Commit

Permalink
✨ scope group info to root types
Browse files Browse the repository at this point in the history
  • Loading branch information
ljiang-ti committed Apr 19, 2023
1 parent 910a213 commit 6cdf02b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 30 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/group-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ function getGroups(rootTypes, groupByDirective) {
Object.keys(rootTypes).forEach((typeName) => {
let rootType = rootTypes[typeName];
if (typeof rootType != "undefined" && rootType != null) {
if (typeof groups[typeName] == "undefined") {
groups[typeName] = {};
}
Object.keys(rootType).forEach((type) => {
groups[type] = getGroupName(rootType[type], groupByDirective);
groups[typeName][type] = getGroupName(rootType[type], groupByDirective);
});
}
});
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ module.exports = class Renderer {
);
}

if (hasProperty(this.group, name)) {
dirPath = path.join(dirPath, toSlug(this.group[name]));
await this.generateCategoryMetafile(this.group[name], dirPath);
if (
hasProperty(this.group, rootTypeName) &&
hasProperty(this.group[rootTypeName], name)
) {
dirPath = path.join(dirPath, toSlug(this.group[rootTypeName][name]));
await this.generateCategoryMetafile(
this.group[rootTypeName][name],
dirPath,
);
}

dirPath = path.join(dirPath, toSlug(rootTypeName));
Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/__data__/schema_with_grouping.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Query {
GPA(skip: Int = 0): Int @doc(category: "Grade")
WeightedGPA(input: String, skip: Int!): Int @doc(category: "Grade")
UnWeightedGPA(input: String, skip: Int!): Int @doc(category: "Grade")
Course(skip: Int = 0): Course @doc(category: "Course")
}

type Query {
Expand Down Expand Up @@ -52,3 +53,8 @@ type DepartmentInformation implements Record {
departmentCloseTime: Time
website: URL
}

type Course {
id: ID!
title: String!
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ position: 1
link: null
",
"course/queries/all-courses.mdx": "all-courses",
"course/queries/course.mdx": "course",
"course/queries/math-courses.mdx": "math-courses",
"course/queries/science-courses.mdx": "science-courses",
"generated.md": "Dummy homepage for tweet.graphql",
Expand Down Expand Up @@ -64,6 +65,7 @@ link: null
position: 1
link: null
",
"misc/objects/course.mdx": "course",
"misc/objects/department-information.mdx": "department-information",
"misc/objects/semester.mdx": "semester",
"misc/queries/_category_.yml": "label: Queries
Expand Down
36 changes: 20 additions & 16 deletions packages/core/tests/unit/group-info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,26 @@ describe("group-info", () => {

expect(getGroups(schemaMap, groupOptions)).toMatchInlineSnapshot(`
{
"Bird": "animal",
"Boolean": "common",
"Elf": "common",
"Fish": "common",
"Query": "common",
"String": "common",
"Unicorn": "common",
"__Directive": "common",
"__DirectiveLocation": "common",
"__EnumValue": "common",
"__Field": "common",
"__InputValue": "common",
"__Schema": "common",
"__Type": "common",
"__TypeKind": "common",
"birds": "animal",
"objects": {
"Bird": "animal",
"Boolean": "common",
"Elf": "common",
"Fish": "common",
"Query": "common",
"String": "common",
"Unicorn": "common",
"__Directive": "common",
"__DirectiveLocation": "common",
"__EnumValue": "common",
"__Field": "common",
"__InputValue": "common",
"__Schema": "common",
"__Type": "common",
"__TypeKind": "common",
},
"queries": {
"birds": "animal",
},
}
`);
});
Expand Down
10 changes: 7 additions & 3 deletions packages/core/tests/unit/renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,12 @@ describe("renderer", () => {
const [type, name, root, group] = [{}, "Foo", "Baz", "lorem"];

const spy = jest.spyOn(rendererInstance, "generateCategoryMetafile");
jest.replaceProperty(rendererInstance, "group", { [name]: group });
jest.replaceProperty(rendererInstance, "group", {
[root]: { [name]: group },
});
jest
.spyOn(Utils.object, "hasProperty")
.mockImplementation((_, prop) => prop === name);
.mockImplementation((_, prop) => prop === root || prop === name);

const dirPath = await rendererInstance.generateCategoryMetafileType(
type,
Expand Down Expand Up @@ -312,7 +314,9 @@ describe("renderer", () => {
jest.replaceProperty(rendererInstance, "options", {
deprecated: "group",
});
jest.replaceProperty(rendererInstance, "group", { [name]: group });
jest.replaceProperty(rendererInstance, "group", {
[root]: { [name]: group },
});
jest.spyOn(Utils.object, "hasProperty").mockReturnValue(true);
jest.spyOn(Utils.graphql, "isDeprecated").mockReturnValue(true);

Expand Down
7 changes: 5 additions & 2 deletions packages/printer-legacy/src/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ const {
graphql: { getNamedType },
} = require("@graphql-markdown/utils");

const getGroup = (type, groups) => {
const getGroup = (type, groups, typeCategory) => {
if (typeof groups !== "object" || groups === null) {
return "";
}
const graphLQLNamedType = getNamedType(type);
const typeName = graphLQLNamedType.name || graphLQLNamedType;
return hasProperty(groups, typeName) ? toSlug(groups[typeName]) : "";
return hasProperty(groups, typeCategory) &&
hasProperty(groups[typeCategory], typeName)
? toSlug(groups[typeCategory][typeName])
: "";
};

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion packages/printer-legacy/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Link {
? DEPRECATED
: "";
const group = hasProperty(options, "groups")
? getGroup(type, options.groups)
? getGroup(type, options.groups, category.plural)
: "";
const url = pathUrl.join(
options.basePath,
Expand Down
16 changes: 12 additions & 4 deletions packages/printer-legacy/tests/unit/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,34 @@ describe("group", () => {

describe("getGroup()", () => {
test("returns empty string if groups not defined", () => {
const group = getGroup({ name: "FooBaz" }, undefined);
const group = getGroup({ name: "FooBaz" }, undefined, undefined);
expect(group).toBe("");
});

test("returns empty string if groups is null", () => {
const group = getGroup({ name: "FooBaz" }, null);
const group = getGroup({ name: "FooBaz" }, null, undefined);
expect(group).toBe("");
});

test("returns group name string if type has group", () => {
jest.spyOn(Utils.graphql, "getNamedType").mockReturnValue("FooBaz");
jest.spyOn(Utils.object, "hasProperty").mockReturnValue(true);
const group = getGroup({ name: "FooBaz" }, { FooBaz: "Group Test" });
const group = getGroup(
{ name: "FooBaz" },
{ objects: { FooBaz: "Group Test" } },
"objects",
);
expect(group).toBe("Group Test");
});

test("returns empty string if type not in group", () => {
jest.spyOn(Utils.graphql, "getNamedType").mockReturnValue("FooBaz");
jest.spyOn(Utils.object, "hasProperty").mockReturnValue(false);
const group = getGroup({ name: "FooBar" }, { FooBaz: "Group Test" });
const group = getGroup(
{ name: "FooBar" },
{ objects: { FooBaz: "Group Test" } },
"objects",
);
expect(group).toBe("");
});
});
Expand Down

0 comments on commit 6cdf02b

Please sign in to comment.