Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript Example? #66

Open
adamsoffer opened this issue Jul 20, 2019 · 6 comments
Open

Typescript Example? #66

adamsoffer opened this issue Jul 20, 2019 · 6 comments

Comments

@adamsoffer
Copy link

Are there any typescript examples that use this library that you know of?

@lfades
Copy link
Owner

lfades commented Jul 22, 2019

@adamsoffer No afaik 😢

@n1xn
Copy link

n1xn commented Aug 1, 2019

@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.

@adamsoffer
Copy link
Author

adamsoffer commented Aug 7, 2019

thanks @NikoMontana !

@n1xn
Copy link

n1xn commented Aug 7, 2019

@adamsoffer here is the updated interface for _app.tsx

import { ApolloClient } from "apollo-boost";

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

@Vadorequest
Copy link

@sinclairnick
Copy link

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>>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants