Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -27,6 +27,25 @@ describe(addMinimalViableSchemaToRequestDocument, () => {
};
}

it("minimal viable schema should include optional param even when it's not used in directive", () => {
const { printedDoc } = testHelper(
schema,
`query @i18n {
film(id: 42) {
__typename
}
}`,
);
expect(printedDoc).toMatchInlineSnapshot(`
"query @i18n @schema(definitions: "{\\"types\\":{\\"Query\\":[2,{\\"film\\":[\\"Film\\",{\\"id\\":10}]}],\\"Film\\":[2,{},[\\"Node\\"]]},\\"directives\\":[[\\"i18n\\",[1],{\\"locale\\":1}]]}") {
film(id: 42) {
__typename
}
}
"
`);
});

it("adds minimal viable schema to operation definition", () => {
const { printedDoc } = testHelper(
schema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,16 @@ describe(extractMinimalViableSchemaForRequestDocument, () => {
);
expect(sdl).toMatchInlineSnapshot(`
"type Query {
countFilms: Int!
countFilms(filter: FilmFilterInput): Int!
}

input FilmFilterInput {
genre: FilmGenre
}

enum FilmGenre {
COMEDY
DRAMA
}
"
`);
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