Skip to content

Commit

Permalink
Add Context types for graphql-ts export (#7877)
Browse files Browse the repository at this point in the history
* merge graphql schemas with list schemas

* add contextualised type capability for graphql-ts extensions

* add changeset

Co-authored-by: Daniel Cousens <dcousens@users.noreply.github.com>
  • Loading branch information
dcousens and dcousens committed Sep 15, 2022
1 parent 52baf28 commit 65c315f
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 201 deletions.
5 changes: 5 additions & 0 deletions .changeset/oh-my-graphql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': patch
---

Adds contextualised types when using the `graphql` export for GraphQL schema extensions
92 changes: 0 additions & 92 deletions examples/extend-graphql-schema-graphql-ts/custom-schema.ts

This file was deleted.

3 changes: 1 addition & 2 deletions examples/extend-graphql-schema-graphql-ts/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { config } from '@keystone-6/core';
import { lists } from './schema';
import { extendGraphqlSchema } from './custom-schema';
import { lists, extendGraphqlSchema } from './schema';

export default config({
db: {
Expand Down
89 changes: 88 additions & 1 deletion examples/extend-graphql-schema-graphql-ts/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { list } from '@keystone-6/core';
import { graphql, list } from '@keystone-6/core';
import { select, relationship, text, timestamp } from '@keystone-6/core/fields';
import { Context, Lists } from '.keystone/types';

export const lists = {
export const lists: Lists = {
Post: list({
fields: {
title: text({ validation: { isRequired: true } }),
Expand All @@ -25,3 +27,88 @@ export const lists = {
},
}),
};

export const extendGraphqlSchema = graphql.extend(base => {
const Statistics = graphql.object<{ authorId: string }>()({
name: 'Statistics',
fields: {
draft: graphql.field({
type: graphql.Int,
resolve({ authorId }, args, context: Context) {
return context.query.Post.count({
where: { author: { id: { equals: authorId } }, status: { equals: 'draft' } },
});
},
}),
published: graphql.field({
type: graphql.Int,
resolve({ authorId }, args, context: Context) {
return context.query.Post.count({
where: { author: { id: { equals: authorId } }, status: { equals: 'published' } },
});
},
}),
latest: graphql.field({
type: base.object('Post'),
async resolve({ authorId }, args, context: Context) {
const [post] = await context.db.Post.findMany({
take: 1,
orderBy: { publishDate: 'desc' },
where: { author: { id: { equals: authorId } } },
});
return post;
},
}),
},
});
return {
mutation: {
publishPost: graphql.field({
// base.object will return an object type from the existing schema
// with the name provided or throw if it doesn't exist
type: base.object('Post'),
args: { id: graphql.arg({ type: graphql.nonNull(graphql.ID) }) },
resolve(source, { id }, context: Context) {
// 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.updateOne({
where: { id },
data: { status: 'published', publishDate: new Date().toISOString() },
});
},
}),
},
query: {
recentPosts: graphql.field({
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 }),
},
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();

// 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 } },
});
},
}),
stats: graphql.field({
type: Statistics,
args: { id: graphql.arg({ type: graphql.nonNull(graphql.ID) }) },
resolve(source, { id }) {
return { authorId: id };
},
}),
},
};
});
101 changes: 0 additions & 101 deletions examples/extend-graphql-schema/custom-schema.ts

This file was deleted.

3 changes: 1 addition & 2 deletions examples/extend-graphql-schema/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { config } from '@keystone-6/core';
import { lists } from './schema';
import { extendGraphqlSchema } from './custom-schema';
import { lists, extendGraphqlSchema } from './schema';

export default config({
db: {
Expand Down
Loading

0 comments on commit 65c315f

Please sign in to comment.