diff --git a/react-relay/react-relay-test.tsx b/react-relay/react-relay-test.tsx new file mode 100644 index 0000000000..e6632e76f9 --- /dev/null +++ b/react-relay/react-relay-test.tsx @@ -0,0 +1,46 @@ +/// +/// + +import * as React from "react" +import * as Relay from "react-relay" + +interface Props { + text: string + userId: string +} + +interface Response { +} + +export default class AddTweetMutation extends Relay.Mutation { + + getMutation () { + return Relay.QL`mutation{addTweet}` + } + + getFatQuery () { + return Relay.QL` + fragment on AddTweetPayload { + tweetEdge + user + } + ` + } + + getConfigs () { + return [{ + type: "RANGE_ADD", + parentName: "user", + parentID: this.props.userId, + connectionName: "tweets", + edgeName: "tweetEdge", + rangeBehaviors: { + "": "append", + }, + }] + } + + getVariables () { + return this.props + } +} diff --git a/react-relay/react-relay.d.ts b/react-relay/react-relay.d.ts new file mode 100644 index 0000000000..1a0ae89069 --- /dev/null +++ b/react-relay/react-relay.d.ts @@ -0,0 +1,112 @@ +// Type definitions for react-relay 0.9.2 +// Project: https://github.com/facebook/relay +// Definitions by: Johannes Schickling +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module "react-relay" { + import * as React from "react"; + + /** Fragments are a hash of functions */ + interface Fragments { + [query: string]: ((variables?: RelayVariables) => string) + } + + interface CreateContainerOpts { + initialVariables?: Object + fragments: Fragments + prepareVariables?(prevVariables: RelayVariables): RelayVariables + } + + interface RelayVariables { + [name: string]: any + } + + /** add static getFragment method to the component constructor */ + interface RelayContainerClass extends React.ComponentClass { + getFragment: ((q: string) => string) + } + + interface RelayQueryRequestResolve { + response: any + } + + interface RelayMutationRequest { + getQueryString(): string + getVariables(): RelayVariables + resolve(result: RelayQueryRequestResolve): any + reject(errors: any): any + } + + interface RelayQueryRequest { + resolve(result: RelayQueryRequestResolve): any + reject(errors: any): any + + getQueryString(): string + getVariables(): RelayVariables + getID(): string + getDebugName(): string + } + + interface RelayNetworkLayer { + supports(...options: string[]): boolean + } + + class DefaultNetworkLayer implements RelayNetworkLayer { + constructor(host: string, options: any) + supports(...options: string[]): boolean + } + + function createContainer(component: React.ComponentClass, params?: CreateContainerOpts): RelayContainerClass + function injectNetworkLayer(networkLayer: RelayNetworkLayer): any + function isContainer(component: React.ComponentClass): boolean + function QL(...args: any[]): string + + class Route { + constructor(params?: RelayVariables) + } + + /** + * Relay Mutation class, where T are the props it takes and S is the returned payload from Relay.Store.update. + * S is typically dynamic as it depends on the data the app is currently using, but it's possible to always + * return some data in the payload using REQUIRED_CHILDREN which is where specifying S is the most useful. + */ + class Mutation { + props: T + + constructor(props: T) + static getFragment(q: string): string + } + + interface Transaction { + getError(): Error + Status(): number + } + + interface StoreUpdateCallbacks { + onFailure?(transaction: Transaction): any + onSuccess?(response: T): any + } + + interface Store { + commitUpdate(mutation: Mutation, callbacks?: StoreUpdateCallbacks): any + } + + var Store: Store + + class RootContainer extends React.Component {} + + interface RootContainerProps extends React.Props{ + Component: RelayContainerClass + route: Route + renderLoading?(): JSX.Element + renderFetched?(data: any): JSX.Element + renderFailure?(error: Error, retry: Function): JSX.Element + } + + interface RelayProp { + variables: any + setVariables(variables: Object): void + } +}