Merge pull request #25914 from ckknight/react-relay-query-null

[@types/react-relay] QueryRendererProps["query"] can be null
This commit is contained in:
Eloy Durán 2018-05-24 00:43:05 +02:00 committed by GitHub
commit 6836579580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -4,6 +4,7 @@
// Matt Martin <https://github.com/voxmatt>
// Eloy Durán <https://github.com/alloy>
// Nicolas Pirotte <https://github.com/npirotte>
// Cameron Knight <https://github.com/ckknight>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.6
@ -76,7 +77,7 @@ export const graphql: GraphqlInterface;
export interface QueryRendererProps {
cacheConfig?: RelayRuntimeTypes.CacheConfig;
environment: RelayRuntimeTypes.Environment;
query: RelayRuntimeTypes.GraphQLTaggedNode;
query?: RelayRuntimeTypes.GraphQLTaggedNode | null;
render(readyState: ReadyState): React.ReactElement<any> | undefined | null;
variables: RelayRuntimeTypes.Variables;
rerunParamExperimental?: RelayRuntimeTypes.RerunParam;

View File

@ -28,16 +28,16 @@ const modernEnvironment = new Environment({ network, store });
// ~~~~~~~~~~~~~~~~~~~~~
// Modern QueryRenderer
// ~~~~~~~~~~~~~~~~~~~~~
const MyQueryRenderer = (props: { name: string }) => (
const MyQueryRenderer = (props: { name: string, show: boolean }) => (
<QueryRenderer
environment={modernEnvironment}
query={graphql`
query={props.show ? graphql`
query ExampleQuery($pageID: ID!) {
page(id: $pageID) {
name
}
}
`}
` : null}
variables={{
pageID: "110798995619330",
}}
@ -52,6 +52,23 @@ const MyQueryRenderer = (props: { name: string }) => (
/>
);
const MyEmptyQueryRenderer = () => (
<QueryRenderer
environment={modernEnvironment}
// NOTE: let's intentionally leave out `query`
// query={undefined}
variables={{}}
render={({ error, props }) => {
if (error) {
return <div>{error.message}</div>;
} else if (props) {
throw new Error('This code path should never be hit');
}
return <div>Loading</div>;
}}
/>
);
// ~~~~~~~~~~~~~~~~~~~~~
// Modern RefetchContainer
// ~~~~~~~~~~~~~~~~~~~~~