All Projects β†’ lucasconstantino β†’ apollo-progressive-fragment-matcher

lucasconstantino / apollo-progressive-fragment-matcher

Licence: MIT License
A smart alternative to the introspection fragment matcher.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to apollo-progressive-fragment-matcher

agollo
πŸš€Go client for ctrip/apollo (https://github.com/apolloconfig/apollo)
Stars: ✭ 563 (+2580.95%)
Mutual labels:  apollo, apollo-client
apollo-local-query
Simpler server rendering with apollo-client 1.x, using a local GraphQL networkInterface
Stars: ✭ 66 (+214.29%)
Mutual labels:  apollo, apollo-client
graphql-workshop
βš’ 🚧 A GraphQL workshop to learn GraphQL implementations
Stars: ✭ 20 (-4.76%)
Mutual labels:  apollo, apollo-client
jest-gql
βœ…πŸš€GraphQL based tests for Jest and Apollo
Stars: ✭ 33 (+57.14%)
Mutual labels:  apollo, apollo-client
apollo-magic-refetch
magically refetches relevant apollo graphql queries after creates, deletes, and association changes
Stars: ✭ 32 (+52.38%)
Mutual labels:  apollo, apollo-client
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (+371.43%)
Mutual labels:  apollo, apollo-client
react-apollo-graphql
Get rid of decorators and use Apollo GraphQL queries and mutations in the simple and readable way.
Stars: ✭ 16 (-23.81%)
Mutual labels:  apollo, apollo-client
apollo-fragment
Use Apollo Link State to connect components to GraphQL fragments in the Apollo Cache
Stars: ✭ 112 (+433.33%)
Mutual labels:  apollo, apollo-client
ember-boilerplate
πŸ‘“ The stable base upon which we build our Ember.js projects at Mirego.
Stars: ✭ 33 (+57.14%)
Mutual labels:  apollo, apollo-client
react-apollo-client-testing-example
A React with Apollo Client application with a mocked GraphQL server and tested Query and Mutation components.
Stars: ✭ 17 (-19.05%)
Mutual labels:  apollo, apollo-client
react-apollo-fragments
True Fragment component for react-apollo
Stars: ✭ 13 (-38.1%)
Mutual labels:  fragments, apollo-client
now-course
Proyecto para el curso de Now.sh en Platzi
Stars: ✭ 19 (-9.52%)
Mutual labels:  apollo, apollo-client
RxApolloClient
RxSwift extensions for Apollo Client
Stars: ✭ 46 (+119.05%)
Mutual labels:  apollo, apollo-client
gitstar
Github Client built with React Apollo
Stars: ✭ 15 (-28.57%)
Mutual labels:  apollo, apollo-client
apollo-cache-instorage
Apollo Cache implementation that facilitates locally storing resources
Stars: ✭ 98 (+366.67%)
Mutual labels:  apollo, apollo-client
github-react-native-apollo-graphql
πŸ“± A GitHub mobile app built with React-Native and Apollo GraphQL
Stars: ✭ 24 (+14.29%)
Mutual labels:  apollo, apollo-client
boilerplate
Boilerplate for @prisma-cms
Stars: ✭ 22 (+4.76%)
Mutual labels:  apollo, apollo-client
nap
[Deprecated] NextJS + Apollo + PassportJS
Stars: ✭ 52 (+147.62%)
Mutual labels:  apollo, apollo-client
les-chat
Real-time messenger with private, public & group chat. Made using PERN + GraphQL stack.
Stars: ✭ 48 (+128.57%)
Mutual labels:  apollo, apollo-client
kontent-boilerplate-express-apollo
Kontent Boilerplate for development of Express application using Apollo server and GraphQL.
Stars: ✭ 21 (+0%)
Mutual labels:  apollo, apollo-client

Apollo Progressive Fragment Matcher

Version License bundlephobia Build Status codecov

A smart alternative to the introspection fragment matcher.


Motivation

Error: You are using the simple (heuristic) fragment matcher... 😱

GraphQL APIs are evolving, and usage of Unions and Interfaces are much more common now then they use to be. Some time ago this kind of feature was considered advanced; I don't think that's true today. The GraphQL clients all need a way to distinguish data between two or more fragments that rely on inherited types (unions & interfaces), what I call the Human and Droid problem.

Apollo has long solved this issue by providing the IntrospectionFragmentMatcher. This fragment matcher, though, requires, that you provide a introspectionQueryResultData, which is your API's introspection query result. Introspection queries result can be huge.

What if we could avoid pre-fetching the introspection? What if we could introspect as we go?

Welcome ProgressiveFragmentMatcher.

Usage

Installation

npm i apollo-progressive-fragment-matcher apollo-link graphql invariant

ProgressiveFragmentMatcher

The Progressive Fragment Matcher has two strategies for matching fragment types:

Progressive introspection (default)

This strategy transforms the outgoing queries to request introspection information on the requesting types. It does cache the results, meaning if on a second query you use the same fragment type, it won't introspect again (nor transform the query, which can be expensive).

This strategy is much like what ApolloClient normally does to inject __typename fields.

Good:

  • Easy to install;
  • Drop-in replacement for IntrospectionFragmentMatcher;

Bad:

  • Query transforms are expensive;
Usage
import ApolloClient from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { from } from 'apollo-link'
import { HttpLink } from 'apollo-link-http'
import { ProgressiveFragmentMatcher } from 'apollo-progressive-fragment-matcher'

const fragmentMatcher = new ProgressiveFragmentMatcher()

const client = new ApolloClient({
  cache: new InMemoryCache({ fragmentMatcher }),
  link: from([fragmentMatcher.link(), new HttpLink()]),
})
Extension based

This strategy is very performatic on the client side, because it does not depend on query transformation. What this strategy does is send the server an extension flag ({ possibleTypes: true }) to request the server to send possible types of any returned type in the query - regardless of the fragments requested.

This strategy requires you have control of the server, and currently only works with ApolloServer custom extensions implementation.

Good:

  • Fast on client;
  • Persisted queries supported;

Bad:

  • Requires server control;
Usage

client:

import ApolloClient from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { from } from 'apollo-link'
import { HttpLink } from 'apollo-link-http'
import { ProgressiveFragmentMatcher } from 'apollo-progressive-fragment-matcher'

const fragmentMatcher = new ProgressiveFragmentMatcher({
  strategy: 'extension',
})

const client = new ApolloClient({
  cache: new InMemoryCache({ fragmentMatcher }),
  link: from([fragmentMatcher.link(), new HttpLink()]),
})

server

import { ApolloServer } from 'apollo-server'
import { PossibleTypesExtension } from 'apollo-progressive-fragment-matcher'

const server = new ApolloServer({
  typeDefs,
  resolvers,
  extensions: [() => new PossibleTypesExtension()],
})

server.listen() // start server

Due to a limitation on ApolloClient's customizing capabilities, both strategies require you append a link created from the fragment matcher instance.

Warning ⚠️

Although well tested, this project is in an experimental stage.

About persisted queries

I have not yet stressed it out on complicating circustances such as persistend queries. I've marked the extension strategy as supporting persisted queries due to the nature of this operation - it relies on no query transformation, therefore should be compatible with persisted queries, but no test prove this concept yet.

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