Skip to content

Commit 74297ba

Browse files
committed
typeWeigthsObject is correctly processing non-null types
1 parent 39dc915 commit 74297ba

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/analysis/buildTypeWeights.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ function parseObjectFields(
7979
// Iterate through the fields and add the required data to the result
8080
Object.keys(fields).forEach((field: string) => {
8181
// The GraphQL type that this field represents
82-
const fieldType: GraphQLOutputType = fields[field].type;
82+
let fieldType: GraphQLOutputType = fields[field].type;
83+
if (isNonNullType(fieldType)) fieldType = fieldType.ofType;
8384
if (
84-
isScalarType(fieldType) ||
85-
(isNonNullType(fieldType) && isScalarType(fieldType.ofType))
85+
isScalarType(fieldType) // ||
8686
) {
8787
result.fields[field] = {
8888
weight: typeWeights.scalar,
@@ -91,14 +91,15 @@ function parseObjectFields(
9191
isInterfaceType(fieldType) ||
9292
isUnionType(fieldType) ||
9393
isEnumType(fieldType) ||
94-
isObjectType(fieldType)
94+
isObjectType(fieldType) // ||
9595
) {
9696
result.fields[field] = {
9797
resolveTo: fieldType.name.toLocaleLowerCase(),
9898
};
9999
} else if (isListType(fieldType)) {
100100
// 'listType' is the GraphQL type that the list resolves to
101-
const listType = fieldType.ofType;
101+
let listType = fieldType.ofType;
102+
if (isNonNullType(listType)) listType = listType.ofType;
102103
if (isScalarType(listType) && typeWeights.scalar === 0) {
103104
// list won't compound if weight is zero
104105
result.fields[field] = {

test/analysis/buildTypeWeights.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ describe('Test buildTypeWeightsFromSchema function', () => {
489489
});
490490
});
491491

492-
xdescribe('Not null operator (!) is used', () => {
492+
describe('Not null operator (!) is used', () => {
493493
test('on a scalar, enum or object type', () => {
494494
schema = buildSchema(`
495495
type Human{
@@ -641,7 +641,7 @@ describe('Test buildTypeWeightsFromSchema function', () => {
641641
});
642642

643643
// this is only if we choose to have 'query' as its own property (seperate from object types) in the user configuration options
644-
xtest('query parameter', () => {
644+
test('query parameter', () => {
645645
const typeWeightObject = buildTypeWeightsFromSchema(schema, {
646646
query: 2,
647647
});

test/analysis/weightFunction.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
8484
});
8585
});
8686

87-
xtest('the list is defined with non-null operators (!)', () => {
87+
test('the list is defined with non-null operators (!)', () => {
8888
const villainsQuery = `query { villains(episode: NEWHOPE, limit: 3) { stars, episode } }`;
8989
const willainsQueryAST: DocumentNode = parse(villainsQuery);
9090
expect(getQueryTypeComplexity(willainsQueryAST, {}, typeWeights)).toBe(4);
@@ -93,7 +93,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
9393
const charQueryAST: DocumentNode = parse(charQuery);
9494
expect(getQueryTypeComplexity(charQueryAST, {}, typeWeights)).toBe(4);
9595

96-
const droidsQuery = `droidsQuery { droids(episode: NEWHOPE, limit: 3) { stars, episode } }`;
96+
const droidsQuery = `query droidsQuery { droids(episode: NEWHOPE, limit: 3) { stars, episode } }`;
9797
const droidsQueryAST: DocumentNode = parse(droidsQuery);
9898
expect(getQueryTypeComplexity(droidsQueryAST, {}, typeWeights)).toBe(4);
9999
});

0 commit comments

Comments
 (0)