Skip to content

Commit

Permalink
test(circular): add self reference test for object type fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Aug 14, 2018
1 parent 1ffb79b commit 8720670
Showing 1 changed file with 73 additions and 4 deletions.
77 changes: 73 additions & 4 deletions tests/functional/circular-refs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "reflect-metadata";
import { IntrospectionObjectType, TypeKind } from "graphql";
import { IntrospectionObjectType, TypeKind, GraphQLObjectType, graphql } from "graphql";

import { Query, ObjectType, Field } from "../../src";
import { Query, ObjectType, Field, Resolver, buildSchema } from "../../src";
import { getMetadataStorage } from "../../src/metadata/getMetadataStorage";
import { getSchemaInfo } from "../helpers/getSchemaInfo";

Expand All @@ -19,14 +19,17 @@ describe("Circular references", () => {
@Field(type => CircularRef2)
ref2: any;
}
class Resolver {
@Resolver()
class SampleResolver {
@Query()
objectQuery(): SampleObject {
return {} as any;
}
}

const { schemaIntrospection: { types } } = await getSchemaInfo({ resolvers: [Resolver] });
const {
schemaIntrospection: { types },
} = await getSchemaInfo({ resolvers: [SampleResolver] });
const circularRef1 = types.find(
type => type.name === "CircularRef1",
) as IntrospectionObjectType;
Expand Down Expand Up @@ -65,4 +68,70 @@ describe("Circular references", () => {
jest.resetModules();
}
});

it("should allow to have self-reference fields in object type", async () => {
@ObjectType()
class SampleObject {
@Field()
stringField: string;

@Field(type => SampleObject, { nullable: true })
selfReferenceField?: SampleObject;

@Field(type => [SampleObject])
selfReferenceArrayField: SampleObject[];
}
@Resolver()
class SampleResolver {
@Query()
objectQuery(): SampleObject {
const obj: SampleObject = {
stringField: "nestedStringField",
selfReferenceArrayField: [],
};
obj.selfReferenceField = obj;
return {
stringField: "stringField",
selfReferenceArrayField: [obj],
selfReferenceField: obj,
};
}
}

const schema = await buildSchema({
resolvers: [SampleResolver],
});
expect(schema).toBeDefined();

const query = /* graphql */ `
query {
objectQuery {
stringField
selfReferenceField {
stringField
}
selfReferenceArrayField {
selfReferenceField {
stringField
}
}
}
}
`;
const { data } = await graphql(schema, query);

expect(data!.objectQuery).toEqual({
stringField: "stringField",
selfReferenceField: {
stringField: "nestedStringField",
},
selfReferenceArrayField: [
{
selfReferenceField: {
stringField: "nestedStringField",
},
},
],
});
});
});

0 comments on commit 8720670

Please sign in to comment.