0.6.7 • Published 3 years ago

appolo-graphql-datasource v0.6.7

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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

3 years ago

0.5.11

3 years ago

0.6.7

3 years ago

0.4.9

3 years ago

0.6.6

3 years ago

0.4.8

3 years ago

0.5.18

3 years ago

0.5.19

3 years ago

0.5.16

3 years ago

0.5.17

3 years ago

0.5.14

3 years ago

0.5.15

3 years ago

0.5.12

3 years ago

0.5.13

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.6

3 years ago

0.5.5

3 years ago

0.5.0

3 years ago

0.5.1

3 years ago

0.4.20

3 years ago

0.4.21

3 years ago

0.5.8

3 years ago

0.5.7

3 years ago

0.5.9

3 years ago

0.4.24

3 years ago

0.4.22

3 years ago

0.4.23

3 years ago

0.4.19

3 years ago

0.4.10

3 years ago

0.5.20

3 years ago

0.4.17

3 years ago

0.4.18

3 years ago

0.4.15

3 years ago

0.4.16

3 years ago

0.4.13

3 years ago

0.4.14

3 years ago

0.4.11

3 years ago

0.4.12

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.5

3 years ago

0.4.7

3 years ago

0.6.4

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.4.5

4 years ago

0.4.6

4 years ago

0.4.1

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.0

4 years ago

0.3.15

4 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.1.0

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago