-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace implementation of the lists API (#6153)
- Loading branch information
Showing
7 changed files
with
124 additions
and
663 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 @@ | ||
--- | ||
'@keystone-next/keystone': major | ||
--- | ||
|
||
Removed implicit chunking from the lists API so that the lists API is a direct translation of the GraphQL API |
43 changes: 2 additions & 41 deletions
43
packages/keystone/src/lib/coerceAndValidateForGraphQLInput.ts
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
89 changes: 89 additions & 0 deletions
89
packages/keystone/src/lib/context/executeGraphQLFieldWithSelection.ts
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,89 @@ | ||
import { | ||
execute, | ||
FragmentDefinitionNode, | ||
GraphQLList, | ||
GraphQLNonNull, | ||
GraphQLOutputType, | ||
GraphQLSchema, | ||
parse, | ||
validate, | ||
} from 'graphql'; | ||
import { KeystoneContext } from '@keystone-next/types'; | ||
import { getVariablesForGraphQLField } from './executeGraphQLFieldToRootVal'; | ||
|
||
function getRootTypeName(type: GraphQLOutputType): string { | ||
if (type instanceof GraphQLNonNull) { | ||
return getRootTypeName(type.ofType); | ||
} | ||
if (type instanceof GraphQLList) { | ||
return getRootTypeName(type.ofType); | ||
} | ||
return type.name; | ||
} | ||
|
||
export function executeGraphQLFieldWithSelection( | ||
schema: GraphQLSchema, | ||
operation: 'query' | 'mutation', | ||
fieldName: string | ||
) { | ||
const rootType = operation === 'mutation' ? schema.getMutationType()! : schema.getQueryType()!; | ||
const field = rootType.getFields()[fieldName]; | ||
if (field === undefined) { | ||
return () => { | ||
throw new Error('You do not have access to this resource'); | ||
}; | ||
} | ||
const { argumentNodes, variableDefinitions } = getVariablesForGraphQLField(field); | ||
const rootName = getRootTypeName(field.type); | ||
return async (args: Record<string, any>, query: string, context: KeystoneContext) => { | ||
const selectionSet = ( | ||
parse(`fragment x on ${rootName} {${query}}`).definitions[0] as FragmentDefinitionNode | ||
).selectionSet; | ||
|
||
const document = { | ||
kind: 'Document', | ||
definitions: [ | ||
{ | ||
kind: 'OperationDefinition', | ||
operation, | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
{ | ||
kind: 'Field', | ||
name: { kind: 'Name', value: field.name }, | ||
arguments: argumentNodes, | ||
selectionSet: selectionSet, | ||
}, | ||
], | ||
}, | ||
variableDefinitions, | ||
}, | ||
], | ||
} as const; | ||
|
||
const validationErrors = validate(schema, document); | ||
|
||
if (validationErrors.length > 0) { | ||
throw validationErrors[0]; | ||
} | ||
|
||
const result = await execute({ | ||
schema, | ||
document, | ||
contextValue: context, | ||
variableValues: Object.fromEntries( | ||
// GraphQL for some reason decides to make undefined values in args | ||
// skip defaulting for some reason | ||
// this ofc doesn't technically fully fix it (bc nested things) | ||
// but for the cases where we care, it does | ||
Object.entries(args).filter(([, val]) => val !== undefined) | ||
), | ||
rootValue: {}, | ||
}); | ||
if (result.errors?.length) { | ||
throw result.errors[0]; | ||
} | ||
return result.data![field.name]; | ||
}; | ||
} |
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
Oops, something went wrong.
7716315
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: