-
Notifications
You must be signed in to change notification settings - Fork 18
Execution result type includes the full schema #12
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
Comments
Agreed, this is very almost perfect, except for that point... ill have a quick play and see if I can crack it.. |
@Pajn I can maybe see how mapped types can be used to pick the first level properties based on the query, but I can't see a solution on how to make it work with an arbitrarily nested GraphQL query/response. Possibly there is a way to have at least some N-levels-down solution... |
Okay, looked at conditional types. This shows more promise |
It can definitely be done. I had to head out but will take a look at it
tomorrow or the day after if you dont work it out before hand :)
|
@mikecann this is what I have so far. Appears to be working recursively. interface User {
id: string
name: string
email: string | null
posts: Post[]
}
interface Post {
id: string
title: string
description: string | null
author: User
}
interface UserRequest {
id?: boolean | number
name?: boolean | number
email?: boolean | number
posts?: PostRequest
}
interface PostRequest {
id?: boolean | number
title?: boolean | number
description?: boolean | number
author?: UserRequest
}
type UserPartial<R extends UserRequest> =
(R extends { id: boolean | number } ? { id: string } : {}) &
(R extends { name: boolean | number } ? { name: string } : {}) &
(R extends { email: boolean | number } ? { email: string | null } : {}) &
(R extends { posts: PostRequest } ? { posts: PostPartial<R['posts']>[] } : {})
type PostPartial<R extends PostRequest> =
(R extends { id: boolean | number } ? { id: string } : {}) &
(R extends { title: boolean | number } ? { title: string } : {}) &
(R extends { description: boolean | number } ? { description: string | null } : {}) &
(R extends { author: UserRequest } ? { author: UserPartial<R['author']> } : {})
const query = <T>(r: T extends UserRequest ? UserRequest : T): UserPartial<T> => {
throw new Error('')
} |
Oh lol, you beat me too it, this is what I just came up with:
|
Ill see if I can make mine recursive with your types.. |
gotta go to bed now, but its almost there, just needs to handle arrays as part of that conditional. |
Oh man I must have been tired, because the above doesnt work for recursive types.. |
Here this solves it recursively, it makes your brain bleed a little trying to work it out tho..
|
BTW using the above one could make a very nice fluent syntax using just a bit more typing and proxy objects.
The above is using the schema from one of my own projects but will work with any object type. ... thinking about it more one could just export GraphQL AST much like https://github.com/apollographql/graphql-tag does, then it could be consumed by Apollo Client and you wouldnt have to write your own client with caching and all other other features that Apollo supports.. maybe https://github.com/prisma/nexus already does most of this.. |
do you think your solution can fit the export type UserPartial<
R extends UserRequest,
F1 extends UserRequest = _FR<R['friends'], UserRequest>,
F2 extends PostRequest = _FR<R['posts'], PostRequest>,
F3 extends PetRequest = _FR<R['pets'], PetRequest>
> = (R extends Required<Pick<R, 'id'>> ? { id: ID } : {}) &
(R extends Required<Pick<R, 'username'>> ? { username: String } : {}) &
(R extends Required<Pick<R, 'email'>> ? { email: String } : {}) &
(R extends Required<Pick<R, 'wasEmployed'>> ? { wasEmployed: Boolean | null } : {}) &
(R extends Required<Pick<R, 'friends'>> ? { friends: UserPartial<F1>[] | null } : {}) &
(R extends Required<Pick<R, 'posts'>> ? { posts: PostPartial<F2>[] } : {}) &
(R extends Required<Pick<R, 'pets'>> ? { pets: PetPartial<F3>[] } : {}) It also does not yet implements
not exactly sure what you have there. is the goal to get the same API as it is now, but with less pre-generated ts code?
Currently, you can also use Apollo client here easily, inside the fetching mechanism |
@helios1138 Awesome. I think you should go with your technique if you can get it to work. I spent half a day trying to come up with a clever non / minimal codegen solution when I should have been working on my project and although theoretically I could make it work I just dont have the time for it right now. BTW I even spent a coupple of hours on a pair programming session with a TS evangelist who works closely with the compiler team on this problem :) Its a really fun problem to solve, wish I had a bit more time on it. For now tho, go with your solution if it works. Im keen to use it in my project! |
Hello, I'm very interested by this client and I think this part is the last missing piece. 😍
I'd like a fully typed equivalent of graphql-tag to pass to any client (in my case I'd use react-query). How can I help? |
Graphql Zeus uses a generic type
View the code here We can simply copy paste that code and have the feature right now |
It would be very nice if the result type only included the selected fields.
I imagine this could be solvable by using a combination of mapped types and conditional types.
Have you looked into this at and have some experience on the possibility?
The text was updated successfully, but these errors were encountered: