nhost icon indicating copy to clipboard operation
nhost copied to clipboard

Replace nhost js GraphQL Client with a third party GraphQL Client

Open elitan opened this issue 4 years ago • 8 comments

The current GraphQL Client in our js SDK (nhost.grapqhl) is our custom-built client with minimal features.

We should consider replacing it with a third-party GraphQL client with support for more features and better maintenance.

Some alternatives are:

  • graphql-request: https://github.com/prisma-labs/graphql-request
  • urql: https://formidable.com/open-source/urql/docs/basics/core/
  • apollo/client: https://www.apollographql.com/docs/react/api/core/ApolloClient

elitan avatar Nov 22 '22 11:11 elitan

@elitan If you do a request with the built-in client of Nhost JS SDK:

const QUERY_GET_ALL_USER = gql
  query QueryGetAllUser {
    users {
      email
      displayName
      id
      emailVerified
      createdAt
      roles {
        role
      }
    }
  }

const { data, error } = await client.graphql.request(QUERY_GET_ALL_USER)

And got the following error: Error: incorrect response data from GraphQL server

Seems that the default accept-encoding header that Axios uses in a node environment causes issues. If you add "accept-encoding: null" to headers everything is working like expected.

const { data, error } = await client.graphql.request(QUERY_GET_ALL_USER, {}, {
  headers: {
    'accept-encoding': null
  }
})

If think the best solution is to use "apollo/client" as third party client for GraphQL.

maximilian-schwarz avatar Nov 26 '22 17:11 maximilian-schwarz

@elitan If you do a request with the built-in client of Nhost JS SDK:

const QUERY_GET_ALL_USER = gql
  query QueryGetAllUser {
    users {
      email
      displayName
      id
      emailVerified
      createdAt
      roles {
        role
      }
    }
  }

const { data, error } = await client.graphql.request(QUERY_GET_ALL_USER)

And got the following error: Error: incorrect response data from GraphQL server

Seems that the default accept-encoding header that Axios uses in a node environment causes issues. If you add "accept-encoding: null" to headers everything is working like expected.

const { data, error } = await client.graphql.request(QUERY_GET_ALL_USER, {}, {
  headers: {
    'accept-encoding': null
  }
})

If think the best solution is to use "apollo/client" as third party client for GraphQL.

I can confirm this issue. The fix also works.

st3phan avatar Nov 26 '22 19:11 st3phan

Thanks for the info. It will help us prioritize this issue.

I think we should use graphql-request instead of urql or @apollo/client because we want a minimal and thin client with no advanced features.

elitan avatar Nov 27 '22 10:11 elitan

Looks good 👍🏻 any plans for the release? 🤗

maximilian-schwarz avatar Nov 27 '22 11:11 maximilian-schwarz

A small comparison:

Library Bundle size Npm weekly downloads Easy to begin with Codegen
graphql-request 7.9kB 2,447K + Vanilla
@apollo/client 42.6kB 767K - Vanilla, React, Vue, Angular, Stencil, Svelte, Nextjs
urql 8kB 142K + React, Vue

I feel urql is a bit too react-driven and not as generic as graphql-request. I would go for graphql-request too. Moreover:

  • we already have a generic @nhost/apollo client
  • as we are working on a @nhost/react-urql library, we could consider adding a @nhost/urql library at a later stage so the client can be reused with other frameworks

plmercereau avatar Nov 27 '22 21:11 plmercereau

For a base, graphql-request is good for thinnest possible.

Urql I'm looking at using for my future projects. Apollo cache and its complexity (too smart for its own good with normalization) has annoyed me enough, besides being a fat chunky package. Urql is much lighter and does caching in minimal and predictable ways. +1 to @nhost/react-urql being actively maintained

fungilation avatar Dec 23 '22 23:12 fungilation

Hi, for vue 2/3 did you condiser the library vue-apollo? Its really nice to work with and its supports "new" composition API.

SymonRod avatar Feb 20 '23 17:02 SymonRod

We've decided not to do this for now.

elitan avatar Feb 26 '23 18:02 elitan