Skip to content

Python: Don’t accept an object for parameters named id (and the return is of the same type) #4191

Closed

Description

@helderco

Overview

Some top-level fields return an object if you have an ID:

def directory(self, id: DirectoryID | Directory | None = None) -> Directory: ...
def file(self, id: FileID | File) -> File: ...

If you already have an object it doesn’t make sense to use these fields to get the same object. So these fields only make sense if you have an ID somehow, and need to get the object they represent.

Note: It’s not clear when you’d need these fields in a codegen client. IDs are an implementation detail.

Solution

Replace all ID inputs with object references, unless the argument name is id and the field’s return type is that object type.

Rename outliers

That’s a good rule of thumb that is future proof, because there’s two outliers with id param names that I think should be renamed, but there can be other cases in the future.

type Git {
-    commit(id: string)
+    commit(hash: string)
}

type Container {
-    withRootfs(id: DirectoryID!): Container!
+    withRootfs(source: DirectoryID!): Container!
}

Note: Git.commit is harmless since it’s not asking for an ID type, but I still think it should be renamed.

Explanation

This is a follow up to the following changes:

Somehow I got confused with the top-level selectors:

Top-level selectors (e.g. File(id FileID)) become weird (File(id *File)) -- if you already have the file, what's the point of having a function to look it up? I've kept them as File(id FileID)

- Originally posted in #3598

In #3870 I just replaced every ID scalar with an object. Later, in #3936, I realized these fields with an id parameter looked weird by passing an object, so I added a union:

# before
def file(self, id: FileID) -> File: ...

# after hiding IDs
def file(self, id: File) -> File: ...

# follow-up to bring back ID here
def file(self, id: FileID | File) -> File: ...

Now I realize these should have been left alone:

def file(self, id: FileID) -> File: ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions