Skip to content

Commit

Permalink
Support directives on Input Object types (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Mar 28, 2024
1 parent ea844d4 commit ab707b9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-goats-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theguild/federation-composition": patch
---

Support directives on Input Object types
26 changes: 25 additions & 1 deletion __tests__/ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ describe('union type', () => {
@join__unionMember(graph: A, member: "Book")
@join__unionMember(graph: B, member: "Movie")
@join__unionMember(graph: B, member: "Book") =
Movie
| Movie
| Book
`);
});
Expand Down Expand Up @@ -841,6 +841,30 @@ describe('input object type', () => {
`);
});

test('directive', () => {
expect(
createInputObjectTypeNode({
name: 'User',
fields: [
{
name: 'name',
type: 'String',
ast: {
directives: [createDirective('custom')],
},
},
],
ast: {
directives: [createDirective('custom')],
},
}),
).toEqualGraphQL(/* GraphQL */ `
input User @custom {
name: String @custom
}
`);
});

test('default value', () => {
expect(
createInputObjectTypeNode({
Expand Down
15 changes: 15 additions & 0 deletions src/graphql/sort-sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DocumentNode,
EnumValueDefinitionNode,
FieldDefinitionNode,
InputValueDefinitionNode,
Kind,
NamedTypeNode,
NameNode,
Expand Down Expand Up @@ -56,6 +57,13 @@ export function sortSDL(doc: DocumentNode) {
fields: sortNodes(node.fields),
};
},
InputObjectTypeDefinition(node) {
return {
...node,
directives: sortNodes(node.directives),
fields: sortNodes(node.fields),
};
},
EnumTypeDefinition(node) {
return {
...node,
Expand Down Expand Up @@ -165,6 +173,9 @@ function sortNodes(nodes: readonly NameNode[] | undefined): readonly NameNode[]
function sortNodes(
nodes: readonly FieldDefinitionNode[] | undefined,
): readonly FieldDefinitionNode[] | undefined;
function sortNodes(
nodes: readonly InputValueDefinitionNode[] | undefined,
): readonly InputValueDefinitionNode[] | undefined;
function sortNodes(nodes: readonly any[] | undefined): readonly any[] | undefined {
if (nodes) {
if (nodes.length === 0) {
Expand Down Expand Up @@ -198,6 +209,10 @@ function sortNodes(nodes: readonly any[] | undefined): readonly any[] | undefine
return sortBy(nodes, 'name.value');
}

if (isOfKindList<InputValueDefinitionNode>(nodes, Kind.INPUT_VALUE_DEFINITION)) {
return sortBy(nodes, 'name.value');
}

if (
isOfKindList<SelectionNode>(nodes, [Kind.FIELD, Kind.FRAGMENT_SPREAD, Kind.INLINE_FRAGMENT])
) {
Expand Down
37 changes: 34 additions & 3 deletions src/subgraph/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export interface InputObjectType {
tags: Set<string>;
isDefinition: boolean;
description?: Description;
ast: {
directives: DirectiveNode[];
};
}

export interface UnionType {
Expand Down Expand Up @@ -187,6 +190,9 @@ export interface InputField {
defaultValue?: string;
description?: Description;
deprecated?: Deprecated;
ast: {
directives: DirectiveNode[];
};
}

export interface EnumValue {
Expand Down Expand Up @@ -723,6 +729,19 @@ export function createSubgraphStateBuilder(
}
break;
}
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
case Kind.INPUT_OBJECT_TYPE_EXTENSION: {
if (fieldDef) {
inputObjectTypeBuilder.field.setDirective(
typeDef.name.value,
fieldDef.name.value,
node,
);
} else {
inputObjectTypeBuilder.setDirective(typeDef.name.value, node);
}
break;
}
default:
// TODO: T07 support directives on other locations than OBJECT, FIELD_DEFINITION, ARGUMENT_DEFINITION
throw new Error(`Directives on "${typeDef.kind}" types are not supported yet`);
Expand Down Expand Up @@ -1625,13 +1644,16 @@ function inputObjectTypeFactory(state: SubgraphState) {
getOrCreateInputObjectType(state, typeName).description = description;
},
setInaccessible(typeName: string) {
const objectType = getOrCreateInputObjectType(state, typeName);
objectType.inaccessible = true;
const inputObjectType = getOrCreateInputObjectType(state, typeName);
inputObjectType.inaccessible = true;

for (const field of objectType.fields.values()) {
for (const field of inputObjectType.fields.values()) {
field.inaccessible = true;
}
},
setDirective(typeName: string, directive: DirectiveNode) {
getOrCreateInputObjectType(state, typeName).ast.directives.push(directive);
},
setTag(typeName: string, tag: string) {
getOrCreateInputObjectType(state, typeName).tags.add(tag);
},
Expand All @@ -1657,6 +1679,9 @@ function inputObjectTypeFactory(state: SubgraphState) {
setTag(typeName: string, fieldName: string, tag: string) {
getOrCreateInputObjectField(state, typeName, fieldName).tags.add(tag);
},
setDirective(typeName: string, fieldName: string, directive: DirectiveNode) {
getOrCreateInputObjectField(state, typeName, fieldName).ast.directives.push(directive);
},
},
};
}
Expand Down Expand Up @@ -1914,6 +1939,9 @@ function getOrCreateInputObjectType(state: SubgraphState, typeName: string): Inp
inaccessible: false,
tags: new Set(),
isDefinition: false,
ast: {
directives: [],
},
};

state.types.set(typeName, inputObjectType);
Expand Down Expand Up @@ -2084,6 +2112,9 @@ function getOrCreateInputObjectField(
type: MISSING,
inaccessible: false,
tags: new Set(),
ast: {
directives: [],
},
};

inputObjectType.fields.set(fieldName, field);
Expand Down
33 changes: 30 additions & 3 deletions src/supergraph/composition/input-object-type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DirectiveNode } from 'graphql';
import { FederationVersion } from '../../specifications/federation.js';
import { Deprecated, Description, InputObjectType } from '../../subgraph/state.js';
import { createInputObjectTypeNode } from './ast.js';
import type { MapByGraph, TypeBuilder } from './common.js';
import { convertToConst, type MapByGraph, type TypeBuilder } from './common.js';

export function inputObjectTypeBuilder(): TypeBuilder<InputObjectType, InputObjectTypeState> {
return {
Expand All @@ -14,8 +15,6 @@ export function inputObjectTypeBuilder(): TypeBuilder<InputObjectType, InputObje
inputObjectTypeState.inaccessible = true;
}

const isDefinition = type.isDefinition && !type.extension;

if (type.description && !inputObjectTypeState.description) {
inputObjectTypeState.description = type.description;
}
Expand All @@ -24,6 +23,12 @@ export function inputObjectTypeBuilder(): TypeBuilder<InputObjectType, InputObje
inputObjectTypeState.hasDefinition = true;
}

if (type.ast.directives) {
type.ast.directives.forEach(directive => {
inputObjectTypeState.ast.directives.push(directive);
});
}

inputObjectTypeState.byGraph.set(graph.id, {
inaccessible: type.inaccessible,
version: graph.version,
Expand Down Expand Up @@ -56,6 +61,10 @@ export function inputObjectTypeBuilder(): TypeBuilder<InputObjectType, InputObje
fieldState.defaultValue = field.defaultValue;
}

field.ast.directives.forEach(directive => {
fieldState.ast.directives.push(directive);
});

fieldState.byGraph.set(graph.id, {
type: field.type,
inaccessible: field.inaccessible,
Expand All @@ -70,6 +79,9 @@ export function inputObjectTypeBuilder(): TypeBuilder<InputObjectType, InputObje
tags: Array.from(inputObjectType.tags),
inaccessible: inputObjectType.inaccessible,
description: inputObjectType.description,
ast: {
directives: convertToConst(inputObjectType.ast.directives),
},
fields: Array.from(inputObjectType.fields.values())
.filter(field => {
if (field.byGraph.size !== inputObjectType.byGraph.size) {
Expand All @@ -92,6 +104,9 @@ export function inputObjectTypeBuilder(): TypeBuilder<InputObjectType, InputObje
: undefined,
description: field.description,
deprecated: field.deprecated,
ast: {
directives: convertToConst(field.ast.directives),
},
join: {
field: hasDifferentType
? Array.from(field.byGraph).map(([graph, fieldByGraph]) => ({
Expand Down Expand Up @@ -119,6 +134,9 @@ export interface InputObjectTypeState {
description?: Description;
byGraph: MapByGraph<InputObjectTypeStateInGraph>;
fields: Map<string, InputObjectTypeFieldState>;
ast: {
directives: DirectiveNode[];
};
}

export type InputObjectTypeFieldState = {
Expand All @@ -130,6 +148,9 @@ export type InputObjectTypeFieldState = {
description?: Description;
deprecated?: Deprecated;
byGraph: MapByGraph<InputObjectFieldStateInGraph>;
ast: {
directives: DirectiveNode[];
};
};

type InputObjectTypeStateInGraph = {
Expand Down Expand Up @@ -159,6 +180,9 @@ function getOrCreateInputObjectType(state: Map<string, InputObjectTypeState>, ty
inaccessible: false,
byGraph: new Map(),
fields: new Map(),
ast: {
directives: [],
},
};

state.set(typeName, def);
Expand All @@ -183,6 +207,9 @@ function getOrCreateField(
tags: new Set(),
inaccessible: false,
byGraph: new Map(),
ast: {
directives: [],
},
};

objectTypeState.fields.set(fieldName, def);
Expand Down

0 comments on commit ab707b9

Please sign in to comment.