Skip to content

Commit a1651a5

Browse files
authored
Merge pull request #93 from oslabs-beta/jd/unbound
Unbounded list functionality with custom directive
2 parents b343289 + 25987e8 commit a1651a5

File tree

5 files changed

+392
-235
lines changed

5 files changed

+392
-235
lines changed

package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"*.{js,ts,css,md}": "prettier --write --ignore-unknown"
6565
},
6666
"dependencies": {
67+
"@graphql-tools/utils": "^8.8.0",
6768
"graphql": "^16.5.0",
6869
"ioredis": "^5.0.5"
6970
}

src/analysis/buildTypeWeights.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ function parseObjectFields(
113113
resolveTo: listType.toString().toLocaleLowerCase(),
114114
};
115115
} else {
116+
// if the @listCost directive is given for the field,
117+
// apply the cost argument's value to the field's weight
118+
const directives = fields[field].astNode?.directives;
119+
120+
if (directives && directives.length > 0) {
121+
directives.forEach((dir) => {
122+
if (dir.name.value === 'listCost') {
123+
if (dir.arguments && dir.arguments[0].value.kind === Kind.INT) {
124+
result.fields[field] = {
125+
resolveTo: listType.toString().toLocaleLowerCase(),
126+
weight: Number(dir.arguments[0].value.value),
127+
};
128+
}
129+
throw new SyntaxError(`@listCost directive improperly configured`);
130+
}
131+
});
132+
}
133+
134+
// if no directive is supplied to list field
116135
fields[field].args.forEach((arg: GraphQLArgument) => {
117136
// If field has an argument matching one of the limiting keywords and resolves to a list
118137
// then the weight of the field should be dependent on both the weight of the resolved type and the limiting argument.
@@ -145,14 +164,16 @@ function parseObjectFields(
145164
return multiplier * (selectionsCost + weight);
146165
// ? what else can get through here
147166
}
167+
148168
// if there is no argument provided with the query, check the schema for a default
149169
if (arg.defaultValue) {
150170
return Number(arg.defaultValue) * (selectionsCost + weight);
151171
}
152172

153-
// FIXME: The list is unbounded. Return the object weight for
173+
// if an unbounded list has no @listCost directive attached
154174
throw new Error(
155-
`ERROR: buildTypeWeights: Unbouned list complexity not supported. Query results should be limited with ${KEYWORDS}`
175+
`ERROR: buildTypeWeights: Use directive @listCost(cost: Int!) on unbounded lists,
176+
or limit query results with ${KEYWORDS}`
156177
);
157178
},
158179
};

0 commit comments

Comments
 (0)