最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $lastName of type String used in position expecting type String - Stack Overflow

programmeradmin3浏览0评论

this may code

Schema

import { gql } from 'apollo-server-express';

export default gql`
  extend type Mutation {
    signUp(
      lastName: String!
    ): String!
  }
`;

Resolvers

{
  Query: {},
  Mutation: {
    signUp: async (
      _,
      { lastName}
    ) => {
      try {
        console.log(lastName)
        return 'ok';    
      } catch (error) {
        return 'error';
      }
    },
  },
};

Request

mutation($lastName:String){
  signUp(lastName:$lastName)
}

Query Veriables

{"lastName":"Darjo" }

I can’t understand, but I get Error

"Variable \"$lastName\" of type \"String\" used in position expecting type \"String!\".",

but when I remove the sign ! lastName: String everything is working.

I just can’t understand. What is the reason ?.

this may code

Schema

import { gql } from 'apollo-server-express';

export default gql`
  extend type Mutation {
    signUp(
      lastName: String!
    ): String!
  }
`;

Resolvers

{
  Query: {},
  Mutation: {
    signUp: async (
      _,
      { lastName}
    ) => {
      try {
        console.log(lastName)
        return 'ok';    
      } catch (error) {
        return 'error';
      }
    },
  },
};

Request

mutation($lastName:String){
  signUp(lastName:$lastName)
}

Query Veriables

{"lastName":"Darjo" }

I can’t understand, but I get Error

"Variable \"$lastName\" of type \"String\" used in position expecting type \"String!\".",

but when I remove the sign ! lastName: String everything is working.

I just can’t understand. What is the reason ?.

Share Improve this question asked Nov 18, 2019 at 17:42 EdgarEdgar 6,85611 gold badges37 silver badges74 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

You've specified that the type of the lastName argument as String!. The ! indicates the type is non-null -- in other words, the argument is required and cannot equal null.

In your query, you're defining a variable ($lastName) and assigning it a type. An argument can be passed a variable, but only if their types match. For example, if the argument takes an Int, you cannot pass it a variable of the type Boolean. Similarly, if an argument is non-null, you cannot pass it a variable that is nullable. That's because a nullable variable could be null, but that would violate the non-nullability of the argument.

Note that the opposite is not true -- a nullable argument can accept a non-null variable.

This is valid:

# String! argument and String! variable
type Mutation {
  signUp(lastName: String!): String!
}

mutation($lastName:String!) {
  signUp(lastName:$lastName)
}

Also valid:

# String argument and String! variable
type Mutation {
  signUp(lastName: String): String!
}

mutation($lastName: String!) {
  signUp(lastName: $lastName)
}

As is this:

# String argument and String variable
type Mutation {
  signUp(lastName: String): String!
}

mutation($lastName: String) {
  signUp(lastName: $lastName)
}

But this is not valid:

# String! argument and String variable
type Mutation {
  signUp(lastName: String!): String!
}

mutation($lastName: String) {
  signUp(lastName: $lastName)
}

The only exception to this is if you provide a default value for the variable. So this is still valid:

# String! argument and String variable
type Mutation {
  signUp(lastName: String!): String!
}

mutation($lastName: String = "Some default value") {
  signUp(lastName: $lastName)
}

You can read about variable validation in the spec.

发布评论

评论列表(0)

  1. 暂无评论