mirror of
https://github.com/vale981/apollo-server
synced 2025-03-05 17:51:40 -05:00
Fix apollo-cache-control
tests by augmenting schemas with cache control support
This commit is contained in:
parent
787846c64b
commit
9e843dbf4b
4 changed files with 57 additions and 15 deletions
|
@ -1,11 +1,11 @@
|
|||
import { buildSchema } from 'graphql';
|
||||
import { buildSchemaWithCacheControlSupport } from './cacheControlSupport';
|
||||
|
||||
import { CacheScope } from '../';
|
||||
import { collectCacheControlHints } from './test-utils/helpers';
|
||||
import { collectCacheControlHints } from './collectCacheControlHints';
|
||||
|
||||
describe('@cacheControl directives', () => {
|
||||
it('should set maxAge: 0 and no scope for a field without cache hints', async () => {
|
||||
const schema = buildSchema(`
|
||||
const schema = buildSchemaWithCacheControlSupport(`
|
||||
type Query {
|
||||
droid(id: ID!): Droid
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ describe('@cacheControl directives', () => {
|
|||
});
|
||||
|
||||
it('should set maxAge to the default and no scope for a field without cache hints', async () => {
|
||||
const schema = buildSchema(`
|
||||
const schema = buildSchemaWithCacheControlSupport(`
|
||||
type Query {
|
||||
droid(id: ID!): Droid
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ describe('@cacheControl directives', () => {
|
|||
});
|
||||
|
||||
it('should set the specified maxAge from a cache hint on the field', async () => {
|
||||
const schema = buildSchema(`
|
||||
const schema = buildSchemaWithCacheControlSupport(`
|
||||
type Query {
|
||||
droid(id: ID!): Droid @cacheControl(maxAge: 60)
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ describe('@cacheControl directives', () => {
|
|||
});
|
||||
|
||||
it('should set the specified maxAge for a field from a cache hint on the target type', async () => {
|
||||
const schema = buildSchema(`
|
||||
const schema = buildSchemaWithCacheControlSupport(`
|
||||
type Query {
|
||||
droid(id: ID!): Droid
|
||||
}
|
||||
|
@ -112,7 +112,7 @@ describe('@cacheControl directives', () => {
|
|||
});
|
||||
|
||||
it('should overwrite the default maxAge when maxAge=0 is specified on the type', async () => {
|
||||
const schema = buildSchema(`
|
||||
const schema = buildSchemaWithCacheControlSupport(`
|
||||
type Query {
|
||||
droid(id: ID!): Droid
|
||||
}
|
||||
|
@ -139,7 +139,7 @@ describe('@cacheControl directives', () => {
|
|||
});
|
||||
|
||||
it('should override the maxAge from the target type with that specified on a field', async () => {
|
||||
const schema = buildSchema(`
|
||||
const schema = buildSchemaWithCacheControlSupport(`
|
||||
type Query {
|
||||
droid(id: ID!): Droid @cacheControl(maxAge: 120)
|
||||
}
|
||||
|
@ -166,7 +166,7 @@ describe('@cacheControl directives', () => {
|
|||
});
|
||||
|
||||
it('should override the maxAge from the target type with that specified on a field, keeping the scope', async () => {
|
||||
const schema = buildSchema(`
|
||||
const schema = buildSchemaWithCacheControlSupport(`
|
||||
type Query {
|
||||
droid(id: ID!): Droid @cacheControl(maxAge: 120)
|
||||
}
|
||||
|
@ -197,7 +197,7 @@ describe('@cacheControl directives', () => {
|
|||
});
|
||||
|
||||
it('should override the scope from the target type with that specified on a field', async () => {
|
||||
const schema = buildSchema(`
|
||||
const schema = buildSchemaWithCacheControlSupport(`
|
||||
type Query {
|
||||
droid(id: ID!): Droid @cacheControl(scope: PRIVATE)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
import { buildSchema } from 'graphql';
|
||||
import { makeExecutableSchema } from 'graphql-tools';
|
||||
|
||||
type FirstArg<F> = F extends (arg: infer A) => any ? A : never;
|
||||
|
||||
export function augmentTypeDefsWithCacheControlSupport(typeDefs) {
|
||||
return (
|
||||
`
|
||||
enum CacheControlScope {
|
||||
PUBLIC
|
||||
PRIVATE
|
||||
}
|
||||
|
||||
directive @cacheControl(
|
||||
maxAge: Int
|
||||
scope: CacheControlScope
|
||||
) on FIELD_DEFINITION | OBJECT | INTERFACE
|
||||
` + typeDefs
|
||||
);
|
||||
}
|
||||
|
||||
export function buildSchemaWithCacheControlSupport(source: string) {
|
||||
return buildSchema(augmentTypeDefsWithCacheControlSupport(source));
|
||||
}
|
||||
|
||||
export function makeExecutableSchemaWithCacheControlSupport(
|
||||
options: FirstArg<typeof makeExecutableSchema>,
|
||||
) {
|
||||
return makeExecutableSchema({
|
||||
...options,
|
||||
typeDefs: augmentTypeDefsWithCacheControlSupport(options.typeDefs),
|
||||
});
|
||||
}
|
|
@ -4,10 +4,10 @@ import {
|
|||
GraphQLTypeResolver,
|
||||
GraphQLIsTypeOfFn,
|
||||
} from 'graphql';
|
||||
import { makeExecutableSchema } from 'graphql-tools';
|
||||
import { makeExecutableSchemaWithCacheControlSupport } from './cacheControlSupport';
|
||||
|
||||
import { CacheScope } from '../';
|
||||
import { collectCacheControlHints } from './test-utils/helpers';
|
||||
import { collectCacheControlHints } from './collectCacheControlHints';
|
||||
|
||||
export interface GraphQLResolvers {
|
||||
[fieldName: string]: (() => any) | GraphQLResolverObject | GraphQLScalarType;
|
||||
|
@ -49,7 +49,10 @@ describe('dynamic cache control', () => {
|
|||
},
|
||||
};
|
||||
|
||||
const schema = makeExecutableSchema({ typeDefs, resolvers });
|
||||
const schema = makeExecutableSchemaWithCacheControlSupport({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
});
|
||||
|
||||
const hints = await collectCacheControlHints(
|
||||
schema,
|
||||
|
@ -90,7 +93,10 @@ describe('dynamic cache control', () => {
|
|||
},
|
||||
};
|
||||
|
||||
const schema = makeExecutableSchema({ typeDefs, resolvers });
|
||||
const schema = makeExecutableSchemaWithCacheControlSupport({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
});
|
||||
|
||||
const hints = await collectCacheControlHints(
|
||||
schema,
|
||||
|
@ -135,7 +141,10 @@ describe('dynamic cache control', () => {
|
|||
},
|
||||
};
|
||||
|
||||
const schema = makeExecutableSchema({ typeDefs, resolvers });
|
||||
const schema = makeExecutableSchemaWithCacheControlSupport({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
});
|
||||
|
||||
const hints = await collectCacheControlHints(
|
||||
schema,
|
||||
|
|
Loading…
Add table
Reference in a new issue