/
index.ts
274 lines (245 loc) · 6.23 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import {
IAnyType,
IModelType,
isFrozenType,
ISimpleType,
isModelType,
types,
UnionStringArray,
} from 'mobx-state-tree';
import { generateFromSchema } from '../src/index';
describe('graphql-mst', () => {
it('should export a generateFromSchema function', () => {
expect(typeof generateFromSchema).toBe('function');
});
it('should parse schema', () => {
const schema = `
type Test {
foo: String!
}
`;
const { Test } = generateFromSchema(schema);
expect(Test).toBeTruthy();
expect(isModelType(Test)).toBe(true);
});
it('should throw on syntax error', () => {
try {
generateFromSchema('');
throw new Error();
} catch (err) {
expect(err.message).toContain('Syntax Error');
}
});
it('should convert scalars to frozen', () => {
const schema = `
scalar JSON
type Test {
foo: JSON
}
`;
const { Test } = generateFromSchema(schema);
expect(isFrozenType(Test.properties.foo)).toBe(true);
});
it('should convert string properties', () => {
const schema = `
type Test {
foo: String
}
`;
const result = generateFromSchema(schema);
const Test: IModelType<{ foo: IAnyType }, {}> = result.Test;
expect(Test.properties.foo).toBeTruthy();
expect(typeof Test.properties.foo).toBe(typeof types.string);
});
it('should convert enum properties', () => {
const schema = `
enum TestEnum {
FOO
BAR
}
type Test {
foo: TestEnum
}
`;
const { Test, TestEnum } = generateFromSchema(schema) as {
Test: any;
TestEnum: ISimpleType<UnionStringArray<['FOO', 'BAR']>>;
};
expect(TestEnum.name).toBe('TestEnum');
expect(TestEnum.create('FOO')).toBe('FOO');
try {
expect(TestEnum.create('BOO' as any)).toBe('BOO');
throw new Error();
} catch (err) {
expect(err.message).toContain('Error while converting');
}
expect(typeof (Test as IModelType<{ foo: typeof TestEnum }, {}>).properties.foo).toBe(
typeof TestEnum
);
});
it('should convert unions', () => {
const schema = `
type Foo { foo: String }
type Bar { bar: String }
union FooBar = Foo | Bar
type Test {
baz: FooBar
}
`;
const result = generateFromSchema(schema);
const Foo: IModelType<{ foo: IAnyType }, {}> = result.Foo;
const Bar: IModelType<{ bar: IAnyType }, {}> = result.Bar;
const Test: IModelType<{ baz: IAnyType }, {}> = result.Test;
expect(Test.create({ baz: Foo.create({ foo: 'foo' }) })).toEqual({
baz: { foo: 'foo' },
});
expect(Test.create({ baz: Bar.create({ bar: 'bar' }) })).toEqual({
baz: { bar: 'bar' },
});
});
it('should convert complex type', () => {
const schema = `
type Foo { foo: String }
type Test {
a: String
b: ID
c: Int
d: Float
e: [String]
f: Foo
}
`;
const result = generateFromSchema(schema);
const test = result.Test.create({
a: 'string',
b: 'id',
c: 1,
d: 1.1,
e: ['one', 'two'],
f: {
foo: 'bar',
},
});
expect(test).toMatchSnapshot();
});
it('should convert arrays', () => {
const schema = `
type Test {
a: [String]
b: [String!]
c: [String]!
d: [String!]!
e: [[String!]]!
}
`;
const { Test } = generateFromSchema(schema);
expect(Test.properties.a.name).toEqual('((string | null)[] | null)');
expect(Test.properties.b.name).toEqual('(string[] | null)');
expect(Test.properties.c.name).toEqual('(string | null)[]');
expect(Test.properties.d.name).toEqual('string[]');
expect(Test.properties.e.name).toEqual('string[][]');
});
it('should convert interfaces', () => {
const schema = `
interface Demo {
foo: String!
}
type Test implements Demo {
bar: String!
}
`;
const { Test } = generateFromSchema(schema);
expect(Test.properties.foo.name).toBe('string');
expect(Test.properties.bar.name).toBe('string');
});
it('should convert input types', () => {
const schema = `
input TestInput {
foo: String!
}
type Test {
bar: TestInput
}
`;
const { Test, TestInput } = generateFromSchema(schema);
expect(TestInput.properties.foo.name).toBe('string');
expect(Test.properties.bar.name).toBe('(TestInput | null)');
});
it('should handle ID types', () => {
let Test;
const schema = `
type Test {
foo: ID!
bar: ID
}
`;
Test = generateFromSchema(schema).Test;
expect(Test.properties.foo.name).toBe('identifier');
Test = generateFromSchema(schema, { Test: { identifier: 'bar' } }).Test;
expect(Test.properties.foo.name).toBe('string');
});
it('should handle ID types in interfaces', () => {
const schema = `
interface TestInterface {
foo: ID!
}
type Test implements TestInterface {
bar: ID!
}
`;
const { Test } = generateFromSchema(schema);
expect(Test.properties.foo.name).toBe('string');
expect(Test.properties.bar.name).toBe('identifier');
const lookup = types
.model({
foos: types.map(Test),
})
.actions(self => ({ add: item => self.foos.put(item) }))
.create({ foos: {} });
lookup.add({ foo: '10', bar: '1' });
lookup.add({ foo: '20', bar: '2' });
expect(lookup.foos.get('1')).toEqual({ foo: '10', bar: '1' });
expect(lookup.foos.get('2')).toEqual({ foo: '20', bar: '2' });
});
});
// type Foo {
// foo: String
// }
// type Bar {
// bar: String
// }
// union TestUnion = Foo | Bar
// enum Baz {
// FOO
// BAR
// }
// interface Character {
// id: ID!
// name: String!
// }
// type Human implements Character {
// id: ID!
// name: String!
// totalCredits: Int
// }
// type Droid implements Character {
// id: ID!
// bleh: ID!
// primaryFunction: String
// }
// type Demo {
// a: String!
// aa: String
// b: [String!]!
// bb: [String!]
// bbb: [String]!
// bbbb: [String]
// bbbbb: [[[String!]!]!]!
// c: TestUnion
// d: [Foo]
// e: Bar
// f: Baz
// g: Droid
// h(yo: UserInput): Human
// u: UserInput
// }