All Projects → DmitryTsepelev → graphql-ruby-persisted_queries

DmitryTsepelev / graphql-ruby-persisted_queries

Licence: MIT License
Persisted queries for graphql-ruby

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to graphql-ruby-persisted queries

graphql-fundamentals
A training repo for learning basic concepts in GraphQL on the client and server
Stars: ✭ 15 (-87.39%)
Mutual labels:  apollo
Ocelot-Social
Free and open-source social network for active citizenship.
Stars: ✭ 49 (-58.82%)
Mutual labels:  apollo
RxApollo
RxSwift extensions for Apollo
Stars: ✭ 57 (-52.1%)
Mutual labels:  apollo
stuyspec.com
🖼 The Stuyvesant Spectator's website, built with React, GraphQL, and Rails.
Stars: ✭ 22 (-81.51%)
Mutual labels:  apollo
cadhub
We're out to raise awareness and put CodeCAD on the map. The success of CadHub can be measured by the amount it promotes the use of CodeCAD within the mechanical/manufacturing industry and the strength the CadHub community.
Stars: ✭ 204 (+71.43%)
Mutual labels:  apollo
rn-chat
Chat app made with React Native, NativeBase, Apollo Hooks and Sequelize.
Stars: ✭ 37 (-68.91%)
Mutual labels:  apollo
react-storefront
An open-source storefront in React.js with Next.js. Built for Headless Commerce, using a modern stack with TypeScript, GraphQL, Apollo, and Tailwind CSS.
Stars: ✭ 265 (+122.69%)
Mutual labels:  apollo
blog-frontend
Frontend of blog created using: GraphQL (Apollo) + Vue + Nuxt.js + TypeScript + Vuetify...
Stars: ✭ 43 (-63.87%)
Mutual labels:  apollo
apollo-chat-graphql-server
Apollo Chat is a Chat Service build on GraphQL Apollo with Subscriptions
Stars: ✭ 13 (-89.08%)
Mutual labels:  apollo
apollo-error-converter
Global Apollo Server Error handling made easy. Remove verbose and repetitive resolver / data source Error handling. Automatic Error catching, logging, and conversion to ApolloErrors.
Stars: ✭ 16 (-86.55%)
Mutual labels:  apollo
apollo-rs
Spec compliant GraphQL Tools in Rust.
Stars: ✭ 314 (+163.87%)
Mutual labels:  apollo
udacity-alumni-fe
A front-end web application and bespoke publishing platform, built by Udacity Alumni for Udacity Alumni
Stars: ✭ 45 (-62.18%)
Mutual labels:  apollo
netlify-faunadb-graphql-auth
Netlify functions example with faunadb, graphql, and authorization
Stars: ✭ 57 (-52.1%)
Mutual labels:  apollo
apollo-docker
Apollo阿波罗配置中心docker
Stars: ✭ 23 (-80.67%)
Mutual labels:  apollo
now-course
Proyecto para el curso de Now.sh en Platzi
Stars: ✭ 19 (-84.03%)
Mutual labels:  apollo
kontent-boilerplate-express-apollo
Kontent Boilerplate for development of Express application using Apollo server and GraphQL.
Stars: ✭ 21 (-82.35%)
Mutual labels:  apollo
React-Realtime-Chat
A full-stack reproduction of the popular realtime chat application, Slack (http://slack.com) using React and GraphQL Subscriptions.
Stars: ✭ 16 (-86.55%)
Mutual labels:  apollo
auth-flow-react-apollo-saga
Full stack login/register flow with React, Apollo, Redux, Redux-saga and MongoDB.
Stars: ✭ 22 (-81.51%)
Mutual labels:  apollo
native-app
🍿 🕐 🎞 React Native App for the PCT environment
Stars: ✭ 39 (-67.23%)
Mutual labels:  apollo
bs-graphql-bindings
BuckleScript binding for graphql-js
Stars: ✭ 50 (-57.98%)
Mutual labels:  apollo

GraphQL::PersistedQueries

GraphQL::PersistedQueries is the implementation of persisted queries for graphql-ruby. With this plugin your backend will cache all the queries, while frontend will send the full query only when it's not found at the backend storage.

  • 🗑Heavy query parameter will be omitted in most of cases – network requests will become less heavy
  • 🤝Clients share cached queries – it's enough to miss cache only once for each unique query
  • 🎅Works for clients without persisted query support

Sponsored by Evil Martians

Getting started

First of all, install and configure apollo's persisted queries on the front–end side:

import { HttpLink, InMemoryCache, ApolloClient } from "@apollo/client";
import { createPersistedQueryLink } from "@apollo/client/link/persisted-queries";
import { sha256 } from 'crypto-hash';

const httpLink = new HttpLink({ uri: "/graphql" });
const persistedQueriesLink = createPersistedQueryLink({ sha256 });
const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: persistedQueriesLink.concat(httpLink);
});

Add the gem to your Gemfile gem 'graphql-persisted_queries' and add the plugin to your schema class:

class GraphqlSchema < GraphQL::Schema
  use GraphQL::PersistedQueries
end

Pass :extensions argument as part of a context to all calls of GraphqlSchema#execute, usually it happens in GraphqlController, GraphqlChannel and tests:

GraphqlSchema.execute(
  params[:query],
  variables: ensure_hash(params[:variables]),
  context: {
    extensions: ensure_hash(params[:extensions])
  },
  operation_name: params[:operationName]
)

You're all set!

Compiled queries (increases performance up to 2x!)

When query arrives to the backend, GraphQL execution engine needs some time to parse it and build the AST. In case of a huge query it might take a lot of time. What if we cache the AST instead of a query text and skip parsing completely? The only thing you need to do is to turn :compiled_queries option on:

class GraphqlSchema < GraphQL::Schema
  use GraphQL::PersistedQueries, compiled_queries: true
end

Using this option might make your endpoint up to 2x faster according to the benchmark.

Heads up! This feature only works on graphql-ruby 1.12.0 or later, but I guess it might be backported.

Advanced usage

All the queries are stored in memory by default, but you can easily switch to another storage (e.g., redis:

class GraphqlSchema < GraphQL::Schema
  use GraphQL::PersistedQueries, store: :redis, redis_client: { redis_url: ENV["MY_REDIS_URL"] }
end

We currently support memory, redis, redis_with_local_cache and memcached out of the box. The detailed documentation can be found here.

When the error occurs, the gem tries to not interrupt the regular flow of the app (e.g., when something is wrong with the storage, it will just answer that persisted query is not found). You can add a custom error handler and try to fix the problem or just log it.

Since our queries are slim now, we can switch back to HTTP GET, you can find a guide here.

batch-link allows to group queries on the client side into a single HTTP request before sending to the server. In this case you need to use GraphqlSchema.multiplex(queries) instead of #execute. The gem supports it too, no action required!

persisted-queries-link uses SHA256 for building hashes by default. Check out this guide if you want to override this behavior.

An experimental tracing feature can be enabled by setting tracing: true when configuring the plugin. Read more about this feature in the Tracing guide.

📖 Read more about the gem internals: Persisted queries in GraphQL: Slim down Apollo requests to your Ruby application

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries.

License

The gem is available as open source under the terms of the MIT License.

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].