Skip to content

suggestions #4219

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

Closed
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
11 changes: 11 additions & 0 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface CollectFieldsContext {
variableValues: VariableValues;
operation: OperationDefinitionNode;
runtimeType: GraphQLObjectType;
maskSuggestions: boolean;
visitedFragmentNames: Set<string>;
}

Expand All @@ -66,12 +67,14 @@ interface CollectFieldsContext {
*
* @internal
*/
// eslint-disable-next-line @typescript-eslint/max-params
export function collectFields(
schema: GraphQLSchema,
fragments: ObjMap<FragmentDetails>,
variableValues: VariableValues,
runtimeType: GraphQLObjectType,
operation: OperationDefinitionNode,
maskSuggestions: boolean,
): {
groupedFieldSet: GroupedFieldSet;
newDeferUsages: ReadonlyArray<DeferUsage>;
Expand All @@ -84,6 +87,7 @@ export function collectFields(
variableValues,
runtimeType,
operation,
maskSuggestions,
visitedFragmentNames: new Set(),
};

Expand Down Expand Up @@ -114,6 +118,7 @@ export function collectSubfields(
operation: OperationDefinitionNode,
returnType: GraphQLObjectType,
fieldDetailsList: FieldDetailsList,
maskSuggestions: boolean,
): {
groupedFieldSet: GroupedFieldSet;
newDeferUsages: ReadonlyArray<DeferUsage>;
Expand All @@ -125,6 +130,7 @@ export function collectSubfields(
runtimeType: returnType,
operation,
visitedFragmentNames: new Set(),
maskSuggestions,
};
const subGroupedFieldSet = new AccumulatorMap<string, FieldDetails>();
const newDeferUsages: Array<DeferUsage> = [];
Expand Down Expand Up @@ -165,6 +171,7 @@ function collectFieldsImpl(
variableValues,
runtimeType,
operation,
maskSuggestions,
visitedFragmentNames,
} = context;

Expand Down Expand Up @@ -263,6 +270,7 @@ function collectFieldsImpl(
newFragmentVariableValues = getFragmentVariableValues(
selection,
fragmentVariableSignatures,
maskSuggestions,
variableValues,
fragmentVariableValues,
);
Expand Down Expand Up @@ -310,6 +318,7 @@ function getDeferUsage(
const defer = getDirectiveValues(
GraphQLDeferDirective,
node,
false,
variableValues,
fragmentVariableValues,
);
Expand Down Expand Up @@ -345,6 +354,7 @@ function shouldIncludeNode(
const skip = getDirectiveValues(
GraphQLSkipDirective,
node,
false,
variableValues,
fragmentVariableValues,
);
Expand All @@ -355,6 +365,7 @@ function shouldIncludeNode(
const include = getDirectiveValues(
GraphQLIncludeDirective,
node,
false,
variableValues,
fragmentVariableValues,
);
Expand Down
37 changes: 31 additions & 6 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const collectSubfields = memoize3(
returnType: GraphQLObjectType,
fieldDetailsList: FieldDetailsList,
) => {
const { schema, fragments, operation, variableValues } =
const { schema, fragments, operation, variableValues, maskSuggestions } =
validatedExecutionArgs;
return _collectSubfields(
schema,
Expand All @@ -107,6 +107,7 @@ const collectSubfields = memoize3(
operation,
returnType,
fieldDetailsList,
maskSuggestions,
);
},
);
Expand Down Expand Up @@ -155,6 +156,7 @@ export interface ValidatedExecutionArgs {
validatedExecutionArgs: ValidatedExecutionArgs,
) => PromiseOrValue<ExecutionResult>;
enableEarlyExecution: boolean;
maskSuggestions: boolean;
}

export interface ExecutionContext {
Expand Down Expand Up @@ -184,6 +186,7 @@ export interface ExecutionArgs {
) => PromiseOrValue<ExecutionResult>
>;
enableEarlyExecution?: Maybe<boolean>;
maskSuggestions?: Maybe<boolean>;
}

export interface StreamUsage {
Expand Down Expand Up @@ -308,8 +311,14 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
cancellableStreams: undefined,
};
try {
const { schema, fragments, rootValue, operation, variableValues } =
validatedExecutionArgs;
const {
schema,
fragments,
rootValue,
operation,
variableValues,
maskSuggestions,
} = validatedExecutionArgs;
const rootType = schema.getRootType(operation.operation);
if (rootType == null) {
throw new GraphQLError(
Expand All @@ -324,6 +333,7 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
variableValues,
rootType,
operation,
maskSuggestions,
);

const { groupedFieldSet, newDeferUsages } = collectedFields;
Expand Down Expand Up @@ -554,12 +564,16 @@ export function validateExecutionArgs(
// FIXME: https://github.com/graphql/graphql-js/issues/2203
/* c8 ignore next */
const variableDefinitions = operation.variableDefinitions ?? [];
const maskSuggestions = args.maskSuggestions ?? false;

const variableValuesOrErrors = getVariableValues(
schema,
variableDefinitions,
rawVariableValues ?? {},
{ maxErrors: 50 },
{
maxErrors: 50,
maskSuggestions,
},
);

if (variableValuesOrErrors.errors) {
Expand All @@ -579,6 +593,7 @@ export function validateExecutionArgs(
subscribeFieldResolver: subscribeFieldResolver ?? defaultFieldResolver,
perEventExecutor: perEventExecutor ?? executeSubscriptionEvent,
enableEarlyExecution: enableEarlyExecution === true,
maskSuggestions,
};
}

Expand Down Expand Up @@ -762,7 +777,8 @@ function executeField(
deferMap: ReadonlyMap<DeferUsage, DeferredFragmentRecord> | undefined,
): PromiseOrValue<GraphQLWrappedResult<unknown>> | undefined {
const validatedExecutionArgs = exeContext.validatedExecutionArgs;
const { schema, contextValue, variableValues } = validatedExecutionArgs;
const { schema, contextValue, variableValues, maskSuggestions } =
validatedExecutionArgs;
const fieldName = fieldDetailsList[0].node.name.value;
const fieldDef = schema.getField(parentType, fieldName);
if (!fieldDef) {
Expand All @@ -788,6 +804,7 @@ function executeField(
const args = experimentalGetArgumentValues(
fieldDetailsList[0].node,
fieldDef.args,
maskSuggestions,
variableValues,
fieldDetailsList[0].fragmentVariableValues,
);
Expand Down Expand Up @@ -1099,6 +1116,7 @@ function getStreamUsage(
const stream = getDirectiveValues(
GraphQLStreamDirective,
fieldDetailsList[0].node,
false,
variableValues,
fieldDetailsList[0].fragmentVariableValues,
);
Expand Down Expand Up @@ -2065,6 +2083,7 @@ function executeSubscription(
contextValue,
operation,
variableValues,
maskSuggestions,
} = validatedExecutionArgs;

const rootType = schema.getSubscriptionType();
Expand All @@ -2081,6 +2100,7 @@ function executeSubscription(
variableValues,
rootType,
operation,
maskSuggestions,
);

const firstRootField = groupedFieldSet.entries().next().value as [
Expand Down Expand Up @@ -2114,7 +2134,12 @@ function executeSubscription(

// Build a JS object of arguments from the field.arguments AST, using the
// variables scope to fulfill any variable references.
const args = getArgumentValues(fieldDef, fieldNodes[0], variableValues);
const args = getArgumentValues(
fieldDef,
fieldNodes[0],
maskSuggestions,
variableValues,
);

// Call the `subscribe()` resolver or the default resolver to produce an
// AsyncIterable yielding raw payloads.
Expand Down
30 changes: 26 additions & 4 deletions src/execution/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ export function getVariableValues(
schema: GraphQLSchema,
varDefNodes: ReadonlyArray<VariableDefinitionNode>,
inputs: { readonly [variable: string]: unknown },
options?: { maxErrors?: number },
options?: { maxErrors?: number; maskSuggestions?: boolean },
): VariableValuesOrErrors {
const errors: Array<GraphQLError> = [];
const maxErrors = options?.maxErrors;
const maskSuggestions = options?.maskSuggestions;
try {
const variableValues = coerceVariableValues(
schema,
Expand All @@ -72,6 +73,7 @@ export function getVariableValues(
}
errors.push(error);
},
maskSuggestions,
);

if (errors.length === 0) {
Expand All @@ -89,6 +91,7 @@ function coerceVariableValues(
varDefNodes: ReadonlyArray<VariableDefinitionNode>,
inputs: { readonly [variable: string]: unknown },
onError: (error: GraphQLError) => void,
maskSuggestions: boolean | undefined,
): VariableValues {
const sources: ObjMap<VariableValueSource> = Object.create(null);
const coerced: ObjMap<unknown> = Object.create(null);
Expand All @@ -105,7 +108,11 @@ function coerceVariableValues(
const defaultValue = varSignature.defaultValue;
if (defaultValue) {
sources[varName] = { signature: varSignature };
coerced[varName] = coerceDefaultValue(defaultValue, varType);
coerced[varName] = coerceDefaultValue(
defaultValue,
varType,
maskSuggestions,
);
} else if (isNonNullType(varType)) {
const varTypeStr = inspect(varType);
onError(
Expand Down Expand Up @@ -136,6 +143,7 @@ function coerceVariableValues(
coerced[varName] = coerceInputValue(
value,
varType,
maskSuggestions,
(path, invalidValue, error) => {
let prefix =
`Variable "$${varName}" got invalid value ` + inspect(invalidValue);
Expand All @@ -158,6 +166,7 @@ function coerceVariableValues(
export function getFragmentVariableValues(
fragmentSpreadNode: FragmentSpreadNode,
fragmentSignatures: ReadOnlyObjMap<GraphQLVariableSignature>,
maskSuggestions: boolean,
variableValues: VariableValues,
fragmentVariableValues?: Maybe<VariableValues>,
): VariableValues {
Expand All @@ -176,6 +185,7 @@ export function getFragmentVariableValues(
const coerced = experimentalGetArgumentValues(
fragmentSpreadNode,
varSignatures,
maskSuggestions,
variableValues,
fragmentVariableValues,
);
Expand All @@ -194,15 +204,22 @@ export function getFragmentVariableValues(
export function getArgumentValues(
def: GraphQLField<unknown, unknown> | GraphQLDirective,
node: FieldNode | DirectiveNode,
maskSuggestions?: boolean | undefined,
variableValues?: Maybe<VariableValues>,
): { [argument: string]: unknown } {
return experimentalGetArgumentValues(node, def.args, variableValues);
return experimentalGetArgumentValues(
node,
def.args,
maskSuggestions,
variableValues,
);
}

export function experimentalGetArgumentValues(
node: FieldNode | DirectiveNode | FragmentSpreadNode,
argDefs: ReadonlyArray<GraphQLArgument | GraphQLVariableSignature>,
variableValues: Maybe<VariableValues>,
maskSuggestions?: boolean | undefined,
variableValues?: Maybe<VariableValues>,
fragmentVariablesValues?: Maybe<VariableValues>,
): { [argument: string]: unknown } {
const coercedValues: { [argument: string]: unknown } = {};
Expand All @@ -222,6 +239,7 @@ export function experimentalGetArgumentValues(
coercedValues[name] = coerceDefaultValue(
argDef.defaultValue,
argDef.type,
maskSuggestions,
);
} else if (isNonNullType(argType)) {
throw new GraphQLError(
Expand Down Expand Up @@ -251,6 +269,7 @@ export function experimentalGetArgumentValues(
coercedValues[name] = coerceDefaultValue(
argDef.defaultValue,
argDef.type,
maskSuggestions,
);
} else if (isNonNullType(argType)) {
throw new GraphQLError(
Expand All @@ -275,6 +294,7 @@ export function experimentalGetArgumentValues(
const coercedValue = coerceInputLiteral(
valueNode,
argType,
maskSuggestions,
variableValues,
fragmentVariablesValues,
);
Expand Down Expand Up @@ -308,6 +328,7 @@ export function experimentalGetArgumentValues(
export function getDirectiveValues(
directiveDef: GraphQLDirective,
node: { readonly directives?: ReadonlyArray<DirectiveNode> | undefined },
maskSuggestions?: boolean | undefined,
variableValues?: Maybe<VariableValues>,
fragmentVariableValues?: Maybe<VariableValues>,
): undefined | { [argument: string]: unknown } {
Expand All @@ -319,6 +340,7 @@ export function getDirectiveValues(
return experimentalGetArgumentValues(
directiveNode,
directiveDef.args,
maskSuggestions,
variableValues,
fragmentVariableValues,
);
Expand Down
7 changes: 6 additions & 1 deletion src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface GraphQLArgs {
operationName?: Maybe<string>;
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
maskSuggestions?: boolean;
}

export function graphql(args: GraphQLArgs): Promise<ExecutionResult> {
Expand Down Expand Up @@ -101,6 +102,7 @@ function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
operationName,
fieldResolver,
typeResolver,
maskSuggestions,
} = args;

// Validate Schema
Expand All @@ -118,7 +120,9 @@ function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
}

// Validate
const validationErrors = validate(schema, document);
const validationErrors = validate(schema, document, undefined, {
maskSuggestions: maskSuggestions ?? false,
});
if (validationErrors.length > 0) {
return { errors: validationErrors };
}
Expand All @@ -133,5 +137,6 @@ function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
operationName,
fieldResolver,
typeResolver,
maskSuggestions,
});
}
Loading
Loading