question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New Rule: Array bang bang

See original GitHub issue

Many have argued that the default behavior of Arrays in the schema should be [Obj!]!: ex:

type Foo {
  bars: [Baz!]!
}

Most of the time, when you see:

type Foo {
  bars: [Baz]
}

It’s a mistake, because then null and [null, null] are valid values - which is most likely not the cast. Thoughts on a new rule for this?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
n1ru4lcommented, Jun 8, 2018

@goldcaddy77


type Comment {
  id: ID!
  text: String
}

type Post {
  id: ID!
  comments: [Comment!]
}

type Query {
  post(postId: ID!): Post
}

type Mutation {}

Lets say loading the comments of Post failed, by using [Comment!] you can throw an Error in the resolver for your post comments and that will be part of the response. The property comments on the Post will be null. But you still have the partial data for the post.

{
  "data": {
    "post": {
       "id": "1",
       "comments": null,
    }
  },
  "errors": [
     { "something": "...." }
  ]
}

However if you use [Comment!]! and throw an Error in your post comments resolver the post will ne null since it cannot exist without the comments field being an Array.

{
  "data": {
    "post": null
  },
  "errors": [
     { "something": "...." }
  ]
}

You could fix this by returning an empty Array and ignoring the error. However in that case the client that consumes your graphql endpoint will never know there might be some comments and e.g. you cannot hande the UI for retry etc.

I hope this clarifies different use cases

0reactions
Caerbannogcommented, Nov 1, 2022

This package by @morris provides a rule that checks for null list items: https://www.npmjs.com/package/graphql-schema-linter-extras It does not check for null lists. Here is the code. ISC license according to package.json.

const {
  ValidationError
} = require("graphql-schema-linter/lib/validation_error");

module.exports = { ListItemsNotNull };

function ListItemsNotNull(context) {
  return {
    ListType(node, key, parent, path, ancestors) {
      if (node.type.kind !== "NonNullType") {
        const ancestorNames = ancestors
          .concat([parent])
          .filter(it => !!it.name)
          .map(it => it.name.value);

        const p = ancestorNames.slice(ancestorNames.length - 2).join(".");

        context.reportError(
          new ValidationError(
            "list-items-not-null",
            `The list field \`${p}\` should not have nullable items.`,
            [node]
          )
        );
      }
    }
  };
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does !!~ (not not tilde/bang bang tilde) alter the result of a ...
Tilde is bitwise NOT - it inverts each bit of the value. As a general rule of thumb, if you use ~ on...
Read more >
Immutable Array Methods: Write Cleaner JavaScript Code
These methods will return a new array based on the operation they've ... Ruby has a nice solution to this in the way...
Read more >
Javascript “Bang, Bang. I Shot You Down” - Use of Double ...
In Javascript, the exclamation mark (“!”) symbol, called a “bang,” is the logical “not” operator. Placed in front of a boolean value it...
Read more >
What is the Double bang (!!) operator in JavaScript?
The ! in JavaScript, also called “bang”, is the logical “not” operator. If you place this operator in front of a boolean value,...
Read more >
Understand Ruby Arrays and Common Array Methods with ...
This method iterates over an array and returns a new array that includes any ... It is a good rule to be wary...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found