Skip to content

Commit

Permalink
tests: interface list
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryThePenguin committed May 30, 2022
1 parent 89b24f7 commit 8cdd9fe
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 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
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

0 comments on commit 8cdd9fe

Please sign in to comment.