File tree Expand file tree Collapse file tree 2 files changed +32
-23
lines changed Expand file tree Collapse file tree 2 files changed +32
-23
lines changed Original file line number Diff line number Diff line change 11import { useMemo } from "react" ;
2- import { GraphQLSchema } from "graphql" ;
2+ import { GraphQLSchema , DocumentNode } from "graphql" ;
33import graphql from "reactive-graphql" ;
4- import gql from "graphql-tag" ;
54
65import useObservable from "./useObservable" ;
76
7+ type ReactiveGraphQLResponse = {
8+ data : any ;
9+ } ;
10+
811export default function getReactiveGraphqlReact ( schema : GraphQLSchema ) {
9- return function reactiveGraphqlReact ( query : string , context ?: Object ) {
10- const observable = useMemo (
11- ( ) =>
12- graphql (
13- gql `
14- ${ query }
15- ` ,
16- schema ,
17- context || { }
18- ) ,
19- [ query , JSON . stringify ( context ) ]
20- ) ;
12+ if ( ! ( schema instanceof GraphQLSchema ) ) {
13+ throw new Error ( "schema needs to be a GraphQLSchema" ) ;
14+ }
2115
22- return useObservable ( observable , null ) ;
16+ return function useReactiveGraphqlReact (
17+ query : DocumentNode ,
18+ context ?: Object
19+ ) {
20+ const observable = useMemo ( ( ) => graphql ( query , schema , context || { } ) , [
21+ query ,
22+ JSON . stringify ( context )
23+ ] ) ;
24+
25+ const [ data , errors ] = useObservable < ReactiveGraphQLResponse , any [ ] > (
26+ observable ,
27+ null
28+ ) ;
29+ return [ data === null ? data : data . data , errors ] ;
2330 } ;
2431}
Original file line number Diff line number Diff line change 11import { useState , useEffect } from "react" ;
2+ import { Observable } from "rxjs" ;
23
3- export default function useObservable ( observable$ , initialValue ) {
4+ export default function useObservable < T , E > (
5+ observable$ : Observable < T > ,
6+ initialValue : T | null = null
7+ ) : [ T , E ] {
48 const [ value , update ] = useState ( initialValue ) ;
59 const [ error , updateError ] = useState ( null ) ;
610
7- useEffect (
8- ( ) => {
9- const s = observable$ . subscribe ( update , updateError ) ;
10- return ( ) => s . unsubscribe ( ) ;
11- } ,
12- [ observable$ ]
13- ) ;
11+ useEffect ( ( ) => {
12+ const s = observable$ . subscribe ( update , updateError ) ;
13+
14+ return ( ) => s . unsubscribe ( ) ;
15+ } , [ observable$ ] ) ;
1416
1517 return [ value , error ] ;
1618}
You can’t perform that action at this time.
0 commit comments