Created
May 25, 2021 22:17
-
-
Save YuriFontella/bd96f6e8c12a90f1ecec624042b2fae6 to your computer and use it in GitHub Desktop.
nextjs-graphql-request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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