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

Convert cache-hints test to typescript #5207

Merged
merged 2 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions tests/api-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@keystone-next/test-utils-legacy": "^14.0.0",
"@keystone-next/types": "^15.0.0",
"@keystone-next/utils-legacy": "^7.0.0",
"apollo-cache-control": "^0.11.6",
"express": "^4.17.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,93 +1,96 @@
const { Integer, Text, Relationship } = require('@keystone-next/fields-legacy');
const {
import { CacheScope } from 'apollo-cache-control';
import { text, relationship, integer } from '@keystone-next/fields';
import {
multiAdapterRunners,
setupServer,
setupFromConfig,
networkedGraphqlRequest,
} = require('@keystone-next/test-utils-legacy');
const { createItems } = require('@keystone-next/server-side-graphql-client-legacy');

function setupKeystone(adapterName) {
return setupServer({
testConfig,
AdapterName,
} from '@keystone-next/test-utils-legacy';
// @ts-ignore
import { createItems } from '@keystone-next/server-side-graphql-client-legacy';
import { list, createSchema, graphQLSchemaExtension } from '@keystone-next/keystone/schema';
import { KeystoneContext } from '@keystone-next/types';

function setupKeystone(adapterName: AdapterName) {
return setupFromConfig({
adapterName,
createLists: keystone => {
keystone.createList('Post', {
fields: {
title: { type: Text },
author: { type: Relationship, ref: 'User.posts', many: true },
},
cacheHint: {
scope: 'PUBLIC',
maxAge: 100,
},
});

keystone.createList('User', {
fields: {
name: {
type: Text,
cacheHint: {
maxAge: 80,
},
config: testConfig({
lists: createSchema({
Post: list({
fields: {
title: text(),
author: relationship({ ref: 'User.posts', many: true }),
},
favNumber: {
type: Integer,
cacheHint: {
maxAge: 10,
scope: 'PRIVATE',
graphql: {
// @ts-ignore
cacheHint: { scope: CacheScope.Public, maxAge: 100 },
},
}),
User: list({
fields: {
name: text({
// @ts-ignore
graphql: { cacheHint: { maxAge: 80 } },
}),
favNumber: integer({
// @ts-ignore
graphql: { cacheHint: { maxAge: 10, scope: CacheScope.Private } },
}),
posts: relationship({ ref: 'Post.author', many: true }),
},
graphql: {
// @ts-ignore
cacheHint: ({ results, operationName, meta }) => {
if (meta) {
return { scope: CacheScope.Public, maxAge: 90 };
}
if (operationName === 'complexQuery') {
return { maxAge: 1 };
}
if (results.length === 0) {
return { maxAge: 5 };
}
return { maxAge: 100 };
},
},
posts: { type: Relationship, ref: 'Post.author', many: true },
},
cacheHint: ({ results, operationName, meta }) => {
if (meta) {
return {
scope: 'PUBLIC',
maxAge: 90,
};
}),
}),
extendGraphqlSchema: graphQLSchemaExtension({
typeDefs: `
type MyType {
original: Int
double: Float
}
if (operationName === 'complexQuery') {
return {
maxAge: 1,
};

type Mutation {
triple(x: Int): Int
}
if (results.length === 0) {
return {
maxAge: 5,
};

type Query {
double(x: Int): MyType
}
return {
maxAge: 100,
};
`,
resolvers: {
Query: {
// @ts-ignore
double: (root, { x }, context, info) => {
info.cacheControl.setCacheHint({ scope: CacheScope.Public, maxAge: 100 });
return { original: x, double: 2.0 * x };
},
},
Mutation: {
triple: (root, { x }) => 3 * x,
},
},
});

// These should be added to the system and tested when we implement cacheHints
// keystone.extendGraphQLSchema({
// types: [{ type: 'type MyType { original: Int, double: Float }' }],
// queries: [
// {
// schema: 'double(x: Int): MyType',
// resolver: (_, { x }) => ({ original: x, double: 2.0 * x }),
// cacheHint: {
// scope: 'PUBLIC',
// maxAge: 100,
// },
// },
// ],
// mutations: [
// {
// schema: 'triple(x: Int): Int',
// resolver: (_, { x }) => 3 * x,
// },
// ],
// });
},
}),
}),
});
}

const addFixtures = async keystone => {
const addFixtures = async (context: KeystoneContext) => {
const users = await createItems({
keystone,
context,
listKey: 'User',
items: [
{ data: { name: 'Jess', favNumber: 1 } },
Expand All @@ -97,7 +100,7 @@ const addFixtures = async keystone => {
});

const posts = await createItems({
keystone,
context,
listKey: 'Post',
items: [
{ data: { author: { connect: [{ id: users[0].id }] }, title: 'One author' } },
Expand All @@ -124,8 +127,8 @@ multiAdapterRunners().map(({ runner, adapterName }) =>
describe('cache hints', () => {
test(
'users',
runner(setupKeystone, async ({ keystone, app }) => {
await addFixtures(keystone);
runner(setupKeystone, async ({ context, app }) => {
await addFixtures(context);

// Basic query
let { data, errors, res } = await networkedGraphqlRequest({
Expand Down Expand Up @@ -230,8 +233,8 @@ multiAdapterRunners().map(({ runner, adapterName }) =>

test(
'posts',
runner(setupKeystone, async ({ keystone, app }) => {
await addFixtures(keystone);
runner(setupKeystone, async ({ context, app }) => {
await addFixtures(context);
// The Post list has a static cache hint

// Basic query
Expand Down Expand Up @@ -327,8 +330,8 @@ multiAdapterRunners().map(({ runner, adapterName }) =>

test(
'mutations',
runner(setupKeystone, async ({ keystone, app }) => {
const { posts } = await addFixtures(keystone);
runner(setupKeystone, async ({ context, app }) => {
const { posts } = await addFixtures(context);

// Mutation responses shouldn't be cached.
// Here's a smoke test to make sure they still work.
Expand All @@ -354,8 +357,8 @@ multiAdapterRunners().map(({ runner, adapterName }) =>
// eslint-disable-next-line jest/no-disabled-tests
test.skip(
'extendGraphQLSchemaQueries',
runner(setupKeystone, async ({ keystone, app }) => {
await addFixtures(keystone);
runner(setupKeystone, async ({ context, app }) => {
await addFixtures(context);

// Basic query
let { data, errors, res } = await networkedGraphqlRequest({
Expand All @@ -380,8 +383,8 @@ multiAdapterRunners().map(({ runner, adapterName }) =>
// eslint-disable-next-line jest/no-disabled-tests
test.skip(
'extendGraphQLSchemaMutations',
runner(setupKeystone, async ({ keystone, app }) => {
await addFixtures(keystone);
runner(setupKeystone, async ({ context, app }) => {
await addFixtures(context);

// Mutation responses shouldn't be cached.
// Here's a smoke test to make sure they still work.
Expand Down