-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Reduce Nexus example to define extension inline #8375
Changes from 4 commits
d75a708
b330c9f
ee829f8
123037d
a211c87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
nexus/nexus-typegen.ts | ||
nexus-types.ts |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import path from 'path'; | ||
import type { GraphQLSchema } from 'graphql'; | ||
import { list } from '@keystone-6/core'; | ||
import { allowAll } from '@keystone-6/core/access'; | ||
import { select, relationship, text, timestamp } from '@keystone-6/core/fields'; | ||
import * as nexus from 'nexus'; | ||
import type { Lists } from '.keystone/types'; | ||
|
||
export const lists = { | ||
export const lists: Lists = { | ||
Post: list({ | ||
access: allowAll, | ||
fields: { | ||
|
@@ -28,3 +32,72 @@ export const lists = { | |
}, | ||
}), | ||
}; | ||
|
||
export function extendGraphqlSchema(baseSchema: GraphQLSchema) { | ||
const NexusPostQuery = nexus.extendType({ | ||
type: 'Query', | ||
definition(t) { | ||
t.field('nexusPosts', { | ||
type: nexus.nonNull(nexus.list('Post')), | ||
args: { | ||
id: nexus.nonNull(nexus.stringArg()), | ||
seconds: nexus.nonNull(nexus.intArg({ default: 600 })), | ||
}, | ||
|
||
async resolve(root, { id, seconds }, context) { | ||
const cutoff = new Date(Date.now() - seconds * 1000); | ||
|
||
// Note we use `context.db.Post` here as we have a return type | ||
// of [Post], and this API provides results in the correct format. | ||
// If you accidentally use `context.query.Post` here you can expect problems | ||
// when accessing the fields in your GraphQL client. | ||
return context.db.Post.findMany({ | ||
where: { author: { id: { equals: id } }, publishDate: { gt: cutoff } }, | ||
}) as Promise<Lists.Post.Item[]>; // TODO: nexus doesn't like <readonly Post[]> | ||
}, | ||
}); | ||
}, | ||
}); | ||
|
||
const NexusThing = nexus.objectType({ | ||
name: 'NexusThing', | ||
definition(t) { | ||
t.int('id'); | ||
t.string('title'); | ||
}, | ||
}); | ||
|
||
const NexusThingQuery = nexus.extendType({ | ||
type: 'Query', | ||
definition(t) { | ||
t.nonNull.list.field('things', { | ||
type: NexusThing, | ||
resolve() { | ||
return [ | ||
{ id: 1, title: 'Keystone' }, | ||
{ id: 2, title: 'Prisma' }, | ||
{ id: 3, title: 'Nexus' }, | ||
]; | ||
}, | ||
}); | ||
}, | ||
}); | ||
|
||
return nexus.makeSchema({ | ||
mergeSchema: { | ||
schema: baseSchema, | ||
}, | ||
outputs: { | ||
typegen: path.join(process.cwd(), 'nexus-types.ts'), | ||
}, | ||
contextType: { | ||
module: path.join(process.cwd(), 'node_modules', '.keystone', 'types.d.ts'), | ||
export: 'Context', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if I prefer this to |
||
}, | ||
types: { | ||
NexusThing, | ||
NexusPostQuery, | ||
NexusThingQuery, | ||
}, | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this updated example is equivalent in fidelity to our |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inline with our other examples