Integrating GraphQL federation into React and Node.js applications can enhance your app’s performance, scalability, and maintainability. This decentralized data graph approach allows you to build independent, connected services, each with its own schema, but all working together seamlessly. Let’s dive into how you can achieve this.
Before we begin, ensure you have Node.js and npm installed. Also, familiarize yourself with GraphQL, React, and Apollo Server, as we’ll be using them throughout this guide.
Setting Up Your Project
First, initialize your project using npm and install necessary dependencies.
In your project root folder, run:
npm init -y
Then, install Apollo Server and GraphQL dependencies:
npm install apollo-server graphql
Creating Your First GraphQL Server
Let’s start by creating a simple GraphQL server using Apollo Server and defining a schema.
In a new file, index.js, write the following:
const { ApolloServer, gql } = require('apollo-server');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello, World!',
},
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
Running Your Server
Start your server by running:
node index.js
Now, you can query ‘hello’ from http://localhost:4000/graphql
Integrating with React
Next, let’s use Apollo Client in our React app to fetch data from our GraphQL server.
First, install Apollo Client and React Apollo Hook:
npm install @apollo/client graphql-tag
Setting Up Apollo Client
In your React app’s index.js, wrap your app with the ApolloProvider component:
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
const client = new ApolloClient({
uri: 'http://localhost:4000/graphql',
cache: new InMemoryCache(),
});
ReactDOM.render(
document.getElementById('root')
);
Using Apollo Client in Components
Now, use the useQuery hook to fetch data:
import { useQuery, gql } from '@apollo/client';
function HelloWorld() {
const { loading, error, data } = useQuery(gql`
query Hello {
hello
}
`);
if (loading) return
Loading...
;
if (error) return
Error :(
;
return
{data.hello}
;
}
Federating Your Services
Now that you have a working GraphQL server and React app, let’s federate your services.
Start by installing the required dependencies:
npm install @apollo/federation @apollo/client
Creating a Gateway
Create a new file, gateway.js, and set up a new Apollo Federation Gateway:
const { ApolloServer } = require('apollo-server');
const { ApolloGateway } = require('@apollo/server-plugin-landing-party/apollo-gateway');
const gateway = new ApolloGateway({
serviceList: [
{ name: 'service1', url: 'http://localhost:4000/graphql' },
{ name: 'service2', url: 'http://localhost:4001/graphql' },
],
});
const server = new ApolloServer({
gateway,
subscriptions: false,
});
server.listen().then(({ url }) => {
console.log(`Gateway ready at ${url}`);
});
Running the Gateway
Start your gateway by running:
node gateway.js
Now, query data from http://localhost:4002/graphql
With this setup, you’ve successfully integrated GraphQL federation into your React and Node.js apps. You can now build independent, connected services, each with its own schema, but all working together seamlessly.
Remember, this is just the beginning. Explore Apollo Federation’s features like shared types, directives, and more to build robust, scalable applications.