Skip to content

Commit

Permalink
Merge Relay packages together
Browse files Browse the repository at this point in the history
Previously, we tried to split the packages similarly to how Relay does it internally. However, we never finished this effort leaving it fragmented. I'd like to propose merging it back together with these steps to follow:

- cleanup the package (remove things we don't need to wrap because Relay already provides the necessary Flow types)
- get it ready for next major version Relay 11

adeira-source-id: 10aba4b8307e7ca9ee757885054c752e4bc8d201
  • Loading branch information
mrtnzlml authored and adeira-github-bot committed Mar 2, 2021
1 parent 29ceaf8 commit a8bad2a
Show file tree
Hide file tree
Showing 43 changed files with 824 additions and 167 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Packages `@adeira/relay-runtime` and `@adeira/relay-utils` merged to this package. Everything should work without any extra changes (except for updating the exports). Please, let us know in case we missed something.

# 3.0.1

- `adeira-fetch-schema` script fixed
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,23 @@ function loadMore() {
```
Similar rules apply to pagination container which solves one particular use-case of refetch container: "load more" pagination. The difference is that you have to use `type PaginationRelayProp` instead.
## Lazy, Eager and Debug logger
You can enable rich log output into browser console like so:
```js
import {
RelayLazyLogger, // less verbose, waits for operations to complete
RelayEagerLogger, // more verbose, logs events as they arrive
RelayDebugLogger, // very verbose, logs everything
} from '@adeira/relay';

return new Environment({
log: RelayLazyLogger, // or RelayEagerLogger or RelayDebugLogger
// network: ...,
// store: ...,
});
```
The logs are being printed only during development.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@adeira/js": "^2.1.0",
"@adeira/logger": "^0.4.0",
"@adeira/monorepo-utils": "^0.11.0",
"@adeira/relay-runtime": "^0.20.0",
"@adeira/signed-source": "^2.0.0",
"@babel/register": "^7.13.8",
"@babel/runtime": "^7.13.9",
Expand Down
9 changes: 6 additions & 3 deletions src/LocalQueryRenderer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// @flow

import { useContext, type Node } from 'react';
import { LocalQueryRenderer as RelayLocalQueryRenderer, ReactRelayContext } from 'react-relay';
import {
LocalQueryRenderer as RelayLocalQueryRenderer,
ReactRelayContext,
type Environment,
} from 'react-relay';
import { invariant } from '@adeira/js';
import type { Variables, GraphQLTaggedNode } from '@adeira/relay-runtime';
import type { Variables, GraphQLTaggedNode } from 'relay-runtime';

import createLocalEnvironment from './createLocalEnvironment';
import type { Environment } from './runtimeTypes.flow';

type CommonProps = {|
+query: GraphQLTaggedNode,
Expand Down
10 changes: 6 additions & 4 deletions src/QueryRenderer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// @flow

import { useContext, type Node } from 'react';
import { QueryRenderer as RelayQueryRenderer, ReactRelayContext } from 'react-relay';
import {
QueryRenderer as RelayQueryRenderer,
ReactRelayContext,
type Environment,
} from 'react-relay';
import { invariant, sprintf } from '@adeira/js';
import { TimeoutError, ResponseError } from '@adeira/fetch';
import type { Variables, CacheConfig, GraphQLTaggedNode } from '@adeira/relay-runtime';

import type { Environment } from './runtimeTypes.flow';
import type { Variables, CacheConfig, GraphQLTaggedNode } from 'relay-runtime';

type ReadyState<T> = {|
+error: ?Error,
Expand Down
4 changes: 1 addition & 3 deletions src/RelayEnvironmentProvider.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @flow

import * as React from 'react';
import { ReactRelayContext } from 'react-relay';

import type { Environment } from './runtimeTypes.flow';
import { ReactRelayContext, type Environment } from 'react-relay';

const { useMemo } = React;

Expand Down
59 changes: 59 additions & 0 deletions src/RelayNetworkTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// @flow strict

// https://github.com/facebook/relay/blob/master/packages/relay-runtime/network/RelayNetworkTypes.js

export type PayloadData = { [key: string]: mixed, ... };

export type PayloadError = {
message: string,
locations?: Array<{
line: number,
column: number,
...
}>,
// Not officially part of the spec, but used at Facebook
severity?: 'CRITICAL' | 'ERROR' | 'WARNING',
...
};

export type PayloadExtensions = { [key: string]: mixed, ... };

/**
* The shape of a GraphQL response as dictated by the
* [spec](https://graphql.github.io/graphql-spec/June2018/#sec-Response-Format)
*/
export type GraphQLResponseWithData = {|
+data: PayloadData,
+errors?: Array<PayloadError>,
+extensions?: PayloadExtensions,
+label?: string,
+path?: Array<string | number>,
|};

export type GraphQLResponseWithoutData = {|
+data?: ?PayloadData,
+errors: Array<PayloadError>,
+extensions?: PayloadExtensions,
+label?: string,
+path?: Array<string | number>,
|};

export type GraphQLResponseWithExtensionsOnly = {|
// Per https://spec.graphql.org/June2018/#sec-Errors
// > If the data entry in the response is not present, the errors entry
// > in the response must not be empty. It must contain at least one error
// This means a payload has to have either a data key or an errors key:
// but the spec leaves room for the combination of data: null plus extensions
// since `data: null` is a *required* output if there was an error during
// execution, but the inverse is not described in the sepc: `data: null`
// does not necessarily indicate that there was an error.
+data: null,
+extensions: PayloadExtensions,
|};

export type GraphQLSingularResponse =
| GraphQLResponseWithData
| GraphQLResponseWithExtensionsOnly
| GraphQLResponseWithoutData;

export type GraphQLResponse = GraphQLSingularResponse | $ReadOnlyArray<GraphQLSingularResponse>;
53 changes: 53 additions & 0 deletions src/RelayRuntimeTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// @flow strict-local

import type { Variables } from 'relay-runtime';
// https://github.com/facebook/relay/blob/master/packages/relay-runtime/util/RelayRuntimeTypes.js

export type DataID = string;

export type RecordProxy = $ReadOnly<{|
copyFieldsFrom: (sourceRecord: RecordProxy) => void,
getDataID: () => string,
getLinkedRecord: (name: string, args?: ?Variables) => ?RecordProxy,
getLinkedRecords: (name: string, args?: ?Variables) => ?$ReadOnlyArray<?RecordProxy>,
getOrCreateLinkedRecord: (name: string, typeName: string, args?: ?Variables) => RecordProxy,
getType: () => string,
getValue: (name: string, args?: ?Variables) => mixed,
setLinkedRecord: (record: RecordProxy, name: string, args?: ?Variables) => RecordProxy,
setLinkedRecords: (
records: $ReadOnlyArray<?RecordProxy>,
name: string,
args?: ?Variables,
) => RecordProxy,
setValue: (value: mixed, name: string, args?: ?Variables) => RecordProxy,
|}>;

export interface RecordSourceProxy {
create(dataID: DataID, typeName: string): RecordProxy;
delete(dataID: DataID): void;
get(dataID: DataID): ?RecordProxy;
getRoot(): RecordProxy;
invalidateStore(): void;
}

export interface ReadOnlyRecordProxy {
getDataID(): DataID;
getLinkedRecord(name: string, args?: ?Variables): ?RecordProxy;
getLinkedRecords(name: string, args?: ?Variables): ?Array<?RecordProxy>;
getType(): string;
getValue(name: string, args?: ?Variables): mixed;
}

export type HandleFieldPayload = {|
// The arguments that were fetched.
+args: Variables,
// The __id of the record containing the source/handle field.
+dataID: DataID,
// The (storage) key at which the original server data was written.
+fieldKey: string,
// The name of the handle.
+handle: string,
// The (storage) key at which the handle's data should be written by the
// handler.
+handleKey: string,
|};
3 changes: 1 addition & 2 deletions src/__flowtests__/CustomQueryRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// @flow

import type { Node } from 'react';
import type { GraphQLTaggedNode } from '@adeira/relay-runtime';

import { QueryRenderer, graphql, createLocalEnvironment } from '../index';
import { QueryRenderer, graphql, createLocalEnvironment, type GraphQLTaggedNode } from '../index';

function placeholder() {
return null;
Expand Down
4 changes: 1 addition & 3 deletions src/__flowtests__/DeclarativeMutationConfig.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @flow

import type { Disposable } from '@adeira/relay-runtime';

import { commitMutation, graphql, createLocalEnvironment } from '../index';
import { commitMutation, graphql, createLocalEnvironment, type Disposable } from '../index';

const environment = createLocalEnvironment();
const mutation = graphql`
Expand Down
4 changes: 1 addition & 3 deletions src/__flowtests__/Disposable.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @flow

import type { Disposable } from '@adeira/relay-runtime';

import { graphql, requestSubscription, type RelayProp } from '../index';
import { graphql, requestSubscription, type RelayProp, type Disposable } from '../index';

type Props = {| +relay: RelayProp |};

Expand Down
7 changes: 3 additions & 4 deletions src/__flowtests__/Environment.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// @flow

import type { Disposable } from '@adeira/relay-runtime';

import {
graphql,
commitMutation,
requestSubscription,
commitLocalUpdate,
createEnvironment,
type RelayProp,
type RefetchRelayProp,
type Disposable,
type PaginationRelayProp,
type RefetchRelayProp,
type RelayProp,
} from '../index';

type PropsA = {| +relay: RelayProp |};
Expand Down
10 changes: 7 additions & 3 deletions src/__flowtests__/mutations.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// @flow

import type { Disposable } from '@adeira/relay-runtime';

import { commitMutation, commitMutationAsync, graphql, createLocalEnvironment } from '../index';
import {
commitMutation,
commitMutationAsync,
graphql,
createLocalEnvironment,
type Disposable,
} from '../index';

const environment = createLocalEnvironment();

Expand Down
4 changes: 1 addition & 3 deletions src/__flowtests__/requestSubscription.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @flow

import type { Disposable } from '@adeira/relay-runtime';

import { requestSubscription, graphql, createLocalEnvironment } from '../index';
import { requestSubscription, graphql, createLocalEnvironment, type Disposable } from '../index';

const environment = createLocalEnvironment();

Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/publicExports.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @flow

import * as publicExports from '../index';

it('exports only what we want to export', () => {
// this is to make sure we are always exporting the subset we want to
expect(Object.keys(publicExports)).toMatchInlineSnapshot(`
Array [
"FetchResponseError",
"FetchTimeoutError",
"createEnvironment",
"createLocalEnvironment",
"createNetworkFetcher",
"getDataFromRequest",
"RelayDebugLogger",
"RelayEagerLogger",
"RelayLazyLogger",
"commitLocalUpdate",
"commitMutation",
"commitMutationAsync",
"createFragmentContainer",
"createPaginationContainer",
"createRefetchContainer",
"fetchQuery",
"LocalQueryRenderer",
"QueryRenderer",
"requestSubscription",
"graphql",
"readInlineData",
"ConnectionHandler",
"RelayEnvironmentProvider",
"useMutation",
"useRelayEnvironment",
]
`);
});
4 changes: 1 addition & 3 deletions src/commitLocalUpdate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// @flow

import { commitLocalUpdate as _commitLocalUpdate } from 'react-relay';
import { commitLocalUpdate as _commitLocalUpdate, type Environment } from 'react-relay';
import type { StoreUpdater } from 'relay-runtime';

import type { Environment } from './runtimeTypes.flow';

/**
* The first parameter `environment` should be from `props.relay.environment`
* to ensure the update is performed in the correct environment.
Expand Down
13 changes: 9 additions & 4 deletions src/createEnvironment.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// @flow

import { RelayLazyLogger } from '@adeira/relay-runtime';
import { Network, Environment as RelayEnvironment, type OperationLoader } from 'relay-runtime';
import {
Network,
Environment as RelayEnvironment,
type OperationLoader,
type Environment,
} from 'relay-runtime';

import createRequestHandler from './createRequestHandler';
import createRelayStore from './createRelayStore';
import type { Environment, RecordMap } from './runtimeTypes.flow';
import createRequestHandler from './createRequestHandler';
import RelayLazyLogger from './loggers/RelayLazyLogger';
import type { RecordMap } from './runtimeTypes.flow';

type Options = {|
+fetchFn: (...args: $ReadOnlyArray<any>) => any,
Expand Down
3 changes: 1 addition & 2 deletions src/createFragmentContainer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// @flow

import type { ComponentType, ElementConfig } from 'react';
import { createFragmentContainer as _createFragmentContainer } from 'react-relay';
import { createFragmentContainer as _createFragmentContainer, type Environment } from 'react-relay';
import { invariant, isObjectEmpty } from '@adeira/js';

import type { FragmentSpec, $RelayProps } from './types.flow';
import type { Environment } from './runtimeTypes.flow';

export type RelayProp = {|
+environment: Environment,
Expand Down
4 changes: 2 additions & 2 deletions src/createLocalEnvironment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @flow

import { RelayLazyLogger } from '@adeira/relay-runtime';
import { Environment as RelayEnvironment } from 'relay-runtime';
import type { Environment } from 'react-relay';

import createRelayStore from './createRelayStore';
import type { Environment } from './runtimeTypes.flow';
import RelayLazyLogger from './loggers/RelayLazyLogger';

/**
* This is just an alternative environment factory for LocalQueryRenderer. There is currently not
Expand Down
7 changes: 3 additions & 4 deletions src/createRefetchContainer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// @flow

import type { ComponentType, ElementConfig } from 'react';
import { createRefetchContainer as _createRefetchContainer } from 'react-relay';
import { createRefetchContainer as _createRefetchContainer, type Environment } from 'react-relay';
import { invariant, isObjectEmpty } from '@adeira/js';
import type { GraphQLTaggedNode, Disposable } from '@adeira/relay-runtime';
import type { ComponentType, ElementConfig } from 'react';
import type { Disposable, GraphQLTaggedNode } from 'relay-runtime';

import type { $RelayProps, FragmentSpec } from './types.flow';
import type { Environment } from './runtimeTypes.flow';

type RefetchOptions = {
+force?: boolean,
Expand Down
Loading

0 comments on commit a8bad2a

Please sign in to comment.