Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL Relay defaultValue for array of object in fragment argument definitions

Tags:

graphql

relay

I am trying to define my createRefetchContainer with fragment. However, I am getting an error about defaultValue of array of object type. I checked the documentation yet I couldn't figure out how to handle default value attribute for array of object type parameter.

I tried to put empty array "[]" or "[{ key: "status", value: "active" }]" but none of them worked.

export default createRefetchContainer(
  translate('admin')(UserList),
  graphql`
    fragment UserList_usersWithPage on Query
      @argumentDefinitions(
        filters: { type: [UserFilterInput], defaultValue: [] }
        pageNumber: { type: Int, defaultValue: 0 }
        pageSize: { type: Int, defaultValue: 25 }
      ) {
      page: findUsersWithPage(filters: $filters, pageNumber: $pageNumber, pageSize: $pageSize) {
        totalPages
        totalElements
        users {
          ...MiniProfile_user @relay(mask: false)
        }
      }
    }

    fragment UserList_filterFields on Query {
      accounts: findDistinctUserAccounts
      departments: findDistinctUserDepartments
      expertises: findDistinctUserExpertises
      ranks: findAllUserRankCodes
    }
  `,
  graphql`
    query UserListRefetchQuery($userFilterInput: [UserFilterInput], $pageNumber: Int, $pageSize: Int) {
      ...UserList_usersWithPage
        @arguments(userFilterInput: $userFilterInput, pageNumber: $pageNumber, pageSize: $pageSize)
    }
  `,
);


ERROR:
GraphQLParser: Expected definition for variable `filters` to be an object 
with the following shape: `{type: string, defaultValue?: mixed, nonNull?: 
boolean, list?: boolean}`, got `[object Object]`. Source: document 
`UserList_usersWithPage` file: `scenes/admin/pages/UserList.js`. 
like image 350
rawsly Avatar asked Jan 22 '26 08:01

rawsly


1 Answers

I found a solution that appears to work by wrapping the full argument type in a string and flagging my array member type as required within the array using "!":

...
@argumentDefinitions(
  filters: { type: "[Filter!]", defaultValue: [] }
)
...

So in OP's example, that'd mean:

export default createRefetchContainer(
  translate('admin')(UserList),
  graphql`
    fragment UserList_usersWithPage on Query
      @argumentDefinitions(
        filters: { type: "[UserFilterInput!]", defaultValue: [] }
        pageNumber: { type: Int, defaultValue: 0 }
        pageSize: { type: Int, defaultValue: 25 }
      ) {
      ...

Note that an empty array is still a valid argument type, as [Type!] allows for empty arrays, but requires that any members be non-null of type Type.

I'm still pretty new to graphql and relay myself, so cannot go into more detail on why this is working at this time.

like image 153
Jack Ratner Avatar answered Jan 25 '26 12:01

Jack Ratner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!