<?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: Marco Fusco</title>
    <description>The latest articles on DEV Community by Marco Fusco (@marco_fusco_1d7b793296d04).</description>
    <link>https://dev.to/marco_fusco_1d7b793296d04</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%2F1315497%2F145f9e17-7fe4-4608-af2d-024b6b764f2e.png</url>
      <title>DEV Community: Marco Fusco</title>
      <link>https://dev.to/marco_fusco_1d7b793296d04</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marco_fusco_1d7b793296d04"/>
    <language>en</language>
    <item>
      <title>Errors and null returned value with React.js Apollo queries and mutations to Node.JS GraphQL API</title>
      <dc:creator>Marco Fusco</dc:creator>
      <pubDate>Fri, 01 Mar 2024 14:41:17 +0000</pubDate>
      <link>https://dev.to/marco_fusco_1d7b793296d04/errors-and-null-returned-value-with-reactjs-apollo-queries-and-mutations-to-nodejs-graphql-api-124</link>
      <guid>https://dev.to/marco_fusco_1d7b793296d04/errors-and-null-returned-value-with-reactjs-apollo-queries-and-mutations-to-nodejs-graphql-api-124</guid>
      <description>&lt;p&gt;Hi, I'm having trouble configuring queries and mutations with React.js, Apollo, and GraphQL. Below I have a component with a query and mutation. These queries and mutations work with GraphiQL, but not with my React app. The query returns undefined, and the mutation gives me a status code 400 error. Within my Node.js API, I have a console.log() statement that prints the passed value to the query, so that query is receiving the request properly, and just isn't returning a value. The mutation also works with GraphiQL, but not with my React app. Below is the code that I have. Any help would be greatly appreciated. Thank you.&lt;/p&gt;

&lt;p&gt;Component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useQuery, useMutation } from "@apollo/client";
import { TEST, CREATE_CLIENT_ACCOUNT } from "../queries/accountQueries";
import TextInputField from "./TextInputField";

function CreateAccountFormWrapper({ name, email, password, confirmPassword, onChangeState }) {
    const { testData, testLoading, testError } = useQuery(TEST, {
        variables: {
            id: 0
        }
    });
    const [ createClientAccount, { data, loading, error }] = useMutation(CREATE_CLIENT_ACCOUNT);

    console.log(testData);

    return (
            &amp;lt;form onSubmit={(e) =&amp;gt; {
                e.preventDefault();

                createClientAccount({
                    variables: {
                        name: name,
                        email: email,
                        password: password
                    }
                })
            }}&amp;gt;
                &amp;lt;div className="flex col"&amp;gt;
                &amp;lt;TextInputField 
                        type={"text"}
                        label={"Name"}
                        value={name}
                        onChangeState={onChangeState}
                    /&amp;gt;
                    &amp;lt;TextInputField 
                        type={"text"}
                        label={"Email"}
                        value={email}
                        onChangeState={onChangeState}
                    /&amp;gt;
                    &amp;lt;TextInputField 
                        type={"password"}
                        label={"Password"}
                        value={password}
                        onChangeState={onChangeState}
                    /&amp;gt;
                    &amp;lt;TextInputField 
                        type={"password"}
                        label={"Confirm password"}
                        value={confirmPassword}
                        onChangeState={onChangeState}
                    /&amp;gt;
                    &amp;lt;input type="submit" className="button centered"/&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/form&amp;gt;
    )
}

export default CreateAccountFormWrapper;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Queries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { gql } from "@apollo/client";

const TEST = gql`
    query Test($id: Int!) {
        test(id: $id) {
            id
        }
    }
`;

const CREATE_CLIENT_ACCOUNT = gql`
    mutation CreateClientAccount($name: String!, $email: String!, $password: String!) {
        createClientAccount(name: $name, email: $email, password: $password) {
            id
            auth
        }
    }
`;

export { TEST, CREATE_CLIENT_ACCOUNT };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Test = new GraphQLObjectType({
    name: 'Test',
    fields: () =&amp;gt; ({
        id: { type: GraphQLInt }
    })
})

const AccountType = new GraphQLObjectType({
    name: 'Account',
    fields: () =&amp;gt; ({
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        email: { type: GraphQLString }
    )}
)}

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        test: {
            type: Test,
            args: {
                id: { type: GraphQLInt }
            },
            resolve(parent, args) {
                console.log(args.id);

                return {
                    id: args.id
                }
            }
        }
    }
)}

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        createClientAccount: {
            type: AccountType,
            args: {
                name: { type: new GraphQLNonNull(GraphQLString) },
                email: { type: new GraphQLNonNull(GraphQLString) },
                password: { type: new GraphQLNonNull(GraphQLString) }
            },
            async resolve(parent, args) {
                return await account.createAccount({
                    name: args.name,
                    email: args.email, 
                    password: args.password
                });
            }
        }
    }
})

module.exports = new GraphQLSchema({
    query: RootQuery,
    mutation: Mutation
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
