Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
refactor(transforms): streamline with upstream
Browse files Browse the repository at this point in the history
remove all uses of fieldToFieldConfig in favor of type.toConfig()
will support extensions for all graphql objects
allow renaming of root and object fields without requiring field argument if field unchanged.
  • Loading branch information
yaacovCR committed Dec 15, 2019
1 parent 9ebad82 commit a6d19bd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 110 deletions.
73 changes: 0 additions & 73 deletions src/stitching/schemaRecreation.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/test/testAlternateMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
ExecutionResult,
subscribe,
parse,
GraphQLField,
GraphQLScalarType,
FieldNode,
printSchema,
GraphQLObjectTypeConfig,
GraphQLFieldConfig,
} from 'graphql';
import {
transformSchema,
Expand All @@ -32,7 +33,6 @@ import {
subscriptionPubSubTrigger,
} from './testingSchemas';
import { forAwaitEach } from 'iterall';
import { fieldToFieldConfig } from '../stitching/schemaRecreation';
import { makeExecutableSchema } from '../makeExecutableSchema';
import {
delegateToSchema,
Expand Down Expand Up @@ -333,11 +333,12 @@ describe('transform object fields', () => {
before(async () => {
transformedPropertySchema = transformSchema(propertySchema, [
new TransformObjectFields(
(typeName: string, fieldName: string, field: GraphQLField<any, any>) => {
const fieldConfig = fieldToFieldConfig(field);
(typeName: string, fieldName: string) => {
if (typeName !== 'Property' || fieldName !== 'name') {
return fieldConfig;
return undefined;
}
const typeConfig = propertySchema.getType(typeName).toConfig() as GraphQLObjectTypeConfig<any, any>;
const fieldConfig = typeConfig.fields[fieldName] as GraphQLFieldConfig<any, any>;
fieldConfig.resolve = () => 'test';
return fieldConfig;
},
Expand Down
2 changes: 0 additions & 2 deletions src/transforms/RenameObjectFields.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { GraphQLField, GraphQLSchema } from 'graphql';
import { Transform } from './transforms';
import { fieldToFieldConfig } from '../stitching/schemaRecreation';
import { Request } from '../Interfaces';
import TransformObjectFields from './TransformObjectFields';

Expand All @@ -12,7 +11,6 @@ export default class RenameObjectFields implements Transform {
(typeName: string, fieldName: string, field: GraphQLField<any, any>) => {
return {
name: renamer(typeName, fieldName, field),
field: fieldToFieldConfig(field)
};
}
);
Expand Down
2 changes: 0 additions & 2 deletions src/transforms/RenameRootFields.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { GraphQLField, GraphQLSchema } from 'graphql';
import { Transform } from './transforms';
import { fieldToFieldConfig } from '../stitching/schemaRecreation';
import TransformRootFields from './TransformRootFields';

export default class RenameRootFields implements Transform {
Expand All @@ -21,7 +20,6 @@ export default class RenameRootFields implements Transform {
) => {
return {
name: renamer(operation, fieldName, field),
field: fieldToFieldConfig(field),
};
},
);
Expand Down
23 changes: 11 additions & 12 deletions src/transforms/TransformObjectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import isEmptyObject from '../utils/isEmptyObject';
import { Request, VisitSchemaKind } from '../Interfaces';
import { Transform } from './transforms';
import { visitSchema } from '../utils/visitSchema';
import { fieldToFieldConfig } from '../stitching/schemaRecreation';

export type ObjectFieldTransformer = (
typeName: string,
fieldName: string,
field: GraphQLField<any, any>,
) => GraphQLFieldConfig<any, any> | { name: string; field: GraphQLFieldConfig<any, any> } | null | undefined;
) => GraphQLFieldConfig<any, any> | RenamedField | null | undefined;

export type FieldNodeTransformer = (
typeName: string,
Expand All @@ -39,6 +38,8 @@ type FieldMapping = {
};
};

type RenamedField = { name: string; field?: GraphQLFieldConfig<any, any> };

export default class TransformObjectFields implements Transform {
private objectFieldTransformer: ObjectFieldTransformer;
private fieldNodeTransformer: FieldNodeTransformer;
Expand Down Expand Up @@ -90,6 +91,7 @@ export default class TransformObjectFields implements Transform {
type: GraphQLObjectType,
objectFieldTransformer: ObjectFieldTransformer
): GraphQLObjectType {
const typeConfig = type.toConfig();
const fields = type.getFields();
const newFields = {};

Expand All @@ -98,26 +100,23 @@ export default class TransformObjectFields implements Transform {
const transformedField = objectFieldTransformer(type.name, fieldName, field);

if (typeof transformedField === 'undefined') {
newFields[fieldName] = fieldToFieldConfig(field);
newFields[fieldName] = typeConfig.fields[fieldName];
} else if (transformedField !== null) {
const newName = (transformedField as { name: string; field: GraphQLFieldConfig<any, any> }).name;
const newName = (transformedField as RenamedField).name;

if (newName) {
newFields[newName] = (transformedField as {
name: string;
field: GraphQLFieldConfig<any, any>;
}).field;
newFields[newName] = (transformedField as RenamedField).field ?
(transformedField as RenamedField).field :
typeConfig.fields[fieldName];

if (newName !== fieldName) {
const typeName = type.name;
if (!this.mapping[typeName]) {
this.mapping[typeName] = {};
}
this.mapping[typeName][newName] = fieldName;

const originalResolver = (transformedField as {
name: string;
field: GraphQLFieldConfig<any, any>;
}).field.resolve;
const originalResolver = newFields[newName].resolve;
(newFields[newName] as GraphQLFieldConfig<any, any>).resolve = (parent, args, context, info) =>
originalResolver(parent, args, context, {
...info,
Expand Down
28 changes: 12 additions & 16 deletions src/transforms/TransformRootFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import isEmptyObject from '../utils/isEmptyObject';
import { Transform } from './transforms';
import { visitSchema } from '../utils/visitSchema';
import { VisitSchemaKind } from '../Interfaces';
import { fieldToFieldConfig } from '../stitching/schemaRecreation';

export type RootTransformer = (
operation: 'Query' | 'Mutation' | 'Subscription',
fieldName: string,
field: GraphQLField<any, any>,
) =>
| GraphQLFieldConfig<any, any>
| { name: string; field: GraphQLFieldConfig<any, any> }
| RenamedField
| null
| undefined;

type RenamedField = { name: string; field?: GraphQLFieldConfig<any, any> };

export default class TransformRootFields implements Transform {
private transform: RootTransformer;

Expand Down Expand Up @@ -61,27 +62,24 @@ function transformFields(
field: GraphQLField<any, any>,
) =>
| GraphQLFieldConfig<any, any>
| { name: string; field: GraphQLFieldConfig<any, any> }
| RenamedField
| null
| undefined,
): GraphQLObjectType {
const typeConfig = type.toConfig();
const fields = type.getFields();
const newFields = {};
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName];
const newField = transformer(fieldName, field);
if (typeof newField === 'undefined') {
newFields[fieldName] = fieldToFieldConfig(field);
newFields[fieldName] = typeConfig.fields[fieldName];
} else if (newField !== null) {
if (
(<{ name: string; field: GraphQLFieldConfig<any, any> }>newField).name
) {
newFields[
(<{ name: string; field: GraphQLFieldConfig<any, any> }>newField).name
] = (<{
name: string;
field: GraphQLFieldConfig<any, any>;
}>newField).field;
if ((newField as RenamedField).name) {
newFields[(newField as RenamedField).name] =
(newField as RenamedField).field ?
(newField as RenamedField).field :
typeConfig.fields[fieldName];
} else {
newFields[fieldName] = newField;
}
Expand All @@ -91,9 +89,7 @@ function transformFields(
return null;
} else {
return new GraphQLObjectType({
name: type.name,
description: type.description,
astNode: type.astNode,
...type,
fields: newFields,
});
}
Expand Down

0 comments on commit a6d19bd

Please sign in to comment.