Skip to content

Commit

Permalink
Merge pull request #71 from brianjquinn/merge-errors-fix
Browse files Browse the repository at this point in the history
merge errors fix (v2)
  • Loading branch information
brianjquinn authored Sep 30, 2020
2 parents d4e8e43 + 28c7524 commit c5a7c35
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
72 changes: 71 additions & 1 deletion lib/delegate/__tests__.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,7 @@ Test('delegateToComponent - target field non-nullable arg is not passed', async
t.end();
});

Test('delegateToComponent - with errors', async (t) => {
Test('delegateToComponent - with errors (selection set order doesnt matter)', async (t) => {
const primitive = new GraphQLComponent({
types: `
type Query {
Expand Down Expand Up @@ -1509,6 +1509,76 @@ Test('delegateToComponent - with errors - delegate graphql data result is comple
t.equal(result.data.bar, null, 'query resolves as expected');
t.equal(result.errors.length, 1, '1 error returned');
t.equal(result.errors[0].message, 'Cannot return null for non-nullable field Foo.b.', 'expected error is propagated regardless of completely null delegate result');
t.deepEqual(result.errors[0].path, ['foo', 'b']);
t.end();
});

Test('delegateToComponent - errors merged as expected for non-nullable list that allows nullable items', async (t) => {
const primitive = new GraphQLComponent({
types: `
type Query {
foos: [Foo]!
}
type Foo {
a: String!
}
`,
resolvers: {
Query: {
foos() {
return [ { a: 'bar'} , {}, { a: 'baz'} ];
}
}
}
});

const composite = new GraphQLComponent({
types: `
type Query {
bar: Bar
}
type Bar {
foos: [Foo]!
}
`,
resolvers: {
Query: {
async bar(_root, _args, context, info) {
const foos = await GraphQLComponent.delegateToComponent(primitive, {
info,
contextValue: context,
targetRootField: 'foos',
subPath: 'foos'
});
return { foos };
}
}
},
imports: [primitive]
});

const document = gql`
query {
bar {
foos {
a
}
}
}
`;

const result = await graphql.execute({
document,
schema: composite.schema,
contextValue: {}
});

t.deepEqual(result.data.bar.foos[0], { a: 'bar' }, 'first item of list resolved as expected');
t.deepEqual(result.data.bar.foos[2], { a: 'baz' }, 'third item of list resolved as expected');
t.equal(result.errors.length, 1, 'one error returned');
t.equal(result.errors[0].message, 'Cannot return null for non-nullable field Foo.a.');
t.deepEqual(result.errors[0].path, ['foos', 1, 'a']);
t.end();
});
13 changes: 7 additions & 6 deletions lib/delegate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const {
visit,
visitWithTypeInfo
} = require('graphql');
const deepSet = require('lodash.set');
const set = require('lodash.set');
const get = require('lodash.get');
const debug = require('debug')('graphql-component:delegate');

/**
Expand Down Expand Up @@ -195,14 +196,14 @@ const createSubOperationDocument = function (component, targetRootField, args, s
const mergeErrors = function (data, errors) {
for (const error of errors) {
const { path } = error;
const mergePath = [];
for (const segment of path) {
mergePath.push(segment);
if (!data[segment]) {
let depth = 1;
while (depth <= path.length) {
if (!get(data, path.slice(0, depth))) {
break;
}
depth++;
}
deepSet(data, mergePath, error);
set(data, path.slice(0, depth), error);
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"cuid": "^2.1.8",
"debug": "^4.1.1",
"graphql-tools": "^6.0.10",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2"
},
"peerDependencies": {
Expand Down

0 comments on commit c5a7c35

Please sign in to comment.