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 { useCallback } from 'react' | |
import { useMutation } from 'react-query' | |
import { stringify } from 'qs' | |
/* We could read this from some config or env */ | |
const API_ROOT = 'https://api.example.com' | |
/* A helper function to create URL from path and params */ | |
function getUrl(path) { | |
return API_ROOT + path |
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 { useState } from 'react' | |
import useMyMutation from './useMyMutation' // Import custom useMyMutation hook | |
// Mutation variables | |
type CreatePostMutationVariables = { | |
title: string, | |
content: string | |
} | |
// Mutation function |
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
export default function getQueryKey(query, variables) { | |
const { name, path, params } = query(variables) | |
return [name, path, params] | |
} |
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 custom useMyQuery hook | |
import useMyQuery from './useMyQuery' | |
// Query variables types | |
type MyPostQueryVariables = { | |
id: number | |
} | |
// Query function |
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 useMyQuery from './useMyQuery' // Import custom useMyQuery hook | |
import myPostsQuery from './queries/myPostsQuery' // Import query function | |
function MyPosts({ category, status }) { | |
const { isLoading, data } = useMyQuery(myPostsQuery, { | |
variables: { | |
category, | |
status | |
} | |
}) |
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 { useQuery } from 'react-query' | |
function MyPosts({ category, status }) { | |
const path = `https://api.example.com/posts?category=${category}&status=${status}` | |
const { isLoading, data } = useQuery( | |
['posts', category, status], | |
() => fetch(path).then(res => res.json() | |
) | |
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 { useQuery } from 'react-query' | |
import { stringify } from 'qs' | |
/* We could read this from some config or env */ | |
const API_ROOT = 'https://api.example.com' | |
/* A helper function to create URL from path and params */ | |
function getUrl(path, params) { | |
let url = API_ROOT + path | |
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
/* Writing Flow types for query variables will help a lot | |
* to recognize what these variables are | |
*/ | |
type MyPostsQueryVariables = { | |
category?: number, | |
status?: 'published' | 'draft' | |
} | |
/* This function is the Query | |
* that will be passed to our custom useQuery hook |
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
query { | |
categories { | |
id | |
name | |
} | |
stores { | |
id | |
name | |
} |
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 React from 'react' | |
import { gql, useMutation, useQuery } from '@apollo/client' | |
const GET_POSTS = gql` | |
query { | |
posts { | |
id | |
title | |
} | |
} |