Introduction
Client libraries make it easier to talk to databases by handling complex details for you. They let you focus on your data without worrying about how to send or get it.
Jump into concepts and practice - no test required
const client = new GraphQLClient(endpoint, options); const data = await client.request(query, variables);
import { GraphQLClient } from 'graphql-request'; const client = new GraphQLClient('https://api.example.com/graphql'); const query = `{ user(id: "1") { name email } }`; const data = await client.request(query);
const variables = { id: "2" }; const query = `query getUser($id: ID!) { user(id: $id) { name email } }`; const data = await client.request(query, variables);
import { GraphQLClient } from 'graphql-request'; async function fetchUser() { const client = new GraphQLClient('https://api.spacex.land/graphql/'); const query = `{ company { name founder } }`; const data = await client.request(query); console.log(data); } fetchUser();
query with an object containing the query..then() is valid.const result = await client.query({ query: GET_USERS });
console.log(result.data.users.length);data property containing the query results.result.data.users.length accesses the number of users returned, which is 5.const response = client.query({ query: GET_POSTS });
console.log(response.data.posts);query method returns a promise, so response is a promise, not the data..then().