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 } from '@apollo/client' | |
const UPDATE_TWEET = gql` | |
mutation ($id: ID!, $input: UpdateTweetInput!) { | |
updateTweet (id: $id, input: $input) { | |
id | |
text | |
is_liked | |
} |
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 { gql } from '@apollo/client' | |
export const GET_TODOS = gql` | |
query { | |
todos { | |
id | |
title | |
completed | |
} | |
} |
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 { useQuery } from '@apollo/client' | |
import { GET_TODOS } from './queries' | |
import Todo from './Todo' | |
export default function Todos() { | |
const { data, loading } = useQuery(GET_TODOS) | |
const todos = data?.todos || [] |
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 { useMutation } from '@apollo/client' | |
import { CREATE_TODO, GET_TODOS } from './queries' | |
export default function CreateTodo() { | |
const [title, setTitle] = useState('') | |
const [createTodo] = useMutation(CREATE_TODO) | |
const handleSubmit = async (event) => { |
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 | |
} | |
} |
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
/* 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
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
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 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 | |
} | |
}) |