Skip to content

Commit

Permalink
Merge pull request #473 from fidelman/anfidelman/relay-mock-payload-g…
Browse files Browse the repository at this point in the history
…enerator-supports-nested-lists

RelayMockPayloadGenerator supports nested lists
  • Loading branch information
fidelman authored Nov 18, 2024
2 parents c4f4af3 + 84d016f commit 19d6454
Show file tree
Hide file tree
Showing 4 changed files with 633 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "RelayMockPayloadGenerator supports nested lists",
"packageName": "@graphitation/graphql-js-operation-payload-generator",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,46 @@ import {
import { readFileSync } from "fs";

import { graphql } from "@graphitation/graphql-js-tag";
import { mergeSchemas } from "@graphql-tools/schema";
import { generate, MockResolvers } from "..";
import { TypeMap } from "./__generated__/schema-types";

import { FIXTURE_TAG } from "relay-test-utils-internal/lib/generateTestsFromFixtures";

const schema = buildSchema(
readFileSync(
require.resolve("relay-test-utils-internal/lib/testschema.graphql"),
"utf8",
),
);
const schema = mergeSchemas({
schemas: [
buildSchema(
readFileSync(
require.resolve("relay-test-utils-internal/lib/testschema.graphql"),
"utf8",
),
),
// extension over "relay-test-utils-internal" schema
buildSchema(/* GraphQL */ `
type Query {
justList(id: ID!): [CustomNodeA!]!
nestedList(id: ID!): [[CustomNodeA!]!]!
deeplyNestedList(id: ID!): [[[CustomNodeA!]!]!]!
}
type CustomNodeA {
id: ID!
nodeB: CustomNodeB!
nodesB: [CustomNodeB!]!
}
type CustomNodeB {
id: ID!
nodeC: CustomNodeC!
nodesC: [CustomNodeC!]!
}
type CustomNodeC {
id: ID!
}
`),
],
});

function testGeneratedData(
documentNode: DocumentNode,
Expand Down Expand Up @@ -2067,3 +2096,191 @@ test("uses explicit mock data over type based mock data", () => {
},
);
});

test("mocks list with nested structure", () => {
testGeneratedData(
graphql`
query RelayMockPayloadGeneratorTestDeeplyNestedMockDataQuery {
justList(id: "my-just-list-id") {
__typename
id
nodeB {
id
__typename
nodeC {
id
__typename
}
nodesC {
id
__typename
}
}
nodesB {
id
__typename
nodeC {
id
__typename
}
nodesC {
id
__typename
}
}
}
}
`,
);
});

test("mocks list with nested structure with provided resolver", () => {
testGeneratedData(
graphql`
query RelayMockPayloadGeneratorTestDeeplyNestedMockDataQuery {
justList(id: "my-just-list-id") {
__typename
id
nodeB {
id
__typename
nodeC {
id
__typename
}
nodesC {
id
__typename
}
}
nodesB {
id
__typename
nodeC {
id
__typename
}
nodesC {
id
__typename
}
}
}
}
`,
{
Query: () =>
({
justList: [
{
__typename: "CustomNodeA",
id: "custom-nodeA-id",
nodeB: {
id: "nodeB-id",
__typename: "CustomNodeB",
nodeC: { id: "nodeC-id", __typename: "CustomNodeC" },
nodesC: [
{ id: "nodesC-id-1", __typename: "CustomNodeC" },
{ id: "nodesC-id-2", __typename: "CustomNodeC" },
],
},
nodesB: [
{
__typename: "CustomNodeB",
id: "nodesB-id-1",
nodeC: { id: "nodeC-id-1", __typename: "CustomNodeC" },
nodesC: [
{ id: "nodesC-id-1-1", __typename: "CustomNodeC" },
{ id: "nodesC-id-1-2", __typename: "CustomNodeC" },
],
},
{
__typename: "CustomNodeB",
id: "nodesB-id-2",
nodeC: { id: "nodeC-id-2", __typename: "CustomNodeC" },
nodesC: [
{ id: "nodesC-id-2-1", __typename: "CustomNodeC" },
{ id: "nodesC-id-2-2", __typename: "CustomNodeC" },
],
},
],
},
],
} as any),
},
);
});

test("mocks 2d list with nested structure", () => {
testGeneratedData(
graphql`
query RelayMockPayloadGeneratorTestDeeplyNestedMockDataQuery {
nestedList(id: "my-nested-list-id") {
__typename
id
nodeB {
id
__typename
nodeC {
id
__typename
}
nodesC {
id
__typename
}
}
nodesB {
id
__typename
nodeC {
id
__typename
}
nodesC {
id
__typename
}
}
}
}
`,
);
});

test("mocks 3d list with nested structure", () => {
testGeneratedData(
graphql`
query RelayMockPayloadGeneratorTestDeeplyNestedMockDataQuery {
deeplyNestedList(id: "my-deeply-nested-list-id") {
__typename
id
nodeB {
id
__typename
nodeC {
id
__typename
}
nodesC {
id
__typename
}
}
nodesB {
id
__typename
nodeC {
id
__typename
}
nodesC {
id
__typename
}
}
}
}
`,
);
});
Loading

0 comments on commit 19d6454

Please sign in to comment.