• Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
    JavaScript
  • Created over 6 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

backend agnostic, graphql style colocation of data requests in React

RouteQL

note - this project is in it's early stages and may change API.

RouteQL takes the idea of expressive colocated querying data from the frontend in tools like GraphQL (we even use the graphql query structure and parser) and the idea of a Query component or routeql higher order component from tools like Apollo and applies them to make these queries backend agnostic. RouteQL transforms queries into route requests, allowing you to use props to determine route and query parameters.

This allows for the expressiveness of a colocated GraphQL query without the need for any specific backend. This project is still in the very early stages but you can see some code examples below and a live example here. You can also read more about the idea behind the project here

The arguments to the Higher Order Component are the same as the props to the query Component and can be seen below. In the future I hope to follow the lead of apollow allowing creation of custom network interfaces.

  • query - a string query that is parsable by the graphql parser. The top level of each entity (person, post, todos) below will inform the route that the query hits. So
query {
  person {
    id
  }
}

would hit /person and fruther arguments would inform further path considerations.

  • apiPrefix - defaults to "" , use this if you would like each route to have some starting route parameters, an apiPrefix of /api/v1 with the above query would hit /api/v1/person
  • getRequestData - called once for each each request, returns an object with keys params and queryParams. In the above to achieve something like /person/1 one would use { params: [1], queryParams: {} };
  • getDataFromResponseBody - optional parameter that is used to transform the response body from the server into an object or array of objects in which each object contains the fields requested (missing fields will be passed as null)
  • pollInterval - an optional parameter that will cause the component to make requests for new data ever n milliseconds. If you wanted to refresh every 2 seconds you could pass a pollInterval of 2000
  • cachePolicy - this project uses the fetch-dedupe package under the hood which handles caching, you can see documentation on options here. We use the same defaults, with the exception that using pollInterval or refetch will always hit the network.

Higher Order Component

class App extends Component {
  render() {
    return this.props.loading ? (
      <h1>Loading Data</h1>
    ) : (
      <div style={{ padding: 20 }}>
        <h1>RouteQL Populated Props</h1>
        <h2>Person Query</h2>
        <ul>
          {Object.entries(this.props.person).map(([key, value]) => (
            <li key={key}>
              {key}: {value}
            </li>
          ))}
        </ul>
        <h2>Post Query</h2>
        <ul>
          {Object.entries(this.props.post).map(([key, value]) => (
            <li key={key}>
              {key}: {typeof value === "object" && value !== null
                ? JSON.stringify(value)
                : value}
            </li>
          ))}
        </ul>
        <h2>Todo List Query</h2>
        <ul>
          {this.props.todos.map(todo => <li key={todo.id}>
              <input type="checkbox" disabled checked={todo.complete} /> {todo.todo}
            </li>)}
        </ul>
      );
  }
}

export default routeql({
  query: `
    query {
      person {
        id
        name
        type
      },
      post {
        id
        title
        body
        metadata {
          author
        }
      },
      todos {
        id,
        todo,
        complete
      }
    }
  `,
  getRequestData: ({ props, routeName }) => {
    switch (routeName) {
      case "person": {
        return { params: [1], queryParams: {} };
      }
      case "post": {
        return { params: [2], queryParams: {} };
      }
      default: {
        return { params: [], queryParams: {} };
      }
    }
  }
})(App);

Query Component With function as children

function RenderPropQuery() {
  return (
    <Query
      query={`query {
              person {
            id
            name
            type
          },
          post {
              id
            title
            body
            metadata {
              author
            }
            },
          todos {
              id,
            todo,
            complete
          }
        }
      `}
      apiPrefix="http://localhost:3000"
      getRequestData={({ props, routeName }) => {
              switch (routeName) {
                case "person": {
                  return { params: [1], queryParams: {} };
                }
                case "post": {
                  return { params: [2], queryParams: {} };
                }
                default: {
                  return { params: [], queryParams: {} };
                }
              }
            }}
    >
      {({ loading, person, post, todos }) =>
              loading ? (
                <h1>Loading Data</h1>
              ) : (
                  <Fragment>
                    <h1>RouteQL Populated Props</h1>
                    <h2>Person Query</h2>
                    <ul>
                      {Object.entries(person).map(([key, value]) => (
                        <li key={key}>
                          {key}: {value}
                        </li>
                      ))}
                    </ul>
                    <h2>Post Query</h2>
                    <ul>
                      {Object.entries(post).map(([key, value]) => (
                        <li key={key}>
                          {key}:{" "}
                          {typeof value === "object" && value !== null
                            ? JSON.stringify(value)
                            : value}
                        </li>
                      ))}
                    </ul>
                    <h2>Todo List Query</h2>
                    <ul>
                      {todos.map(todo => (
                        <li key={todo.id}>
                          <input type="checkbox" disabled checked={todo.complete} />{" "}
                          {todo.todo}
                        </li>
                      ))}
                    </ul>
                  </Fragment>
                )
            }
    </Query>
  );
}

More Repositories

1

redux-test-recorder

a redux middleware to automatically generate tests for reducers through ui interaction
JavaScript
495
star
2

react-native-syntax-highlighter

a syntax highlighter for react native using https://github.com/conorhastings/react-syntax-highlighter under the hood
JavaScript
169
star
3

react-emoji-react

a clone of slack emoji reactions in react
JavaScript
120
star
4

use-reducer-with-side-effects

JavaScript
88
star
5

react-syntax-highlighter-virtualized-renderer

a way to use react-virtualized to render lines of code for react-syntax-highlighter
JavaScript
31
star
6

react-component-demo

A React Component To make live editable demos of other React Components
JavaScript
24
star
7

remove-flow-types-loader

JavaScript
20
star
8

react-close-on-escape

higher order component to close other react component on press of escape key (or fire whatever function you want but probably it should close)
JavaScript
14
star
9

react-thermometer

a simple thermometer gauge component (does not tell temperature)
JavaScript
12
star
10

apply-loader-after-first-build-webpack-plugin

a webpack plugin that allows you to dynamically add a loader after the initial build has occurred (useful when using watch) and want to do something like lint only changed and new files
JavaScript
5
star
11

redux-test-recorder-react

component providing a gui for redux-test-recorder in react applications
JavaScript
4
star
12

react-code-window

JavaScript
3
star
13

put-stuff-on-website

JavaScript
3
star
14

whitelist-object

a small module for reducing an object to only the desired keys
JavaScript
3
star
15

match-default

JavaScript
3
star
16

github-stars-elm

get total github stars for user , learning elm by writing elm
Elm
3
star
17

redux-session-storage

redux middleware for recording redux actions for a particular session to session storage
JavaScript
3
star
18

react-one-of

take in a set of components and make sure only one is rendered.
JavaScript
3
star
19

react-allowed

a react component that calls a function prop isAllowed to determine whether or not to render children
JavaScript
3
star
20

get-emoji

give emoji name get emoji image
JavaScript
2
star
21

storybook-addon-react-profiler

JavaScript
2
star
22

redux-connected-proptypes

an interface for defining React component props using redux, react-redux and PropTypes
JavaScript
2
star
23

react-async-branch

a component that will asynchronously load, one of two components based on a condition
JavaScript
2
star
24

react-test-generator

generate basic sanity check tests for react components, major WIP, ready to be used by someone, who knows when?
JavaScript
2
star
25

netflix-originals

a react calendar app for netflix original movie releases
JavaScript
2
star
26

react-expiration

A simple component that uses a render callback to expose if a the current date is past a user set expirationDate
JavaScript
2
star
27

clojurescript

learning clojurescript
Clojure
1
star
28

object-value-equality

JavaScript
1
star
29

state-reducers-hook

manage state by dispatching changes using react hooks
TypeScript
1
star
30

AgeComparer

Compare your age to the ages to the average age of professional athletes and become depressed about how relatively old you are in comparison.
JavaScript
1
star
31

object-description-filter

filter nested object based on descriptor object
JavaScript
1
star
32

version-upgrade

don't manually go through all of the different places your node version is stored, let this do it for you!
JavaScript
1
star
33

highlight-code

a javascript module to add syntax highlighting to a code string, uses inline styles for encapsulation
JavaScript
1
star
34

is-today-the-day-that-the-birth-of-jesus-is-celebrated

JavaScript
1
star
35

elm-md

a markdown editor written in elm
Elm
1
star
36

react-app-init

an opinionated, declarative way to start a react/redux app.
JavaScript
1
star
37

use-worker-hook

JavaScript
1
star
38

react-style-px-suffix-codemod

append px to shorthand values in style objects in react in prep for react 15 warning
JavaScript
1
star
39

GIF-Framer

takes in an animated gif and returns all the frames as individual images
JavaScript
1
star
40

highlight.js-js-styles

all of highlight.js css stylesheets as javascript objects
JavaScript
1
star