You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(appsync): implement resolvable fields for code-first schema (#9660)
Implemented interfaces and resolvable fields for code-first schema.
`Field` extends `GraphqlType` and will allow you to define arguments.
<details>
<summary> Field Example </summary>
```gql
type Node {
test(argument: string): String
}
```
The CDK code required would be:
```ts
const field = new appsync.Field(appsync.GraphqlType.string(), {
args: {
argument: appsync.GraphqlType.string(),
},
});
const type = new appsynce.ObjectType('Node', {
definition: { test: field },
});
```
</details>
`ResolvableField` extends `Field` and will allow you to define arguments and its resolvers.
[**Object Types**](#Object-Types) can have fields that resolve and perform operations on
your backend.
<details>
<summary> Resolvable Field Example </summary>
For example, if we want to create the following type:
```gql
type Query {
get(argument: string): String
}
```
The CDK code required would be:
```ts
const field = new appsync.Field(appsync.GraphqlType.string(), {
args: {
argument: appsync.GraphqlType.string(),
},
dataSource: api.addNoneDataSource('none'),
requestMappingTemplate: dummyRequest,
responseMappingTemplate: dummyResponse,
});
const type = new appsynce.ObjectType('Query', {
definition: { get: field },
});
</details>
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
CDK offers the ability to generate your schema in a code-first approach.
186
186
A code-first approach offers a developer workflow with:
187
187
-**modularity**: organizing schema type definitions into different files
188
188
-**reusability**: simplifying down boilerplate/repetitive code
189
189
-**consistency**: resolvers and schema definition will always be synced
190
190
191
-
The code-first approach allows for dynamic schema generation. You can generate your schema based on variables and templates to reduce code duplication.
191
+
The code-first approach allows for **dynamic** schema generation. You can generate your schema based on variables and templates to reduce code duplication.
192
192
193
-
####Code-First Example
193
+
### Code-First Example
194
194
195
-
We are going to reference the [example](#Example) through a code-first approach.
195
+
To showcase the code-first approach. Let's try to model the following schema segment.
Notice how we can utilize the `generateEdgeAndConnection` function to generate
307
+
Object Types. In the future, if we wanted to create more Object Types, we can simply
308
+
create the base Object Type (i.e. Film) and from there we can generate its respective
309
+
`Connections` and `Edges`.
310
+
311
+
Check out a more in-depth example [here](https://github.com/BryanPan342/starwars-code-first).
312
+
313
+
### GraphQL Types
232
314
233
315
One of the benefits of GraphQL is its strongly typed nature. We define the
234
316
types within an object, query, mutation, interface, etc. as **GraphQL Types**.
235
317
236
318
GraphQL Types are the building blocks of types, whether they are scalar, objects,
237
319
interfaces, etc. GraphQL Types can be:
238
320
- [**Scalar Types**](https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html): Id, Int, String, AWSDate, etc.
239
-
-**Object Types**: types that you generate (i.e. `demo` from the example above)
240
-
-**Interface Types**: abstract types that define the base implementation of other
321
+
- [**Object Types**](#Object-Types): types that you generate (i.e. `demo` from the example above)
322
+
- [**Interface Types**](#Interface-Types): abstract types that define the base implementation of other
241
323
Intermediate Types
242
324
243
325
More concretely, GraphQL Types are simply the types appended to variables.
244
326
Referencing the object type `Demo` in the previous example, the GraphQL Types
245
327
is `String!` and is applied to both the names `id` and `version`.
246
328
247
-
#### Intermediate Types
329
+
### Field and Resolvable Fields
330
+
331
+
While `GraphqlType` is a base implementation for GraphQL fields, we have abstractions
332
+
on top of `GraphqlType` that provide finer grain support.
333
+
334
+
#### Field
335
+
336
+
`Field` extends `GraphqlType` and will allow you to define arguments. [**Interface Types**](#Interface-Types) are not resolvable and this class will allow you to define arguments,
337
+
but not its resolvers.
338
+
339
+
For example, if we want to create the following type:
340
+
341
+
```gql
342
+
typeNode {
343
+
test(argument:string):String
344
+
}
345
+
```
346
+
347
+
The CDK code required would be:
348
+
349
+
```ts
350
+
constfield=newappsync.Field({
351
+
returnType: appsync.GraphqlType.string(),
352
+
args: {
353
+
argument: appsync.GraphqlType.string(),
354
+
},
355
+
});
356
+
consttype=newappsync.InterfaceType('Node', {
357
+
definition: { test: field },
358
+
});
359
+
```
360
+
361
+
#### Resolvable Fields
362
+
363
+
`ResolvableField` extends `Field` and will allow you to define arguments and its resolvers.
364
+
[**Object Types**](#Object-Types) can have fields that resolve and perform operations on
365
+
your backend.
366
+
367
+
You can also create resolvable fields for object types.
368
+
369
+
```gql
370
+
typeInfo {
371
+
node(id:String):String
372
+
}
373
+
```
374
+
375
+
The CDK code required would be:
376
+
377
+
```ts
378
+
constinfo=newappsync.ObjectType('Info', {
379
+
definition: {
380
+
node: newappsync.ResolvableField({
381
+
returnType: appsync.GraphqlType.string(),
382
+
args: {
383
+
id: appsync.GraphqlType.string(),
384
+
},
385
+
dataSource: api.addNoneDataSource('none'),
386
+
requestMappingTemplate: dummyRequest,
387
+
responseMappingTemplate: dummyResponse,
388
+
}),
389
+
},
390
+
});
391
+
```
392
+
393
+
To nest resolvers, we can also create top level query types that call upon
394
+
other types. Building off the previous example, if we want the following graphql
395
+
type definition:
396
+
397
+
```gql
398
+
typeQuery {
399
+
get(argument:string):Info
400
+
}
401
+
```
402
+
403
+
The CDK code required would be:
404
+
405
+
```ts
406
+
constquery=newappsync.ObjectType('Query', {
407
+
definition: {
408
+
get: newappsync.ResolvableField({
409
+
returnType: appsync.GraphqlType.string(),
410
+
args: {
411
+
argument: appsync.GraphqlType.string(),
412
+
},
413
+
dataSource: api.addNoneDataSource('none'),
414
+
requestMappingTemplate: dummyRequest,
415
+
responseMappingTemplate: dummyResponse,
416
+
}),
417
+
},
418
+
});
419
+
```
420
+
421
+
Learn more about fields and resolvers [here](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-overview.html).
248
422
249
-
Intermediate Types are abstractions above Scalar Types. They have a set of defined
423
+
### Intermediate Types
424
+
425
+
Intermediate Types are defined by Graphql Types and Fields. They have a set of defined
250
426
fields, where each field corresponds to another type in the system. Intermediate
251
427
Types will be the meat of your GraphQL Schema as they are the types defined by you.
252
428
253
429
Intermediate Types include:
254
430
- [**Interface Types**](#Interface-Types)
255
431
- [**Object Types**](#Object-Types)
256
432
257
-
####Interface Types
433
+
### Interface Types
258
434
259
435
**Interface Types** are abstract types that define the implementation of other
260
436
intermediate types. They are useful for eliminating duplication and can be used
@@ -269,7 +445,7 @@ const node = new appsync.InterfaceType('Node', {
269
445
});
270
446
```
271
447
272
-
####Object Types
448
+
### Object Types
273
449
274
450
**Object Types** are types that you declare. For example, in the [code-first example](#code-first-example)
275
451
the `demo` variable is an **Object Type**. **Object Types** are defined by
@@ -297,7 +473,7 @@ You can create Object Types in three ways:
297
473
298
474
`scalar-types.ts` - a file for scalar type definitions
0 commit comments