Query data and error are undefined
See original GitHub issueIntended outcome:
I’m using useQuery
to query and render some data. The loading
prop works as expected, the value is true
and then changes to false
. When loading is false, data
and error
are both undefined
.
I checked the network tab and the data is being received (I see the data
prop)
I also checked what’s going on with the Apollo chrome extension and I can see the data
I also was able to verify the correct result from the BE using the fetch
API
Actual outcome:
data
and error
props from useQuery
are undefined
.
How to reproduce the issue:
Here’s the component that uses useQuery
and fetch
const QUERY = gql`
query AccountStatus {
accountStatus {
__typename
missingCode
completed
reason
}
}
`
const MissingThings = () => {
const x = useQuery(QUERY)
const { loading, data, error } = x
console.log('--x', x)
useEffect(() => {
fetch(`${process.env.REACT_APP_GRAPHQL_URL}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization:
'Bearer <TOKEN>',
},
body: JSON.stringify({
variables: {},
query: `
query AccountStatus {
accountStatus {
__typename
missingCode
completed
reason
}
}
`,
}),
})
.then((result) => result.json())
.then((result) => console.log('--result', result))
.catch(console.error)
}, [])
if (loading) {
return null
}
if (error) {
console.log('--error', error)
return null
}
console.log('--data', data)
return <div>All good</div>
}
And this is the Apollo Client
const ApolloClientProvider = ({ children }: any) => {
const { auth, account } = useGlobalProvider()
const headers =
auth && account ? { Authorization: `Bearer ${auth?.token}` } : {}
console.log('--headers', headers)
console.log('--auth', auth)
const wsLink = new WebSocketLink({
uri: process.env.REACT_APP_GRAPHQL_WS_URL as string,
options: {
reconnect: true,
connectionParams: () => ({
headers,
}),
},
})
const httpLink = new HttpLink({ uri: process.env.REACT_APP_GRAPHQL_URL })
const authMiddleware = new ApolloLink((operation, forward) => {
// add the authorization to the headers
operation.setContext({
headers:
auth && account ? { Authorization: `Bearer ${auth?.token}` } : {},
})
return forward(operation)
})
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query)
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
)
},
wsLink,
httpLink,
)
const logoutLink = onError((error) => {
console.log('APOLLO ERROR!', error)
if (
error.networkError?.message.includes('JWTExpired') ||
error.graphQLErrors?.some(
({ extensions, message }) =>
extensions?.code === 'invalid-jwt' || message.includes('JWTExpired'),
)
) {
navigate('/logout')
}
})
const client = new ApolloClient({
cache: new InMemoryCache(),
link: ApolloLink.from([logoutLink, authMiddleware, splitLink]),
})
return <ApolloProvider client={client}>{children}</ApolloProvider>
}
Versions System: OS: macOS 11.2.3 Binaries: Node: 14.8.0 - ~/n/bin/node npm: 6.14.7 - ~/n/bin/npm Browsers: Chrome: 90.0.4430.72 Safari: 14.0.3 npmPackages: @apollo/client: ^3.3.15 => 3.3.15
Issue Analytics
- State:
- Created 2 years ago
- Reactions:27
- Comments:41 (6 by maintainers)
Top GitHub Comments
Why is this closed?
This should be resolved in @apollo/client@latest - let us know if not.