<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amine Elbarry</title>
    <description>The latest articles on DEV Community by Amine Elbarry (@elbarryamine).</description>
    <link>https://dev.to/elbarryamine</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F891935%2F1eab4af1-0a76-45ea-9683-6a6b02dad832.jpg</url>
      <title>DEV Community: Amine Elbarry</title>
      <link>https://dev.to/elbarryamine</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elbarryamine"/>
    <language>en</language>
    <item>
      <title>React Query Implementation And Why Should You Use It</title>
      <dc:creator>Amine Elbarry</dc:creator>
      <pubDate>Sun, 01 Jan 2023 11:59:52 +0000</pubDate>
      <link>https://dev.to/elbarryamine/react-query-implementation-and-why-should-you-use-it-4j73</link>
      <guid>https://dev.to/elbarryamine/react-query-implementation-and-why-should-you-use-it-4j73</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ngpb0zqrlvm75xwaqmo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ngpb0zqrlvm75xwaqmo.png" alt="react-query" width="800" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can view the full article here &lt;a href="https://www.elbarryamine.com/blog/react-query-implementation-and-why-should-you-use-it" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Hi 👋 You, Today, I'm going to discuss React Query and why i am telling you that you should use it.&lt;/p&gt;

&lt;p&gt;React Query is a library that makes managing asynchronous data in React applications easier. It provides a simple API for retrieving, caching, and updating data and makes it simple to handle data that changes over time. In this article, we'll look at the advantages of using React Query as well as how to get started using it in your projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why React Query ?
&lt;/h2&gt;

&lt;p&gt;You may want to use React Query in your React applications for a variety of reasons, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React Query provides a simple API for handling data, making it easier to manage data in your application.&lt;/li&gt;
&lt;li&gt;React Query can improve your application's performance by reducing the number of network requests made and only fetching data when it is required.&lt;/li&gt;
&lt;li&gt;React Query makes it simple to handle errors that may occur when fetching data, such as network or server-side errors. It includes tools for displaying error messages to users as well as retrying failed requests.&lt;/li&gt;
&lt;li&gt;React Query includes tools for debugging and profiling the performance of your data management, making it easier to understand how your application works and identify potential issues.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, React Query can be a helpful tool for managing asynchronous data in your React applications, allowing you to create more performant and reliable applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Let's start by installing React Query&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i @tanstack/react-query&lt;/code&gt;&lt;br&gt;
or&lt;br&gt;
&lt;code&gt;yarn add @tanstack/react-query&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then, in your React components, you can use the useQuery hook to fetch data. The useQuery hook accepts an options object containing the query to be executed as well as any additional parameters, such as variables or query options.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  QueryClient,
  QueryClientProvider,
  useQuery,
} from '@tanstack/react-query'

const queryClient = new QueryClient()

export default function App() {
return (
  &amp;lt;QueryClientProvider client={queryClient}&amp;gt;
    &amp;lt;Example /&amp;gt;
  &amp;lt;/QueryClientProvider&amp;gt;
 )
}

function Example() {
const { isLoading, error, data } = useQuery({
  queryKey: ['get-user'],
  queryFn: () =&amp;gt;fetch('/api/user').then((res) =&amp;gt; res.json())
})
if (isLoading) return 'Loading...'
if (error) return 'An error has occurred: ' + error.message
return (
  &amp;lt;div&amp;gt;
    &amp;lt;h1&amp;gt;{data.user.fullName}&amp;lt;/h1&amp;gt;
    &amp;lt;h1&amp;gt;{data.user.lastName}&amp;lt;/h1&amp;gt;
  &amp;lt;/div&amp;gt;
 )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useQuery hook is used to retrieve a user from an API. i am extracting just three properties for this hook : data, error, and isLoading. You can use these properties to show the user loading indicators, error messages, or the data itself.&lt;/p&gt;

&lt;p&gt;Finally, React Query is an excellent tool for managing asynchronous data in React applications. It simplifies data management, improves performance, and makes handling error states easier. You can build more performant and reliable applications and have a better developer experience by using React Query. I hope this article has given you a good introduction to React Query and inspired you to use it in your own projects.&lt;/p&gt;

&lt;p&gt;you can view the full article here &lt;a href="https://www.elbarryamine.com/blog/react-query-implementation-and-why-should-you-use-it" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you have it, perhaps I was able to assist you a little, Thanks for reading&lt;/p&gt;

&lt;p&gt;Amine Elbarry signing off 👋&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>How To Upload Files With GraphQl And NestJs</title>
      <dc:creator>Amine Elbarry</dc:creator>
      <pubDate>Fri, 29 Jul 2022 21:54:00 +0000</pubDate>
      <link>https://dev.to/elbarryamine/how-to-upload-files-with-nestjs-and-graphql-2iig</link>
      <guid>https://dev.to/elbarryamine/how-to-upload-files-with-nestjs-and-graphql-2iig</guid>
      <description>&lt;h1&gt;
  
  
  Quick demonstration on using GraphQL and Nestjs to upload files (NestJs Code First Approch)
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;Hi 👋 You, Today, I'm going to discuss how to upload files using graphql. You may already be familiar with how to upload files using the Rest API, but now you tried Graphql and you're wondering how to upload your cat pictures.&lt;/p&gt;

&lt;p&gt;If you're using Nest Js and the Code-first GraphQL approch this guide is for you&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;p&gt;Let's start by installing our dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @nestjs/graphql @nestjs/apollo graphql apollo-server-express graphql-upload@14
yarn add @nestjs/graphql @nestjs/apollo graphql apollo-server-express graphql-upload@14
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now everything is installed go to your &lt;strong&gt;app.module.ts&lt;/strong&gt; file and import ApolloDriver, ApolloDriverConfig &amp;amp; GraphQLModule.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { GraphQLModule } from '@nestjs/graphql';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add GraphQLModule config to the App module imports.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Module({
imports: [
  GraphQLModule.forRoot&amp;lt;ApolloDriverConfig&amp;gt;({
  driver: ApolloDriver,
  autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
 }),
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets generate our graphql code now by using nest cli in our terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest g resource cats
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Select GraphQL (code first)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;? What transport layer do you use?
  REST API
  GraphQL (code first)
  GraphQL (schema first)
  Microservice (non-HTTP)
  WebSockets
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a folder inside you &lt;strong&gt;src&lt;/strong&gt; Directory called &lt;strong&gt;cats&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's Code
&lt;/h3&gt;

&lt;p&gt;Now let's start by writing our mutation to create a cat object with an image&lt;/p&gt;

&lt;p&gt;let's start by editing our &lt;strong&gt;createCatInput&lt;/strong&gt; which is imported by our &lt;strong&gt;createCatmutation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@InputType()
export class CreateCatInput {
  @Field(() =&amp;gt; Int, { description: 'Example field (placeholder)' })
  exampleField: number;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our cat will have these properties name, breed &amp;amp; image&lt;/p&gt;

&lt;p&gt;Create a file upload type that we can use for our image field which look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Stream } from 'stream';

export interface FileUpload {
  filename: string;
  mimetype: string;
  encoding: string;
  createReadStream: () =&amp;gt; Stream;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Add The Fields With GraphQLUpload Scalar Type wich is imported from our package &lt;strong&gt;graphql-upload&lt;/strong&gt; which gives us support for GraphQL multipart requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as GraphQLUpload from 'graphql-upload/GraphQLUpload.js';

@InputType()
export class CreateCatInput {
  @Field(() =&amp;gt; String)
  name: string;
  @Field(() =&amp;gt; String)
  breed: string;
  @Field(() =&amp;gt; GraphQLUpload)
  image: Promise&amp;lt;FileUpload&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then head to the cat entity and create a similar type and modify the image field as string so we can return just the filename&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ObjectType()
export class Cat {
  @Field(() =&amp;gt; String)
  name: string;
  @Field(() =&amp;gt; String)
  breed: string;
  @Field(() =&amp;gt; String)
  image: string;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok now we go to our &lt;strong&gt;cats.service.ts&lt;/strong&gt; where we can handle our image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create({ breed, name, image }: CreateCatInput) {
  // your code goes here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will return the breed and name precisely as we received them. &lt;strong&gt;readable stream ( image )&lt;/strong&gt; go ahead and create a new folder, you can name it &lt;strong&gt;upload&lt;/strong&gt;,we will save our images inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async create({ breed, name, image }: CreateCatInput) {
  const { createReadStream, filename } = await image;
  return new Promise(async (resolve) =&amp;gt; {
  createReadStream()
   .pipe(createWriteStream(join(process.cwd(), `./src/upload/${filename}`)))
   .on('finish', () =&amp;gt;
     resolve({
      breed,
      name,
      image: filename,
     }),
   )
   .on('error',() =&amp;gt; {
       new HttpException('Could not save image', HttpStatus.BAD_REQUEST);
    });
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally go to &lt;strong&gt;app.module.ts&lt;/strong&gt; and add GraphQLUpload Middleware&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.js';

const app = await NestFactory.create(AppModule);
app.use(graphqlUploadExpress({ maxFileSize: 1000000, maxFiles: 10 }));
await app.listen(4000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete Reading on my &lt;a href="https://elbarryamine.com/blog/how-to-upload-files-with-nest-js-and-graphql"&gt;portfolio&lt;/a&gt; &lt;/p&gt;

</description>
      <category>node</category>
      <category>graphql</category>
      <category>api</category>
      <category>nest</category>
    </item>
    <item>
      <title>How To Structure Your NextJs Application</title>
      <dc:creator>Amine Elbarry</dc:creator>
      <pubDate>Tue, 19 Jul 2022 17:04:21 +0000</pubDate>
      <link>https://dev.to/elbarryamine/how-to-structure-your-nextjs-application-488c</link>
      <guid>https://dev.to/elbarryamine/how-to-structure-your-nextjs-application-488c</guid>
      <description>&lt;h1&gt;
  
  
  How To Structure Your NextJs Application
&lt;/h1&gt;

&lt;p&gt;A full guide for NextJs to build scalable, production-ready applications&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Next?
&lt;/h2&gt;

&lt;p&gt;As with ReactJs, NextJs (The React Framework for Production) gives you the freedom of how you design your application and where to place your code, it has all the best features that ReactJs lacks, including image and font optimizations, fast rendering, and hybrid static and server rendering. But &lt;strong&gt;with great power comes great responsibility&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you navigate through your app, you frequently pause to consider where to put this or that, and occasionally you find yourself wondering &lt;strong&gt;should I have put this code somewhere else instead ?&lt;/strong&gt; For this reason, you need a scalable production-ready folder structure that neither React nor NextJs ship with. so your question is, how should I structure my code? Don't worry , I have a solution.&lt;/p&gt;

&lt;p&gt;Remember that there are numerous approaches you can take to this. I'm not saying that this is the best. Since each person is unique, you could like another one over mine,only you can decide what is the best for you and your team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Staring Fresh
&lt;/h2&gt;

&lt;p&gt;Let's first look at the structure NextJs provides for us:&lt;/p&gt;

&lt;p&gt;If you started a new NextJs application, you will see certain folders in your root folder. We won't worry about the other files, such as eslint and nextconfig, because they dont matter to us now.&lt;/p&gt;

&lt;p&gt;` &amp;gt; node_modules&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;pages&lt;br&gt;
api&lt;br&gt;
    hello.tsx&lt;br&gt;
  _app.tsx&lt;br&gt;
  index.tsx&lt;br&gt;
public&lt;br&gt;
styles&lt;br&gt;
  .. rest of the files`&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Continue Reading Here &lt;em&gt;&lt;a href="https://elbarryamine.com/blog/how-to-structure-your-nextjs-application"&gt;https://elbarryamine.com/blog/how-to-structure-your-nextjs-application&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
  </channel>
</rss>
