9

In my apps, I am using following NPM modules to play with Strapi, GraphQL and Next.js:

In the next step, I am creating Apollo config file, example below:

import { HttpLink } from "apollo-link-http";
import { withData } from "next-apollo";

const config = {
  link: new HttpLink({
    uri: "http://localhost:1337/graphql",
  })
};
export default withData(config);

and then inside a class component, I am using a static method getInitialProps() to fetch data from the Strapi via GraphQL query.

Everything is fine but maybe there is another, better way via React hooks or any other?

piet.t
  • 11,718
  • 21
  • 43
  • 52
Mario Boss
  • 1,784
  • 3
  • 20
  • 43
  • 3
    Take a look at https://github.com/UnlyEd/next-right-now, it's a boilerplate with built-in GraphQL support, you may find it easier to get started with, or could use it as a learning resource. See https://github.com/UnlyEd/next-right-now/blob/master/src/pages/index.tsx#L68 for GraphQL query usage. Also, it uses TypeScript and has built-in GraphQL autocompletion in WebStorm. – Vadorequest Mar 25 '20 at 17:38
  • 1
    Also check out the 1st party examples: https://github.com/zeit/next.js/tree/canary/examples. There are several stripped down graphql examples (they have graphql in their name) – AlexMA Mar 27 '20 at 13:15

2 Answers2

5

I found one more nice hook solution for Next.js and GraphQL.

I want to share it with you. Let's start.

Note: I assume that you have Next.js application already installed. If not please follow this guide.

To build this solution we need:

1. run npm command:

npm install --save @apollo/react-hooks apollo-cache-inmemory apollo-client apollo-link-http graphql graphql-tag isomorphic-unfetch next-with-apollo

2. create Appolo config file, eg. in folder ./config and call it appollo.js. File code below:

import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import withApollo from "next-with-apollo";
import { createHttpLink } from "apollo-link-http";
import fetch from "isomorphic-unfetch";

const GRAPHQL_URL = process.env.BACKEND_URL || "https://api.graphql.url";

const link = createHttpLink({
  fetch,
  uri: GRAPHQL_URL
});

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

3. create _app.js file (kind of wrapper) in ./pages folder with below code:

import React from "react";
import Head from "next/head";
import { ApolloProvider } from "@apollo/react-hooks";
import withData from "../config/apollo";

const App = ({ Component, pageProps, apollo }) => {
  return (
    <ApolloProvider client={apollo}>
      <Head>
        <title>App Title</title>
      </Head>
      <Component {...pageProps} />
    </ApolloProvider>
  )
};

export default withData(App);

4. create reusable query component, eg. ./components/query.js

import React from "react";  
import { useQuery } from "@apollo/react-hooks";

const Query = ({ children, query, id }) => {  
  const { data, loading, error } = useQuery(query, {
    variables: { id: id }
  });

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error: {JSON.stringify(error)}</p>;
  return children({ data });
};

export default Query;

5. create a component for our data fetched via GraphQL

import React from "react";
import Query from "../components/query";
import GRAPHQL_TEST_QUERY from "../queries/test-query";

const Example = () => {  
  return (
    <div>
      <Query query={GRAPHQL_TEST_QUERY} id={null}>
        {({ data: { graphqlData } }) => {
          return (
            <div>
              {graphqlData.map((fetchedItem, i) => {
                return (
                  <div key={fetchedItem.id}>
                    {fetchedItem.name}
                  </div>
                );
              })}
            </div>
          );
        }}
      </Query>
    </div>
  );
};

export default Example;

6. create our GraphQL query inside ./queries/test-query. Note: I assume that we have access to our example data and properties id and name via GraphQL

import gql from "graphql-tag";

const GRAPHQL_TEST_QUERY = gql`
  query graphQLData {
    exampleTypeOfData {
      id
      name
    }
  }
`;

export default GRAPHQL_TEST_QUERY;

7. to display our result create index.js file (homepage) in ./pages folder with below code:

import Example from './components/example';

const Index = () => <div><Example /></div>

export default Index;

That's all.. enjoy and extend this solution as you want..

Mario Boss
  • 1,784
  • 3
  • 20
  • 43
2

I have found one more interestng solution with using apollo-server-micro and lodash

Quick guide:

  1. create Next.js app (example name: next-app) and install required packages

    npm i apollo-server-micro lodash
    
  2. create required files in you Next.js app (next-app)

    • /next-app/pages/api/graphql/index.js
    • /next-app/pages/api/graphql/resolvers.js
    • /next-app/pages/api/graphql/typeDefs.js
  3. add code to index.js

    import { ApolloServer } from 'apollo-server-micro';
    import resolvers from './resolvers';
    import typeDefs from './TypeDef';
    
    const apolloServer = new ApolloServer({
        typeDefs,
        resolvers,
    });
    
    export const config = {
        api: {
            bodyParser: false
        }
    };
    
    export default apolloServer.createHandler({ path: '/api/graphql' });
    
  4. add code to typeDefs.js

    import { gql } from 'apollo-server-micro';
    
    const typeDefs = gql`
        type User {
            id: Int!
            name: String!
            age: Int
            active: Boolean!
        }
        type Query {
            getUser(id: Int): User
        }
    `;
    
    export default typeDefs;
    
  5. add code to resolvers.js

    import lodash from 'lodash/collection';
    
    const users = [
        { id: 1, name: 'Mario', age: 38, active: true },
        { id: 2, name: 'Luigi', age: 40, active: true},
        { id: 3, name: 'Wario', age: 36, active: false }
    ];
    
    const resolvers = {
        Query: {
            getUser: (_, { id }) => {
                return lodash.find(users, { id });
            }
        }
    };
    
    export default resolvers;
    
  6. test your Next.js app (next-app) by running below command and checking graphql URL http://localhost:3000/api/graphql

    npm run dev
    
Mario Boss
  • 1,784
  • 3
  • 20
  • 43