graphql.js icon indicating copy to clipboard operation
graphql.js copied to clipboard

How to populate Int Variables in Typescript

Open digimangos opened this issue 3 years ago • 2 comments

I'm trying to use a query which has an "Int!" variable, but this is always being sent as a string.

await this.getOctokit().graphql(
          `query ($orgName: String!, $projectNumber: Int!){
            organization(login: $orgName) {
                projectNext(number: $projectNumber) {
                  id
                }
            }}`
        ,{
         orgName,
         projectNumber
        })

projectNumber is defined as projectNumber: number

But the resultant graphQL query is sent as a string:


    "request": {
        "query": "query ($orgName: String!, $projectNumber: Int!){\n            organization(login: $orgName) {\n                projectNext(number: $projectNumber) {\n                  id\n                }\n            }}",
        "variables": {
            "orgName": "myorg",
            "projectNumber": "4"
        }
    }

which returns the error:

        {
            "extensions": {
                "value": "4",
                "problems": [
                    {
                        "path": [],
                        "explanation": "Could not coerce value \"4\" to Int"
                    }
                ]
            },
            "locations": [
                {
                    "line": 1,
                    "column": 27
                }
            ],
            "message": "Variable $projectNumber of type Int! was provided invalid value"
        }

Is it possible for the variable to be resolved as "projectNumber": 4? I notice the example in the README.md - Variables, doesn't show the population of an Int variable instead its defaulted in. So perhaps it's not supported?

digimangos avatar May 13 '22 09:05 digimangos

I've run into this issue. Is there a solution or any workaround?

peter-evans avatar Mar 30 '23 06:03 peter-evans

Well I guess the workaround is to use template literals.

    const query = `
      query($owner: String!, $repo: String!) {
        repository(owner: $owner, name: $repo) {
          pullRequest(number: ${pullNumber}) {
            id
          }
        }
      }
    `

peter-evans avatar Mar 30 '23 06:03 peter-evans