Skip to content

Commit

Permalink
Name operations (#1700)
Browse files Browse the repository at this point in the history
* fix type guard

* allow specification of operation name

alternative to #1651
  • Loading branch information
yaacovCR committed Jun 28, 2020
1 parent b8815ad commit dbe38eb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/delegate/src/Subschema.ts
Expand Up @@ -15,7 +15,7 @@ export function setObjectSubschema(result: any, subschema: GraphQLSchema | Subsc
result[OBJECT_SUBSCHEMA_SYMBOL] = subschema;
}

export function isSubschemaConfig(value: any): value is SubschemaConfig {
export function isSubschemaConfig(value: any): value is SubschemaConfig | Subschema {
return Boolean((value as SubschemaConfig).schema);
}

Expand Down
11 changes: 11 additions & 0 deletions packages/delegate/src/createRequest.ts
Expand Up @@ -32,6 +32,7 @@ export function getDelegatingOperation(parentType: GraphQLObjectType, schema: Gr

export function createRequestFromInfo({
info,
operationName,
operation = getDelegatingOperation(info.parentType, info.schema),
fieldName = info.fieldName,
selectionSet,
Expand All @@ -44,6 +45,7 @@ export function createRequestFromInfo({
fragments: info.fragments,
variableDefinitions: info.operation.variableDefinitions,
variableValues: info.variableValues,
targetOperationName: operationName,
targetOperation: operation,
targetFieldName: fieldName,
selectionSet,
Expand All @@ -58,6 +60,7 @@ export function createRequest({
fragments,
variableDefinitions,
variableValues,
targetOperationName,
targetOperation,
targetFieldName,
selectionSet,
Expand Down Expand Up @@ -130,8 +133,16 @@ export function createRequest({
selectionSet: newSelectionSet,
};

const operationName = targetOperationName
? {
kind: Kind.NAME,
value: targetOperationName,
}
: undefined;

const operationDefinition: OperationDefinitionNode = {
kind: Kind.OPERATION_DEFINITION,
name: operationName,
operation: targetOperation,
variableDefinitions: Object.keys(variableDefinitionMap).map(varName => variableDefinitionMap[varName]),
selectionSet: {
Expand Down
2 changes: 2 additions & 0 deletions packages/delegate/src/delegateToSchema.ts
Expand Up @@ -33,6 +33,7 @@ export function delegateToSchema(options: IDelegateToSchemaOptions | GraphQLSche

const {
info,
operationName,
operation = getDelegatingOperation(info.parentType, info.schema),
fieldName = info.fieldName,
returnType = info.returnType,
Expand All @@ -46,6 +47,7 @@ export function delegateToSchema(options: IDelegateToSchemaOptions | GraphQLSche
fieldName,
selectionSet,
fieldNodes,
operationName,
});

return delegateRequest({
Expand Down
3 changes: 3 additions & 0 deletions packages/delegate/src/types.ts
Expand Up @@ -35,6 +35,7 @@ export type DelegationBinding = (delegationContext: DelegationContext) => Array<

export interface IDelegateToSchemaOptions<TContext = Record<string, any>, TArgs = Record<string, any>> {
schema: GraphQLSchema | SubschemaConfig | Subschema;
operationName?: string;
operation?: Operation;
fieldName?: string;
returnType?: GraphQLOutputType;
Expand All @@ -58,6 +59,7 @@ export interface IDelegateRequestOptions extends Omit<IDelegateToSchemaOptions,

export interface ICreateRequestFromInfo {
info: GraphQLResolveInfo;
operationName?: string;
operation: Operation;
fieldName: string;
selectionSet?: SelectionSetNode;
Expand All @@ -72,6 +74,7 @@ export interface ICreateRequest {
variableDefinitions?: ReadonlyArray<VariableDefinitionNode>;
variableValues?: Record<string, any>;
targetOperation: Operation;
targetOperationName?: string;
targetFieldName: string;
selectionSet?: SelectionSetNode;
fieldNodes?: ReadonlyArray<FieldNode>;
Expand Down

0 comments on commit dbe38eb

Please sign in to comment.