I have a type of character with the following datamodel:

type Character {
  id: ID! @unique
  name: String! @unique
  name_ja: String @unique
  name_ko: String @unique
  name_zh_CN: String @unique
  name_zh_TW: String @unique
  name_zh_HK: String @unique
  games: [Game]
}

The character should have all name fields be unique. No problem with this so far.

Here is the schema mutation for creating a character:

createCharacter(
        name: String!
        name_ja: String
        name_ko: String
        name_zh_CN: String
        name_zh_TW: String
        name_zh_HK: String
        game: String!
): Character!

Here is the actual node mutation:

async createCharacter(parent, args, ctx, info) {
        // Check if user is logged in.
        if (!ctx.request.userId) {
            throw new Error('You must be logged in');
        }
        // Get the arguments
        const character = {...args};
        const game = args.game
        console.log(character);
        delete character.game;
        const newCharacter = await ctx.db.mutation.createCharacter({
            data: {
                ...character,
                games: {
                    connect: {
                        name: game
                    }
                }
            }
        }, info);
        return newCharacter;
    },

Here's the GQL mutation on the React side:

const CREATE_CHARACTER_MUTATION = gql`
    mutation CREATE_CHARACTER_MUTATION(
        $name: String!
        $name_ja: String!
        $name_ko: String!
        $name_zh_CN: String!
        $name_zh_TW: String!
        $name_zh_HK: String!
        $game: String!
    ) {
        createCharacter(
            name: $name
            name_ja: $name_ja
            name_ko: $name_ko
            name_zh_CN: $name_zh_CN
            name_zh_TW: $name_zh_TW
            name_zh_HK: $name_zh_HK
            game: $game
        ) {
            id
            name
            name_ja
            name_ko
            name_zh_CN
            name_zh_TW
            name_zh_HK
            games {
                id
                name
            }
        }
    }
`;

And yet when I pass a unique character name through, it gives me this error: "A unique constraint would be violated on Character. Details: Field name = name"

Strangely, I was able to push one character name through, and on the second attempt and every subsequent attempt, it throws this error.

I am clearing the state (save for the game) on submit of the form:

                                    e.preventDefault();
                                    const res = await createCharacter();
                                    console.log(res);
                                    this.setState({
                                        name: '',
                                        name_ja: '',
                                        name_ko: '',
                                        name_zh_CN: '',
                                        name_zh_TW: '',
                                        name_zh_HK: ''
                                    })
                                }}>

And can confirm that the character name is being passed to state properly after a submit. Regardless, this still happens after a refresh.

0 Answers