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

graphql-helper

v0.3.0

Published

A simple helper library for constructing GraphQL queries.

Downloads

69

Readme

graphql-helper Build Status

A GraphQL helper library for constructing queries and accumulating fragments. This library is meant for statically determined queries, and encourages the use of variables and fragments over string concatenation.

This library is fairly short and written in a literate style, it is encouraged to take the time to read through the source code.

yarn add graphql-helper

Example:

import { fragment, query } from 'graphql-helper'
import fetch from 'isomorphic-fetch'

const Contributor = fragment('Contributor', 'User') `{
  name
  slug
}`

const PostPage = fragment('PostPage', 'Post') `{
  title
  body
  contributors {
    ${Contributor}
  }
}`

const PostQuery = query('PostQuery', { postId: 'ID!' }) `{
  post(id: $postId) {
    id
    ${PostPage}
  }
}`


// Write your own app-specific dispatcher.
// In this case, we just have a simple function, but this could live in
// a react library, an elm effects module, an ember service...

function runQuery(op, vars): Promise<Result> {
  return fetch('http://localhost:3000/graphql', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        query: op,
        variables: vars,
      }),
    })
    .then(r => r.json())
    .then({ data, errors }) => errors ? Promise.reject(errors) : Promise.resolve(data))
   })
}


// Usage
runQuery(PostQuery, { postId: 123 })
  .then(data => {
    // data = {
    //   post: {
    //     id: 123,
    //     title: "foo",
    //     body: "bar",
    //     contributors: [
    //       { name: 'Daria Morgendorffer', slug: '/daria' },
    //       { name: 'Jane Lane', slug: '/jane' }
    //     ]
    //   }
    // }
  })

Fragments

A fragment represents the data requirements of some component or aspect of an application.

Consider the graphql fragment:

fragment FullPost on Post {
  id
  slug
  title
  body
  contributors {
    ...Contributor
  }
  author {
    name
    ...Author
  }
}

Suppose Author and Contributor are fragment definitions that we have already defined, then we can define FullPost as follows:

import { fragment } from 'graphql-helper'
import { Author, Contributor } from 'some-module'

const FullPost = fragment('FullPost', 'Post') `{
  id
  slug
  title
  body
  contributors {
    ${Contributor}
  }
  author {
    name
    ${Author}
  }
}`

Queries

A query represents some operation that fetches data.

Consider the GraphQL query:

query GetPost($id: ID!) {
  post(id: $id) {
    __typename
    id
    ...FullPost
  }
}

Suppose FullPost is already defined above. Then we can define this query as follows:

const GetPost = query('GetPost', { id: 'ID!' }) `{
  post(id: $id) {
    __typename
    id
    ${FullPost}
  }
}`

And we can open up the resulting query object yields the following:

GetPost.__GRAPHQL_QUERY__
// => true

GetPost.name
// => 'GetPost'

GetPost.definition
// => `query GetPost($id: ID!) {
  post(id: $id) {
    __typename
    id
    ...FullPost
  }
}`

GetPost.fragments
// => { FullPost, Author, Contributor }

// the following are equivalent:
GetPost.definition
GetPost.toString()
// => `query GetPost($id: ID!) {
  post(id: $id) {
    __typename
    id
    ...FullPost
  }
}

fragment FullPost on Post {
  id
  slug
  id
  slug
  title
  body
  contributors {
    ...Contributor
  }
  author {
    name
    ...Author
  }
}

fragment Contributor on User {
  ...etc
`

Mutations

A mutation represents some operation which changes data.

Consider a relay-compatible mutation createPost:

type RootMutation {
  ...
  createPost(input: CreatePostInput): CreatePostPayload
  ...
}

input CreatePostInput {
  clientMutationId: String!
  title: String!
  body: String!
}

type CreatePostPayload {
  clientMutationId: String!
  post: Post!
}

Then there exists a natural, "free mutation" that performs just that mutation:

mutation CreatePost($input: CreatePostInput) {
  payload: createPost(input: $Input) {
    # application decides which fields to fetch
    clientMutationId
    post {
      ...FullPost
    }
  }
}

Noting that the clientMutationId is a special field, we provide a condensed syntax for such a query as follows:

const CreatePost = GraphQL.mutation('createPost', {
  title: 'String!',
  body: 'String!',
}) `{
  clientMutationId
  post {
    ${FullPost}
  }
}`

And we can open up the resulting query object yields the following:

CreatePost.__GRAPHQL_MUTATION__
// => true

CreatePost.name
// => 'CreatePost'

CreatePost.definition
// => `mutation CreatePost($input: CreatePostInput!) {
  payload: createPost(input: $input) {
    clientMutationId
    post {
      ...FullPost
    }
  }
}`

CreatePost.fragments
// => { FullPost, Author, Contributor }

// the following are equivalent:
CreatePost.definition
CreatePost.toString()
// => `mutation CreatePost($input: CreatePostInput!) {
  payload: createPost(input: $input) {
    clientMutationId
    post {
      ...FullPost
    }
  }
}

fragment FullPost on Post {
  id
  slug
  id
  slug
  title
  body
  contributors {
    ...Contributor
  }
  author {
    name
    ...Author
  }
}

fragment Contributor on User {
  ...etc
`

Document

The GraphQL.document([ QueryOne, QueryTwo, MutationOne, ... ]) method generates a complete document object which is useful for persisted queries. This should never appear in your application logic, although build tools may use this to heavily optimize a production build by persisting a document at build time.

See source code for type declaration and implementation.

TODO

  • Support for static analysis and pre-compilation of queries