Estaba usando qraphql (biblioteca de JavaScript graphql-request) en un proyecto y me encontré con un typeError. Aquí está el código:
import { request, gql } from 'graphql-request' const graphqlAPI = process.env.NEXT_PUBLIC_GRAPHCMS_ENDPOINT export const getPosts = async () => { const query = gql` query MyQuery { postsConnection { edges { node { author { bio name id photo { url } } createdAt slug title excerpt featuredImage { url } categories { name slug } } } } } ` const result = await request(graphqlAPI, query) return result.postsConnection.edges }El error decía que había un problema con el parámetro del documento de la solicitud.
2 errores:
Quitar el comando
const graphqlAPI = process.env.NEXT_PUBLIC_GRAPHCMS_ENDPOINTy el archivo .env asociado a él. Después de eso usa el siguiente código:
import { gql } from 'graphql-request'; import { GraphQLClient } from 'graphql-request'; export const getPosts = async () => { // new endpoint const graphQLClient = new GraphQLClient( endpoint // here add your endpoint ); const query = gql` query MyQuery { postsConnection { edges { node { author { bio name id photo { url } } createdAt slug title excerpt featuredImage { url } categories { name slug } } } } } ` const result = await graphQLClient.request(query) return result.PostsConnection; }No importa, es porque el Next_endpoint no estaba definido, ¡todo bien ahora!