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

Prevent circular references #23

Merged
merged 1 commit into from
Feb 18, 2019
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
90 changes: 56 additions & 34 deletions src/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function buildDocumentNode({
models,
firstCall: true,
path: [],
ancestors: [],
ignore,
}),
],
Expand All @@ -146,12 +147,14 @@ function resolveSelectionSet({
models,
firstCall,
path,
ancestors,
ignore,
}: {
parent: GraphQLNamedType;
type: GraphQLNamedType;
models: string[];
path: string[];
ancestors: GraphQLNamedType[];
firstCall?: boolean;
ignore: Ignore;
}): SelectionSetNode | undefined {
Expand All @@ -160,32 +163,35 @@ function resolveSelectionSet({

return {
kind: 'SelectionSet',
selections: types.map<InlineFragmentNode>(t => {
const fields = t.getFields();

return {
kind: 'InlineFragment',
typeCondition: {
kind: 'NamedType',
name: {
kind: 'Name',
value: t.name,
selections: types
.filter(t => !hasCircularRef([...ancestors, t]))
.map<InlineFragmentNode>(t => {
const fields = t.getFields();

return {
kind: 'InlineFragment',
typeCondition: {
kind: 'NamedType',
name: {
kind: 'Name',
value: t.name,
},
},
},
selectionSet: {
kind: 'SelectionSet',
selections: Object.keys(fields).map(fieldName => {
return resolveField({
type: t,
field: fields[fieldName],
models,
path: [...path, fieldName],
ignore,
});
}),
},
};
}),
selectionSet: {
kind: 'SelectionSet',
selections: Object.keys(fields).map(fieldName => {
return resolveField({
type: t,
field: fields[fieldName],
models,
path: [...path, fieldName],
ancestors,
ignore,
});
}),
},
};
}),
};
}

Expand Down Expand Up @@ -214,15 +220,24 @@ function resolveSelectionSet({

return {
kind: 'SelectionSet',
selections: Object.keys(fields).map(fieldName => {
return resolveField({
type: type,
field: fields[fieldName],
models,
path: [...path, fieldName],
ignore,
});
}),
selections: Object.keys(fields)
.filter(
fieldName =>
!hasCircularRef([
...ancestors,
getNamedType(fields[fieldName].type),
])
)
.map(fieldName => {
return resolveField({
type: type,
field: fields[fieldName],
models,
path: [...path, fieldName],
ancestors,
ignore,
});
}),
};
}
}
Expand Down Expand Up @@ -281,12 +296,14 @@ function resolveField({
models,
firstCall,
path,
ancestors,
ignore,
}: {
type: GraphQLObjectType;
field: GraphQLField<any, any>;
models: string[];
path: string[];
ancestors: GraphQLNamedType[];
firstCall?: boolean;
ignore: Ignore;
}): SelectionNode {
Expand Down Expand Up @@ -331,6 +348,7 @@ function resolveField({
models,
firstCall,
path: [...path, field.name],
ancestors: [...ancestors, type],
ignore,
}),
arguments: args,
Expand All @@ -346,3 +364,7 @@ function resolveField({
arguments: args,
};
}

function hasCircularRef(types: GraphQLNamedType[]): boolean {
return types.some((t, i) => types.indexOf(t) !== i);
}
43 changes: 42 additions & 1 deletion tests/operation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { print, parse, DocumentNode } from 'graphql';
import { print, parse, DocumentNode, buildSchema } from 'graphql';
import gql from 'graphql-tag';

import { schema, models } from './schema';
Expand Down Expand Up @@ -273,3 +273,44 @@ test('should work with Subscription', async () => {
`)
);
});

test('should work with circular ref', async () => {
const document = buildOperation({
schema: buildSchema(`
type A {
b: B
}

type B {
c: C
}

type C {
end: String
a: A
}

type Query {
a: A
}
`),
kind: 'query',
field: 'a',
models,
ignore: [],
})!;

expect(clean(document)).toEqual(
clean(`
query aQuery {
a {
b {
c {
end
}
}
}
}
`)
);
});