-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add createType and dropType to schema module. Closes #112
- Loading branch information
Showing
13 changed files
with
366 additions
and
2 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
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,38 @@ | ||
import { freeze } from '../util/object-utils.js' | ||
import { IdentifierNode } from './identifier-node.js' | ||
import { OperationNode } from './operation-node.js' | ||
import { ValueListNode } from './value-list-node.js' | ||
import { ValueNode } from './value-node.js' | ||
|
||
export type CreateTypeNodeParams = Omit<Partial<CreateTypeNode>, 'kind'> | ||
|
||
export interface CreateTypeNode extends OperationNode { | ||
readonly kind: 'CreateTypeNode' | ||
readonly name: IdentifierNode | ||
readonly enum?: ValueListNode | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const CreateTypeNode = freeze({ | ||
is(node: OperationNode): node is CreateTypeNode { | ||
return node.kind === 'CreateTypeNode' | ||
}, | ||
|
||
create(name: string): CreateTypeNode { | ||
return freeze({ | ||
kind: 'CreateTypeNode', | ||
name: IdentifierNode.create(name), | ||
}) | ||
}, | ||
|
||
cloneWithEnum(createType: CreateTypeNode, values: string[]): CreateTypeNode { | ||
return freeze({ | ||
...createType, | ||
enum: ValueListNode.create( | ||
values.map((value) => ValueNode.createImmediate(value)) | ||
), | ||
}) | ||
}, | ||
}) |
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,34 @@ | ||
import { freeze } from '../util/object-utils.js' | ||
import { IdentifierNode } from './identifier-node.js' | ||
import { OperationNode } from './operation-node.js' | ||
|
||
export type DropTypeNodeParams = Omit<Partial<DropTypeNode>, 'kind' | 'name'> | ||
|
||
export interface DropTypeNode extends OperationNode { | ||
readonly kind: 'DropTypeNode' | ||
readonly name: IdentifierNode | ||
readonly ifExists?: boolean | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const DropTypeNode = freeze({ | ||
is(node: OperationNode): node is DropTypeNode { | ||
return node.kind === 'DropTypeNode' | ||
}, | ||
|
||
create(name: string): DropTypeNode { | ||
return freeze({ | ||
kind: 'DropTypeNode', | ||
name: IdentifierNode.create(name), | ||
}) | ||
}, | ||
|
||
cloneWith(dropType: DropTypeNode, params: DropTypeNodeParams): DropTypeNode { | ||
return freeze({ | ||
...dropType, | ||
...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
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
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,55 @@ | ||
import { OperationNodeSource } from '../operation-node/operation-node-source.js' | ||
import { CompiledQuery } from '../query-compiler/compiled-query.js' | ||
import { Compilable } from '../util/compilable.js' | ||
import { preventAwait } from '../util/prevent-await.js' | ||
import { QueryExecutor } from '../query-executor/query-executor.js' | ||
import { QueryId } from '../util/query-id.js' | ||
import { freeze } from '../util/object-utils.js' | ||
import { CreateTypeNode } from '../operation-node/create-type-node.js' | ||
|
||
export class CreateTypeBuilder implements OperationNodeSource, Compilable { | ||
readonly #props: CreateTypeBuilderProps | ||
|
||
constructor(props: CreateTypeBuilderProps) { | ||
this.#props = freeze(props) | ||
} | ||
|
||
toOperationNode(): CreateTypeNode { | ||
return this.#props.executor.transformQuery( | ||
this.#props.createTypeNode, | ||
this.#props.queryId | ||
) | ||
} | ||
|
||
asEnum(values: string[]): CreateTypeBuilder { | ||
return new CreateTypeBuilder({ | ||
...this.#props, | ||
createTypeNode: CreateTypeNode.cloneWithEnum( | ||
this.#props.createTypeNode, | ||
values | ||
), | ||
}) | ||
} | ||
|
||
compile(): CompiledQuery { | ||
return this.#props.executor.compileQuery( | ||
this.toOperationNode(), | ||
this.#props.queryId | ||
) | ||
} | ||
|
||
async execute(): Promise<void> { | ||
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId) | ||
} | ||
} | ||
|
||
preventAwait( | ||
CreateTypeBuilder, | ||
"don't await CreateTypeBuilder instances directly. To execute the query you need to call `execute`" | ||
) | ||
|
||
export interface CreateTypeBuilderProps { | ||
readonly queryId: QueryId | ||
readonly executor: QueryExecutor | ||
readonly createTypeNode: CreateTypeNode | ||
} |
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,54 @@ | ||
import { DropTypeNode } from '../operation-node/drop-type-node.js' | ||
import { OperationNodeSource } from '../operation-node/operation-node-source.js' | ||
import { CompiledQuery } from '../query-compiler/compiled-query.js' | ||
import { Compilable } from '../util/compilable.js' | ||
import { preventAwait } from '../util/prevent-await.js' | ||
import { QueryExecutor } from '../query-executor/query-executor.js' | ||
import { QueryId } from '../util/query-id.js' | ||
import { freeze } from '../util/object-utils.js' | ||
|
||
export class DropTypeBuilder implements OperationNodeSource, Compilable { | ||
readonly #props: DropTypeBuilderProps | ||
|
||
constructor(props: DropTypeBuilderProps) { | ||
this.#props = freeze(props) | ||
} | ||
|
||
ifExists(): DropTypeBuilder { | ||
return new DropTypeBuilder({ | ||
...this.#props, | ||
dropTypeNode: DropTypeNode.cloneWith(this.#props.dropTypeNode, { | ||
ifExists: true, | ||
}), | ||
}) | ||
} | ||
|
||
toOperationNode(): DropTypeNode { | ||
return this.#props.executor.transformQuery( | ||
this.#props.dropTypeNode, | ||
this.#props.queryId | ||
) | ||
} | ||
|
||
compile(): CompiledQuery { | ||
return this.#props.executor.compileQuery( | ||
this.toOperationNode(), | ||
this.#props.queryId | ||
) | ||
} | ||
|
||
async execute(): Promise<void> { | ||
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId) | ||
} | ||
} | ||
|
||
preventAwait( | ||
DropTypeBuilder, | ||
"don't await DropTypeBuilder instances directly. To execute the query you need to call `execute`" | ||
) | ||
|
||
export interface DropTypeBuilderProps { | ||
readonly queryId: QueryId | ||
readonly executor: QueryExecutor | ||
readonly dropTypeNode: DropTypeNode | ||
} |
Oops, something went wrong.