Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

further improve bare delegation #1482

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
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
19 changes: 12 additions & 7 deletions packages/delegate/src/createRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ export function createRequest({
}
: undefined;

argumentNodeMap = fieldNodes[0].arguments.reduce(
(prev, curr) => ({
...prev,
[curr.name.value]: curr,
}),
{}
);
argumentNodeMap = {};

const args = fieldNodes[0]?.arguments;
if (args) {
argumentNodeMap = args.reduce(
(prev, curr) => ({
...prev,
[curr.name.value]: curr,
}),
argumentNodeMap
);
}
}

const newVariables = Object.create(null);
Expand Down
2 changes: 1 addition & 1 deletion packages/delegate/src/results/handleObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function handleObject(

setObjectSubschema(object, subschema);

if (skipTypeMerging || !info.mergeInfo) {
if (skipTypeMerging || !info?.mergeInfo) {
return object;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/delegate/src/results/handleResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function handleResult(
const type = getNullableType(returnType);

if (result == null) {
return handleNull(info.fieldNodes, responsePathAsArray(info.path), errors);
return handleNull(info?.fieldNodes, responsePathAsArray(info?.path), errors);
}

if (isLeafType(type)) {
Expand Down
147 changes: 134 additions & 13 deletions packages/delegate/tests/createRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { graphql, Kind } from 'graphql';
import { graphql, Kind, GraphQLError } from 'graphql';

import { createRequest } from '../src/createRequest';
import { makeExecutableSchema } from '@graphql-tools/schema';
Expand All @@ -8,21 +8,30 @@ describe('bare requests', () => {
test('should work', async () => {
const innerSchema = makeExecutableSchema({
typeDefs: `
type Test {
field: String
}
type Query {
test(input: String): String
test(input: String): Test
}
`,
resolvers: {
Query: {
test: (_root, args) => args.input
Test: {
field: (parent) => parent.input,
},
Query: {
test: (_root, args) => ({ input: args.input }),
}
},
});

const outerSchema = makeExecutableSchema({
typeDefs: `
type Test {
field: String
}
type Query {
delegate(input: String): String
delegate(input: String): Test
}
`,
resolvers: {
Expand All @@ -35,6 +44,16 @@ describe('bare requests', () => {
kind: Kind.NAME,
value: 'delegate',
},
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [{
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: 'field'
},
}],
},
arguments: [{
kind: Kind.ARGUMENT,
name: {
Expand Down Expand Up @@ -63,38 +82,72 @@ describe('bare requests', () => {
outerSchema,
`
query {
delegate(input: "test")
delegate(input: "test") {
field
}
}
`,
);

expect(result.data.delegate).toEqual('test');
expect(result).toEqual({
data: {
delegate: {
field: 'test',
},
},
});
});

test('should work with adding args on delegation', async () => {
const innerSchema = makeExecutableSchema({
typeDefs: `
type Test {
field: String
}
type Query {
test(input: String): String
test(input: String): Test
}
`,
resolvers: {
Query: {
test: (_root, args) => args.input
Test: {
field: (parent) => parent.input,
},
Query: {
test: (_root, args) => ({ input: args.input }),
}
},
});

const outerSchema = makeExecutableSchema({
typeDefs: `
type Test {
field: String
}
type Query {
delegate(input: String): String
delegate(input: String): Test
}
`,
resolvers: {
Query: {
delegate: (_root, args) => {
const request = createRequest({
fieldNodes: [{
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: 'delegate',
},
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [{
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: 'field'
},
}],
},
}],
targetOperation: 'query',
targetFieldName: 'test',
});
Expand All @@ -112,11 +165,79 @@ describe('bare requests', () => {
outerSchema,
`
query {
delegate(input: "test")
delegate(input: "test") {
field
}
}
`,
);

expect(result.data.delegate).toEqual('test');
expect(result).toEqual({
data: {
delegate: {
field: 'test',
},
},
});
});

test('should work with errors', async () => {
const innerSchema = makeExecutableSchema({
typeDefs: `
type Query {
test: String
}
`,
resolvers: {
Query: {
test: () => { throw new Error('test') },
}
},
});

const outerSchema = makeExecutableSchema({
typeDefs: `
type Query {
delegate: String
}
`,
resolvers: {
Query: {
delegate: () => {
const request = createRequest({
fieldNodes: [{
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: 'delegate',
},
}],
targetOperation: 'query',
targetFieldName: 'test',
});
return delegateRequest({
request,
schema: innerSchema,
});
},
},
},
});

const result = await graphql(
outerSchema,
`
query {
delegate
}
`,
);

expect(result).toEqual({
data: {
delegate: null,
},
errors: [new GraphQLError('test')],
});
});
});