Skip to content
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

Support documentation categories by different root types #833

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 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
21 changes: 17 additions & 4 deletions packages/core/tests/unit/group-info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ describe("group-info", () => {
type Elf @tag(category: "fantasy") {
name: String!
}

type Query {
birds: [Bird!]! @doc(category: "animal")
ljiang-ti marked this conversation as resolved.
Show resolved Hide resolved
}
`);

const groupOptions = {
Expand Down Expand Up @@ -91,6 +95,7 @@ describe("group-info", () => {
describe("getGroups()", () => {
const schemaMap = {
objects: schema.getTypeMap(),
queries: schema.getQueryType().getFields(),
};

test("returns undefined if groupByDirective not defined", () => {
Expand All @@ -103,11 +108,13 @@ describe("group-info", () => {
expect.assertions(1);

expect(getGroups(schemaMap, groupOptions)).toMatchInlineSnapshot(`
{
{
"objects": {
"Bird": "animal",
"Boolean": "common",
"Elf": "common",
"Fish": "common",
"Query": "common",
"String": "common",
"Unicorn": "common",
"__Directive": "common",
Expand All @@ -118,18 +125,24 @@ describe("group-info", () => {
"__Schema": "common",
"__Type": "common",
"__TypeKind": "common",
}
`);
},
"queries": {
"birds": "animal",
},
}
`);
});
});

describe("getGroupName()", () => {
test("returns group name if category directive", () => {
expect.assertions(1);
expect.assertions(2);

const type = schema.getType("Bird");
const queryType = schema.getQueryType().getFields()["birds"];

expect(getGroupName(type, groupOptions)).toBe("animal");
expect(getGroupName(queryType, groupOptions)).toBe("animal");
});

test.each([
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