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-schema-from-swagger

v0.4.1

Published

Create apollo ready resolvers and type definitions for one or multiple swagger enabled apis

Downloads

6

Readme

GRAPHQL-SCHEMA-FROM-SWAGGER

Dynamically create your apollo ready graphql schema from one or many swagger enabled apis

Installation

npm install graphql-schema-from-swagger

Usage

Single Api Example

import { generate } from 'graphql-schema-from-swagger';

const apolloSchema = generate(accountApiSwaggerJson, {
  // This is a callback function for all the get requests made to this api.
  apiResolver: async ({
      parent,
      parentType,
      parameters,
      rawUrl,
      fieldName,
      type,
    }) => {
      // Come up with a strategy to set the variables on the raw url for things like nested urls
      var endpoint = setRawUrl({
        rawUrl,
        fieldName,
        parameters,
        parentType,
        parent,
      });

      // fetch data using your favorite http client
      return await restClient.get(endpoint.url, parameters);
    },
  }
});

/*
  apolloSchema looks like:
  {
    typeDefs: ``, // Your graphql schema as a string,
    resolvers: {}, // The resolvers for your graphql types
  }
*/

Multiple Api Example

import { schemaFromMultiple } from 'graphql-schema-from-swagger';

const apolloSchema = schemaFromMultiple([
  {
    swaggerJson: accountApiSwaggerJson,
    options: {
      apiResolver: async ({
        parent,
        parentType,
        parameters,
        rawUrl,
        fieldName,
        type,
        context,
      }) => {
        // Come up with a strategy to set the variables on the raw url for things like nested urls
        var endpoint = setRawUrl({
          rawUrl,
          fieldName,
          parameters,
          parentType,
          parent,
        });

        // fetch data using your favorite http client
        return await restClient.get(endpoint.url, parameters);
      }
    }
  },
  {
    swaggerJson: blogApiSwaggerJson,
    options: {
      apiResolver: async ({
        parent,
        parentType,
        parameters,
        rawUrl,
        fieldName,
        type,
        context,
      }) => {
        // Come up with a strategy to set the variables on the raw url for things like nested urls
        var endpoint = setRawUrl({
          rawUrl,
          fieldName,
          parameters,
          parentType,
          parent,
        });

        // fetch data using your favorite http client
        return await restClient.get(endpoint.url, parameters);
      }
    }
  }
]);

apiResolver

This is where all the resolvers for you get requests funnel through.

As part of the callback you are given relevant information to help set variables on the raw url:

parent

A hydrated parent object think blog when dealing with posts

parentType

"Blog" when dealing with posts

parameters

An object from what's sent from the client i.e. { take: 2, blogId: 1 }

rawUrl

The url as defined by your swagger endpoint (it's up to you to set the variables in the curly braces)

fieldName

i.e. "posts" or "blog"

type

i.e "Post", "Comment", "Account"

context The apollo context object

Things to note

To get a raw GraphQl Schema Object (graphqljs) you can call use:


import { generateTypes } from 'graphql-schema-from-swagger';

const schema = generateTypes(accountSwaggerJson, { apiResolver });
  • Only supports "GET" requests at the moment

Check out the tests for tinkering with example swagger json files (npm run test:watch)