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

Commit

Permalink
fix(transforms): fix nested field name changes
Browse files Browse the repository at this point in the history
closes #43
  • Loading branch information
yaacovCR committed Mar 26, 2020
1 parent 2abda1b commit f53371f
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 122 deletions.
107 changes: 107 additions & 0 deletions src/test/testAlternateMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
printSchema,
GraphQLFieldConfig,
GraphQLObjectType,
graphqlSync,
} from 'graphql';
import { forAwaitEach } from 'iterall';
import { expect } from 'chai';
Expand Down Expand Up @@ -672,6 +673,112 @@ type Query {
});
});

describe('rename nested object fields with interfaces', () => {
it('should work', () => {
const originalNode = {
aList: [
{
anInnerObject: {
_linkType: 'linkedItem',
aString: 'Hello, world'
}
}
]
};

const transformedNode = {
ALIST: [
{
ANINNEROBJECT: {
_linkType: 'linkedItem',
ASTRING: 'Hello, world'
}
}
]
};

const originalSchema = makeExecutableSchema({
typeDefs: `
interface _Linkable {
_linkType: String!
}
type linkedItem implements _Linkable {
_linkType: String!
aString: String!
}
type aLink {
anInnerObject: _Linkable
}
type aObject {
aList: [aLink!]
}
type Query {
node: aObject
}
`,
resolvers: {
_Linkable: {
__resolveType: (linkable: { _linkType: string }) => linkable._linkType
},
Query: {
node: () => originalNode,
}
}
});

const transformedSchema = transformSchema(originalSchema, [
new RenameObjectFields((typeName, fieldName) => {
if (typeName === 'Query') {
return fieldName;
}

// Remote uses leading underscores for special fields. Leave them alone.
if (fieldName[0] === '_') {
return fieldName;
};

return fieldName.toUpperCase();
})
]);

const originalQuery = `
query {
node {
aList {
anInnerObject {
_linkType
... on linkedItem {
aString
}
}
}
}
}
`;

const transformedQuery = `
query {
node {
ALIST {
ANINNEROBJECT {
_linkType
... on linkedItem {
ASTRING
}
}
}
}
}
`;

const originalResult = graphqlSync(originalSchema, originalQuery);
const transformedResult = graphqlSync(transformedSchema, transformedQuery);

expect(originalResult).to.deep.equal({ data: { node: originalNode }});
expect(transformedResult).to.deep.equal({ data: { node: transformedNode }});
});
});

describe('WrapType transform', () => {
it('should work', async () => {
const transformedPropertySchema = transformSchema(propertySchema, [
Expand Down
44 changes: 21 additions & 23 deletions src/wrap/AddMergedTypeSelectionSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,31 @@ function addMergedTypeSelectionSets(
return visit(
document,
visitWithTypeInfo(typeInfo, {
leave: {
[Kind.SELECTION_SET](
node: SelectionSetNode,
): SelectionSetNode | null | undefined {
const parentType:
| GraphQLType
| null
| undefined = typeInfo.getParentType();
if (parentType != null) {
const parentTypeName = parentType.name;
let selections = node.selections;
[Kind.SELECTION_SET](
node: SelectionSetNode,
): SelectionSetNode | null | undefined {
const parentType:
| GraphQLType
| null
| undefined = typeInfo.getParentType();
if (parentType != null) {
const parentTypeName = parentType.name;
let selections = node.selections;

if (mapping[parentTypeName] != null) {
const selectionSet = mapping[parentTypeName].selectionSet;
if (selectionSet != null) {
selections = selections.concat(selectionSet.selections);
}
if (mapping[parentTypeName] != null) {
const selectionSet = mapping[parentTypeName].selectionSet;
if (selectionSet != null) {
selections = selections.concat(selectionSet.selections);
}
}

if (selections !== node.selections) {
return {
...node,
selections,
};
}
if (selections !== node.selections) {
return {
...node,
selections,
};
}
},
}
},
}),
);
Expand Down
76 changes: 39 additions & 37 deletions src/wrap/MapFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,54 @@ function transformDocument(
const newDocument: DocumentNode = visit(
document,
visitWithTypeInfo(typeInfo, {
[Kind.SELECTION_SET]: node => {
const parentType:
| GraphQLType
| null
| undefined = typeInfo.getParentType();
if (parentType != null) {
const parentTypeName = parentType.name;
const fieldNodeTransformers = fieldNodeTransformerMap[parentTypeName];
let newSelections: Array<SelectionNode> = [];
leave: {
[Kind.SELECTION_SET]: node => {
const parentType:
| GraphQLType
| null
| undefined = typeInfo.getParentType();
if (parentType != null) {
const parentTypeName = parentType.name;
const fieldNodeTransformers = fieldNodeTransformerMap[parentTypeName];
let newSelections: Array<SelectionNode> = [];

node.selections.forEach(selection => {
if (selection.kind === Kind.FIELD) {
const fieldName = selection.name.value;
node.selections.forEach(selection => {
if (selection.kind === Kind.FIELD) {
const fieldName = selection.name.value;

let transformedSelection;
if (fieldNodeTransformers != null) {
const fieldNodeTransformer = fieldNodeTransformers[fieldName];
if (fieldNodeTransformer != null) {
transformedSelection = fieldNodeTransformer(
selection,
fragments,
);
let transformedSelection;
if (fieldNodeTransformers != null) {
const fieldNodeTransformer = fieldNodeTransformers[fieldName];
if (fieldNodeTransformer != null) {
transformedSelection = fieldNodeTransformer(
selection,
fragments,
);
} else {
transformedSelection = selection;
}
} else {
transformedSelection = selection;
}
} else {
transformedSelection = selection;
}

if (Array.isArray(transformedSelection)) {
newSelections = newSelections.concat(transformedSelection);
} else if (transformedSelection.kind === Kind.FIELD) {
newSelections.push(transformedSelection);
if (Array.isArray(transformedSelection)) {
newSelections = newSelections.concat(transformedSelection);
} else if (transformedSelection.kind === Kind.FIELD) {
newSelections.push(transformedSelection);
} else {
newSelections.push(transformedSelection);
}
} else {
newSelections.push(transformedSelection);
newSelections.push(selection);
}
} else {
newSelections.push(selection);
}
});
});

return {
...node,
selections: newSelections,
};
}
return {
...node,
selections: newSelections,
};
}
},
},
}),
);
Expand Down

0 comments on commit f53371f

Please sign in to comment.