Vercel + NextJS : Client network socket disconnected before secure TLS connection was established

Hello There!

Here is some kind of error coming only on the vercel production mode. It’s working fine in the local development and local production mode.

Can anyone have solution for this?

Error Screenshot:

Here is the code:

import { gql, GraphQLClient } from 'graphql-request';
import { gql as graphTag } from 'graphql-tag';

const endpoint = 'https://graphql.fauna.com/graphql';

export const graphQLClient = new GraphQLClient(endpoint, {
  headers: {
    authorization: `Bearer ${process.env.NEXT_PUBLIC_FAUNADB_SECRET}`,
    'X-Schema-Preview': 'partial-update-mutation',
  },
});

export type FGL_RETURN_TYPE = {
  data: [];
  after?: string;
  before?: string;
  error?: any;
}

export const listAllShops = async (size?: Number, $cursor?: String | null) => {
  const query = gql`
    query allShops($size: Int, $cursor: String) {
      allShops(_size: $size, _cursor: $cursor) {
        data {
          _id
          _ts
          domain
          token
        }
        before
    		after
      }
    }
  `
  return await graphQLClient
    .request(query, {
      size: size??20,
      cursor: $cursor??null
    })
    .then((response) => {
      if(response && response.allShops) {
        return { data: response.allShops.data, before: response.allShops.before, after: response.allShops.after } as FGL_RETURN_TYPE;
      }
      return { data: [] } as FGL_RETURN_TYPE;
    })
    .catch((error) => {
      return { data: [], error: error } as FGL_RETURN_TYPE;
    })
}

If I understand correctly, the shared code is a module that you import and use with a handler function for a Next API function, is that right? It looks like you are creating a client globally and using that in your handler function. We are aware of some connection issues when a client is used like this in Lambda functions, and Vercel is backed by AWS Lambda. Known issues in Fauna drivers | Fauna Documentation

Can you move the creation of your client into the handler call?

I understand that you’re using graphql-request and not the faunadb package client. But this appears to be a broader Lambda issue: [AWS-Lambda] - NetworkingError: Client network socket disconnected before secure TLS connection was established · Issue #3591 · aws/aws-sdk-js · GitHub

One change that you might try, and let us know how it goes, is to export a function that returns the client for each handler call.

export const getGraphQLClient = () => new GraphQLClient(endpoint, {
  headers: {
    authorization: `Bearer ${process.env.NEXT_PUBLIC_FAUNADB_SECRET}`,
    'X-Schema-Preview': 'partial-update-mutation',
  },
});
1 Like

No, @ptpaterson
This solution not work at vercel

and also tried with handler call too but still same issue

import { gql, GraphQLClient } from 'graphql-request';
import { gql as graphTag } from 'graphql-tag';

const endpoint = 'https://graphql.fauna.com/graphql';


export type FGL_RETURN_TYPE = {
  data: [];
  after?: string;
  before?: string;
  error?: any;
}

export const listAllShops = async (size?: Number, $cursor?: String | null) => {
  const query = gql`
    query allShops($size: Int, $cursor: String) {
      allShops(_size: $size, _cursor: $cursor) {
        data {
          _id
          _ts
          domain
          token
        }
        before
    		after
      }
    }
  `
  const graphQLClient = new GraphQLClient(endpoint, {
    headers: {
      authorization: `Bearer ${process.env.NEXT_PUBLIC_FAUNADB_SECRET}`,
      'X-Schema-Preview': 'partial-update-mutation',
    },
  });

  return graphQLClient
    .request(query, {
      size: size??20,
      cursor: $cursor??null
    })
    .then((response) => {
      if(response && response.allShops) {
        return { data: response.allShops.data, before: response.allShops.before, after: response.allShops.after } as FGL_RETURN_TYPE;
      }
      return { data: [] } as FGL_RETURN_TYPE;
    })
    .catch((error) => {
      return { data: [], error: error } as FGL_RETURN_TYPE;
    })
}

Hi @cnviradiya. Were you able to work through this?

I don’t think there is anything we can do on our side, since this appears to be an issue from Vercel’s Lambda implementation. However, if you were able to get it to work, then any advice you can offer the community would be valuable.

Cheers,
Paul