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

apollo-typed-components

v3.0.0

Published

Using typescript or flow types from the apollo-cli generate command, generate typed react-apollo Query and Mutation components

Downloads

3

Readme

apollo-typed-components

Command-line tool to generate ready-to-use react-apollo flow typed Query and Mutation components. Utilizes type output from apollo-cli

Usage

# Use apollo-cli to get schema definition from graphql server
$ apollo schema:download --endpoint=http://localhost:3001/graphql schema.json

# Generate typescript or flow types from queries.graphql files
$ apollo codegen:generate --schema=schema.json --target=flow --outputFlat src/types.js
$ apollo codegen:generate --schema=schema.json --target=typescript --outputFlat src/types.ts

# Generate components utilizing generated typescript or flow types
$ apollo-typed-components --target=flow
$ apollo-typed-components --target=typescript

A corresponding ApolloComps.(tsx, js) file will be generated next to each queries.graphql file, with one Query/Mutation component export for each operation defined in queries.graphql. The components automatically will have type-safety with the generated types and also will automatically pass down the query/mutation DocumentNode.

Assumptions

The generated files make some assumptions about how the consuming project is structured.

  • query and mutation operations are defined in queries.graphql files
  • The types generated from apollo-cli are accessible via import type { ... } from "types". To allow this:
    • module.system.node.resolve_dirname=./src in .flowconfig options for flow
    • for typescript { "baseUrl": "src" } in tsconfig.json
    • output generated types to src/types/apolloTypes.(js, tsx), and then re-export them out of src/types/index.js using export (type) *

Example Output

Given a queries.graphql file:

query GetList(
  $first: Int!
  $after: String
) {
  list(
    first: $first
    after: $after
  ) {
    pageInfo {
      totalCount
    }
  }
}

mutation DeleteItem($input: DeleteItemInput!) {
  deleteItem(input: $input) {
    success
  }
}

apollo-flow-components will generate this ApolloComps.js file in the same directory:

/* DO NOT EDIT Generated using apollo-typed-components */
// @flow
import * as React from "react";
import { Query, Mutation } from "react-apollo";
import {
  GetList,
  DeleteItem
} from "./queries.graphql"
import type {
  GetList as GetListType,
  GetListVariables,
  DeleteItem as DeleteItemType,
  DeleteItemVariables
} from "types";

class GetListQueryClass extends Query<GetListType, GetListVariables> {};
class DeleteItemMutationClass extends Mutation<DeleteItemType, DeleteItemVariables> {};

export const GetListQuery = (props: $Diff<React.ElementConfig<typeof GetListQueryClass>, { query: any }>) => <GetListQueryClass query={GetList} {...props} />;
export const DeleteItemMutation = (props: $Diff<React.ElementConfig<typeof DeleteItemMutationClass>, { mutation: any }>) => <DeleteItemMutationClass mutation={DeleteItem} {...props} />;

Then, to utilize the components,

import { GetListQuery } from "./ApolloComps"

const Comp = () => (
  <GetListQuery variables={{ first: 10 }}>
    {({ data, loading, error }) => ... }
  </GetListQuery>
)