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

supergraph-orm

v0.1.1

Published

GraphQL ORM for GraphQL query delegation

Downloads

2

Readme

supergraph-orm

GraphQL ORM for GraphQL query delegation

Overview

supergraph-orm is an ORM layer on top of any existing executable GraphQL schema. This can be a local executable schema, or a remote one. It makes query delegation (a common pattern in GraphQL gateways) easier.

Install

yarn add supergraph-orm
# or
npm install --save supergraph-orm

Example

Consider the following executable schema definition:

const users = []

const typeDefs = `
  type Query {
    allUsers: [User]
    hello(name: String): HelloPayload
  }
  
  type Mutation {
    createUser: User
  }
  
  type User {
    id: Int
    name: String
  }
  
  type HelloPayload {
    message: String
  }`

const resolvers = {
  Query: {
    hello: (_, { name }) => ({ message: `Hello ${name || 'world'}!` })
    allUsers: () => users
  },
  Mutation: {
    createUser: (_ { name }) => {
      const newUser = { id: users.length, name }
      users.push(newUser)
      return newUser
    }
  }
}

If you create an executable schema based on this definition, you'll be able to send the following queries:

// Create executable schema
const executableSchema = makeExecutableSchema({
  typeDefs,
  resolvers
})

const orm = new Orm({executableSchema})

// Execute the `hello` query, returning all fields
orm.query.hello()

// Execute the `hello` query with a parameter
orm.query.hello({ name: 'John' })

// Execute the 'createUser' mutation
orm.mutation.createUser({ name: 'Joe' })

// Execute the 'allUsers' query, returning only the user names
orm.query.allUsers(null, { name })

API

constructor(options: OrmOptions): Orm

The Orm type has the following fields:

| Key | Required | Type | Default | Note | | --- | --- | --- | --- | --- | | executableSchema | Yes | GraphQLSchema | - | Instance of an executable GraphQL schema | | fragmentReplacements | No | FragmentReplacements | null | A list of GraphQL fragment definitions, specifying fields that are required for the resolver to function correctly |

query and mutation

query and mutation are public properties on your Orm instance. They both are of type Query and expose a number of auto-generated delegate resolver functions that are named after the fields on the Query and Mutation types in your GraphQL schema.

Each of these delegate resolvers in essence provides a convenience API for executing queries/mutations against your schema, so you don't have to spell out the full query/mutation from scratch. This is all handled by the delegate resolver function under the hood.

Delegate resolver have the following interface:

(args: any, info: GraphQLResolveInfo | string): Promise<T>

The input arguments are used as follows:

  • args: An object carrying potential arguments for the query/mutation
  • info: An object representing the selection set of the query/mutation, either expressed directly as a string or in the form of GraphQLResolveInfo (you can find more info about the GraphQLResolveInfo type here)

The generic type T corresponds to the type of the respective field.

request

The request method allows executing any GraphQL query/mutation against your schema. The functionality is identical to the auto-generated delegate resolves, but the API is more verbose as you need to spell out the full query/mutation.

Here is an example of how it can be used:

const query = `
  query ($userId: ID!){
    user(id: $userId) {
      id
      name
    }
  }
`

const variables = { userId: '123' }

orm.request(query, variables)
  .then(result => console.log(result))
// sample result:
// { "id": "abc", "name": "Sarah" }

Usage

  • graphcool-binding uses supergraph-orm and adds functionality to it specific to Graphcool endpoints.