npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

relay-modern-typescript-transformer

v0.0.3

Published

A transformer for Typescript that transforms GraphQL queries to require functions.

Downloads

17

Readme

relay-modern-typescript-transformer

A Typescript transformer that transforms GraphQL queries to require functions.

Tutorial

Install with Yarn (or NPM)

$ yarn add relay-modern-typescript-transformer
$ yarn add relay-compiler

relay-compiler is used to compile the queries into optimized .graphql files.

Read the Relay Modern docs for more info.

Add transform to Typescript config

For fuse-box, just add:

const { transformer } = require('relay-modern-typescript-transformer');

And then:

fuse = FuseBox.init(
    {
      ...
      transformers: {
        before: [
          transformer,
        ],
      },
    });

Run relay-compiler on your sources

./node_modules/.bin/relay-compiler --src ./src --schema ./src/data/schema.json --extensions tsx 

relay-compiler is designed for Javascript sources, but work somewhat well on TS sources as well. If it doesn't work for some reason, extract your query into a separate, smaller source file.

Start fusebox

node fuse.js

Or whatever way you boot your project.

Done!

You can now use Relay Modern with Typescript!

Example

const query = graphql`
query WelcomeScreenQuery {
  pokemon(name: "Pikachu") {
    name
    weight {
      minimum
      maximum
    }
  }
}  
`;

export class WelcomeScreen extends React.Component {
  render() {
    return (
      <QueryRenderer
        environment={environment}
        query={query}
        variables={{}}
        render={({ error, props }) => {
          if (error) {
            return <div>{error.message}</div>;
          } else if (props) {
            return <WelcomePane />;
          }
          return <div>Loading</div>;
        }}
      />
    );
  }
}

This code is placed in:

src/WelcomeScreen.tsx

When running the compiler, you will get this:

src/__generated__/WelcomeScreenQuery.graphql.js

The transformer will compile

const query = graphql...

into

const query = function () { return require('./__generated__/WelcomeScreenQuery.graphql.js')`.

Remember to name your query according to constraints.

Troubleshooting

It's not working? First, make sure that relay-compiler has created the graphql-files.

They should be placed along side your source files in a folder name __generated__/.

graphql: Unexpected invocation at runtime. Either the Babel transform was not set up, or it failed to identify this call site.

If you get this error, then the transformer is missing. Make sure you have added it correctly to your config used by the Typescript compiler.