Skip to content

Commit

Permalink
Reduce Nexus example to define extension inline (#8375)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Cousens <dcousens@users.noreply.github.com>
  • Loading branch information
dcousens and dcousens authored Mar 7, 2023
1 parent c6c9078 commit b91c11e
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 132 deletions.
4 changes: 2 additions & 2 deletions examples/extend-graphql-schema-graphql-tools/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ type Query {
authorsCount(where: AuthorWhereInput! = {}): Int
keystone: KeystoneMeta!

""" Return all posts for a user from the last <days> days """
recentPosts(id: ID!, days: Int! = 7): [Post]
""" Return all posts for a user from the last <seconds> seconds """
recentPosts(id: ID!, seconds: Int! = 600): [Post]

""" Compute statistics for a user """
stats(id: ID!): Statistics
Expand Down
18 changes: 8 additions & 10 deletions examples/extend-graphql-schema-graphql-tools/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const lists: Lists = {
}),
};

export const extendGraphqlSchema = (schema: GraphQLSchema) =>
mergeSchemas({
schemas: [schema],
export function extendGraphqlSchema(baseSchema: GraphQLSchema) {
return mergeSchemas({
schemas: [baseSchema],
typeDefs: `
type Mutation {
""" Publish a post """
Expand All @@ -45,8 +45,8 @@ export const extendGraphqlSchema = (schema: GraphQLSchema) =>
}
type Query {
""" Return all posts for a user from the last <days> days """
recentPosts(id: ID!, days: Int! = 7): [Post]
""" Return all posts for a user from the last <seconds> seconds """
recentPosts(id: ID!, seconds: Int! = 600): [Post]
""" Compute statistics for a user """
stats(id: ID!): Statistics
Expand Down Expand Up @@ -87,11 +87,8 @@ export const extendGraphqlSchema = (schema: GraphQLSchema) =>
},
},
Query: {
recentPosts: (root, { id, days }, context: Context) => {
// Create a date string <days> in the past from now()
const cutoff = new Date(
new Date().setUTCDate(new Date().getUTCDate() - days)
).toUTCString();
recentPosts: (root, { id, seconds }, context: 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.
Expand Down Expand Up @@ -132,3 +129,4 @@ export const extendGraphqlSchema = (schema: GraphQLSchema) =>
},
},
});
}
2 changes: 1 addition & 1 deletion examples/extend-graphql-schema-graphql-ts/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ type Query {
author(where: AuthorWhereUniqueInput!): Author
authorsCount(where: AuthorWhereInput! = {}): Int
keystone: KeystoneMeta!
recentPosts(id: ID!, days: Int! = 7): [Post!]
recentPosts(id: ID!, seconds: Int! = 600): [Post!]
stats(id: ID!): Statistics
}

Expand Down
9 changes: 3 additions & 6 deletions examples/extend-graphql-schema-graphql-ts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,10 @@ export const extendGraphqlSchema = graphql.extend(base => {
type: graphql.list(graphql.nonNull(base.object('Post'))),
args: {
id: graphql.arg({ type: graphql.nonNull(graphql.ID) }),
days: graphql.arg({ type: graphql.nonNull(graphql.Int), defaultValue: 7 }),
seconds: graphql.arg({ type: graphql.nonNull(graphql.Int), defaultValue: 600 }),
},
resolve(source, { id, days }, context: Context) {
// Create a date string <days> in the past from now()
const cutoff = new Date(
new Date().setUTCDate(new Date().getUTCDate() - days)
).toISOString();
resolve(source, { id, seconds }, context: 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.
Expand Down
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
6 changes: 2 additions & 4 deletions examples/extend-graphql-schema-nexus/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { config } from '@keystone-6/core';
import { mergeSchemas } from '@graphql-tools/schema';
import { fixPrismaPath } from '../example-utils';
import { lists } from './schema';
import { nexusSchema } from './nexus';
import { lists, extendGraphqlSchema } from './schema';

export default config({
db: {
Expand All @@ -13,5 +11,5 @@ export default config({
...fixPrismaPath,
},
lists,
extendGraphqlSchema: keystoneSchema => mergeSchemas({ schemas: [keystoneSchema, nexusSchema] }),
extendGraphqlSchema,
});
20 changes: 0 additions & 20 deletions examples/extend-graphql-schema-nexus/nexus/index.ts

This file was deleted.

46 changes: 0 additions & 46 deletions examples/extend-graphql-schema-nexus/nexus/types/Post.ts

This file was deleted.

27 changes: 0 additions & 27 deletions examples/extend-graphql-schema-nexus/nexus/types/Thing.ts

This file was deleted.

2 changes: 0 additions & 2 deletions examples/extend-graphql-schema-nexus/nexus/types/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion examples/extend-graphql-schema-nexus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@graphql-tools/schema": "^9.0.0",
"@keystone-6/core": "^5.0.0",
"graphql": "^16.6.0",
"nexus": "1.3.0"
"nexus": "^1.3.0"
},
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/extend-graphql-schema-nexus"
}
13 changes: 3 additions & 10 deletions examples/extend-graphql-schema-nexus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ type Query {
author(where: AuthorWhereUniqueInput!): Author
authorsCount(where: AuthorWhereInput! = {}): Int
keystone: KeystoneMeta!
nexusPosts(authorId: String, days: Int! = 7): [NexusPost]!
things: [Thing]!
nexusPosts(id: String!, seconds: Int! = 600): [Post]!
things: [NexusThing]!
}

type KeystoneMeta {
Expand Down Expand Up @@ -338,14 +338,7 @@ enum KeystoneAdminUISortDirection {
DESC
}

type NexusPost {
id: String
title: String
status: String
content: String
}

type Thing {
type NexusThing {
id: Int
title: String
}
78 changes: 77 additions & 1 deletion examples/extend-graphql-schema-nexus/schema.ts
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: {
Expand All @@ -28,3 +32,75 @@ 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,
},
types: {
NexusThing,
NexusPostQuery,
NexusThingQuery,
},

// Typescript output settings, probably something you might commit in dev
shouldGenerateArtifacts: process.env.NODE_ENV !== 'production',
outputs: {
typegen: path.join(process.cwd(), 'nexus-types.ts'),
},
contextType: {
module: path.join(process.cwd(), 'node_modules', '.keystone', 'types.d.ts'),
export: 'Context',
},
});
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11174,7 +11174,7 @@ next@^13.0.3:
"@next/swc-win32-ia32-msvc" "13.1.1"
"@next/swc-win32-x64-msvc" "13.1.1"

nexus@1.3.0:
nexus@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/nexus/-/nexus-1.3.0.tgz#d7e2671d48bf887e30e2815f509bbf4b0ee2a02b"
integrity sha512-w/s19OiNOs0LrtP7pBmD9/FqJHvZLmCipVRt6v1PM8cRUYIbhEswyNKGHVoC4eHZGPSnD+bOf5A3+gnbt0A5/A==
Expand Down

0 comments on commit b91c11e

Please sign in to comment.