Search code examples
reactjstypescriptaxiosgraphqlmutation

Mutation graphql calling from react


When I call from client to server, 400 Error occured. I completely don't know where should I check.

When I called just "id" then, it works. however, when I added a more parameter "parentId", it doesn't work.

Any idea?

Apollograhpql (Server)

** schema.gql **
type Mutation {
  updatePackaging(id: String!, parentId: String): Packaging!
}

** ProjectResolver.ts **
@Mutation(() => Packaging)
async updatePackaging(
  @Arg("id") id: string,
  @Arg("parentId", { nullable: true }) parentId: string,
  @Ctx() context: Context
) {
  console.log(`씨발!!! ${id}, ${parentId}`);

  return {
    id,
    parentId,
  };
}

Client (React + ts)

# This works!
export const fetchUpdatePackaging = (packaging: types.Packaging) =>
  http.post<{ updatePackaging: types.Packaging | null }>("/", {
    query: `
      mutation UpdatePackaging($id: String!) {
        updatePackaging(id: $id) {
          id
        }
      }
    `,
    variables: {
      id: packaging.id,
    },
  });


# This DOSE NOT Works for same API (400 Bad Request). WHY???????
# This just added a more parameter : parentId
export const fetchUpdatePackaging = (packaging: types.Packaging) =>
  http.post<{ updatePackaging: types.Packaging | null }>("/", {
    query: `
      mutation UpdatePackaging($id: String!, $parentId: String) {
        updatePackaging(id: $id, parentId: $parentId) {
          id
          parentId
        }
      }
    `,
    variables: {
      id: packaging.id,
      parentId: packaging.parentId,
    },
  });


Solution

  • This is the model "Packaging" And I found that "parentId" was missed. Here's so many columns and I missed the columns.

    import { Field, ObjectType } from "type-graphql";
    import { PackagingComponent } from "./PackagingComponent";
    
    @ObjectType()
    export class Packaging {
      @Field()
      id!: string;
    
      @Field()
      parentId?: string;
    
      // ... more
    }