All Projects → cvburgess → Sqldatasource

cvburgess / Sqldatasource

Licence: mit
SQL DataSource for Apollo GraphQL projects

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Sqldatasource

Graphql Apq
🎯 Automatic persisted queries (APQ) for any GraphQL server.
Stars: ✭ 43 (-75.57%)
Mutual labels:  graphql, apollo, apollo-server
Graphql Modules
Enterprise Grade Tooling For Your GraphQL Server
Stars: ✭ 962 (+446.59%)
Mutual labels:  graphql, apollo, apollo-server
Firestore Apollo Graphql
An example of a GraphQL setup with a Firebase Firestore backend. Uses Apollo Engine/Server 2.0 and deployed to Google App Engine.
Stars: ✭ 371 (+110.8%)
Mutual labels:  graphql, apollo, apollo-server
Awesome Apollo Graphql
A curated list of amazingly awesome things regarding Apollo GraphQL ecosystem 🌟
Stars: ✭ 126 (-28.41%)
Mutual labels:  graphql, apollo, apollo-server
Apollo Server Vercel
⚫ Production-ready Node.js GraphQL server for Vercel Serverless Functions
Stars: ✭ 69 (-60.8%)
Mutual labels:  graphql, apollo, apollo-server
Graphql Advanced Projection
Fully customizable Mongoose/MongoDB projection generator.
Stars: ✭ 46 (-73.86%)
Mutual labels:  graphql, apollo, apollo-server
Chatty
A WhatsApp clone with React Native and Apollo (Tutorial)
Stars: ✭ 481 (+173.3%)
Mutual labels:  graphql, apollo, apollo-server
Portara
Portara directive is a rate limiter / throttler for GraphQL
Stars: ✭ 158 (-10.23%)
Mutual labels:  graphql, apollo, apollo-server
React Hipstaplate
A ReactJS full-stack boilerplate based on typescript with ssr, custom apollo-server and huge stack of modern utilities which will help you to start your own project
Stars: ✭ 74 (-57.95%)
Mutual labels:  graphql, apollo, apollo-server
Join Monster Graphql Tools Adapter
Use Join Monster to fetch your data with Apollo Server.
Stars: ✭ 130 (-26.14%)
Mutual labels:  graphql, apollo, sql
Scalable React Typescript Boilerplate
⭐️ Scalable micro-framework featuring React and TypeScript
Stars: ✭ 174 (-1.14%)
Mutual labels:  graphql, apollo
Graphql Directive
Use custom directives in your GraphQL schema and queries 🎩
Stars: ✭ 142 (-19.32%)
Mutual labels:  graphql, apollo
Reason Apollo Hooks
Deprecated in favor of https://github.com/reasonml-community/graphql-ppx
Stars: ✭ 140 (-20.45%)
Mutual labels:  graphql, apollo
Micro Medium Api
Microservice for fetching the latest posts of Medium with GraphQL.
Stars: ✭ 138 (-21.59%)
Mutual labels:  graphql, apollo-server
Federation
Apollo Federation
Stars: ✭ 171 (-2.84%)
Mutual labels:  graphql, apollo
Nest User Auth
A starter build for a back end which implements managing users with MongoDB, Mongoose, NestJS, Passport-JWT, and GraphQL.
Stars: ✭ 145 (-17.61%)
Mutual labels:  graphql, apollo-server
Meteor Apollo Accounts
Meteor accounts in GraphQL
Stars: ✭ 145 (-17.61%)
Mutual labels:  graphql, apollo
Fakerql
Hosted faker GraphQL endpoint for frontend developers
Stars: ✭ 152 (-13.64%)
Mutual labels:  graphql, apollo-server
Next Graphql Blog
🖊 A Blog including a server and a client. Server is built with Node, Express & a customized GraphQL-yoga server. Client is built with React, Next js & Apollo client.
Stars: ✭ 152 (-13.64%)
Mutual labels:  graphql, apollo
Graphql Cli
📟 Command line tool for common GraphQL development workflows
Stars: ✭ 1,814 (+930.68%)
Mutual labels:  graphql, apollo

SQLDataSource

This package combines the power of Knex with the ease of use of Apollo DataSources.

BREAKING CHANGES IN v1.0.0

In v1.0.0 this lib has a new fluid interface that plays nicely with Knex and stays more true to the spirit of Apollo DataSources.

const query = this.knex.select("*").from("fruit").where({ id: 1 }).cache();

query.then(data => /* ... */ );

To use ( or not use ) the caching feature in v1, simply add .cache() to your Knex query.

Read more below about getting set up and customizing the cache controls.

Getting Started

Installation

To install SQLDataSource: npm i datasource-sql

Usage

// MyDatabase.js

const { SQLDataSource } = require("datasource-sql");

const MINUTE = 60;

class MyDatabase extends SQLDataSource {
  getFruits() {
    return this.knex
      .select("*")
      .from("fruit")
      .where({ id: 1 })
      .cache(MINUTE);
  }
}

module.exports = MyDatabase;

And use it in your Apollo server configuration:

// index.js

const MyDatabase = require("./MyDatabase");

const knexConfig = {
  client: "pg",
  connection: {
    /* CONNECTION INFO */
  }
};

// you can also pass a knex instance instead of a configuration object
const db = new MyDatabase(knexConfig);

const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache,
  context,
  dataSources: () => ({ db })
});

Caching ( .cache( ttl ) )

If you were to make the same query over the course of multiple requests to your server you could also be making needless requests to your server - especially for expensive queries.

SQLDataSource leverages Apollo's caching strategy to save results between requests and makes that available via .cache().

This method accepts one OPTIONAL parameter, ttl that is the number of seconds to retain the data in the cache.

The default value for cache is 5 seconds.

Unless configured, SQLDataSource uses an InMemoryLRUCache like the RESTDataSource.

SQLDataSource Properties

SQLDataSource is an ES6 Class that can be extended to make a new SQLDataSource and extends Apollo's base DataSource class under the hood.

( See the Usage section above for an example )

Like all DataSources, SQLDataSource has an initialize method that Apollo will call when a new request is routed.

If no cache is provided in your Apollo server configuration, SQLDataSource falls back to the same InMemoryLRUCache leveraged by RESTDataSource.

context

The context from your Apollo server is available as this.context.

knex

The knex instance is made available as this.knex or this.db.

Debug mode

To enable more enhanced logging via knex-tiny-logger, set DEBUG to a truthy value in your environment variables.

Contributing

All contributions are welcome!

Please open an issue or pull request.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].