简体   繁体   中英

GraphQL POST passes even though parts of required schema are missing

I have a mutation defined as such:

# POST a new Translation
    createTranslation(
      name: String!
      options: [OptionInput!]!
    ): Translation

From my understanding, using the two quotation marks for [OptionInput!]! means that an array is REQUIRED, and an object of the type OptionInput is also REQUIRED.

However, from my client-side, I am able to POST successfully without any objects in the Options array:

const createTranslation = gql`
  mutation createTranslation(
    $name: String!,
    $options: [OptionInput!]!
  ) {
    createTranslation(
      name: $name,
      options: $options
    ) {
      _id
    }
  }
`;

Even though I'm expecting validation errors out of the box (sample response):

{
  "data": {
    "translations": [
      {
        "_id": "59fa1b7a3d4d4805ed6f7539",
        "name": "Sample Translation Name!!!",
        "options": []
      }
    ]
  }
}

What's the proper way to ensure a subfield that uses an array of objects doesn't contain any null values?

PS: Here's my input OptionInput

input OptionInput {
    text: String!
    value: String!
  }

As the docs state, if the type inside the list is non-null:

This means that the list itself can be null, but it can't have any null members.

That means an empty array is still valid, but an array where any of the items themselves are null is not.

[OptionInput] means all of these are valid:

  • null
  • [{},null]
  • [{},{}]
  • []

[OptionInput!] means all of these are valid:

  • null
  • [{},{}]
  • []

[OptionInput]! means all of these are valid:

  • [{},null]
  • [{},{}]
  • []

[OptionInput!]! means all of these are valid:

  • [{},{}]
  • []

If you need to prevent the client from providing an empty array, you'll need to check if the argument is an empty an array inside your resolver and then just throw an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM