-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(response-cache): added getScope callback in buildResponseCacheKey
- Loading branch information
Showing
6 changed files
with
280 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@envelop/response-cache': minor | ||
--- | ||
|
||
Added `getScope` callback in `buildResponseCacheKey` params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { | ||
FieldNode, | ||
GraphQLList, | ||
GraphQLNonNull, | ||
GraphQLObjectType, | ||
GraphQLOutputType, | ||
GraphQLSchema, | ||
Kind, | ||
parse, | ||
SelectionNode, | ||
visit, | ||
} from 'graphql'; | ||
import { memoize1 } from '@graphql-tools/utils'; | ||
import { CacheControlDirective, isPrivate } from './plugin'; | ||
|
||
/** Parse the selected query fields */ | ||
function parseSelections( | ||
selections: ReadonlyArray<SelectionNode> = [], | ||
record: Record<string, any>, | ||
) { | ||
for (const selection of selections) { | ||
if (selection.kind === Kind.FIELD) { | ||
record[selection.name.value] = {}; | ||
parseSelections(selection.selectionSet?.selections, record[selection.name.value]); | ||
} | ||
} | ||
} | ||
|
||
/** Iterate over record and parse its fields with schema type */ | ||
function parseRecordWithSchemaType( | ||
type: GraphQLOutputType, | ||
record: Record<string, any>, | ||
prefix?: string, | ||
): Set<string> { | ||
let fields: Set<string> = new Set(); | ||
if (type instanceof GraphQLNonNull || type instanceof GraphQLList) { | ||
fields = new Set([...fields, ...parseRecordWithSchemaType(type.ofType, record, prefix)]); | ||
} | ||
|
||
if (type instanceof GraphQLObjectType) { | ||
const newPrefixes = [...(prefix ?? []), type.name]; | ||
fields.add(newPrefixes.join('.')); | ||
|
||
const typeFields = type.getFields(); | ||
for (const key of Object.keys(record)) { | ||
const field = typeFields[key]; | ||
if (!field) { | ||
continue; | ||
} | ||
|
||
fields.add([...newPrefixes, field.name].join('.')); | ||
if (Object.keys(record[key]).length > 0) { | ||
fields = new Set([...fields, ...parseRecordWithSchemaType(field.type, record[key])]); | ||
} | ||
} | ||
} | ||
|
||
return fields; | ||
} | ||
|
||
function getSchemaCoordinatesFromQuery(schema: GraphQLSchema, query: string): Set<string> { | ||
const ast = parse(query); | ||
let fields: Set<string> = new Set(); | ||
|
||
const visitField = (node: FieldNode) => { | ||
const record: Record<string, any> = {}; | ||
const queryFields = schema.getQueryType()?.getFields()[node.name.value]; | ||
|
||
if (queryFields) { | ||
record[node.name.value] = {}; | ||
parseSelections(node.selectionSet?.selections, record[node.name.value]); | ||
|
||
fields.add(`Query.${node.name.value}`); | ||
fields = new Set([ | ||
...fields, | ||
...parseRecordWithSchemaType(queryFields.type, record[node.name.value]), | ||
]); | ||
} | ||
}; | ||
|
||
// Launch the field visitor | ||
visit(ast, { | ||
Field: visitField, | ||
}); | ||
|
||
return fields; | ||
} | ||
|
||
export const getScopeFromQuery = ( | ||
schema: GraphQLSchema, | ||
query: string, | ||
): NonNullable<CacheControlDirective['scope']> => { | ||
const fn = memoize1(({ query }: { query: string }) => { | ||
const schemaCoordinates = getSchemaCoordinatesFromQuery(schema, query); | ||
|
||
for (const coordinate of schemaCoordinates) { | ||
if (isPrivate(coordinate)) { | ||
return 'PRIVATE'; | ||
} | ||
} | ||
return 'PUBLIC'; | ||
}); | ||
return fn({ query }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters