Skip to content

Instantly share code, notes, and snippets.

@YuriFontella
Created May 25, 2021 22:17
Show Gist options
  • Save YuriFontella/bd96f6e8c12a90f1ecec624042b2fae6 to your computer and use it in GitHub Desktop.
Save YuriFontella/bd96f6e8c12a90f1ecec624042b2fae6 to your computer and use it in GitHub Desktop.
nextjs-graphql-request
import Head from 'next/head'
import { GraphQLClient, gql } from 'graphql-request'
import { useState, useEffect } from 'react'
const Home = () => {
const [data, setData] = useState()
const endpoint = 'http://localhost:4000/graphql'
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
'x-user': 'logged',
},
})
const id = 1
const query = gql`
{
me(id: ${id}) {
name
}
}
`
useEffect(async () => {
setData(await graphQLClient.request(query))
}, [])
return (
<div>
<Head>
<title>Create Next App Graphql</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
{JSON.stringify(data, 2, null)}
</div>
)
}
export default Home
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment