Giter Site home page Giter Site logo

chollier / babel-plugin-flow-relay-query Goto Github PK

View Code? Open in Web Editor NEW

This project forked from guymers/babel-plugin-flow-relay-query

0.0 2.0 0.0 74 KB

Babel plugin which converts Flow types into Relay fragments

License: MIT License

JavaScript 100.00%

babel-plugin-flow-relay-query's Introduction

babel-plugin-flow-relay-query

Babel 6 plugin which converts Flow types into Relay fragments.

Installation

npm install --save-dev babel-plugin-flow-relay-query

Usage

Instead of importing babel-relay-plugin, import babel-plugin-flow-relay-query

Replace

var getBabelRelayPlugin = require("babel-relay-plugin");

with

var getBabelRelayPlugin = require("babel-plugin-flow-relay-query");

Example

Create a marker function called generateFragmentFromProps

function generateFragmentFromProps(fragmentName?: string): Function {}

If fragmentName is not provided then it will default to the key in the fragments object, capitalized.

import generateFragmentFromProps from "./generateFragmentFromProps";

type ArticleProps = {
  article: {
    title: string,
    posted: string,
    content: string,

    author: {
      name: string,
      email: string
    }
  }
};

class Article extends React.Component<void, ArticleProps, void> {
  render() {
    const { article } = this.props;
    return (
      <div>
        <ArticleTitle article={article} />
        <div>{article.author.name} [{article.author.email}]</div>
        <div>{article.content})</div>
      </div>
    );
  }
}

export default Relay.createContainer(Article, {
  fragments: {
    article: generateFragmentFromProps(),
  },
});

The article fragment will be transformed into:

export default Relay.createContainer(Article, {
  fragments: {
    article: () => Relay.QL`
      fragment on Article {
        title,
        posted,
        content,
        author {
          name,
          email
        },
        ${ArticleTitle.getFragment("article")}
      }
    `
  }
});

Also supports class properties and functional components:

class Article extends React.Component {
  props: ArticleProps;
  ...
}
const Article = ({ article }: ArticleProps) => (
  <div>
    <div>{article.title} ({article.posted})</div>
    <div>{article.author.name} [{article.author.email}]</div>
    <div>{article.content})</div>
  </div>
);

babel-plugin-flow-relay-query's People

Contributors

guymers avatar

Watchers

 avatar  avatar

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.