Skip to content

Passing variables is mismatched between typed and runtime behaviour #1586

Closed
@dallasjohnson

Description

@dallasjohnson

When I try to satisfy the generate types in VSCode calling a mutation I need to provide the variables wrapped in a variables : {} (as below). to silence any errors.

import type { BookCreateInput } from '@/gql_generated/graphql'
import { graphql } from '@/gql_generated'

const CREATE_BOOK = graphql(`
  mutation CreateBook($input: [BookCreateInput!]!) {
    createBooks(input: $input) {
      books {
        id
        dateCreated
        dateUpdated
        author
        title
        description
        price
        image
        status
      }
    }
  }
`)

const { mutate: createBook } = useMutation(CREATE_BOOK)

const handleSubmit = async (input: BookCreateInput) => {
  try {
    console.log('data', JSON.stringify(input, null, 2))
    await createBook({ variables: { input: [input] } })
    router.push('/houses')
  } catch (error) {
    if (error instanceof ApolloError) {
      console.error('Apollo Error creating house:', JSON.stringify(error, null, 2))
    } else {
      console.error('Error creating house:', error)
    }
  }
}

However, this fails at runtime with an error missing the input variable.

By changing:
await createBook({ variables: { input: [input] } })
to
await createBook({ input: [input] } )

it works at runtime but emits the error in the IDE.

I'm not sure if this a version mismatch, a bug or there is something else I am doing wrong here.
Any help would be appreciated.

Versions

 "@apollo/client": "^3.13.4",
    "@vue/apollo-composable": "^4.2.2",
    "graphql": "^16.10.0",
    "pinia": "^3.0.1",
    "vue": "^3.5.13",
    "vue-router": "^4.5.0"

with this generate types:
"@graphql-codegen/cli": "^5.0.5",

Additional context
My IDE is Cursor (forked from VSCode)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions