GraphQL mutation in KeystoneJS: “Cannot use ‘in’ operator to search for ‘id’ in undefined”

GraphQL mutation in KeystoneJS: “Cannot use ‘in’ operator to search for ‘id’ in undefined”


0

In a KeystoneJS GraphQL project I’m trying to create a new data object (an “Article”) in the ‘resolveInput’ hook of another, existing, data object (a “Proposal” — when a Proposal is approved, I create an Article based on that Proposals’data).

This worked fine using the Mongoose adapter, but I’ve tried to do it using the built in GraphQL API, using keystone.executeQuery and I get the following error:

GraphQL mutation in KeystoneJS: "Cannot use 'in' operator to search for 'id' in undefined"

My Article list has a relationship with one Proposal (I’m leaving out the other fields)

fields: {
  proposal: {
                isUnique: false,
                type: fields_1.Relationship,
                ref: 'Proposal',
                access: {
                    create: true,
                    read: true,
                    update: false
                }
            }

}

and I create the new Article thus (I’m omitting some code)

hooks: {
    resolvedInput: async params => {
        const articleCreateInput = {
                                title: cleanTitle,
                                text: cleanText,
                                visible: HIDDEN,
                                proposal: {
                                    connect: {
                                        id: proposalId
                                    }
                                }
                            };
                            const articleCreationResult = await keystone.executeQuery(createArticle, { variables: articleCreateInput });

  }
}

As far as I can see this is the correct way to do it, using connect, connecting an existing item to one you are creating.

My query is

const createArticle = `mutation createArticle($data:ArticleCreateInput) {
  createArticle(data:$data) {
    id
    title
    visible
    publishDate
  }
} `;

and as far as I can see I’m following the schema correctly

GraphQL mutation in KeystoneJS: "Cannot use 'in' operator to search for 'id' in undefined"

I’m sure I’m making an obvious mistake but at the moment I don’t see it — and I’m not sure whether that mistake is a GraphQL mistake or a KeystoneJS mistake (or both).

9

  • check query arguments (variables) – is articleCreateInput properly defined

    – xadm

    Mar 25, 2020 at 23:19

  • It works fine from the playground…

    – Cerulean

    Mar 26, 2020 at 8:36

  • … but in code … logged before executeQuery?

    – xadm

    Mar 26, 2020 at 9:17

  • @xadm It's { title: 'This is the new thing', text: 'abcdefh', visible: 'hidden', proposal: { connect: { id: '5e5e74c2ddd03b71b42281a8' } } }

    – Cerulean

    Mar 26, 2020 at 9:53

  • relation? one to one?

    – xadm

    Mar 26, 2020 at 9:56

1 Answer
1


0

I was having the same trouble and my problem was that I was sending the variables of the query like this:

{var1: 'val1', var2: 'val2'}

instead of

{data: {var1: 'val1', var2: 'val2'}}



Leave a Reply

Your email address will not be published. Required fields are marked *