Skip to content

Commit

Permalink
Types cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Jul 23, 2019
1 parent 1f00664 commit 8c1d07b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/apollo-federation/src/composition/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
DocumentNode,
GraphQLObjectType,
specifiedDirectives,
TypeDefinitionNode,
TypeExtensionNode,
} from 'graphql';
import { mapValues } from 'apollo-env';
import { transformSchema } from 'apollo-graphql';
Expand All @@ -31,8 +33,6 @@ import {
ServiceName,
ExternalFieldDefinition,
ServiceNameToKeyDirectivesMap,
FederatedTypeDefinitionNode,
FederatedTypeExtensionNode,
} from './types';
import { validateSDL } from 'graphql/validation/validate';
import { compositionRules } from './rules';
Expand All @@ -52,11 +52,11 @@ const EmptyMutationDefinition = {

// Map of all definitions to eventually be passed to extendSchema
interface DefinitionsMap {
[name: string]: FederatedTypeDefinitionNode[];
[name: string]: TypeDefinitionNode[];
}
// Map of all extensions to eventually be passed to extendSchema
interface ExtensionsMap {
[name: string]: FederatedTypeExtensionNode[];
[name: string]: TypeExtensionNode[];
}

/**
Expand Down
61 changes: 48 additions & 13 deletions packages/apollo-federation/src/composition/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
SelectionNode,
DocumentNode,
FieldDefinitionNode,
TypeDefinitionNode,
TypeExtensionNode,
} from 'graphql';
import { SelectionNode, DocumentNode, FieldDefinitionNode } from 'graphql';

export type ServiceName = string | null;

Expand Down Expand Up @@ -81,10 +75,51 @@ declare module 'graphql/type/definition' {
}
}

export type FederatedTypeDefinitionNode = TypeDefinitionNode & {
serviceName: string | null;
};
declare module 'graphql/language/ast' {
interface UnionTypeDefinitionNode {
serviceName?: string | null;
}
interface UnionTypeExtensionNode {
serviceName?: string | null;
}

interface EnumTypeDefinitionNode {
serviceName?: string | null;
}

interface EnumTypeExtensionNode {
serviceName?: string | null;
}

interface ScalarTypeDefinitionNode {
serviceName?: string | null;
}

interface ScalarTypeExtensionNode {
serviceName?: string | null;
}

interface ObjectTypeDefinitionNode {
serviceName?: string | null;
}

interface ObjectTypeExtensionNode {
serviceName?: string | null;
}

export type FederatedTypeExtensionNode = TypeExtensionNode & {
serviceName: string | null;
};
interface InterfaceTypeDefinitionNode {
serviceName?: string | null;
}

interface InterfaceTypeExtensionNode {
serviceName?: string | null;
}

interface InputObjectTypeDefinitionNode {
serviceName?: string | null;
}

interface InputObjectTypeExtensionNode {
serviceName?: string | null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ import {
Kind,
EnumTypeDefinitionNode,
EnumValueDefinitionNode,
TypeDefinitionNode,
} from 'graphql';
import { FederatedTypeDefinitionNode } from '../../types';
import { errorWithCode, logServiceAndType } from '../../utils';
import { isString } from 'util';

function isEnumDefinition(node: FederatedTypeDefinitionNode) {
function isEnumDefinition(node: TypeDefinitionNode) {
return node.kind === Kind.ENUM_TYPE_DEFINITION;
}

type TypeToDefinitionsMap = {
[typeNems: string]: FederatedTypeDefinitionNode[];
};

type FederatedEnumDefinition = EnumTypeDefinitionNode & {
serviceName: string;
[typeNems: string]: TypeDefinitionNode[];
};

export function MatchingEnums(context: SDLValidationContext): ASTVisitor {
Expand All @@ -27,8 +23,8 @@ export function MatchingEnums(context: SDLValidationContext): ASTVisitor {
// group all definitions by name
// { MyTypeName: [{ serviceName: "A", name: {...}}]}
let definitionsByName: {
[typeName: string]: FederatedTypeDefinitionNode[];
} = (definitions as FederatedTypeDefinitionNode[]).reduce(
[typeName: string]: TypeDefinitionNode[];
} = (definitions as TypeDefinitionNode[]).reduce(
(typeToDefinitionsMap: TypeToDefinitionsMap, node) => {
const name = node.name.value;
if (typeToDefinitionsMap[name]) {
Expand All @@ -54,7 +50,7 @@ export function MatchingEnums(context: SDLValidationContext): ASTVisitor {
for (const {
values,
serviceName,
} of definitions as FederatedEnumDefinition[]) {
} of definitions as EnumTypeDefinitionNode[]) {
if (serviceName && values)
simpleEnumDefs.push({
serviceName,
Expand Down

0 comments on commit 8c1d07b

Please sign in to comment.