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

tests: query for nested interface #56

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions __tests__/gql/queries/object-kitchen-sink.gql
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ query TestObject($id: ID!) {
label
}
}
interfaceNestedNonNullField {
... on TestInterface {
id
label
}
... on TestImplOne {
description
}
}
relayConnectionField(first: 1, after: "VGVzdFJlbGF5Tm9kZTox") {
...testRelayConnection
}
Expand Down
2 changes: 2 additions & 0 deletions __tests__/gql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Query {
testInterface(id: ID, label: String): TestInterface
testInterfaceNonNull(id: ID, label: String): TestInterface!
testInterfaceOptional(id: ID!): TestInterface
testInterfaceMultiple(id: ID, label: String): [TestInterface]
testObject(id: ID!): TestObject
testObjectNonNull(id: ID!): TestObject!
testObjects(size: String): [TestObject]
Expand Down Expand Up @@ -91,6 +92,7 @@ type TestObject {
hasManyNestedNonNullField: [TestOption!]!
interfaceField: TestInterface
interfaceNonNullField: TestInterface!
interfaceNestedNonNullField: [TestInterface!]!
relayConnectionField(first: Int, after: String): TestRelayConnection
relayConnectionFilteredField(
first: Int
Expand Down
12 changes: 12 additions & 0 deletions __tests__/integration/queries/object-kitchen-sink-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ describe("Integration | queries | object", function () {
server.create("test-union-one", { oneName: "foo" }),
server.create("test-union-two", { twoName: "bar" }),
];
const testInterfaces = [
server.create("test-impl-one", {
description: "description",
label: "impl",
}),
server.create("test-impl-two", { label: "impl" }),
];

seedUnassociatedRecords(server);

Expand All @@ -41,6 +48,7 @@ describe("Integration | queries | object", function () {
hasManyNonNullField: testOptions,
hasManyNestedNonNullField: testOptions,
interfaceField: testImpl,
interfaceNestedNonNullField: testInterfaces,
interfaceNonNullField: testImpl,
relayConnectionField: testRelayNodes,
relayConnectionFilteredField: testRelayNodes,
Expand All @@ -66,6 +74,10 @@ describe("Integration | queries | object", function () {
hasManyNonNullField: [{ id: "1", name: "opt" }],
hasManyNestedNonNullField: [{ id: "1", name: "opt" }],
interfaceField: { id: "1", label: "impl" },
interfaceNestedNonNullField: [
{ id: "2", label: "impl", description: "description" },
{ id: "1", label: "impl" },
],
interfaceNonNullField: { id: "1", label: "impl" },
relayConnectionField: {
edges: [{ cursor: "VGVzdFJlbGF5Tm9kZToy", node: { id: "2" } }],
Expand Down
17 changes: 17 additions & 0 deletions __tests__/unit/resolvers/mirage-field-resolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ describe("Unit | resolvers | mirage field resolver", function () {
args,
context,
info,
false,
type
);
});

it("can resolve interface list types", function () {
const type = typeMap.TestInterface;
const info = { returnType: queryFields.testInterfaceMultiple.type };

mirageGraphQLFieldResolver(obj, args, context, info);

expect(resolveInterface).toHaveBeenCalledWith(
obj,
args,
context,
info,
true,
type
);
});
Expand Down
15 changes: 13 additions & 2 deletions lib/resolvers/interface.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import resolveList from "./list";
import resolveObject from "./object";

function getTypeFromInlineFragment(info) {
Expand Down Expand Up @@ -39,15 +40,25 @@ function resolveFromImplementations(obj, args, context, info, type) {
* @param {Object} args
* @param {Object} context
* @param {Object} info
* @param {Boolean} isList
* @param {Object} type An unwrapped type.
* @see {@link https://graphql.org/learn/execution/#root-fields-resolvers}
* @see resolveObject
* @returns {Object} A record from Mirage's database.
*/
export default function resolveInterface(obj, args, context, info, type) {
export default function resolveInterface(
obj,
args,
context,
info,
isList,
type
) {
const implType = getTypeFromInlineFragment(info);

return implType
return isList
? resolveList(obj, args, context, info, type)
: implType
? resolveObject(obj, args, context, info, implType)
: resolveFromImplementations(obj, args, context, info, type);
}
2 changes: 1 addition & 1 deletion lib/resolvers/mirage.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function mirageGraphQLFieldResolver(obj, args, context, info) {
let { isList, type } = unwrapType(info.returnType);

return isInterfaceType(type)
? resolveInterface(obj, args, context, info, type)
? resolveInterface(obj, args, context, info, isList, type)
: isUnionType(type)
? resolveUnion(obj, args, context, info, isList, type)
: !isObjectType(type)
Expand Down