graphql-ts-client icon indicating copy to clipboard operation
graphql-ts-client copied to clipboard

Use outside of react/apollo

Open bkniffler opened this issue 2 years ago • 2 comments

Currently, the generated code is tightly coupled with react. Would be amazing to also use outside of react by maybe outsourcing the react hooks and specific wrappers to another library. Would make it easier to use different clients under the hood also like urql.

If decoupled, it could look something like: Bildschirmfoto 2021-08-19 um 15 41 18

bkniffler avatar Aug 19 '21 13:08 bkniffler

Yes, I have thought about this problem, but there are so many frameworks that users may adopt, and it is impossible to exhaust them one by one. Therefore, the current design is like this

In the NodeJS file of the code generator, 3 generators are supported, user can choose one of them

  1. ApolloGenerator: Generates code with react hook & apollo. User need to import'graphql' and'@apollo/client'

  2. AsyncGenerator: Generates async methods that only with network fetch. No local cache, no state manangement, no react dependency. User need to import 'graphql-request'. Please see async demo to know more

  3. Generator: Only generates types for object, interface, union and input, ignore queris and mutations and they must be implemented by user.

In fact, there is another way that users extend the code generator themselves and create a new derived class from Generator (in fact, both ApolloGenerator and AsyncGenerator inherit from Generator), but this is too difficult for users

babyfish-ct avatar Aug 19 '21 18:08 babyfish-ct

Fwiw: I have a use case where I would like to use Apollo in a NextJS React project, but I don't want to use the hooks provided.

I would like to use the Apollo Client directly outside of the React element tree, inside of NextJS's getStaticPaths.

I'm not sure if this is possible with the current exports from the Apollo file.

hcharley avatar Dec 17 '21 22:12 hcharley