Search code examples
javascriptgraphqlrelayjsgraphql-js

Additional arguments to "connectionArgs" Relay js


For example I have connection type:

let usersType = new GraphQLObjectType({
    name: 'Users',
    description: 'users array',
    fields: () => ({
        array: {
            type: userConnection,
            description: 'all users',
            args: connectionArgs,
            searchFor: {
                type: GraphQLString
            },
            resolve: (root, args) => {
                return connectionFromArray(get(), args);
            }
        }
    })
});

in this case in query I can specify only (first, last, after, before) arguments, but what if I need to pass some additional arguments like userName etc. is that possible?

basically I need something like:

query {
    array (first: 1, userName: "name")
}

and in users type I can handle request like:

resolve: (root, args) => connectionFromArray(get(args.userName), args.args)

Solution

  • Yes it's possible, you just need to extend the relay helper connectionArgs with the new argument like this:

    args: {
       ...connectionArgs,
       searchFor: { type: GraphQLString }
    }
    

    And then access it in the resolve function:

    resolve: (root, args) => {
       // if the field argument 'searchFor' exists
       if (args.searchFor) {
       ...
       }
       ...
    }