Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing variables is mismatched between typed and runtime behaviour #1586

Closed
dallasjohnson opened this issue Mar 25, 2025 · 1 comment
Closed

Comments

@dallasjohnson
Copy link

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)

@dallasjohnson
Copy link
Author

I managed to resolve this by starting a fresh project. My type defs were clearly confused and a new project seem to fix this up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant