I was using qraphql (JavaScript graphql-request library) in a project and ran into a typeError. Here's the code:
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
}
The error said there was a problem with the document parameter of the request.
2 Errors:
Remove the command
const graphqlAPI = process.env.NEXT_PUBLIC_GRAPHCMS_ENDPOINT
and the .env file associated with it. After that use the following code:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With