Skip to content
Merged
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
Expand Up @@ -37,9 +37,9 @@ import { ExclamationTriangleIcon } from '@radix-ui/react-icons';
import { compareLists, diffArrays, matchArrays } from './compare-lists';

type RootFieldsType = {
query: GraphQLField<any, any>;
mutation: GraphQLField<any, any>;
subscription: GraphQLField<any, any>;
query: GraphQLField<any, any> | null;
mutation: GraphQLField<any, any> | null;
subscription: GraphQLField<any, any> | null;
};

const TAB = <>&nbsp;&nbsp;</>;
Expand Down Expand Up @@ -745,123 +745,127 @@ export function SchemaDefinitionDiff({
newSchema: GraphQLSchema | undefined | null;
annotations: (coordinat: string) => ReactElement | null;
}) {
const defaultNames = {
query: 'Query',
mutation: 'Mutation',
subscription: 'Subscription',
};
const oldQuery = oldSchema?.getQueryType();
const oldMutation = oldSchema?.getMutationType();
const oldSubscription = oldSchema?.getSubscriptionType();
const oldRoot: RootFieldsType = {
query: {
args: [],
name: 'query',
type:
oldSchema?.getQueryType() ??
({ name: defaultNames.query, toString: () => defaultNames.query } as GraphQLOutputType),
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
},
mutation: {
args: [],
name: 'mutation',
type:
oldSchema?.getMutationType() ??
({
name: defaultNames.mutation,
toString: () => defaultNames.mutation,
} as GraphQLOutputType),
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
},
subscription: {
args: [],
name: 'subscription',
type:
oldSchema?.getSubscriptionType() ??
({
name: defaultNames.subscription,
toString: () => defaultNames.subscription,
} as GraphQLOutputType),
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
},
query: oldQuery
? {
args: [],
name: 'query',
type: oldQuery,
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
}
: null,
mutation: oldMutation
? {
args: [],
name: 'mutation',
type: oldMutation,
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
}
: null,
subscription: oldSubscription
? {
args: [],
name: 'subscription',
type: oldSubscription,
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
}
: null,
};

const newQuery = newSchema?.getQueryType();
const newMutation = newSchema?.getMutationType();
const newSubscription = newSchema?.getSubscriptionType();
const newRoot: RootFieldsType = {
query: {
args: [],
name: 'query',
type:
newSchema?.getQueryType() ??
({ name: defaultNames.query, toString: () => defaultNames.query } as GraphQLOutputType),
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
},
mutation: {
args: [],
name: 'mutation',
type:
newSchema?.getMutationType() ??
({
name: defaultNames.mutation,
toString: () => defaultNames.mutation,
} as GraphQLOutputType),
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
},
subscription: {
args: [],
name: 'subscription',
type:
newSchema?.getSubscriptionType() ??
({
name: defaultNames.subscription,
toString: () => defaultNames.subscription,
} as GraphQLOutputType),
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
},
query: newQuery
? {
args: [],
name: 'query',
type: newQuery,
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
}
: null,
mutation: newMutation
? {
args: [],
name: 'mutation',
type: newMutation,
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
}
: null,
subscription: newSubscription
? {
args: [],
name: 'subscription',
type: newSubscription,
astNode: null,
deprecationReason: null,
description: null,
extensions: {},
}
: null,
};
Comment on lines +748 to 824
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for creating oldRoot and newRoot is duplicated. This can be extracted into a helper function to improve readability and maintainability by reducing code duplication.

  const createRootFieldsForSchema = (schema: GraphQLSchema | undefined | null): RootFieldsType => {
    const createField = (
      type: import('graphql').GraphQLObjectType<any, any> | null | undefined,
      name: 'query' | 'mutation' | 'subscription',
    ): GraphQLField<any, any> | null => {
      if (!type) {
        return null;
      }
      return {
        args: [],
        name,
        type,
        astNode: null,
        deprecationReason: null,
        description: null,
        extensions: {},
      };
    };

    return {
      query: createField(schema?.getQueryType(), 'query'),
      mutation: createField(schema?.getMutationType(), 'mutation'),
      subscription: createField(schema?.getSubscriptionType(), 'subscription'),
    };
  };

  const oldRoot = createRootFieldsForSchema(oldSchema);
  const newRoot = createRootFieldsForSchema(newSchema);

// @todo verify using this as the path is correct.
const path = [''];
const changeType = determineChangeType(oldSchema, newSchema);

const newSchemaDef = [oldRoot.mutation, oldRoot.query, oldRoot.subscription].every(
field => field === null,
);
const removedSchemaDef = [newRoot.mutation, newRoot.query, newRoot.subscription].every(
field => field === null,
);
const schemaDefType = newSchemaDef ? 'addition' : removedSchemaDef ? 'removal' : 'mutual';

return (
<>
<ChangeSpacing type={changeType} />
<ChangeRow coordinate={path.join('.')} annotations={annotations}>
<ChangeRow coordinate={path.join('.')} annotations={annotations} type={schemaDefType}>
<Keyword term="schema" />
{' {'}
</ChangeRow>
<DiffField
oldField={oldRoot.query}
newField={newRoot.query}
parentPath={path}
annotations={annotations}
/>
<DiffField
oldField={oldRoot.mutation}
newField={newRoot.mutation}
parentPath={path}
annotations={annotations}
/>
<DiffField
oldField={oldRoot.subscription}
newField={newRoot.subscription}
parentPath={path}
annotations={annotations}
/>
<ChangeRow>{'}'}</ChangeRow>
{oldRoot.query || newRoot.query ? (
<DiffField
oldField={oldRoot.query}
newField={newRoot.query!}
parentPath={path}
annotations={annotations}
/>
) : null}
{oldRoot.mutation || newRoot.mutation ? (
<DiffField
oldField={oldRoot.mutation}
newField={newRoot.mutation!}
parentPath={path}
annotations={annotations}
/>
) : null}
{oldRoot.subscription || newRoot.subscription ? (
<DiffField
oldField={oldRoot.subscription}
newField={newRoot.subscription!}
parentPath={path}
annotations={annotations}
/>
) : null}
<ChangeRow type={schemaDefType}>{'}'}</ChangeRow>
</>
);
}
Expand Down
Loading