Created
August 17, 2019 18:11
-
-
Save samkingco/53e77e04aeb07d28abf1d8d4730f27f1 to your computer and use it in GitHub Desktop.
GraphQL resolver type issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Resolvers } from "./schema-types"; | |
| const users = [ | |
| { | |
| id: "user-1", | |
| name: "Sam", | |
| commentIds: ["comment-2"] | |
| } | |
| ]; | |
| const comments = [ | |
| { | |
| id: "comment-1", | |
| text: "comment one", | |
| userId: "user-1" | |
| }, | |
| { | |
| id: "comment-2", | |
| text: "comment two", | |
| userId: "user-1" | |
| } | |
| ]; | |
| export const resolvers1: Resolvers = { | |
| Query: { | |
| user: () => ({ | |
| ...users[0], | |
| comments: users[0].commentIds.map(id => comments.find(i => i.id === id)) | |
| }), | |
| comments: () => | |
| comments.map(comment => ({ | |
| ...comment, | |
| user: users.find(user => user.id === comment.userId) | |
| })) | |
| } | |
| }; | |
| export const resolvers2: Resolvers = { | |
| Query: { | |
| user: () => users[0], | |
| comments: () => comments | |
| }, | |
| User: { | |
| comments: user => | |
| comments.filter(comment => user.comments.includes(comment.id)) | |
| }, | |
| Comment: { | |
| user: comment => users.find(user => comment.user) | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Auto generated file | |
| import { GraphQLResolveInfo } from 'graphql'; | |
| export type Maybe<T> = T | null; | |
| /** All built-in and custom scalars, mapped to their actual values */ | |
| export type Scalars = { | |
| ID: string, | |
| String: string, | |
| Boolean: boolean, | |
| Int: number, | |
| Float: number, | |
| }; | |
| export type Comment = { | |
| __typename?: 'Comment', | |
| id: Scalars['String'], | |
| text: Scalars['String'], | |
| user: User, | |
| }; | |
| export type Query = { | |
| __typename?: 'Query', | |
| user?: Maybe<User>, | |
| comments?: Maybe<Array<Maybe<Comment>>>, | |
| }; | |
| export type User = { | |
| __typename?: 'User', | |
| id: Scalars['String'], | |
| name: Scalars['String'], | |
| comments: Array<Maybe<Comment>>, | |
| }; | |
| export type WithIndex<TObject> = TObject & Record<string, any>; | |
| export type ResolversObject<TObject> = WithIndex<TObject>; | |
| export type ResolverTypeWrapper<T> = Promise<T> | T; | |
| export type ResolverFn<TResult, TParent, TContext, TArgs> = ( | |
| parent: TParent, | |
| args: TArgs, | |
| context: TContext, | |
| info: GraphQLResolveInfo | |
| ) => Promise<TResult> | TResult; | |
| export type StitchingResolver<TResult, TParent, TContext, TArgs> = { | |
| fragment: string; | |
| resolve: ResolverFn<TResult, TParent, TContext, TArgs>; | |
| }; | |
| export type Resolver<TResult, TParent = {}, TContext = {}, TArgs = {}> = | |
| | ResolverFn<TResult, TParent, TContext, TArgs> | |
| | StitchingResolver<TResult, TParent, TContext, TArgs>; | |
| export type SubscriptionSubscribeFn<TResult, TParent, TContext, TArgs> = ( | |
| parent: TParent, | |
| args: TArgs, | |
| context: TContext, | |
| info: GraphQLResolveInfo | |
| ) => AsyncIterator<TResult> | Promise<AsyncIterator<TResult>>; | |
| export type SubscriptionResolveFn<TResult, TParent, TContext, TArgs> = ( | |
| parent: TParent, | |
| args: TArgs, | |
| context: TContext, | |
| info: GraphQLResolveInfo | |
| ) => TResult | Promise<TResult>; | |
| export interface SubscriptionSubscriberObject<TResult, TKey extends string, TParent, TContext, TArgs> { | |
| subscribe: SubscriptionSubscribeFn<{ [key in TKey]: TResult }, TParent, TContext, TArgs>; | |
| resolve?: SubscriptionResolveFn<TResult, { [key in TKey]: TResult }, TContext, TArgs>; | |
| } | |
| export interface SubscriptionResolverObject<TResult, TParent, TContext, TArgs> { | |
| subscribe: SubscriptionSubscribeFn<any, TParent, TContext, TArgs>; | |
| resolve: SubscriptionResolveFn<TResult, any, TContext, TArgs>; | |
| } | |
| export type SubscriptionObject<TResult, TKey extends string, TParent, TContext, TArgs> = | |
| | SubscriptionSubscriberObject<TResult, TKey, TParent, TContext, TArgs> | |
| | SubscriptionResolverObject<TResult, TParent, TContext, TArgs>; | |
| export type SubscriptionResolver<TResult, TKey extends string, TParent = {}, TContext = {}, TArgs = {}> = | |
| | ((...args: any[]) => SubscriptionObject<TResult, TKey, TParent, TContext, TArgs>) | |
| | SubscriptionObject<TResult, TKey, TParent, TContext, TArgs>; | |
| export type TypeResolveFn<TTypes, TParent = {}, TContext = {}> = ( | |
| parent: TParent, | |
| context: TContext, | |
| info: GraphQLResolveInfo | |
| ) => Maybe<TTypes>; | |
| export type NextResolverFn<T> = () => Promise<T>; | |
| export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs = {}> = ( | |
| next: NextResolverFn<TResult>, | |
| parent: TParent, | |
| args: TArgs, | |
| context: TContext, | |
| info: GraphQLResolveInfo | |
| ) => TResult | Promise<TResult>; | |
| /** Mapping between all available schema types and the resolvers types */ | |
| export type ResolversTypes = ResolversObject<{ | |
| Query: ResolverTypeWrapper<{}>, | |
| User: ResolverTypeWrapper<User>, | |
| String: ResolverTypeWrapper<Scalars['String']>, | |
| Comment: ResolverTypeWrapper<Comment>, | |
| Boolean: ResolverTypeWrapper<Scalars['Boolean']>, | |
| }>; | |
| /** Mapping between all available schema types and the resolvers parents */ | |
| export type ResolversParentTypes = ResolversObject<{ | |
| Query: {}, | |
| User: User, | |
| String: Scalars['String'], | |
| Comment: Comment, | |
| Boolean: Scalars['Boolean'], | |
| }>; | |
| export type CommentResolvers<ContextType = any, ParentType extends ResolversParentTypes['Comment'] = ResolversParentTypes['Comment']> = ResolversObject<{ | |
| id?: Resolver<ResolversTypes['String'], ParentType, ContextType>, | |
| text?: Resolver<ResolversTypes['String'], ParentType, ContextType>, | |
| user?: Resolver<ResolversTypes['User'], ParentType, ContextType>, | |
| }>; | |
| export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = ResolversObject<{ | |
| user?: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType>, | |
| comments?: Resolver<Maybe<Array<Maybe<ResolversTypes['Comment']>>>, ParentType, ContextType>, | |
| }>; | |
| export type UserResolvers<ContextType = any, ParentType extends ResolversParentTypes['User'] = ResolversParentTypes['User']> = ResolversObject<{ | |
| id?: Resolver<ResolversTypes['String'], ParentType, ContextType>, | |
| name?: Resolver<ResolversTypes['String'], ParentType, ContextType>, | |
| comments?: Resolver<Array<Maybe<ResolversTypes['Comment']>>, ParentType, ContextType>, | |
| }>; | |
| export type Resolvers<ContextType = any> = ResolversObject<{ | |
| Comment?: CommentResolvers<ContextType>, | |
| Query?: QueryResolvers<ContextType>, | |
| User?: UserResolvers<ContextType>, | |
| }>; | |
| /** | |
| * @deprecated | |
| * Use "Resolvers" root object instead. If you wish to get "IResolvers", add "typesPrefix: I" to your config. | |
| */ | |
| export type IResolvers<ContextType = any> = Resolvers<ContextType>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const schema = ` | |
| type Query { | |
| user: User | |
| comments: [Comment]! | |
| } | |
| type User { | |
| id: String! | |
| name: String! | |
| comments: [Comment]! | |
| } | |
| type Comment { | |
| id: String! | |
| text: String! | |
| user: User! | |
| } | |
| `; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment