Skip to content
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

Merged
merged 5 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/extend-graphql-schema-nexus/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nexus/nexus-typegen.ts
nexus-types.ts
2 changes: 1 addition & 1 deletion examples/extend-graphql-schema-nexus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ type Query {
author(where: AuthorWhereUniqueInput!): Author
authorsCount(where: AuthorWhereInput! = {}): Int
keystone: KeystoneMeta!
nexusPosts(authorId: String, seconds: Int! = 600): [Post]!
nexusPosts(id: String!, seconds: Int! = 600): [Post]!
things: [NexusThing]!
}

Expand Down
29 changes: 19 additions & 10 deletions examples/extend-graphql-schema-nexus/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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 { mergeSchemas } from '@graphql-tools/schema';
import * as nexus from 'nexus';
import type { Context, Lists } from '.keystone/types';
import type { Lists } from '.keystone/types';

export const lists: Lists = {
Post: list({
Expand Down Expand Up @@ -40,18 +40,20 @@ export function extendGraphqlSchema(baseSchema: GraphQLSchema) {
t.field('nexusPosts', {
type: nexus.nonNull(nexus.list('Post')),
args: {
authorId: nexus.stringArg(),
id: nexus.nonNull(nexus.stringArg()),
Copy link
Member Author

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

seconds: nexus.nonNull(nexus.intArg({ default: 600 })),
},
async resolve(root, { authorId, seconds }, context: Context) {

async resolve(root, { id, seconds }, context) {
const cutoff = new Date(Date.now() - seconds * 1000);

return await context.db.Post.findMany({
where: {
...(authorId ? { author: { id: authorId } } : null),
publishDate: { gt: cutoff },
},
});
// 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[]>
},
});
},
Expand Down Expand Up @@ -85,6 +87,13 @@ export function extendGraphqlSchema(baseSchema: GraphQLSchema) {
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',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if I prefer this to context: Context, but, it's how nexus works

},
types: {
NexusThing,
NexusPostQuery,
Expand Down