Skip to content

Commit ff3d48c

Browse files
committed
refactor
1 parent c6f38ab commit ff3d48c

File tree

2 files changed

+56
-48
lines changed

2 files changed

+56
-48
lines changed

src/definitions/input.ts

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
import {
15-
GraphQLSchema,
16-
InputObjectTypeDefinitionNode,
17-
Kind,
18-
TypeNode,
19-
} from "graphql";
14+
import { GraphQLSchema, InputObjectTypeDefinitionNode } from "graphql";
2015
import { shouldIncludeTypeDefinition } from "../helpers/should-include-type-definition";
2116
import { buildTypeMetadata } from "../helpers/build-type-metadata";
2217
import { buildAnnotations } from "../helpers/build-annotations";
2318
import { indent } from "@graphql-codegen/visitor-plugin-common";
2419
import { CodegenConfigWithDefaults } from "../helpers/build-config-with-defaults";
20+
import { inputTypeHasMatchingOutputType } from "../helpers/input-type-has-matching-output-type";
2521

2622
export function buildInputObjectDefinition(
2723
node: InputObjectTypeDefinitionNode,
@@ -32,21 +28,8 @@ export function buildInputObjectDefinition(
3228
return "";
3329
}
3430

35-
const typeNameWithoutInput = getTypeNameWithoutInput(node.name.value);
36-
const matchingType = schema.getType(typeNameWithoutInput)?.astNode;
37-
const matchingTypeFields =
38-
matchingType?.kind === Kind.OBJECT_TYPE_DEFINITION
39-
? matchingType.fields
40-
: [];
41-
const inputFields = node.fields;
42-
const fieldsMatch = matchingTypeFields?.every((field) => {
43-
const matchingInputField = inputFields?.find(
44-
(inputField) => inputField.name.value === field.name.value,
45-
);
46-
if (!matchingInputField) return false;
47-
return fieldsAreEquivalent(field.type, matchingInputField.type);
48-
});
49-
if (matchingTypeFields?.length && fieldsMatch) {
31+
const typeWillBeConsolidated = inputTypeHasMatchingOutputType(node, schema);
32+
if (typeWillBeConsolidated) {
5033
return "";
5134
}
5235

@@ -77,30 +60,3 @@ export function buildInputObjectDefinition(
7760
${classMembers}
7861
)`;
7962
}
80-
81-
function getTypeNameWithoutInput(name: string) {
82-
return name.endsWith("Input") ? name.replace("Input", "") : name;
83-
}
84-
85-
function fieldsAreEquivalent(
86-
typeField: TypeNode,
87-
inputField: TypeNode,
88-
): boolean {
89-
switch (typeField.kind) {
90-
case Kind.NAMED_TYPE:
91-
return (
92-
inputField.kind === Kind.NAMED_TYPE &&
93-
typeField.name.value === getTypeNameWithoutInput(inputField.name.value)
94-
);
95-
case Kind.LIST_TYPE:
96-
return (
97-
inputField.kind === Kind.LIST_TYPE &&
98-
fieldsAreEquivalent(typeField.type, inputField.type)
99-
);
100-
case Kind.NON_NULL_TYPE:
101-
return (
102-
inputField.kind === Kind.NON_NULL_TYPE &&
103-
fieldsAreEquivalent(typeField.type, inputField.type)
104-
);
105-
}
106-
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Kind, TypeNode } from "graphql/index";
2+
import { GraphQLSchema, InputObjectTypeDefinitionNode } from "graphql";
3+
4+
export function inputTypeHasMatchingOutputType(
5+
inputTypeNode: InputObjectTypeDefinitionNode,
6+
schema: GraphQLSchema,
7+
) {
8+
const typeNameWithoutInput = getTypeNameWithoutInput(
9+
inputTypeNode.name.value,
10+
);
11+
const matchingType = schema.getType(typeNameWithoutInput)?.astNode;
12+
const matchingTypeFields =
13+
matchingType?.kind === Kind.OBJECT_TYPE_DEFINITION
14+
? matchingType.fields
15+
: [];
16+
const inputFields = inputTypeNode.fields;
17+
const fieldsMatch = matchingTypeFields?.every((field) => {
18+
const matchingInputField = inputFields?.find(
19+
(inputField) => inputField.name.value === field.name.value,
20+
);
21+
if (!matchingInputField) return false;
22+
return fieldsAreEquivalent(field.type, matchingInputField.type);
23+
});
24+
return Boolean(matchingTypeFields?.length && fieldsMatch);
25+
}
26+
27+
function getTypeNameWithoutInput(name: string) {
28+
return name.endsWith("Input") ? name.replace("Input", "") : name;
29+
}
30+
31+
function fieldsAreEquivalent(
32+
typeField: TypeNode,
33+
inputField: TypeNode,
34+
): boolean {
35+
switch (typeField.kind) {
36+
case Kind.NAMED_TYPE:
37+
return (
38+
inputField.kind === Kind.NAMED_TYPE &&
39+
typeField.name.value === getTypeNameWithoutInput(inputField.name.value)
40+
);
41+
case Kind.LIST_TYPE:
42+
return (
43+
inputField.kind === Kind.LIST_TYPE &&
44+
fieldsAreEquivalent(typeField.type, inputField.type)
45+
);
46+
case Kind.NON_NULL_TYPE:
47+
return (
48+
inputField.kind === Kind.NON_NULL_TYPE &&
49+
fieldsAreEquivalent(typeField.type, inputField.type)
50+
);
51+
}
52+
}

0 commit comments

Comments
 (0)