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

appolo-graphql-datasource

v0.6.7

Published

GraphQL Datasource for Apollo Server

Downloads

59

Readme

apollo-graphql-datasource

Connect your GraphQL server to an existing GraphQL API using DataSources.

Note: This is designed to work with Apollo Server 2.0 and Data Sources

GraphQL Data Source

Install

npm i apollo-graphql-datasource --save

Usage

Define a data source by creating new the GraphQLDataSource instance. The below example will create a GraphQL datasource to the Apollo Federation Demo gateway. In the real world project, your schema may conflict with the destination schema. Therefore, a prefix should be added to the destination schema types, in this example: Demo0

const GraphQLDataSource = require('appolo-graphql-datasource');
const { gql } = require('apollo-server');

const typeDefs = `
  type Demo0Product {
    upc: String!
    name: String
    price: Int
    weight: Int
    reviews: [Demo0Review]
    inStock: Boolean
    shippingEstimate: Int

    calulatedField(inputArgs: SomeInput): Int
    calulatedField2(inputArgs: SomeInput!): CalulatedField2Response
  }

  type Demo0Review {
    id: ID!
    body: String
    author: Demo0User
    product: Demo0Product
  }

  type Demo0User {
    id: ID!
    name: String
    username: String
    reviews: [Demo0Review]
  }

  enum ProductEnum {
    VALUE_1
    VALUE_2
  }

  input SomeInput {
    args1: Int
    args2: String!
  }

  type CalulatedField2Response {
    value1: Int
    value2: Int
    value3: String
  }

  type Query {
    Demo0me: Demo0User
    Demo0topProducts(first: Int = 5, status: ProductEnum): [Demo0Product]
  }

  type Mutation {
    Demo0doSth(first: Int = 5): [Demo0Product]
  }
`;

const dataSource = new GraphQLDataSource(
  'http://federation.gateway.url/',
  typeDefs,
  'Demo0',
);

module.exports = () => ({
  demoFederationAPI: dataSource,
});

GraphQL Operations

  • The query and mutation methods on the GraphQLDataSource make a request to the GraphQL server. The datasource will foward the client's query to the destination server.
  • The query and mutation methods accepts a second parameter, options, which can be used to pass the additional headers.
  • The datasource also handles:
    • mutiple queries or mutations in one request
    • fragment
    • enum
    • fields with arguments
function genericQuery(parent, args, context, info) {
  const { demoFederationAPI } = context.dataSources;
  const { secretToken } = context;
  const headers = {
    serviceSecret: secretToken,
  };

  return demoFederationAPI.query(info, { headers });
}

module.exports = {
  Query: {
    Demo0topProducts: (...params) => genericQuery(...params),
    // ...others queries
  }
}

Transform Types to Scalar

If you want to transform some types to scalar, provide them in constructor

const dataSource = new GraphQLDataSource(
  'http://federation.gateway.url/',
  typeDefs,
  'Demo0',
  ['calulatedField2'], // provide Types to transform
);