Nexus: Default Arg Value Type Error

Created on 1 May 2019  路  6Comments  路  Source: graphql-nexus/nexus

Given the following objectType:

import { extendType, intArg, stringArg } from 'nexus'
import { Research } from '../db/entities/comptool/Research'

export const FilterQueries = extendType({
  type: 'Query',
  definition(t) {
    t.list.string('companies', {
      description:
        'Default returns the first 20 in alphabetical order. Searchable',
      args: {
        limit: intArg({
          default: 20,
          required: false,
        }),
        offset: intArg({
          default: 0,
          required: false,
        }),
        search: stringArg({
          default: '',
          required: false,
        }),
      },
      resolve: async (_root, { limit, offset, search }) => {
        return Research.findCompaniesFilter(limit, offset, search).then(
          results => {
            return results.map(r => r.current_company)
          },
        )
      },
    })
  },
})

Why would limit, offset, search have a type of string(or number) | null | undefined when there's a default value?

typbug

All 6 comments

I believe the type should be string (or number) | null rather than
string (or number) | null | undefined because required: false is just an alias for nullable: true, I don't believe the default value will be used if null is explicitly provided as the value for the argument, only if it's omitted. Need to double check that though.

I think I'm running into this as well. I have

        type: arg({
          type: 'ItemType',
          default: 'TASK',
          required: false,
        }),

and expected the type of type to be one of the values of ItemType but instead I get:

Type '"TASK" | "EVENT" | "NOTE" | null | undefined' is not assignable to type '"TASK" | "EVENT" | "NOTE"'.
  Type 'undefined' is not assignable to type '"TASK" | "EVENT" | "NOTE"'.

@tgriesser

I don't believe the default value will be used if null is explicitly provided as the value for the argument, only if it's omitted. Need to double check that though.

That's my understanding too. https://graphql.github.io/graphql-spec/draft/#sec-Coercing-Field-Arguments. Note how if the value is provided, default does not apply. Ditto with variable values https://graphql.github.io/graphql-spec/draft/#CoerceVariableValues().

I believe the type should be string (or number) | null rather than string (or number) | null | undefined

Yep this is correct

and expected the type of type to be one of the values of ItemType but instead I get:

@chrisdrackett

expected the type of type to be one of the values of ItemType

This is not possible in GraphQL IIUC the spec.

I will mark this issue as a bug because as already stated:

I believe the type should be string (or number) | null rather than string (or number) | null | undefined

Is this bug solved? @tgriesser

Correct me if I'm wrong on this understanding:

  • required: true assumes the fields/args are nullable: false (non-null), so does not accept null value.
  • nullable: true makes the fields/args able to get null value, but (shouldn't) doesn't allow undefined?

Does the nullable: true flag make the fields/args as optional, or this only allow to get null value?

because required: false is just an alias for nullable: true

Another one, nullable: true === required: false?

Any news about a fix?

Was this page helpful?
0 / 5 - 0 ratings