0

I have a deeply nested graphql for GitHub which deep inside returns info about repositories. I would like to pull out the repository object into a fragment then reference that fragment in the original query. The file type is *.graphql. I've tried separate files, a single file that starts with the named fragment, then the query. CodeGen always fails. Does anyone have a repo or example of CodeGen and .graphql files with fragments?

This is the query, I would like to replace the node with a named fragment.

query OrgReposAg(
  $organization: String!
  $pageSize: Int
  $after: String
) {
  organization(login: $organization) {
    repositories(
      after: $after
      first: $pageSize
      orderBy: { field: STARGAZERS, direction: DESC }
    ) {
      totalCount
      pageInfo {
        startCursor
        hasNextPage
        endCursor
      }
      edges {
        cursor
        node {
          repositoryName: name
          id
          url
          descriptionHTML
          updatedAt
          stargazers {
            totalCount
          }
          forks {
            totalCount
          }
          issues(states: [OPEN]) {
            totalCount
          }
          pullRequests(states: [OPEN]) {
            totalCount
          }
        }
      }
    }
  }
}

Named fragment

fragment RepoInfoAg on Repository {
  repositoryName: name
  id
  url
  descriptionHTML
  updatedAt
  stargazers {
    totalCount
  }
  forks {
    totalCount
  }
  issues(states: [OPEN]) {
    totalCount
  }
  pullRequests(states: [OPEN]) {
    totalCount
  }
}

I have the fragment as a separate file,which gets me the type for the fragment. I use this when extracting the data. The error looks like:

$ npm run codegen

> typescript-action@1.6.0 codegen C:\repos\code\diberry\typescript-action
> graphql-codegen --config codegen.yml

✔ Parse Configuration
⚠ Generate outputs
  ❯ Generate to ./src/generated/graphql.sdk.ts
    ✔ Load GraphQL schemas
    ✔ Load GraphQL documents
    ✖ GraphQL Document Validation failed with 3 errors;
      Error 0: Cannot qu…
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! typescript-action@1.6.0 codegen: `graphql-codegen --config codegen.yml`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the typescript-action@1.6.0 codegen script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\diberry\AppData\Roaming\npm-cache\_logs\2022-12-03T23_19_06_573Z-debug.log

Log wasn't helpful but here it is:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'codegen'
1 verbose cli ]
2 info using npm@6.14.17
3 info using node@v14.20.0
4 verbose run-script [ 'precodegen', 'codegen', 'postcodegen' ]
5 info lifecycle typescript-action@1.6.0~precodegen: typescript-action@1.6.0
6 info lifecycle typescript-action@1.6.0~codegen: typescript-action@1.6.0
7 verbose lifecycle typescript-action@1.6.0~codegen: unsafe-perm in lifecycle true
8 verbose lifecycle typescript-action@1.6.0~codegen: PATH: C:\Users\diberry\AppData\Roaming\nvm\v14.20.0\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\repos\code\diberry\typescript-action\node_modules\.bin;C:\Users\diberry\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\diberry\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Python39\Scripts;C:\Python39;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\dotnet;C:\ProgramData\chocolatey\bin;C:\Users\diberry\AppData\Roaming\nvm;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\diberry\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn;C:\Program Files\Azure Data Studio\bin;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn;C:\Program Files\Microsoft SQL Server\150\Tools\Binn;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\Binn;C:\Program Files\Microsoft SQL Server\150\DTS\Binn;C:\Program Files\7-Zip;C:\repos\clean-repo-tool;C:\Program Files\Docker\Docker\resources\bin;C:\Users\diberry\AppData\Local\Microsoft\WindowsApps;C:\Users\diberry\.dotnet\tools;C:\Program Files\Azure Data Studio\bin;C:\Program Files (x86)\GitHub CLI;C:\Users\diberry\AppData\Roaming\npm;C:\Users\diberry\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\diberry\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Users\diberry\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Azure Data Studio\bin;C:\Users\diberry\.dotnet\tools;C:\Users\diberry\AppData\Local\GitHubDesktop\bin;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl
9 verbose lifecycle typescript-action@1.6.0~codegen: CWD: C:\repos\code\diberry\typescript-action
10 silly lifecycle typescript-action@1.6.0~codegen: Args: [ '/d /s /c', 'graphql-codegen --config codegen.yml' ]
11 silly lifecycle typescript-action@1.6.0~codegen: Returned: code: 1  signal: null
12 info lifecycle typescript-action@1.6.0~codegen: Failed to exec codegen script
13 verbose stack Error: typescript-action@1.6.0 codegen: `graphql-codegen --config codegen.yml`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Users\diberry\AppData\Roaming\nvm\v14.20.0\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:400:28)
13 verbose stack     at ChildProcess.<anonymous> (C:\Users\diberry\AppData\Roaming\nvm\v14.20.0\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:400:28)
13 verbose stack     at maybeClose (internal/child_process.js:1088:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:296:5)
14 verbose pkgid typescript-action@1.6.0
15 verbose cwd C:\repos\code\diberry\typescript-action
16 verbose Windows_NT 10.0.22621
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "codegen"
18 verbose node v14.20.0
19 verbose npm  v6.14.17
20 error code ELIFECYCLE
21 error errno 1
22 error typescript-action@1.6.0 codegen: `graphql-codegen --config codegen.yml`
22 error Exit status 1
23 error Failed at the typescript-action@1.6.0 codegen script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
DFBerry
  • 1,818
  • 1
  • 19
  • 37
  • Seems like the interesting error text is cut off: "Error 0: Cannot qu..." which is usually the beginning of an error like this "Cannot query field x on type Y" which indicates that there is nothing wrong with you setup. You are querying the API incorrectly. I recommend to use a tool like GraphiQL or install the VSCode extension to get in editor feedback about your queries. – Herku Dec 04 '22 at 12:34

1 Answers1

0

Got edges/node syntax with fragments working. Here is the final graphql.

query OrgReposAg(
  $organization: String!
  $pageSize: Int
  $after: String
) {
  organization(login: $organization) {
    repositories(
      after: $after
      first: $pageSize
      orderBy: { field: STARGAZERS, direction: DESC }
    ) {
      totalCount
      pageInfo {
        startCursor
        hasNextPage
        endCursor
      }
      edges {
        cursor
        node {
        ...MyRepoFields
        }
      }
    }
  }
}

# This fragment extracts each repository in the edges array
# to a named type MyRepoFields, created by codegen
fragment MyRepoFields on Repository {
  repositoryName: name
  id
  url
  descriptionHTML
  updatedAt
  stargazers {
    totalCount
  }
  forks {
    totalCount
  }
  issues(states: [OPEN]) {
    totalCount
  }
  pullRequests(states: [OPEN]) {
    totalCount
  }
}
DFBerry
  • 1,818
  • 1
  • 19
  • 37