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

RelayMockPayloadGenerator supports nested lists #473

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
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
Loading