Git Product home page Git Product logo

Comments (6)

n1xn avatar n1xn commented on August 18, 2024 14

@adamsoffer I got it working like this

withApolloClient.tsx

import withApollo from "next-with-apollo";
import ApolloClient, { InMemoryCache } from "apollo-boost";

const GRAPHQL_ENDPOINT_DEVELOPMENT = "http://localhost:4000";
const GRAPHQL_ENDPOINT_PRODUCTION = "http://ALIAS.USER.now.sh/API_ENDPOINT";

const GRAPHQL_ENDPOINT =
  process.env.NODE_ENV === "development"
    ? GRAPHQL_ENDPOINT_DEVELOPMENT
    : GRAPHQL_ENDPOINT_PRODUCTION;

export default withApollo(
  ({ ctx, headers, initialState }) =>
    new ApolloClient({
      uri: GRAPHQL_ENDPOINT,
      cache: new InMemoryCache().restore(initialState || {})
    })
);

_app.tsx

import React from "react";
import App, { Container } from "next/app";
import { ApolloProvider } from "react-apollo";
import { ApolloProvider as ApolloHooksProvider } from "react-apollo-hooks";
import withApolloClient from "../lib/withApolloClient";
import { ApolloClient } from "apollo-boost";

interface Props {
  apollo: ApolloClient<{}>;
}

class MyApp extends App<Props> {
  render() {
    const { Component, pageProps, apollo } = this.props;

    return (
      <Container>
        <ApolloProvider client={apollo}>
          <ApolloHooksProvider client={apollo}>
            <Component {...pageProps} />
          </ApolloHooksProvider>
        </ApolloProvider>
        <style jsx global>{`
          body {
            margin: 0;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
              Helvetica, Arial, sans-serif, "Apple Color Emoji",
              /* Emojis*/ "Segoe UI Emoji", /* Emojis*/ "Segoe UI Symbol"; /* Emojis*/
          }
        `}</style>
      </Container>
    );
  }
}

export default withApolloClient(MyApp);

index.tsx

import React from "react";
import styled from "styled-components";
import { Query } from "react-apollo";
import {
  filterPosts,
  filterPostsVariables
} from "../src/graphql/generated/filterPosts";
import { FILTER_POSTS } from "../src/graphql/queries";

class FilterPostsQuery extends Query<filterPosts, filterPostsVariables> {}

const Test = styled.h1`
  color: green;
`;

const Index = () => {
  return (
    <FilterPostsQuery
      query={FILTER_POSTS}
      variables={{ searchString: "te" }}
      ssr={false}
    >
      {({ data, loading }) => {
        if (loading) return <Test>Loading</Test>;
        if (data)
          return <Test>{data.filterPosts && data.filterPosts[0].id}</Test>;
      }}
    </FilterPostsQuery>
  );
};

export default Index;


This example is now using apollo-codegen and I am using ssr prop to switch between logic.

from next-with-apollo.

n1xn avatar n1xn commented on August 18, 2024 2

@adamsoffer here is the updated interface for _app.tsx

import { ApolloClient } from "apollo-boost";

interface Props {
  apollo: ApolloClient<{}>;
}

from next-with-apollo.

Vadorequest avatar Vadorequest commented on August 18, 2024 1

https://github.com/UnlyEd/next-right-now is completely OSS and uses this package, it also uses TS.

See

from next-with-apollo.

lfades avatar lfades commented on August 18, 2024

@adamsoffer No afaik 😢

from next-with-apollo.

adamsoffer avatar adamsoffer commented on August 18, 2024

thanks @NikoMontana !

from next-with-apollo.

sinclairnick avatar sinclairnick commented on August 18, 2024

Use these instead of the next type signatures for getStaticProps and getServerSideProps.

type GetServerSidePropsWithApollo<
  P extends { [key: string]: any } = { [key: string]: any },
  Q extends ParsedUrlQuery = ParsedUrlQuery
> = (
  context: GetServerSidePropsContext<Q> & {
    apolloClient: ApolloClient<InMemoryCache>;
  }
) => Promise<GetServerSidePropsResult<P>>;

type GetStaticPropsWithApollo<
  P extends { [key: string]: any } = { [key: string]: any },
  Q extends ParsedUrlQuery = ParsedUrlQuery
> = (
  context: GetStaticPropsContext<Q> & {
    apolloClient: ApolloClient<InMemoryCache>;
  }
) => Promise<GetStaticPropsResult<P>>;

from next-with-apollo.

Related Issues (20)

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.