You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using graphql-code-generator with Next.js and getStaticProps, we have to use ApolloClient to run the queries, like this:
exportconstgetStaticProps=async()=>{const{ data }=awaitclient.query<PageQuery>({query: PageDocument,variables: {siteId: process.env.NEXT_API_SITE_ID,
urlPath,},});
...
}
It would be nice to have a hook generated that injects the client in order to have a cleaner code and avoid importing the document. So the example will become something like this:
exportconstgetStaticProps=async()=>{const{ data }=awaitusePageQuery(client,{siteId: process.env.NEXT_API_SITE_ID,
urlPath,});
...
}
Atm, I'm using a custom plugin to generate additional hooks, but it would be nice to have some opinion on this; maybe it could have been done in a better way... If there's no way to do it yet, it would be a nice feature to include I think :)
Note: in these examples, I need the hooks only in the server-side generated queries, so I didn't put withHooks option, but I have other projects in which I using both... In those cases, I would need to include the plugins 'typescript', 'typescript-operations', 'typescript-react-apollo' (and not using client-preset) and name the queries with the ApolloClient differently... (I don't know how to generate a useQuery hook with the optional apollo client)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
When using graphql-code-generator with Next.js and
getStaticProps
, we have to use ApolloClient to run the queries, like this:It would be nice to have a hook generated that injects the client in order to have a cleaner code and avoid importing the document. So the example will become something like this:
Atm, I'm using a custom plugin to generate additional hooks, but it would be nice to have some opinion on this; maybe it could have been done in a better way... If there's no way to do it yet, it would be a nice feature to include I think :)
Note: in these examples, I need the hooks only in the server-side generated queries, so I didn't put
withHooks
option, but I have other projects in which I using both... In those cases, I would need to include the plugins'typescript', 'typescript-operations', 'typescript-react-apollo'
(and not usingclient-preset
) and name the queries with the ApolloClient differently... (I don't know how to generate auseQuery
hook with the optional apollo client)Here there is my custom plugin:
And in
codegen.ts
Hope is clear what I mean :)
Beta Was this translation helpful? Give feedback.
All reactions