/
schema.ts
95 lines (86 loc) · 2.25 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { readFileSync } from 'fs';
import { IGraphqlApi } from './graphqlapi-base';
/**
* Configuration for bound graphql schema
*
* Returned from ISchema.bind allowing late binding of schemas to graphqlapi-base
*/
export interface ISchemaConfig {
/**
* The ID of the api the schema is bound to
*/
apiId: string;
/**
* The schema definition string
*/
definition: string;
}
/**
* Used for configuring schema bind behavior.
*
* This is intended to prevent breaking changes to implementors of ISchema
* if needing to add new behavior.
*/
export interface SchemaBindOptions {}
/**
* Interface for implementing your own schema
*
* Useful for providing schema's from sources other than assets
*/
export interface ISchema {
/**
* Binds a schema string to a GraphQlApi
*
* @returns ISchemaConfig with apiId and schema definition string
* @param api the api to bind the schema to
* @param options configuration for bind behavior
*/
bind(api: IGraphqlApi, options?: SchemaBindOptions): ISchemaConfig;
}
/**
* The options for configuring a schema from an existing file
*/
export interface SchemaProps {
/**
* The file path for the schema. When this option is
* configured, then the schema will be generated from an
* existing file from disk.
*/
readonly filePath: string;
};
/**
* The Schema for a GraphQL Api
*
* If no options are configured, schema will be generated
* code-first.
*/
export class SchemaFile implements ISchema {
/**
* Generate a Schema from file
*
* @returns `SchemaAsset` with immutable schema defintion
* @param filePath the file path of the schema file
*/
public static fromAsset(filePath: string): SchemaFile {
return new SchemaFile({ filePath });
}
/**
* The definition for this schema
*/
public definition: string;
public constructor(options: SchemaProps) {
this.definition = readFileSync(options.filePath).toString('utf-8');
}
/**
* Called when the GraphQL Api is initialized to allow this object to bind
* to the stack.
*
* @param api The binding GraphQL Api
*/
public bind(api: IGraphqlApi, _options?: SchemaBindOptions): ISchemaConfig {
return {
apiId: api.apiId,
definition: this.definition,
};
}
}