4
votes

I'm curretly following this tutorial on Meteor/Apollo/GraphQL, and having huge troubles to make a mutation with arguments/variables. Here is my code and some notes at the bottom !

The code

Schema

type Resolution {
    _id: String!
    name: String!
}

type Mutation {
    createResolution(name: String!): Resolution
}

Resolver

import Resolutions from './resolutions'

export default {
    Query: {
        resolutions() {
            return Resolutions.find({}).fetch()
        }
    },
    Mutation: {
        createResolution(obj, args, context) {
            console.log('hey i get here')
        }
    }
}

The component using the mutation

import React, { Component } from 'react'
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'

const createResolutionQuery = gql`
    mutation createResolution($name: String!) {
        createResolution(name: $name) {
            _id
        }
    }
`

class ResolutionForm extends Component {
    submitForm = () => {
        this.props
            .createResolution({
                variables: {
                    name: this.name.value
                }
            })
            .then(d => console.log('data received'))
            .catch(e => console.log(e))
    }

    render() {
        return (
            <div>
                <input type="text" ref={input => (this.name = input)} />
                <button onClick={this.submitForm}>Submit</button>
            </div>
        )
    }
}

export default graphql(createResolutionQuery, {
    name: 'createResolution'
})(ResolutionForm)

What i know

  • When i try to send my query to the server, i get an http 400 error, and i get the following graphql error : "Unknown argument "name" on field "createResolution" of type "Mutation"."
  • The createResolution is available in my graphiQL but does not show any arguments in the doc.
  • It's stipulated in the tutorial that changing the .graphql schema does not trigger meteor server reloading, to apply change i have to modify my "register-api" file which is responsible for making the executable schema and create the apollo server with it. I made fake change to trigger it but it did not changed anything.
  • I tried to relaunch the server after erasing my browser's cache with no result.

So I think my problem is with the mutation arguments (brilliant deduction I know), but I can't figure out where is the typo or where I'm missing something. Help from somebody with a fresh look is welcome, thanks :)

Edit

Reinstall npm packages solved the issue.

1
try it with this.props.createResolution(this.name.value)..., i think that you don't have to pass the variables config since you're not using the apollo client.Lafi
Hey, thanks, i tried but my component is indeed expecting this object with the variables prop. I don't think the issue come from cleint side but from the server related files where my arguments are not recognized.Halkeand
schema looks good. can you put your repo on github?Loren
Yep here it is !Halkeand

1 Answers

2
votes

All looks good I made a small change and added it as a pull request to your github repo.

createResolution(obj, {name}, context) {
        console.log('hey i get here')
        const id = Resolutions.insert({
            name,
        })
        return Resolutions.findOne(id)
    }

Running on my machine I get no errors.