Hello everyone,
I am very new to using Prismic and graphql . So please be nice to me
I wanted to start a prismic project just to get to know how it all works. Therefore I started with just a title element with a uid in Prismic and tried connecting this to the next.js file via graphql.
I know that I get the data of the title I want from prismic but I can not display it in the localhost. It always gives me server errors, saying that something is undefined.
I used the following code. I am very sure that it is obvious for experts what I am doing wrong but I am triing to connect the content since 5 days and i don´t know what to try anymore.
I would very much aprechiate any help.
Thanks in advance.
Code:
import Head from 'next/head'
import React from "react"
import {Link, RichText, Date} from 'prismic-reactjs';
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
useQuery,
gql,
} from "@apollo/client";
import { PrismicLink } from "apollo-link-prismic";
const client = new ApolloClient({
link: PrismicLink({
uri: "https://testsitecustomer.cdn.prismic.io/graphql",
}),
cache: new InMemoryCache()
});
var test = "";
export const document= client.query({
query: gql query{ documentation(lang: "*", uid: "prismic") { title } }
}).then(response => {
console.log("response");
test = response;
console.log(test.data.documentation.title[0].text);
//console.log(response);
}).catch(error => {
console.log("error");
console.error(error);
});
function linkResolver(doc) {
// Define the url depending on the document type
if (doc.type === 'page') {
return '/page/' + doc.uid;
} else if (doc.type === 'blog_post') {
return '/blog/' + doc.uid;
}
// Default to homepage
return '/';
}
export default function Home() {
return (
<>
My test
<main className={styles.main}>
<span>{ test.data.documentation.title[0].text }</span>
</main>
</>
)
}