Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Reduce size impact of populateExchange (#122)
Browse files Browse the repository at this point in the history
* Reuse more helpers and add mutations where necessary in populateExchange

Refactor some parts to avoid unnecessary immutability for micro-perf optimisations,
and reduce size where possible by reusing helpers and swapping out immutability for
mutability.

Total minzipped size:
8.24kB -> 8.19kB

* Remove usage of Array.from

This isn't supported in IE11 and hence breaks compatibility
if it isn't user polyfilled.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

* Remove another internal immutable change in addTypenamesToQuery

* Extract unwrapType helper and enhance type-safety

* Remove userFragments/typeFragments reduce

Removes additional closures that aren't needed.
Addresses PR comments

* Remove additional map for for-loop

* Use Kind enum from graphql

* Refactor extractSelectionsFromQuery and addFragmentsToQuery

* Add IntrospectionQuery type from populate to cache

* Simplify GraphQLFlatType

* Moving map.ts to src/helpers/
  • Loading branch information
kitten authored Dec 8, 2019
1 parent d668ae8 commit 5607ee7
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 174 deletions.
7 changes: 5 additions & 2 deletions docs/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,14 @@ field somewhere, maybe due to a typo.

## (18) Invalid TypeInfo state <a id="18"></a>

> Invalid TypeInfo state: Found an abstract type when none was expected.
> Invalid TypeInfo state: Found no flat schema type when one was expected.
When you're using the populate exchange with an introspected schema, it will
start collecting used fragments and selection sets on all of your queries.
This error may occur if it hits unexpected abstract types when doing so.
This error may occur if it hits unexpected types or inexistent types when doing so.

Check whether your schema is up-to-date or whether you're using an invalid
field somewhere, maybe due to a typo.

Please open an issue if it happens on a query that you expect to be supported
by the `populateExchange`.
13 changes: 12 additions & 1 deletion src/ast/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import {
InlineFragmentNode,
FieldNode,
FragmentDefinitionNode,
GraphQLOutputType,
Kind,
isListType,
isNonNullType,
} from 'graphql';

import { SelectionSet } from '../types';
import { SelectionSet, GraphQLFlatType } from '../types';

/** Returns the name of a given node */
export const getName = (node: { name: NameNode }): string => node.name.value;
Expand Down Expand Up @@ -40,3 +43,11 @@ export const isFieldNode = (node: SelectionNode): node is FieldNode =>
export const isInlineFragment = (
node: SelectionNode
): node is InlineFragmentNode => node.kind === Kind.INLINE_FRAGMENT;

export const unwrapType = (
type: null | undefined | GraphQLOutputType
): GraphQLFlatType | null => {
return type && (isListType(type) || isNonNullType(type))
? type.ofType
: type || null;
};
3 changes: 2 additions & 1 deletion src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CacheOutcome,
} from 'urql';

import { IntrospectionQuery } from 'graphql';
import { filter, map, merge, pipe, share, tap } from 'wonka';
import { query, write, writeOptimistic } from './operations';
import { SchemaPredicates } from './ast/schemaPredicates';
Expand Down Expand Up @@ -85,7 +86,7 @@ export interface CacheExchangeOpts {
resolvers?: ResolverConfig;
optimistic?: OptimisticMutationConfig;
keys?: KeyingConfig;
schema?: object;
schema?: IntrospectionQuery;
}

export const cacheExchange = (opts?: CacheExchangeOpts): Exchange => ({
Expand Down
File renamed without changes.
Loading

0 comments on commit 5607ee7

Please sign in to comment.