1
votes

I've beat on this issue for what seems like a week and nothing online solves it. No data object is returned, not even "null". I'm converting from a working REST CRUD app to GraphQL. Not as easy as I expected.

My mutation in Playground:

mutation createMember($input: MemberInput!) {
    createMember (input: $input) {
          first_name
          last_name
          user_name
    }
}

Playground Query Variables below: (Not in the headers section.)

{
  "input": {
    "first_name": "John",
    "last_name": "Creston",
    "user_name": "jc"
    }
}

The schema: (The queries work fine and the Member type, an entity in TypeORM, works with them and full REST CRUD.)

input MemberInput {
    first_name: String!
    last_name: String!
    user_name: String!
}

type Mutation {
    createMember(input: MemberInput!): Member!
}

type Query {
    getMembers: [Member]
    getMember(member_id: Int!): Member!
    checkUserName(user_name: String): Member
    checkEmail(email: String): Member
}

I don't see how the resolver could be the problem for this error message but I'll add the Nestjs resolver:

@Mutation('createMember')
  async createMember(@Args('input') input: MemberInput): Promise<Members> {
    console.log('input in resolver: ', input);
    return await this.membersService.addItem(input);
  }

The service works with REST and the queries so that should be a problem. The console.log in the resolver never appears in terminal.

From the Copy CURL button:

curl 'http://localhost:3000/graphql' -H 'Accept-Encoding: gzip,    
deflate, br' -H 'Content-Type: application/json' -H 'Accept: 
application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: 
http://localhost:3000' --data-binary '{"query":"mutation 
createMember($input: MemberInput!) {\n  createMember (input: $input) 
{\n      first_name\n      middle_initial\n      last_name\n     
user_name\n      pitch\n      main_skill_title\n    \tskill_id_array\n      
skills_comments\n      other_skills\n      links\n      country\n      
email\n      member_status\n  }\n}"}' --compressed
2
That's a request validation error and it means exactly what it says, the variable $input is missing. You can double check whether the variables are being sent by opening the network tab in your browser's dev tools and examining the request playground is making to your server. I can promise there is no input variable in the request body if you're seeing this error.Daniel Rearden
Not to sound like a jerk, but are you sure you're putting the variables in the correct text field? I can toggle between "VARIABLES" and "HTTP HEADERS" and the only indicator of which tab I'm on is that its color is a slightly lighter grey. I've mixed those up before.Daniel Rearden
@DanielRearden Yes. When I click on Headers there is nothing. When I click back on Variables there is my JSON. Variables is highlighted. What is interesting is that I get the same message if the JSON is in either one. I don't understand that.Preston
Could be some weird bug with Playground itself. On a whim, can you hit the COPY CURL button and edit your question with the resulting command?Daniel Rearden
@DanielRearden I added it. Hopefully you understand that output. My mutation and input are briefed in my post for simplicity. This shows the full object. Both have the same error when I edited out much of the object. I've since deleted the briefed test.Preston

2 Answers

9
votes

I had same problem, in variables i had comma at the end of last parameter. gql gave me same error.

change this:

{
    "input": {
        "param1": "val1",
        "param2": "val2",
    }
}

to this:

{
    "input": {
        "param1": "val1",
        "param2": "val2" // no comma here
    }
}
1
votes

A bit complicated to explain but we've probably all be there. I was trying different "solutions" and the remains of one was accidentally left in my JSON data object. The full object has more properties than my brief one above. An array shouldn't be "[]" in JSON. I was getting tired and desperate last night. I changed the resolver to what I posted above but it didn't fix the problem because of the experiment in JSON caused the same error as before. So I was looking in the wrong places. Once I noticed and removed the quotes my full object's data was posted to the Postgres db.

The code above works.

It would be nice if there were more specific GraphQL errors. I suspected that this one is very general but that didn't help. It didn't indicate that the original problem was in the resolver and later that I had a data input error.