DEV Community

Cover image for Getting started with GraphQL in .NET 6 - Part 3 (Consume with ApolloClient React)
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on

Getting started with GraphQL in .NET 6 - Part 3 (Consume with ApolloClient React)

Before your API is ready to consume. You need to update about CORS. I update the Program.cs to have CORS settings. The CORS settings looks like this.

// ...

builder.Services.AddCors(options =>
        builder =>


// ...




// ...
Enter fullscreen mode Exit fullscreen mode

Note: This settings is not best practice for production. Please setting it up correctly for production. More information please read this documentation.

Our API is ready to consume!

Prepare your Front End

Now, let's start we use our FE client. I will not bring the step by step how to code, since the step will be complex.

  • Prepare your tools, you will need Node.js and yarn for run this frontend code. For node you can visit here, for yarn you can use the v1 is here.

  • Download/clone the repository. For the repository, you can visit here:

GitHub logo bervProject / react-graphql-client

React GraphQL Client Example

Getting Started with Create React App

This project was bootstrapped with Create React App.

Available Scripts

In the project directory, you can run:

yarn start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

yarn test

Launches the test runner in the interactive watch mode.
See the section about running tests for more information.

yarn build

Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.
Your app is ready to be deployed!

See the section about deployment for more information.

yarn eject

Note: this is a one-way operation. Once you eject, you can’t go back!

If you aren’t satisfied…

  • Install the dependencies, use yarn install.

  • Run your code. yarn start.

  • Now. Ready to try. :)

  • For demo purpose, you can see this video.


Some codes that you need to understand are here:

  • Setup client url at Index.tsx:
import ReactDOM from 'react-dom';
import {
} from "@apollo/client";
import 'bulma/css/bulma.min.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const client = new ApolloClient({
  uri: 'https://localhost:7298/graphql', // you need setup the URL here, if you have different URL
  cache: new InMemoryCache()

  <ApolloProvider client={client}>
    <App />

// ...
Enter fullscreen mode Exit fullscreen mode
  • Check the query and mutation implementation at Notes.tsx. You can separate the gql to another folder so you will have good code structure.
import { useMemo, useState } from "react";
import { Button, Columns, Form, Heading } from "react-bulma-components";
import { gql, useMutation, useQuery } from "@apollo/client";

const NOTES_QUERY = gql`
  query GET_NOTES {
    notesFromEF {

const NOTES_MUTATION = gql`
  mutation CREATE_NOTE($message: String!) {
    createNote(message: $message) {

function mapToDataNotes(data: any): Array<any> {
  if (data && Array.isArray(data.notesFromEF)) {
    return data.notesFromEF;
  return [];

export default function Notes() {
  const [note, setNote] = useState<string>("");
  const { loading, data, refetch } = useQuery(NOTES_QUERY);
  const [createNote, {loading: loadingAdd }] = useMutation(NOTES_MUTATION);

  const addNote = async () => {
    if (!!note) {
      await createNote({
        variables: {
          message: note
      await refetch();
    } else {

  const getDataList = useMemo(() => mapToDataNotes(data), [data]);

  return (
                onChange={(e) => setNote(}
          <div className="buttons">
              loading={loading || loadingAdd}
              Add New Note
              loading={loading || loadingAdd}
              onClick={async () => {
                await refetch();
              Refresh Data
          <Heading>Note List</Heading>
          <p className="content">
              { => <li key={}>{note.message}</li>)}
Enter fullscreen mode Exit fullscreen mode

Note & Suggestion: You could move and separate the list component if you want it and to have smaller component to handle the mutation.

Backend Code

GitHub logo bervProject / GraphQLNETExample

Part of post:

Next Part Plan

For the next part, I am going to post about deployment the API to Azure Web App. Stay tune!

Thank you

Yey, you are connected to API with this frontend code! Thank you for reading. Any suggestions? Feel free to ask.

Awesome GIF

Top comments (1)

alirezabashiri profile image
Alireza Bashiri

This is underrated, thanks!