Skip to content

Commit 42c2615

Browse files
committed
Finish Chapter 8
1 parent 29dedf1 commit 42c2615

File tree

3 files changed

+114
-11
lines changed

3 files changed

+114
-11
lines changed

nexus-typegen.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ declare global {
2929
}
3030

3131
export interface NexusGenInputs {
32+
LinkOrderByInput: { // input type
33+
createdAt?: NexusGenEnums['Sort'] | null; // Sort
34+
description?: NexusGenEnums['Sort'] | null; // Sort
35+
url?: NexusGenEnums['Sort'] | null; // Sort
36+
}
3237
}
3338

3439
export interface NexusGenEnums {
40+
Sort: "asc" | "desc"
3541
}
3642

3743
export interface NexusGenScalars {
@@ -48,6 +54,10 @@ export interface NexusGenObjects {
4854
token: string; // String!
4955
user: NexusGenRootTypes['User']; // User!
5056
}
57+
Feed: { // root type
58+
count: number; // Int!
59+
links: NexusGenRootTypes['Link'][]; // [Link!]!
60+
}
5161
Link: { // root type
5262
createdAt: NexusGenScalars['DateTime']; // DateTime!
5363
description: string; // String!
@@ -75,13 +85,17 @@ export interface NexusGenUnions {
7585

7686
export type NexusGenRootTypes = NexusGenObjects
7787

78-
export type NexusGenAllTypes = NexusGenRootTypes & NexusGenScalars
88+
export type NexusGenAllTypes = NexusGenRootTypes & NexusGenScalars & NexusGenEnums
7989

8090
export interface NexusGenFieldTypes {
8191
AuthPayload: { // field return type
8292
token: string; // String!
8393
user: NexusGenRootTypes['User']; // User!
8494
}
95+
Feed: { // field return type
96+
count: number; // Int!
97+
links: NexusGenRootTypes['Link'][]; // [Link!]!
98+
}
8599
Link: { // field return type
86100
createdAt: NexusGenScalars['DateTime']; // DateTime!
87101
description: string; // String!
@@ -97,7 +111,7 @@ export interface NexusGenFieldTypes {
97111
vote: NexusGenRootTypes['Vote'] | null; // Vote
98112
}
99113
Query: { // field return type
100-
feed: NexusGenRootTypes['Link'][]; // [Link!]!
114+
feed: NexusGenRootTypes['Feed']; // Feed!
101115
}
102116
User: { // field return type
103117
email: string; // String!
@@ -117,6 +131,10 @@ export interface NexusGenFieldTypeNames {
117131
token: 'String'
118132
user: 'User'
119133
}
134+
Feed: { // field return type name
135+
count: 'Int'
136+
links: 'Link'
137+
}
120138
Link: { // field return type name
121139
createdAt: 'DateTime'
122140
description: 'String'
@@ -132,7 +150,7 @@ export interface NexusGenFieldTypeNames {
132150
vote: 'Vote'
133151
}
134152
Query: { // field return type name
135-
feed: 'Link'
153+
feed: 'Feed'
136154
}
137155
User: { // field return type name
138156
email: 'String'
@@ -166,6 +184,14 @@ export interface NexusGenArgTypes {
166184
linkId: number; // Int!
167185
}
168186
}
187+
Query: {
188+
feed: { // args
189+
filter?: string | null; // String
190+
orderBy?: NexusGenInputs['LinkOrderByInput'][] | null; // [LinkOrderByInput!]
191+
skip?: number | null; // Int
192+
take?: number | null; // Int
193+
}
194+
}
169195
}
170196

171197
export interface NexusGenAbstractTypeMembers {
@@ -176,9 +202,9 @@ export interface NexusGenTypeInterfaces {
176202

177203
export type NexusGenObjectNames = keyof NexusGenObjects;
178204

179-
export type NexusGenInputNames = never;
205+
export type NexusGenInputNames = keyof NexusGenInputs;
180206

181-
export type NexusGenEnumNames = never;
207+
export type NexusGenEnumNames = keyof NexusGenEnums;
182208

183209
export type NexusGenInterfaceNames = never;
184210

schema.graphql

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ A date-time string at UTC, such as 2007-12-03T10:15:30Z, compliant with the `dat
1212
"""
1313
scalar DateTime
1414

15+
type Feed {
16+
count: Int!
17+
links: [Link!]!
18+
}
19+
1520
type Link {
1621
createdAt: DateTime!
1722
description: String!
@@ -21,6 +26,12 @@ type Link {
2126
voters: [User!]!
2227
}
2328

29+
input LinkOrderByInput {
30+
createdAt: Sort
31+
description: Sort
32+
url: Sort
33+
}
34+
2435
type Mutation {
2536
login(email: String!, password: String!): AuthPayload!
2637
post(description: String!, url: String!): Link!
@@ -29,7 +40,12 @@ type Mutation {
2940
}
3041

3142
type Query {
32-
feed: [Link!]!
43+
feed(filter: String, orderBy: [LinkOrderByInput!], skip: Int, take: Int): Feed!
44+
}
45+
46+
enum Sort {
47+
asc
48+
desc
3349
}
3450

3551
type User {

src/graphql/Link.ts

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
import { extendType, nonNull, objectType, stringArg } from "nexus";
1+
import {
2+
extendType,
3+
nonNull,
4+
objectType,
5+
stringArg,
6+
intArg,
7+
inputObjectType,
8+
enumType,
9+
arg,
10+
list,
11+
} from "nexus";
12+
import { Prisma } from "@prisma/client";
213

314
export const Link = objectType({
415
name: "Link",
@@ -26,13 +37,63 @@ export const Link = objectType({
2637
},
2738
});
2839

40+
export const Feed = objectType({
41+
name: "Feed",
42+
definition(t) {
43+
t.nonNull.list.nonNull.field("links", { type: Link });
44+
t.nonNull.int("count");
45+
},
46+
});
47+
48+
export const LinkOrderByInput = inputObjectType({
49+
name: "LinkOrderByInput",
50+
definition(t) {
51+
t.field("description", { type: Sort });
52+
t.field("url", { type: Sort });
53+
t.field("createdAt", { type: Sort });
54+
},
55+
});
56+
57+
export const Sort = enumType({
58+
name: "Sort",
59+
members: ["asc", "desc"],
60+
});
61+
2962
export const LinkQuery = extendType({
3063
type: "Query",
3164
definition(t) {
32-
t.nonNull.list.nonNull.field("feed", {
33-
type: "Link",
34-
resolve(parent, args, context) {
35-
return context.prisma.link.findMany();
65+
t.nonNull.field("feed", {
66+
type: "Feed",
67+
args: {
68+
filter: stringArg(),
69+
skip: intArg(),
70+
take: intArg(),
71+
orderBy: arg({ type: list(nonNull(LinkOrderByInput)) }),
72+
},
73+
async resolve(parent, args, context) {
74+
const where = args.filter
75+
? {
76+
OR: [
77+
{ description: { contains: args.filter } },
78+
{ url: { contains: args.filter } },
79+
],
80+
}
81+
: {};
82+
const links = await context.prisma.link.findMany({
83+
where,
84+
skip: args?.skip as number | undefined,
85+
take: args?.take as number | undefined,
86+
orderBy: args?.orderBy as
87+
| Prisma.Enumerable<Prisma.LinkOrderByWithRelationInput>
88+
| undefined,
89+
});
90+
91+
const count = await context.prisma.link.count({ where });
92+
93+
return {
94+
links,
95+
count,
96+
};
3697
},
3798
});
3899
},

0 commit comments

Comments
 (0)