GithubHelp home page GithubHelp logo

villesau / nexus-to-pothos-codemod Goto Github PK

View Code? Open in Web Editor NEW
15.0 1.0 0.0 178 KB

Transforms Nexus code to Pothos

License: MIT License

TypeScript 96.27% JavaScript 3.73%
codemod graphql nexus pothos codemods transform

nexus-to-pothos-codemod's Introduction

Nexus to Pothos codemod

npm version PRs Welcome

This is a codemod to migrate from Nexus to Pothos

This aims to transform all the nexus types, queries and mutations to Pothos equivalents. Please note that the codemod is by no means complete. You still need some manual adjustments. Below is a list of known missing features.

You can check out the __textfixtures__ folder to see full list of supported transformations.

Why should I migrate to use Pothos?

  • Pothos has superior type safety
  • Pothos does not require you to generate types which makes type feedback much much faster
  • Pothos is actively maintained

Install

$ yarn global add nexus-to-pothos-codemod

or

$ npm install -g nexus-to-pothos-codemod

Usage

$ nexus-to-pothos-codemod ./**/*.ts --ignore-pattern="**/node_modules/**" --parser=ts

The CLI is the same as in jscodeshift except you can omit the transform file.

Alternatively, you can run the codemod using jscodeshift as follows:

$ yarn global add jscodeshift
$ yarn add nexus-to-pothos-codemod
$ jscodeshift -t node_modules/nexus-to-pothos-codemod/transform.ts --ignore-pattern="**/node_modules/**" ./**/*.js  --parser=ts

Migrating the codebase

The codemod might not be 100% accurate. You will need to do some manual adjustments. Might be good to compare the generated GraphQL schema with the old one to see if there are any differences.

What is missing?

  • automatic import updates

  • it assumes some conventions such as async nodes((_, args, ctx) {} instead of nodes: async ((_, args, ctx) => {} which causes exceptions

  • does not understand computed fields such as:

    export const Object = objectType({
    name: 'Object',
    definition(t) {
      objectFields.forEach(objectField => t.string(objectField));
    }
    });
    
  • might fail on complex types

  • unionType not transformed

  • scalarType not transformed

  • lists not handled properly always, [] needs to be mostly added manually

  • args of connnectionField not transformed preoperly

  • authScope in connectionField and some nested fields not transformed properly

  • all caveats might not be discovered since the codemod is developed against single codebase with it's own conventions

Parts that causes exceptions can be commented out and fixed manually. Rest are fairly easy to compare to the old schema. Be extra careful with the authScope.

An example builder config:

import SchemaBuilder from '@pothos/core';
import RelayPlugin from '@pothos/plugin-relay';
import ScopeAuthPlugin from '@pothos/plugin-scope-auth';
import { GraphQLJSONObject } from 'graphql-type-json';

export const builder = new SchemaBuilder<{
  Context: {};
  DefaultEdgesNullability: false;
  DefaultNodeNullability: false;
  Scalars: {
    ID: {
      Output: number | string;
      Input: string;
    };
    JSONObject: {
      Output: object;
      Input: object;
    };
  };
}>({
  plugins: [ScopeAuthPlugin, RelayPlugin],
  authScopes: context => ({}),
  scopeAuthOptions: {
    unauthorizedError: () => new Error(`Not authorized`)
  },
  relayOptions: {
    clientMutationId: 'omit',
    cursorType: 'ID',
    edgesFieldOptions: {
      nullable: false
    },
    nodeFieldOptions: {
      nullable: false
    }
  }
});

builder.queryType();
builder.mutationType();
builder.addScalarType('JSONObject', GraphQLJSONObject, {});

Contributing

Contributions are more than welcome! Some useful tools for developing this are https://astexplorer.net/ and your editors builtin debugger.

nexus-to-pothos-codemod's People

Contributors

villesau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nexus-to-pothos-codemod's Issues

Fails to parse string references to types

export const foo = queryField('foo', {
  type: 'Boolean',
  args: {
    appID: nonNull('UUID'),
  },
  resolve: async function (_parent, args, context) {
    return true;
  },
});

produces the following error

nexus-to-pothos-codemod src/schema/app.ts --parser=ts
Processing 1 files...
Spawning 1 workers...
Sending 1 files to free worker...
 ERR src/schema/app.ts Transformation error (Cannot read properties of undefined (reading 'name'))
TypeError: Cannot read properties of undefined (reading 'name')
    at map (/Users/user/.nodenv/versions/16.13.1/lib/node_modules/nexus-to-pothos-codemod/transform.ts:76:35)
    at Array.map (<anonymous>)
    at NodePath.<anonymous> (/Users/user/.nodenv/versions/16.13.1/lib/node_modules/nexus-to-pothos-codemod/transform.ts:54:59)
    at NodePath.<anonymous> (/Users/user/.nodenv/versions/16.13.1/lib/node_modules/nexus-to-pothos-codemod/node_modules/jscodeshift/src/collections/Node.js:142:47)
    at /Users/user/.nodenv/versions/16.13.1/lib/node_modules/nexus-to-pothos-codemod/node_modules/jscodeshift/src/Collection.js:75:36
    at Array.forEach (<anonymous>)
    at Collection.forEach (/Users/user/.nodenv/versions/16.13.1/lib/node_modules/nexus-to-pothos-codemod/node_modules/jscodeshift/src/Collection.js:74:18)
    at Collection.replaceWith (/Users/user/.nodenv/versions/16.13.1/lib/node_modules/nexus-to-pothos-codemod/node_modules/jscodeshift/src/collections/Node.js:140:17)
    at Collection.replaceWith (/Users/user/.nodenv/versions/16.13.1/lib/node_modules/nexus-to-pothos-codemod/node_modules/jscodeshift/src/Collection.js:413:43)
    at transform (/Users/user/.nodenv/versions/16.13.1/lib/node_modules/nexus-to-pothos-codemod/transform.ts:40:20)
All done.
Results:
1 errors
0 unmodified
0 skipped
0 ok

If I unquote UUID as follows, then the conversion is successful:

export const foo = queryField('foo', {
  type: 'Boolean',
  args: {
    appID: nonNull(UUID),
  },
  resolve: async function (_parent, args, context) {
    return true;
  },
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.