@homebound/graphql-id-linter
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -25,5 +25,10 @@ "use strict"; | ||
const possibleFieldsIncludesId = "id" in type.getFields(); | ||
if (possibleFieldsIncludesId && !selectionSetIncludesId(node.selections)) { | ||
if (possibleFieldsIncludesId && !selectionSetIncludes(node.selections, "id")) { | ||
context.reportError(new graphql_1.GraphQLError(`Missing "id" field in "${type.name}" in ${location}`, node, undefined, undefined, location ? [location] : undefined)); | ||
} | ||
// The enum detail pattern is specific pattern we use for enums, if we detect it, | ||
// we need the `code` to be there b/c it's used by the apollo type policy as the key. | ||
if (isEnumDetailObject(type) && !selectionSetIncludes(node.selections, "code")) { | ||
context.reportError(new graphql_1.GraphQLError(`Missing "code" field in "${type.name}" in ${location}`, node, undefined, undefined, location ? [location] : undefined)); | ||
} | ||
return undefined; | ||
@@ -36,7 +41,7 @@ }, | ||
/** Given a set of SelectionNodes from a SelectionSet, ensure "id" is included. */ | ||
function selectionSetIncludesId(selectionNodes) { | ||
function selectionSetIncludes(selectionNodes, fieldName) { | ||
return selectionNodes.some(selectionNode => { | ||
switch (selectionNode.kind) { | ||
case "Field": | ||
return selectionNode.name.value === "id"; | ||
return selectionNode.name.value === fieldName; | ||
// Fragments have their own selection sets. If they do not include an "id", | ||
@@ -50,2 +55,14 @@ // we will catch it when we visit those selection sets. | ||
} | ||
/** | ||
* Look for the FooDetail/code/name pattern of our enum detail objects. | ||
* | ||
* NOTE: Borrowed from graphql-typescript-factories. A helper library may be in order? | ||
*/ | ||
function isEnumDetailObject(object) { | ||
return (object instanceof graphql_1.GraphQLObjectType && | ||
object.name.endsWith("Detail") && | ||
Object.keys(object.getFields()).length >= 2 && | ||
!!object.getFields()["code"] && | ||
!!object.getFields()["name"]); | ||
} | ||
//# sourceMappingURL=IdOnObjectSelectionSetRule.js.map |
{ | ||
"name": "@homebound/graphql-id-linter", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"main": "./build/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./build/", |
Sorry, the diff of this file is not supported yet
9777
100