sangria icon indicating copy to clipboard operation
sangria copied to clipboard

Problem with validating a default argument value typed by a list of enums

Open NicolasRouquette opened this issue 6 years ago • 1 comments

The Github schema uses lists of enums in several places; e.g.:

        {
          "kind": "INTERFACE",
          "name": "RepositoryOwner",
          "description": "Represents an owner of a Repository.",
          "fields": [
            ...
            {
              "name": "pinnedRepositories",
              "description": "A list of repositories this user has pinned to their profile",
              "args": [
                ...
                {
                  "name": "affiliations",
                  "description": "Affiliation options for repositories returned from the connection",
                  "type": {
                    "kind": "LIST",
                    "name": null,
                    "ofType": {
                      "kind": "ENUM",
                      "name": "RepositoryAffiliation",
                      "ofType": null
                    }
                  },
                  "defaultValue": "[\"OWNER\", \"COLLABORATOR\"]"
                },
                ...

where RepositoryAffiliation is defined as follows:

        {
          "kind": "ENUM",
          "name": "RepositoryAffiliation",
          "description": "The affiliation of a user to a repository",
          "fields": null,
          "inputFields": null,
          "interfaces": null,
          "enumValues": [
            {
              "name": "OWNER",
              "description": "Repositories that are owned by the authenticated user.",
              "isDeprecated": false,
              "deprecationReason": null
            },
            {
              "name": "COLLABORATOR",
              "description": "Repositories that the user has been added to as a collaborator.",
              "isDeprecated": false,
              "deprecationReason": null
            },
            {
              "name": "ORGANIZATION_MEMBER",
              "description": "Repositories that the user has access to through being a member of an organization. This includes every repository on every team that the user is on.",
              "isDeprecated": false,
              "deprecationReason": null
            }
          ],
          "possibleTypes": null
        }

This schema should be valid; however, with sangria 1.4.2, I get two errors:

Invalid default value of argument 'affiliations' in field 'repositories' defined in output type 'RepositoryOwner'. Field 'RepositoryOwner.repositories.[affiliations]' has wrong value: Enum value expected.
Invalid default value of argument 'affiliations' in field 'repositories' defined in output type 'RepositoryOwner'. Field 'RepositoryOwner.repositories.[affiliations]' has wrong value: Enum value expected.

NicolasRouquette avatar Jan 23 '19 21:01 NicolasRouquette

According to the graphql spec (see: https://facebook.github.io/graphql/draft/#sec-Enums):

GraphQL has a constant literal to represent enum input values. GraphQL string literals must not be accepted as an enum input and instead raise a query error.

Query variable transport serializations which have a different representation for non‐string symbolic values (for example, EDN) should only allow such values as enum input values. Otherwise, for most transport serializations that do not, strings may be interpreted as the enum input value with the same name.

This suggests that github's serialization is correct and that Sangria's input validation is too strict. That is, it needs to allow the representation of an enum value as a string with the same name.

Nonetheless, I made an experiment, tweaking the github schema to change the default value from using strings to literals; i.e., instead of:

"defaultValue": "[\"OWNER\", \"COLLABORATOR\"]"

I changed it to:

"defaultValue": "[OWNER, COLLABORATOR]"

There were no errors when building the schema from Json introspection.

NicolasRouquette avatar Jan 24 '19 00:01 NicolasRouquette