Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when the query is configured in polling the loading is set to false in the next function #160

Merged
merged 1 commit into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions examples/relay-hook-example/todo/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import * as React from 'react';

import {useState} from 'react';
import { useState } from 'react';

//import {useLazyLoadQuery, RelayEnvironmentProvider} from 'react-relay/hooks';

Expand All @@ -31,15 +31,16 @@ import {
Store,
type RequestNode,
type Variables,
Observable
} from 'relay-runtime';

import TodoApp, {fragmentSpec} from './components/TodoApp';
import TodoApp, { fragmentSpec } from './components/TodoApp';
//import { useQuery, RelayEnvironmentProvider } from 'relay-hooks';

import TodoTextInput from './components/TodoTextInput';
import type {appQueryResponse} from 'relay/appQuery.graphql';
import type { appQueryResponse } from 'relay/appQuery.graphql';
import QueryApp from './query/QueryApp';

/*
async function fetchQuery(
operation: RequestNode,
variables: Variables,
Expand All @@ -56,14 +57,41 @@ async function fetchQuery(
});

return response.json();
}*/

function fetchQuery(
operation,
variables,
) {
return Observable.create((sink) => {
fetch('http://localhost:3000/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: operation.text,
variables,
}),
})
.then(response => response.json())
.then(data => {
if (data.errors) {
sink.error(data.errors);
return
}
sink.next(data);
sink.complete();
});
})
}

const modernEnvironment: Environment = new Environment({
network: Network.create(fetchQuery),
store: new Store(new RecordSource()),
});

const AppTodo = function(appProps) {
const AppTodo = function (appProps) {
const [userId, setUserId] = useState('me');

console.log('renderer apptodo', userId);
Expand Down Expand Up @@ -110,19 +138,21 @@ const AppTodo = function(appProps) {
};
const isServer = typeof window === 'undefined';
const skip = isServer;
const LayoutTodo = ({userId}) => {
const LayoutTodo = ({ userId }) => {
console.log('LayoutTodo', userId, isServer);
const {data, retry, error, isLoading} = useQuery(
const { data, retry, error, isLoading } = useQuery(
QueryApp,
{userId},
{ userId },
{
fetchPolicy: 'store-or-network',
skip
skip,
networkCacheConfig: { force: true, poll: isServer ? undefined : 1 * 60 * 1000 }
},
);

if(isLoading || skip) {
console.log('loading', isLoading, skip);
console.log('loading', isLoading, skip);
if (isLoading || skip) {

return <div>loading</div>
} else if (error) {
return (
Expand All @@ -140,7 +170,7 @@ const LayoutTodo = ({userId}) => {

const App = (
<RelayEnvironmentProvider environment={modernEnvironment}>
<AppTodo />
<AppTodo />
</RelayEnvironmentProvider>
);

Expand Down
1 change: 1 addition & 0 deletions src/FetchResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export function fetchResolver({
next: () => {
const store = environment.lookup(operation.fragment);
promise = null;
operation.request.cacheConfig?.poll && updateLoading(false);
resolveNetworkPromise();
onNext(operation, store);
},
Expand Down