diff --git a/examples/react/basic-graphql-request/src/index.tsx b/examples/react/basic-graphql-request/src/index.tsx index af4e81a8d4..64d9d020f5 100644 --- a/examples/react/basic-graphql-request/src/index.tsx +++ b/examples/react/basic-graphql-request/src/index.tsx @@ -14,13 +14,13 @@ const endpoint = 'https://graphqlzero.almansi.me/api' const queryClient = new QueryClient() type Post = { - id: number + id: string title: string body: string } function App() { - const [postId, setPostId] = React.useState(-1) + const [postId, setPostId] = React.useState(null) return ( @@ -34,7 +34,7 @@ function App() { loading sequences)

- {postId > -1 ? ( + {postId !== null ? ( ) : ( @@ -71,7 +71,7 @@ function usePosts() { function Posts({ setPostId, }: { - setPostId: React.Dispatch> + setPostId: React.Dispatch> }) { const queryClient = useQueryClient() const { status, data, error, isFetching } = usePosts() @@ -116,26 +116,26 @@ function Posts({ ) } -function usePost(postId: number) { +function usePost(postId: string) { return useQuery({ queryKey: ['post', postId], queryFn: async () => { const { post } = await request<{ post: Post }>( endpoint, gql` - query { - post(id: ${postId}) { - id - title - body + query Post($id: ID!) { + post(id: $id) { + id + title + body + } } - } `, + { id: postId }, ) return post }, - enabled: !!postId, }) } @@ -143,19 +143,19 @@ function Post({ postId, setPostId, }: { - postId: number - setPostId: React.Dispatch> + postId: string + setPostId: React.Dispatch> }) { const { status, data, error, isFetching } = usePost(postId) return (
- {!postId || status === 'pending' ? ( + {status === 'pending' ? ( 'Loading...' ) : status === 'error' ? ( Error: {error.message}