Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION]: specific GitObject usage? (ie, Blob & Tree) #747

Open
1 task done
noltron000 opened this issue Apr 10, 2023 · 1 comment
Open
1 task done

[QUESTION]: specific GitObject usage? (ie, Blob & Tree) #747

noltron000 opened this issue Apr 10, 2023 · 1 comment
Labels
hacktoberfest Issues for participation in Hacktoberfest Status: Up for grabs Issues that are ready to be worked on by anyone Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects

Comments

@noltron000
Copy link

noltron000 commented Apr 10, 2023

Question

Hi there! I was playing around with some queries in typescript. I was querying some trees/blobs off of the Repository object, and these results are typed like GitObject.

However, I notice that the types are a bit prohibitive. In schema.d.ts, I notice that Repository is defined like so:

// This type definition snippet is truncated to be more legible.
// Really, this extends lots of interfaces and has lots more attributes.
export type Repository = {
  /** A Git object in the repository */
  object?: Maybe<GitObject>;
}

You'll see that, GitObject is used here, although I'd like to tell TypeScript that its a Blob or Tree, both of which are types that can appear here. Instead of Maybe<GitObject>, the type definition should be something like Maybe<GitObject | Tree | Blob | /* ...etc */>. This lack of additional types is how its a bit prohibitive. I know I could use as in typescript to work around this, but I don't like to do so.

Is there a way to do this currently, with this generated schema? Am I missing something?

Current Usage

I have a work-around, but its slightly verbose:

// This is just using graphql from octokit...
import {graphqlWithAuth} from "./graphql-with-auth"

import {
  Blob,
  GitObject as BaseGitObject,
  Tree as BaseTree,
  TreeEntry as BaseTreeEntry,
  Repository as BaseRepository,
} from "@octokit/graphql-schema"

type Maybe<T> = T | null

// NOTE: Would be great if these types were powered by type-parameters sort of like this, out of the box!
type GitObject<T extends BaseGitObject = BaseGitObject> = T

interface Repository<Obj extends GitObject = GitObject> extends BaseRepository {
  object?: Maybe<Obj>
}

interface Tree<Obj extends GitObject = GitObject> extends BaseTree {
  entries?: Maybe<Array<TreeEntry<Obj>>>;
}

interface TreeEntry<Obj extends GitObject = GitObject> extends BaseTreeEntry {
  object?: Maybe<Obj>;
}

const callAPI = async ( ) => {
  const response = await graphqlWithAuth<{repository: Repository<Tree<Blob>>}>(MY_GITHUB_QUERY)

  // Just a dummy example to show this type works just fine.
  console.log(response.repository.object?.entries?.[0]?.object?.text)
}

Describe the need

This is sort of verbose, and it'd be lovely if I didn't have to write all these interfaces out.

For maintainers, if you don't find this relevant or something, feel free to just leave a comment and close this, I'm just trying to help! Thanks for your efforts!

Code of Conduct

  • I agree to follow this project's Code of Conduct
@noltron000 noltron000 added Status: Triage This is being looked at and prioritized Type: Feature New feature or request labels Apr 10, 2023
@ghost ghost added this to Features in JS Apr 10, 2023
@noltron000
Copy link
Author

Ah, my title isn't the best, let me see if I can revise that...

@noltron000 noltron000 changed the title [QUESTION]: Specifically typed GitObjects (like Tree & Blob) aren't internally used? [QUESTION]: specific GitObject usage? (ie, Blob & Tree) Apr 10, 2023
@kfcampbell kfcampbell added Type: Support Any questions, information, or general needs around the SDK or GitHub APIs Priority: Normal Status: Up for grabs Issues that are ready to be worked on by anyone and removed Type: Feature New feature or request Status: Triage This is being looked at and prioritized labels Apr 11, 2023
@ghost ghost moved this from Features to Support in JS Apr 11, 2023
@nickfloyd nickfloyd added the hacktoberfest Issues for participation in Hacktoberfest label Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest Issues for participation in Hacktoberfest Status: Up for grabs Issues that are ready to be worked on by anyone Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
Status: 🔥 Backlog
JS
  
Support
Development

No branches or pull requests

3 participants