Better relay types

This commit is contained in:
Chi Vinh Le
2018-07-04 15:01:47 -03:00
parent 95bf54366c
commit 9f79ee1c6d
8 changed files with 42 additions and 35 deletions
@@ -6,7 +6,7 @@ import { InferableComponentEnhancerWithProps } from "recompose";
* from Relay.
*/
export default <T>(
fragmentSpec: GraphQLTaggedNode
fragmentSpec: { [P in keyof T]: GraphQLTaggedNode }
): InferableComponentEnhancerWithProps<T, { [P in keyof T]: any }> => (
component: React.ComponentType<any>
) => createFragmentContainer(component, fragmentSpec) as any;
@@ -1,6 +1,11 @@
import * as React from "react";
import { compose, hoistStatics, InferableComponentEnhancer } from "recompose";
import { CSelector, CSnapshot, Environment } from "relay-runtime";
import {
CSelector,
CSnapshot,
Environment,
GraphQLTaggedNode,
} from "relay-runtime";
import { withContext } from "../bootstrap";
@@ -25,7 +30,7 @@ export const LOCAL_ID = "client:root.local";
* must have the `LOCAL_ID`.
*/
function withLocalStateContainer<T>(
fragmentSpec: any
fragmentSpec: GraphQLTaggedNode
): InferableComponentEnhancer<{ local: T }> {
return compose(
withContext(({ relayEnvironment }) => ({ relayEnvironment })),
@@ -33,7 +38,7 @@ function withLocalStateContainer<T>(
class LocalStateContainer extends React.Component<Props, any> {
constructor(props: Props) {
super(props);
const fragment = fragmentSpec.data().default;
const fragment = (fragmentSpec as any).data().default;
if (fragment.kind !== "Fragment") {
throw new Error("Expected fragment");
}
@@ -24,7 +24,7 @@ type ConnectionConfig<T> = Overwrite<
* from Relay.
*/
export default <T, InnerProps>(
fragmentSpec: GraphQLTaggedNode,
fragmentSpec: { [P in keyof T]: GraphQLTaggedNode },
connectionConfig: ConnectionConfig<InnerProps>
): InferableComponentEnhancerWithProps<
T & { relay: RelayPaginationProp },
@@ -10,7 +10,7 @@ import { InferableComponentEnhancerWithProps } from "recompose";
* from Relay.
*/
export default <T>(
fragmentSpec: GraphQLTaggedNode,
fragmentSpec: { [P in keyof T]: GraphQLTaggedNode },
refetchQuery: GraphQLTaggedNode
): InferableComponentEnhancerWithProps<
T & { relay: RelayRefetchProp },
@@ -15,8 +15,8 @@ const AppContainer: StatelessComponent<InnerProps> = props => {
return <App {...props.data} />;
};
const enhanced = withFragmentContainer<{ data: Data }>(
graphql`
const enhanced = withFragmentContainer<{ data: Data }>({
data: graphql`
fragment AppContainer on Query
@argumentDefinitions(
assetID: { type: "ID!" }
@@ -29,8 +29,8 @@ const enhanced = withFragmentContainer<{ data: Data }>(
...StreamContainer_asset
}
}
`
)(AppContainer);
`,
})(AppContainer);
export type AppContainerProps = PropTypesOf<typeof enhanced>;
export default enhanced;
@@ -16,8 +16,8 @@ const AssetListContainer: StatelessComponent<InnerProps> = props => {
return <AssetList assets={assets} />;
};
const enhanced = withFragmentContainer<{ assets: Data }>(
graphql`
const enhanced = withFragmentContainer<{ assets: Data }>({
assets: graphql`
fragment AssetListContainer_assets on AssetsConnection {
edges {
node {
@@ -26,8 +26,8 @@ const enhanced = withFragmentContainer<{ assets: Data }>(
}
}
}
`
)(AssetListContainer);
`,
})(AssetListContainer);
export type AssetListContainerProps = PropTypesOf<typeof enhanced>;
export default enhanced;
@@ -14,16 +14,16 @@ const CommentContainer: StatelessComponent<InnerProps> = props => {
return <Comment {...rest} {...props.data} />;
};
const enhanced = withFragmentContainer<{ data: Data }>(
graphql`
const enhanced = withFragmentContainer<{ data: Data }>({
data: graphql`
fragment CommentContainer on Comment {
author {
username
}
body
}
`
)(CommentContainer);
`,
})(CommentContainer);
export type CommentContainerProps = PropTypesOf<typeof enhanced>;
export default enhanced;
@@ -45,26 +45,28 @@ class StreamContainer extends React.Component<InnerProps> {
}
const enhanced = withPaginationContainer<{ asset: Data }, InnerProps>(
graphql`
fragment StreamContainer_asset on Asset
@argumentDefinitions(
count: { type: "Int", defaultValue: 5 }
cursor: { type: "Cursor" }
orderBy: { type: "COMMENT_SORT", defaultValue: CREATED_AT_DESC }
) {
id
isClosed
comments(first: $count, after: $cursor, orderBy: $orderBy)
@connection(key: "Stream_comments") {
edges {
node {
id
...CommentContainer
{
asset: graphql`
fragment StreamContainer_asset on Asset
@argumentDefinitions(
count: { type: "Int", defaultValue: 5 }
cursor: { type: "Cursor" }
orderBy: { type: "COMMENT_SORT", defaultValue: CREATED_AT_DESC }
) {
id
isClosed
comments(first: $count, after: $cursor, orderBy: $orderBy)
@connection(key: "Stream_comments") {
edges {
node {
id
...CommentContainer
}
}
}
}
}
`,
`,
},
{
direction: "forward",
getConnectionFromProps(props) {