I'm using Apollo client for my React front-end to be able to use GraphQL hosted by Laravel Lighthouse.
Normal uses cases work, but I'm now finding myself facing certain cases where I'd like to pre-fetch a GraphQL result on the initial page request instead of having the browser load the page and then send a separate ajax query.
I see that Apollo client supports both "Store rehydration" and "Server-side rendering", but I haven't found any documentation within Lighthouse about how to do it.
I think my Blade view needs to contain something like this:
<script>
window.__APOLLO_STATE__ = '{!!$postsGqlResultJson ?? "null" !!}'; // https://www.apollographql.com/docs/react/performance/server-side-rendering/
</script>
So then the question is: how do I generate the $postsGqlResultJson
?
UPDATE:
Let's say I have in the front-end:
const POSTS = gql`
query Posts($first: Int, $page: Int) {
posts(first: $first, page: $page, orderBy: { field: CREATED_AT, order: DESC }) {
paginatorInfo {
currentPage
hasMorePages
total
}
data {
id
...
}
}
`;
const options = {
variables: {
first: 100,
page: 1,
}
};
const { loading, error, data } = useQuery(query, options);
I would think that Lighthouse might offer some sort of PHP function equivalent of useQuery
, where I can somehow provide those same arguments (the POSTS gql and the options object), and it would return a JSON string of the results data, nested as described in the GQL.
Am I misunderstanding? I really appreciate your help. :-)