Skip to content

Commit

Permalink
feat: ✨mediaPage as subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Banou26 committed Feb 28, 2024
1 parent 2b7a56c commit 49ff9e8
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 4 deletions.
39 changes: 38 additions & 1 deletion src/generated/graphql.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"mutationType": {
"name": "Mutation"
},
"subscriptionType": null,
"subscriptionType": {
"name": "Subscription"
},
"types": [
{
"kind": "OBJECT",
Expand Down Expand Up @@ -2825,6 +2827,41 @@
}
]
},
{
"kind": "OBJECT",
"name": "Subscription",
"fields": [
{
"name": "dummy",
"type": {
"kind": "SCALAR",
"name": "Any"
},
"args": []
},
{
"name": "mediaPage",
"type": {
"kind": "OBJECT",
"name": "MediaPage",
"ofType": null
},
"args": [
{
"name": "input",
"type": {
"kind": "NON_NULL",
"ofType": {
"kind": "SCALAR",
"name": "Any"
}
}
}
]
}
],
"interfaces": []
},
{
"kind": "OBJECT",
"name": "Team",
Expand Down
19 changes: 19 additions & 0 deletions src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,17 @@ export type ResourceEdge = HandleEdge & {
node: Resource;
};

export type Subscription = {
__typename?: 'Subscription';
dummy?: Maybe<Scalars['String']>;
mediaPage?: Maybe<MediaPage>;
};


export type SubscriptionMediaPageArgs = {
input: MediaPageInput;
};

export type Team = Handle & {
__typename?: 'Team';
handles: HandleConnection;
Expand Down Expand Up @@ -1198,6 +1209,7 @@ export type ResolversTypes = {
ResourceConnection: ResolverTypeWrapper<ResourceConnection>;
ResourceEdge: ResolverTypeWrapper<ResourceEdge>;
String: ResolverTypeWrapper<Scalars['String']>;
Subscription: ResolverTypeWrapper<{}>;
Team: ResolverTypeWrapper<Team>;
Uri: ResolverTypeWrapper<Scalars['Uri']>;
};
Expand Down Expand Up @@ -1266,6 +1278,7 @@ export type ResolversParentTypes = {
ResourceConnection: ResourceConnection;
ResourceEdge: ResourceEdge;
String: Scalars['String'];
Subscription: {};
Team: Team;
Uri: Scalars['Uri'];
};
Expand Down Expand Up @@ -1659,6 +1672,11 @@ export type ResourceEdgeResolvers<ContextType = ServerContext, ParentType extend
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type SubscriptionResolvers<ContextType = ServerContext, ParentType extends ResolversParentTypes['Subscription'] = ResolversParentTypes['Subscription']> = {
dummy?: SubscriptionResolver<Maybe<ResolversTypes['String']>, "dummy", ParentType, ContextType>;
mediaPage?: SubscriptionResolver<Maybe<ResolversTypes['MediaPage']>, "mediaPage", ParentType, ContextType, RequireFields<SubscriptionMediaPageArgs, 'input'>>;
};

export type TeamResolvers<ContextType = ServerContext, ParentType extends ResolversParentTypes['Team'] = ResolversParentTypes['Team']> = {
handles?: Resolver<ResolversTypes['HandleConnection'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
Expand Down Expand Up @@ -1715,6 +1733,7 @@ export type Resolvers<ContextType = ServerContext> = {
Resource?: ResourceResolvers<ContextType>;
ResourceConnection?: ResourceConnectionResolvers<ContextType>;
ResourceEdge?: ResourceEdgeResolvers<ContextType>;
Subscription?: SubscriptionResolvers<ContextType>;
Team?: TeamResolvers<ContextType>;
Uri?: GraphQLScalarType;
};
Expand Down
4 changes: 3 additions & 1 deletion src/generated/schema/resolvers.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ import { playbackSourcePage as Query_playbackSourcePage } from './resolvers/Q
import { Resource } from './resolvers/Resource';
import { ResourceConnection } from './resolvers/ResourceConnection';
import { ResourceEdge } from './resolvers/ResourceEdge';
import { dummy as Subscription_dummy } from './resolvers/Subscription/dummy';
import { mediaPage as Subscription_mediaPage } from './resolvers/Subscription/mediaPage';
import { Team } from './resolvers/Team';
import { Uri } from './resolvers/Uri';
export const resolvers: Resolvers = {
Query: { dummy: Query_dummy,episode: Query_episode,episodePage: Query_episodePage,media: Query_media,mediaPage: Query_mediaPage,origin: Query_origin,originAuthentication: Query_originAuthentication,originPage: Query_originPage,originUser: Query_originUser,originUserMediaPage: Query_originUserMediaPage,playbackSource: Query_playbackSource,playbackSourcePage: Query_playbackSourcePage },
Mutation: { dummy: Mutation_dummy,originAuthenticate: Mutation_originAuthenticate },

Subscription: { dummy: Subscription_dummy,mediaPage: Subscription_mediaPage },
CountryCode: CountryCode,
Date: Date,
Episode: Episode,
Expand Down
4 changes: 4 additions & 0 deletions src/generated/schema/resolvers/Subscription/dummy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { SubscriptionResolvers } from './../../types.generated';
export const dummy: NonNullable<SubscriptionResolvers['dummy']> = {
subscribe: async (_parent, _arg, _ctx) => { /* Implement Subscription.dummy resolver logic here */ },
}
4 changes: 4 additions & 0 deletions src/generated/schema/resolvers/Subscription/mediaPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { SubscriptionResolvers } from './../../types.generated';
export const mediaPage: NonNullable<SubscriptionResolvers['mediaPage']> = {
subscribe: async (_parent, _arg, _ctx) => { /* Implement Subscription.mediaPage resolver logic here */ },
}
2 changes: 1 addition & 1 deletion src/generated/schema/typeDefs.generated.ts

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/generated/schema/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,17 @@ export type ResourceEdge = HandleEdge & {
node: Resource;
};

export type Subscription = {
__typename?: 'Subscription';
dummy?: Maybe<Scalars['String']['output']>;
mediaPage?: Maybe<MediaPage>;
};


export type SubscriptionMediaPageArgs = {
input: MediaPageInput;
};

export type Team = Handle & {
__typename?: 'Team';
handles: HandleConnection;
Expand Down Expand Up @@ -1190,6 +1201,7 @@ export type ResolversTypes = {
Resource: ResolverTypeWrapper<Resource>;
ResourceConnection: ResolverTypeWrapper<ResourceConnection>;
ResourceEdge: ResolverTypeWrapper<ResourceEdge>;
Subscription: ResolverTypeWrapper<{}>;
Team: ResolverTypeWrapper<Team>;
Uri: ResolverTypeWrapper<Scalars['Uri']['output']>;
};
Expand Down Expand Up @@ -1258,6 +1270,7 @@ export type ResolversParentTypes = {
Resource: Resource;
ResourceConnection: ResourceConnection;
ResourceEdge: ResourceEdge;
Subscription: {};
Team: Team;
Uri: Scalars['Uri']['output'];
};
Expand Down Expand Up @@ -1651,6 +1664,11 @@ export type ResourceEdgeResolvers<ContextType = any, ParentType extends Resolver
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type SubscriptionResolvers<ContextType = any, ParentType extends ResolversParentTypes['Subscription'] = ResolversParentTypes['Subscription']> = {
dummy?: SubscriptionResolver<Maybe<ResolversTypes['String']>, "dummy", ParentType, ContextType>;
mediaPage?: SubscriptionResolver<Maybe<ResolversTypes['MediaPage']>, "mediaPage", ParentType, ContextType, RequireFields<SubscriptionMediaPageArgs, 'input'>>;
};

export type TeamResolvers<ContextType = any, ParentType extends ResolversParentTypes['Team'] = ResolversParentTypes['Team']> = {
handles?: Resolver<ResolversTypes['HandleConnection'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
Expand Down Expand Up @@ -1707,6 +1725,7 @@ export type Resolvers<ContextType = any> = {
Resource?: ResourceResolvers<ContextType>;
ResourceConnection?: ResourceConnectionResolvers<ContextType>;
ResourceEdge?: ResourceEdgeResolvers<ContextType>;
Subscription?: SubscriptionResolvers<ContextType>;
Team?: TeamResolvers<ContextType>;
Uri?: GraphQLScalarType;
};
Expand Down
4 changes: 4 additions & 0 deletions src/graphql/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ extend type Query {
playbackSource(input: PlaybackSourceInput): PlaybackSource
}
extend type Subscription {
mediaPage(input: MediaPageInput!): MediaPage
}
"""
Media is a type of handle that represents a media.
It generally represents a Movie, TV Show, Game, Package, ect...
Expand Down
4 changes: 4 additions & 0 deletions src/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ type Query {
type Mutation {
dummy: String
}
type Subscription {
dummy: String
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
`
3 changes: 2 additions & 1 deletion src/urql/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, fetchExchange, gql } from 'urql';
import { Client, fetchExchange, gql, subscriptionExchange } from 'urql';
import { cacheExchange } from '@urql/exchange-graphcache'
import { YogaServerInstance } from 'graphql-yoga'
import { devtoolsExchange } from '@urql/devtools'
Expand Down Expand Up @@ -149,6 +149,7 @@ export const makeScannarrClient = (
const client = new Client({
url: 'http://d/graphql',
exchanges: [devtoolsExchange, cache, fetchExchange],
fetchSubscriptions: true,
fetch: async (input: RequestInfo | URL, init?: RequestInit | undefined) => {
const { body, headers } = await handleRequest(input, init)
return new Response(body, { headers: Object.fromEntries(headers.entries()) })
Expand Down
32 changes: 32 additions & 0 deletions src/urql/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,38 @@ export const serverResolvers = ({ origins, context }: { origins: any[], context?
nodes: scannarrHandles.map(media => populateMedia(media))
}
}
},
Subscription: {
mediaPage: {
async *subscribe(parent, args, ctx, info) {
const modifiedCtx = {
...ctx,
params: {
...ctx.params,
query: ctx.params.query.replace('subscription', 'query')
}
}
modifiedCtx.request.headers.set('accept', 'application/graphql-response+json, application/graphql+json, application/json, text/event-stream, multipart/mixed')
const _results = getOriginResultsStreamed({ ctx: modifiedCtx, origins, context })
let results: any[] = []
for await (const result of _results) {
if (result.error) console.error(result.error)
if (!result.data.mediaPage) continue
results = [...results, result]

const { scannarrHandles } = groupRelatedHandles({
typename: 'Media',
results: (results?.flatMap(results => results.data.mediaPage?.nodes ?? []) ?? []) as Media[]
})

yield {
mediaPage: {
nodes: scannarrHandles.map(media => populateMedia(media))
}
}
}
}
}
}
})

Expand Down

0 comments on commit 49ff9e8

Please sign in to comment.