ProductPromotion
Logo

React.JS

made by https://0x3d.site

GitHub - guymers/babel-plugin-flow-relay-query: Babel plugin which converts Flow types into Relay fragments
Babel plugin which converts Flow types into Relay fragments - guymers/babel-plugin-flow-relay-query
Visit Site

GitHub - guymers/babel-plugin-flow-relay-query: Babel plugin which converts Flow types into Relay fragments

GitHub - guymers/babel-plugin-flow-relay-query: Babel plugin which converts Flow types into Relay fragments

babel-plugin-flow-relay-query

Babel plugin which converts Flow types into GraphQL 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 or generateFragmentFromPropsFor:

type FragmentOptions = {
  name?: string;
  type?: string;
  templateTag?: string;
  directives?: {
    [name: string]: { [arg: string]: boolean|number|string };
  };
}

function generateFragmentFromProps(options?: FragmentOptions): Function {}
function generateFragmentFromPropsFor(component: ReactClass<*>, options?: FragmentOptions): Function {}

Or just import one of the ones that are shipped with the plugin:

import { generateFragmentFromProps } from "babel-plugin-flow-relay-query/lib/markers";
import { generateFragmentFromPropsFor } from "babel-plugin-flow-relay-query/lib/markers";

If a fragment type is not provided in the options then it will default to the key in the fragments object, capitalized.

import React from "react";
import Relay from "react-relay";
import { generateFragmentFromProps } from "babel-plugin-flow-relay-query/lib/markers";

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>
);

This plugin can also create GraphQL strings for Apollo:

First set apollo generation options globally:

var ChildFragmentTransformations = require("babel-plugin-flow-relay-query/lib/ChildFragmentTransformations");
var babelRelayPlugin = require("babel-plugin-flow-relay-query");
const schema = require("??/schema.json");
module.exports = babelRelayPlugin(schema.data, {}, {
  defaultTemplateTag: "gql",
  defaultFragmentName: ChildFragmentTransformations.apolloFragmentName,
  childFragmentTransformations: ChildFragmentTransformations.apollo
});
import React from "react";
import gql from "graphql-tag";
import { generateFragmentFromPropsFor } from "babel-plugin-flow-relay-query/lib/markers";

type ArticleProps = {
  article: {
    title: string;
    content: string;
  }
};

class Article extends React.Component {
  props: ArticleProps;

  render() {
    const { article } = this.props;
    return (
      <div>
        <ArticleTitle article={article} />
        <div>{article.author.name} [{article.author.email}]</div>
        <ArticleBody article={article} />
        <Footer />
      </div>
    );
  }
}

Article.fragments = {
  article: generateFragmentFromPropsFor(Article)
};

export default Article;

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory