Skip to content

Commit 85e5c51

Browse files
authored
extractMinimalViableSchemaForRequestDocument returns optional arguments in fields and directives (#614)
* extractMinimalViableSchemaForRequestDocument now returns optional arguments in fields and directives even when they are not used
1 parent 46406bb commit 85e5c51

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "extractMinimalViableSChemaForRequestDocument now returns optional arguments even when they are not used",
4+
"packageName": "@graphitation/supermassive",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/supermassive/src/utilities/__tests__/extractMinimalViableSchemaForRequestDocument.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ describe(extractMinimalViableSchemaForRequestDocument, () => {
413413
`);
414414
});
415415

416-
it("omits optional arguments", () => {
416+
it("includes optional arguments", () => {
417417
const { sdl } = testHelper(
418418
schema,
419419
`query {
@@ -422,8 +422,38 @@ describe(extractMinimalViableSchemaForRequestDocument, () => {
422422
);
423423
expect(sdl).toMatchInlineSnapshot(`
424424
"type Query {
425-
countFilms: Int!
425+
countFilms(filter: FilmFilterInput): Int!
426+
}
427+
428+
input FilmFilterInput {
429+
genre: FilmGenre
430+
}
431+
432+
enum FilmGenre {
433+
COMEDY
434+
DRAMA
435+
}
436+
"
437+
`);
438+
});
439+
440+
it("should include optional param even when it's not used in directive", () => {
441+
const { sdl } = testHelper(
442+
schema,
443+
`query @i18n {
444+
film(id: 42) {
445+
__typename
446+
}
447+
}`,
448+
);
449+
expect(sdl).toMatchInlineSnapshot(`
450+
"type Query {
451+
film(id: ID!): Film
426452
}
453+
454+
type Film implements Node
455+
456+
directive @i18n(locale: String) on QUERY
427457
"
428458
`);
429459
});

packages/supermassive/src/utilities/extractMinimalViableSchemaForRequestDocument.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
isCompositeType,
1717
isEnumType,
1818
isInputObjectType,
19-
isNonNullType,
2019
isObjectType,
2120
isScalarType,
2221
isSpecifiedScalarType,
@@ -39,7 +38,6 @@ import {
3938
encodeDirectiveLocation,
4039
EnumTypeDefinitionTuple,
4140
FieldDefinition,
42-
getDirectiveDefinitionArgs,
4341
getDirectiveName,
4442
getFieldArgs,
4543
getFields,
@@ -102,7 +100,7 @@ export function extractMinimalViableSchemaForRequestDocument(
102100
assertExistingField(field, node, ancestors);
103101
assertAllArgumentsAreDefined(field, node, ancestors);
104102

105-
const fieldDef = addField(typeDef, field, node);
103+
const fieldDef = addField(typeDef, field);
106104
addReferencedOutputType(schema, types, getFieldTypeReference(fieldDef));
107105
addReferencedInputTypes(schema, types, getFieldArgs(fieldDef));
108106
},
@@ -116,7 +114,7 @@ export function extractMinimalViableSchemaForRequestDocument(
116114
unknownDirectives.push(node);
117115
return;
118116
}
119-
addDirective(directives, directive, node);
117+
addDirective(directives, directive);
120118
},
121119
FragmentDefinition(node, _key, _parent, _path, ancestors): void {
122120
const type = typeInfo.getType();
@@ -223,31 +221,15 @@ function addCompositeType(
223221
function addField(
224222
type: ObjectTypeDefinitionTuple | InterfaceTypeDefinitionTuple,
225223
field: GraphQLField<unknown, unknown>,
226-
fieldNode: FieldNode,
227224
): FieldDefinition {
228225
const fields = getFields(type);
229226

230-
const existingFieldDef: FieldDefinition = fields[field.name];
231-
const previouslyAddedArgs =
232-
getFieldArgs(existingFieldDef) ?? Object.create(null);
233-
234-
const nodeArgs = new Set(fieldNode.arguments?.map((arg) => arg.name.value));
235-
236-
const argsFilter = (argDef: GraphQLArgument) =>
237-
Boolean(
238-
previouslyAddedArgs[argDef.name] ||
239-
isNonNullType(argDef.type) ||
240-
argDef.defaultValue !== undefined ||
241-
nodeArgs.has(argDef.name),
242-
);
243-
244-
return (fields[field.name] = encodeFieldDef(field, argsFilter));
227+
return (fields[field.name] = encodeFieldDef(field));
245228
}
246229

247230
function addDirective(
248231
directives: DirectiveDefinitionTuple[],
249232
directive: GraphQLDirective,
250-
node: DirectiveNode,
251233
) {
252234
const name = directive.name;
253235
let tuple = directives.find((d) => getDirectiveName(d) === name);
@@ -256,17 +238,7 @@ function addDirective(
256238
directives.push(tuple);
257239
}
258240

259-
const previouslyAddedArgs = getDirectiveDefinitionArgs(tuple);
260-
const nodeArgs = new Set(node.arguments?.map((arg) => arg.name.value));
261-
262-
const argsFilter = (argDef: GraphQLArgument) =>
263-
Boolean(
264-
previouslyAddedArgs?.[argDef.name] ||
265-
isNonNullType(argDef.type) ||
266-
argDef.defaultValue !== undefined ||
267-
nodeArgs.has(argDef.name),
268-
);
269-
const [hasArgs, argsRecord] = encodeArguments(directive.args, argsFilter);
241+
const [hasArgs, argsRecord] = encodeArguments(directive.args);
270242
if (hasArgs) {
271243
setDirectiveDefinitionArgs(tuple, argsRecord);
272244
}

0 commit comments

Comments
 (0)