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

Show total unused fields (#5878) #6225

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
66 changes: 65 additions & 1 deletion packages/web/app/src/pages/target-explorer-unused.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ const UnusedSchemaView_UnusedSchemaExplorerFragment = graphql(`
__typename
... on GraphQLObjectType {
name
fields {
__typename
args {
__typename
}
}
}
... on GraphQLInterfaceType {
name
fields {
__typename
args {
__typename
}
}
}
... on GraphQLUnionType {
name
Expand All @@ -35,6 +47,9 @@ const UnusedSchemaView_UnusedSchemaExplorerFragment = graphql(`
}
... on GraphQLInputObjectType {
name
fields {
__typename
}
}
... on GraphQLScalarType {
name
Expand Down Expand Up @@ -70,14 +85,48 @@ const UnusedSchemaView = memo(function _UnusedSchemaView(props: {
return grouped;
}, [types]);

const letters = Array.from(typesGroupedByFirstLetter.keys()).sort();
const letters = useMemo(() => {
return Array.from(typesGroupedByFirstLetter.keys()).sort();
}, [typesGroupedByFirstLetter]);

useEffect(() => {
if (!selectedLetter) {
setSelectedLetter(letters[0]);
}
}, [selectedLetter, setSelectedLetter]);

const unused = useMemo(() => {
const count = {
fields: 0,
fieldArguments: 0,
types: 0,
};

for (const type of types) {
if (type.__typename === 'GraphQLInputObjectType') {
count.types++;
count.fields += type.fields.length;
} else if (
type.__typename === 'GraphQLObjectType' ||
type.__typename === 'GraphQLInterfaceType'
) {
count.types++;

for (const field of type.fields) {
if (field.args.length === 0) {
// if there are no arguments, it means the entire field is unused
count.fields++;
} else {
// if there are arguments, the field is used, but the arguments are not
count.fieldArguments += field.args.length;
}
}
}
}

return count;
}, [types]);

if (types.length === 0) {
return (
<div className="flex h-[250px] shrink-0 items-center justify-center rounded-md border border-dashed">
Expand All @@ -97,8 +146,23 @@ const UnusedSchemaView = memo(function _UnusedSchemaView(props: {
return null;
}

const unusedFieldsMessage = [
unused.fields ? `${unused.fields} unused fields` : null,
unused.fieldArguments ? `${unused.fieldArguments} unused field arguments` : null,
]
.filter(Boolean)
.join(' and ');

return (
<div className="space-y-6">
{unusedFieldsMessage.length ? (
<div>
<p className="text-sm text-gray-400">
You have a total of {unusedFieldsMessage} within {unused.types} different types in the
selected time period
</p>
</div>
) : null}
<div>
<TooltipProvider>
{letters.map(letter => (
Expand Down
Loading