graphql-modules icon indicating copy to clipboard operation
graphql-modules copied to clipboard

Make replaceExtensions work with inheritTypeDefs

Open kamilkisiela opened this issue 4 years ago • 3 comments
trafficstars

Related #1634

kamilkisiela avatar Apr 12 '21 15:04 kamilkisiela

The latest changes of this PR are available as alpha in npm (based on the declared changesets):

[email protected]

theguild-bot avatar Apr 12 '21 15:04 theguild-bot

i tweaked my test to be more real about list having multiple products below, else its a weird testcase :)

@kamilkisiela I validated now the extension on other modules types are working with 'replaceExtensions' to true, the option now only merges extension on Query/Mutation/Subscription.

Do you need to alter the docs at https://www.graphql-modules.com/docs/essentials/testing#turning-type-extensions-into-definitions ? I cant imagine you dont want to flatten eachother typings but since it was an option before this PR im not so sure :)

maapteh avatar Apr 13 '21 14:04 maapteh

my testcase:

import "reflect-metadata";
import { createModule, testkit, gql } from "graphql-modules";

describe("testModule", () => {
  const ListModule = createModule({
    id: "list",
    typeDefs: gql`
      extend type Query {
        list: List!
      }
      type List {
        id: ID!
        title: String!
      }
    `,
    resolvers: {
      Query: {
        list() {
          return {
            id: "123-456-789",
            title: "foo"
          };
        }
      }
    }
  });

  const ProductModule = createModule({
    id: "product",
    typeDefs: gql`
      extend type Query {
        product: Product!
      }
      type Product {
        id: ID!
      }
      # this module extends on list
      extend type List {
        products: [Product!]
      }
    `,
    resolvers: {
      Query: {
        product() {
          return {
            id: "42"
          };
        }
      },
      List: {
        products: () => {
          return [
            {
              id: "42"
            }
          ];
        }
      }
    }
  });

  test("should not throw error", () => {
    expect(() =>
      testkit.testModule(ProductModule, {
        replaceExtensions: true,
        inheritTypeDefs: [ListModule]
      })
    ).not.toThrow();
  });

  test("product extends list correctly while testing product module", async () => {
    const app = testkit.testModule(ProductModule, {
      inheritTypeDefs: [ListModule],
      replaceExtensions: true
    });

    const result = await testkit.execute(app, {
      document: gql`
        query product {
          product {
            id
          }
        }
      `
    });

    expect(result.data.product.id).toBe("42");
  });
});

maapteh avatar Apr 13 '21 14:04 maapteh