Skip to content

Is the order of argument definitions significant? #577

Open
@spawnia

Description

@spawnia

I am aware the spec defines that the order in which the arguments are passed does not matter: https://graphql.github.io/graphql-spec/draft/#sec-Language.Arguments

However, We are currently dealing with an issue where the order in which those arguments are applied is significant, see nuwave/lighthouse#728

Let me give you a simple example. Suppose we defined a query that concats two strings:

type Query {
	concat(left: String!, right: String!): String
}

Given the rule that arguments are unordered and maintain identical semantical reason no matter in which order they are passed, the following calls must return the same result. Both fields should return foobar.

{
	concat(left: "foo", right: "bar")
    concat(right: "bar", left: "foo")
}

This indicates that on the server side, there must be an implicit ordering that determines how those arguments should be handled. In this particular case, the implementation will probably just do that by just doing something like this in my resolver.

return args.left + args.right

Things are working fine at this point.

Now, i might want to grow the number of allowed arguments to the concat function. As the spec declares, the order in which the user passes them must be normalized. Instead of spelling out each argument, i might want to do something like this in my resolver:

return reduce(args, (acc, item) => acc + item);

In that case, it would be practical for me if the arguments were always passed to the resolver in the order that i defined in the schema, instead of me having to take care of ensuring the correct order myself. This can be a subtle cause of bugs.

I am not quite sure if this issue falls within the scope of the spec or is just an implementation detail that might be handled by the various GraphQL libraries.
It would surely make it easier on implementors if they would not have to worry about
the order in which a user passes the arguments and are rather always passed them in
a previously defined order.

It would be worthwile to clarify if the schema definition itself can be used
to define the canonical order that arguments will be normalized to, or if the following two definitions are considered equivalent:

type Query {
	concat(left: String!, right: String!): String
    # Switch the argument order in the schema definition
	concat(right: String!, left: String!): String
}

A simple note like #470 could at least enable the implementations to build atop the ordering of argument definitions safely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions