Socket
Socket
Sign inDemoInstall

appolo-graphql-datasource

Package Overview
Dependencies
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

appolo-graphql-datasource

GraphQL Datasource for Apollo Server


Version published
Weekly downloads
5
increased by150%
Maintainers
1
Weekly downloads
 
Created
Source

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
);

FAQs

Package last updated on 21 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc