Title: Variable validation broken when variables are used inside an input object value · Issue #3276 · graphql-java/graphql-java · GitHub
Open Graph Title: Variable validation broken when variables are used inside an input object value · Issue #3276 · graphql-java/graphql-java
X Title: Variable validation broken when variables are used inside an input object value · Issue #3276 · graphql-java/graphql-java
Description: Setup I'll use the following simple schema to illustrate the issue: type Query { items(pagination: Pagination = {limit: 10, offset: 0}): [String] } input Pagination { limit: Int offset: Int } While variables are commonly used as the whol...
Open Graph Description: Setup I'll use the following simple schema to illustrate the issue: type Query { items(pagination: Pagination = {limit: 10, offset: 0}): [String] } input Pagination { limit: Int offset: Int } While...
X Description: Setup I'll use the following simple schema to illustrate the issue: type Query { items(pagination: Pagination = {limit: 10, offset: 0}): [String] } input Pagination { limit: Int offset: Int } W...
Opengraph URL: https://github.com/graphql-java/graphql-java/issues/3276
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Variable validation broken when variables are used inside an input object value","articleBody":"**Setup**\r\n\r\nI'll use the following simple schema to illustrate the issue:\r\n\r\n```graphql\r\ntype Query {\r\n items(pagination: Pagination = {limit: 10, offset: 0}): [String]\r\n}\r\n\r\ninput Pagination {\r\n limit: Int\r\n offset: Int\r\n}\r\n```\r\n\r\nWhile variables are commonly used as the whole argument value:\r\n\r\n```graphql\r\nquery Items($pagination: Pagination) {\r\n items(pagination: $pagination) { #variable is the entire input object (Pagination)\r\n ...\r\n }\r\n}\r\n```\r\n\r\nthey can also be used as the values of fields inside an input object:\r\n\r\n```graphql\r\nquery Items($limit: Int) {\r\n items(pagination: {limit: $limit, offset: 0}) { #variable is only a part of the input\r\n ...\r\n }\r\n}\r\n```\r\n\r\nI've scoured the spec for a confirmation on whether this is indeed legal usage of variables and, while this situation is never directly addressed either way, the section on [Input Coercion](https://spec.graphql.org/draft/#example-704b8) lists the following examples which seem to indicate this is legitimate usage:\r\n\r\n```graphql Definition\r\ninput ExampleInputObject {\r\n a: String\r\n b: Int!\r\n}\r\n```\r\n\r\n| Literal Value | Variables | Coerced Value |\r\n| ------------------------ | ----------------------- | ------------------------------------ |\r\n| `{ a: $var, b: 123 }` | `{ var: null }` | `{ a: null, b: 123 }` |\r\n| `{ b: $var }` | `{ var: 123 }` | `{ b: 123 }` |\r\n\r\n**The bug**\r\n\r\nWhen a variable is used inside an input object **_which has a default value_** (like in my `Pagination` example), graphql-java's variable type validation freaks out, because it compares the type of the variable (`Int`) against the default value of the entire argument (`Pagination`) [here](https://github.com/graphql-java/graphql-java/blob/7f229de387e5e515e99ff270a1d1b9316b6e0338/src/main/java/graphql/validation/rules/VariableTypesMatch.java#L63):\r\n\r\n```java\r\n//Notice the getValidationContext().getArgument()\r\nOptional\u003cInputValueWithState\u003e schemaDefault = Optional.ofNullable(getValidationContext().getArgument()).map(v -\u003e v.getArgumentDefaultValue());\r\n... \r\n//expectedType is Int\r\nschemaDefaultValue = ValuesResolver.valueToLiteral(schemaDefault.get(), expectedType, getValidationContext().getGraphQLContext(), getValidationContext().getI18n().getLocale());\r\n```\r\n\r\n**To Reproduce**\r\n\r\nHere's a self-contained example:\r\n\r\n```java\r\nGraphQLSchema schema = GraphQLSchema.newSchema()\r\n .query(GraphQLObjectType.newObject()\r\n .name(\"Query\")\r\n .field(items -\u003e items\r\n .name(\"items\")\r\n .type(GraphQLList.list(Scalars.GraphQLString))\r\n .argument(pagination -\u003e pagination\r\n .name(\"pagination\")\r\n //skipped adding the default limit/offset values as it doesn't change anything\r\n .defaultValueProgrammatic(new HashMap\u003c\u003e())\r\n .type(GraphQLInputObjectType.newInputObject()\r\n .name(\"Pagination\")\r\n .field(limit -\u003e limit\r\n .name(\"limit\")\r\n .type(Scalars.GraphQLInt))\r\n .field(offset -\u003e offset\r\n .name(\"offset\")\r\n .type(Scalars.GraphQLInt))\r\n .build())))\r\n .build())\r\n .codeRegistry(GraphQLCodeRegistry.newCodeRegistry()\r\n .dataFetcher(FieldCoordinates.coordinates(\"Query\", \"items\"), (DataFetcher\u003c?\u003e) env -\u003e Collections.singletonList(\"test\"))\r\n .build())\r\n .build();\r\n\r\n GraphQL gql = GraphQLRuntime.newGraphQL(schema).build();\r\n\r\n Map\u003cString, Object\u003e vars = new HashMap\u003c\u003e();\r\n vars.put(\"limit\", 5);\r\n vars.put(\"offset\", 0);\r\n\r\n ExecutionInput in = ExecutionInput.newExecutionInput()\r\n .query(\"query Items( $limit: Int, $offset: Int) {\\n\" +\r\n \" items(\\n\" +\r\n \" pagination: {limit: $limit, offset: $offset} \\n\" +\r\n \" )\\n\" +\r\n \"}\")\r\n .variables(vars)\r\n .build();\r\n ExecutionResult result = gql.execute(in);\r\n```\r\n\r\nLeads to:\r\n\r\n\u003e graphql.AssertException: Expected a value that can be converted to type 'Int' but it was a 'HashMap'\r\n","author":{"url":"https://github.com/kaqqao","@type":"Person","name":"kaqqao"},"datePublished":"2023-07-30T16:11:44.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/3276/graphql-java/issues/3276"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0e5e520b-1bee-1199-5a75-9d41823b2859 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E91E:17065F:832387:B03D8F:6A61868E |
| html-safe-nonce | 44db2587e82087f6d83851da1a08275ee0318fe0a700266d01feda85728db378 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTFFOjE3MDY1Rjo4MzIzODc6QjAzRDhGOjZBNjE4NjhFIiwidmlzaXRvcl9pZCI6IjI5NzI2MTM1NDI0MTg2Nzk0MzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 686d1394dcdbdbbd545abe9b66062bc997bf3eb028d38569b37f4d555d57f8d0 |
| hovercard-subject-tag | issue:1827988843 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/graphql-java/graphql-java/3276/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b0cf14495995a5807a80b842138bfb959c0f290ed5be40318c51534d74f0ba39/graphql-java/graphql-java/issues/3276 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b0cf14495995a5807a80b842138bfb959c0f290ed5be40318c51534d74f0ba39/graphql-java/graphql-java/issues/3276 |
| og:image:alt | Setup I'll use the following simple schema to illustrate the issue: type Query { items(pagination: Pagination = {limit: 10, offset: 0}): [String] } input Pagination { limit: Int offset: Int } While... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kaqqao |
| hostname | github.com |
| expected-hostname | github.com |
| None | c28c2f6607f2aacbd934868d997038895c9780d08babfe918550d73773326f4c |
| turbo-cache-control | no-preview |
| go-import | github.com/graphql-java/graphql-java git https://github.com/graphql-java/graphql-java.git |
| octolytics-dimension-user_id | 14289921 |
| octolytics-dimension-user_login | graphql-java |
| octolytics-dimension-repository_id | 38602457 |
| octolytics-dimension-repository_nwo | graphql-java/graphql-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38602457 |
| octolytics-dimension-repository_network_root_nwo | graphql-java/graphql-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cd05235d52ffb3318513d883a56c87a121015c8b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width