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

Rework for compatibility with TypeORM v0.3.x #538

Merged
merged 26 commits into from
Nov 4, 2022
Merged

Rework for compatibility with TypeORM v0.3.x #538

merged 26 commits into from
Nov 4, 2022

Conversation

sgarner
Copy link
Contributor

@sgarner sgarner commented Nov 3, 2022

⚠️ BREAKING CHANGES ahead!

TypeORM v0.3.0 changed the way relations and data sources work. The relations option to findX() methods now expects an object instead of an array of strings (the array format is deprecated and will be removed in v0.4.0).

This has necessitated significant changes to typeorm-graphql-joiner to use the new object format. A relations object now looks like:

{
    profile: true,
    photos: true,
    videos: {
        videoAttributes: true,
    }
}

The library has been reworked to produce that kind of object, and to work with a TypeORM DataSource rather than a legacy TypeORM Connection. 🔌

A number of other changes and improvements have also been made:

  • Moved functions for inspecting GraphQLResolveInfo into a separate package, 📦 graphql-info-inspector
  • Created a new package 📦 typeorm-relations with functions for manipulating a TypeORM relations object
  • Renamed this package from typeorm-graphql-joiner to 📦 typeorm-relations-graphql
  • Renamed RelationMapper class to GraphRelationBuilder
  • Renamed method buildRelationListForQuery to buildForQuery
  • Renamed method buildRelationList to build

Migration Guide

If you were using typeorm-graphql-joiner with TypeORM v0.2.x before, and you're upgrading to TypeORM v0.3.x, you'll need to do the following:

  1. Remove dependency typeorm-graphql-joiner in your package.json

  2. Add dependency typeorm-relations-graphql in your package.json

  3. Replace code like:

    // OLD WAY
    import { RelationMapper } from 'typeorm-graphql-joiner';
    
    const relationMapper = new RelationMapper(connection);

    with:

    // NEW WAY
    import { GraphRelationBuilder } from 'typeorm-relations-graphql';
    
    const graphRelationBuilder = new GraphRelationBuilder(dataSource);
  4. Replace code like:

    // OLD WAY
    const relations = relationMapper.buildRelationListForQuery(Product, info);
    await productRepository.find({ relations: [...relations] });

    with:

    // NEW WAY
    const relationMap = graphRelationBuilder.buildForQuery(Product, info);
    await productRepository.find({ relations: relationMap.toFindOptionsRelations() });
  5. If you were using relationMapper.isFieldSelected() before, add graphql-info-inspector to your dependencies and use it like this:

    // NEW WAY
    import { isFieldSelected } from 'graphql-info-inspector';
    
    // In a Product resolver, for example:
    if (isFieldSelected('image.tags', info)) {
        // Client wants `product { image { tags { ... } } }`
    }
  6. If you need to manipulate the relations after mapping what's selected by the GraphQL query (for example, when there are relations that you always want to fetch for other reasons), previously you could just modify the resulting Set or Array of strings but now things are a little more complicated. However GraphRelationBuilder returns a RelationMap instance which makes this easy: 🔥

    const relationMap = graphRelationBuilder.buildForQuery(Product, info);
    
    // Always fetch product image relation
    relationMap.add('image');
    
    // Or, always fetch product image and tags
    relationMap.add({ image: { tags: true } });

    RelationMap is provided by 📦 typeorm-relations, read its docs to see what else you can do with it.

Additionally, make sure you're aware of TypeORM's own breaking changes when upgrading from v0.2.x to v0.3.0.

@sgarner sgarner merged commit 4872eb8 into master Nov 4, 2022
@sgarner sgarner deleted the typeorm-0.3 branch November 4, 2022 04:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants