Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "extractMinimalViableSChemaForRequestDocument now returns optional arguments even when they are not used",
"packageName": "@graphitation/supermassive",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ describe(extractMinimalViableSchemaForRequestDocument, () => {
`);
});

it("omits optional arguments", () => {
it("includes optional arguments", () => {
const { sdl } = testHelper(
schema,
`query {
Expand All @@ -422,8 +422,38 @@ describe(extractMinimalViableSchemaForRequestDocument, () => {
);
expect(sdl).toMatchInlineSnapshot(`
"type Query {
countFilms: Int!
countFilms(filter: FilmFilterInput): Int!
}

input FilmFilterInput {
genre: FilmGenre
}

enum FilmGenre {
COMEDY
DRAMA
}
"
`);
});

it("should include optional param even when it's not used in directive", () => {
const { sdl } = testHelper(
schema,
`query @i18n {
film(id: 42) {
__typename
}
}`,
);
expect(sdl).toMatchInlineSnapshot(`
"type Query {
film(id: ID!): Film
}

type Film implements Node

directive @i18n(locale: String) on QUERY
"
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
isCompositeType,
isEnumType,
isInputObjectType,
isNonNullType,
isObjectType,
isScalarType,
isSpecifiedScalarType,
Expand All @@ -39,7 +38,6 @@ import {
encodeDirectiveLocation,
EnumTypeDefinitionTuple,
FieldDefinition,
getDirectiveDefinitionArgs,
getDirectiveName,
getFieldArgs,
getFields,
Expand Down Expand Up @@ -102,7 +100,7 @@ export function extractMinimalViableSchemaForRequestDocument(
assertExistingField(field, node, ancestors);
assertAllArgumentsAreDefined(field, node, ancestors);

const fieldDef = addField(typeDef, field, node);
const fieldDef = addField(typeDef, field);
addReferencedOutputType(schema, types, getFieldTypeReference(fieldDef));
addReferencedInputTypes(schema, types, getFieldArgs(fieldDef));
},
Expand All @@ -116,7 +114,7 @@ export function extractMinimalViableSchemaForRequestDocument(
unknownDirectives.push(node);
return;
}
addDirective(directives, directive, node);
addDirective(directives, directive);
},
FragmentDefinition(node, _key, _parent, _path, ancestors): void {
const type = typeInfo.getType();
Expand Down Expand Up @@ -223,31 +221,15 @@ function addCompositeType(
function addField(
type: ObjectTypeDefinitionTuple | InterfaceTypeDefinitionTuple,
field: GraphQLField<unknown, unknown>,
fieldNode: FieldNode,
): FieldDefinition {
const fields = getFields(type);

const existingFieldDef: FieldDefinition = fields[field.name];
const previouslyAddedArgs =
getFieldArgs(existingFieldDef) ?? Object.create(null);

const nodeArgs = new Set(fieldNode.arguments?.map((arg) => arg.name.value));

const argsFilter = (argDef: GraphQLArgument) =>
Boolean(
previouslyAddedArgs[argDef.name] ||
isNonNullType(argDef.type) ||
argDef.defaultValue !== undefined ||
nodeArgs.has(argDef.name),
);

return (fields[field.name] = encodeFieldDef(field, argsFilter));
return (fields[field.name] = encodeFieldDef(field));
}

function addDirective(
directives: DirectiveDefinitionTuple[],
directive: GraphQLDirective,
node: DirectiveNode,
) {
const name = directive.name;
let tuple = directives.find((d) => getDirectiveName(d) === name);
Expand All @@ -256,17 +238,7 @@ function addDirective(
directives.push(tuple);
}

const previouslyAddedArgs = getDirectiveDefinitionArgs(tuple);
const nodeArgs = new Set(node.arguments?.map((arg) => arg.name.value));

const argsFilter = (argDef: GraphQLArgument) =>
Boolean(
previouslyAddedArgs?.[argDef.name] ||
isNonNullType(argDef.type) ||
argDef.defaultValue !== undefined ||
nodeArgs.has(argDef.name),
);
const [hasArgs, argsRecord] = encodeArguments(directive.args, argsFilter);
const [hasArgs, argsRecord] = encodeArguments(directive.args);
if (hasArgs) {
setDirectiveDefinitionArgs(tuple, argsRecord);
}
Expand Down