Coder Social home page Coder Social logo

apollo-movieweb's Introduction

Frontend, Movie Web built with React, Apollo, GraphQL

The Web connected to MovieQl-Server
The Web made by Apollo

$ yarn add styled-components
$ yarn add react-router-dom
$ yarn add apollo-boost
$ yarn add @apollo-react-hooks
$ yarn add graphql

What i learn

ApolloClient setting & Resolvers & Mutation Usage

  • uri: To connect backend-side
  • resolvers: Resolve/Send serveral thing from client (like field)
import ApolloClient from "apollo-boost";

const client = new ApolloClient({
  uri: "http://localhost:4000/",
  resolvers: {
    // The field name must be same with api (type in db)
    Movie: {},
    // This Mutaion is same with backend-Mutation
    Mutation: {}
    }
  }
});

Query usage from front

  • Using gql``/ to write query
  • Using useQuery(query, variables) to send query
  • query getMovie($id: Int!) {} => query for Apollo
  • movie(id: $id){} => query for graphql
import { gql } from "apollo-boost";

const GET_MOVIE = gql`
    query getMovie($id: Int!) {
        movie(id: $id) {
        id
        }
    }
`;

const { loading, data } = useQuery(GET_MOVIE, {
    variables: { id: parseInt(id) }

useParams()

import { useParams } from "react-router-dom";

export default = () => {
    const { id } = useParams();

    ~
}

<a href/> & Link

  • <a href> kill app, you must use <Link />
import { Link } from "react-router-dom";

// https://localhost:4000 => https://localhost:4000/#/${id}
<Link to={`/${id}`}> 
    ~
</Link>

Optional Chaining (JS Grammer)

// {data && data.movie && data.movie.medium_cover_image}
<Poster bg={data?.movie?.medium_cover_image}></Poster>

Using Local-State

  • Using Local-State(data) with Backend-State(data)
  • @client make query only send to client-side (telegraph ql)
  • To use Local-State from different page, you must get id by query => Apollo distinguish by id
// Apollo.js

const client = new ApolloClient({
  uri: "http://localhost:4000/",
  resolvers: {
    
    // isLiked is apply on Local-State 
    Movie: {
      isLiked: () => false
    },

    // cache: local cache of Apollo 
    // cache.writeData: Write data on 'local'
    Mutation: {
      togglelikeMovie: (_, { id, isLiked }, { cache }) => {
        cache.writeData({
          id: `Movie:${id}`,
          data: {
            isLiked: !isLiked
          }
        });
      }
    }
  }
});
// Home.js

const GET_MOVIES = gql`
  {
    movies {
      id
      medium_cover_image
      isLiked @client
    }
  }
`;
// Detail.js

const GET_MOVIE = gql`
  query getMovie($id: Int!) {
    movie(id: $id) {
      id
      title
      medium_cover_image
      description_intro
      isLiked @client
    }
  }
`;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.