Use input and enumeration types

Use input and enumeration types

Using input types, we have improved the reusability of the parameters passed to the change, while also not prone to errors. When combining input types and enumerated types, we can more specifically specify the input types that can be provided for specific fields. The input type and the enumeration type are very easy to use, and they can be used in conjunction with each other to do more with less.

image

  • Run changes with new input type

image

You also need to send the corresponding JSON in the Query Variables panel:

image

If the category field is not provided, it defaults to PORTRAIT. In other words, if we provide a category value, it will perform enumeration type verification before the operation is sent to the server. If valid, pass it as a parameter to the parser.

  • One-to-many connection

Because connections are created using fields of object type, they can be mapped to parser functions. In these functions, we can use the parent parameter to determine and return the connected data.

image

  • Many-to-many connection

image

GraphQL does not require exact matching of types in the data model and schema.

Custom scalar

GraphQL has its default scalar type, which can be used for any field. Scalars like Int, Float, String, Boolean, and ID are suitable for most situations, but there are cases where you need to create custom scalar types to meet data requirements.

The custom DateTime scalar is added to our typeDefs and used in the Photo type of the created field. The created field is used to store the release date and time of a specific photo:

image

Each field in the schema needs to be mapped to a parser. Of course, custom types also need to be mapped to DateTime type parsers. We created a custom scalar type for DateTime because we want to parse and verify any field that uses this scalar as a JavaScript Date type.

Because there are many formats to represent the date and time as a string. For example, the following strings represent valid dates: ● "4/18/2018" ● "4/18/20181: 30: 00 PM" ● "Sun Apr 15201812: 10: 17 GMT-0700 (PDT)" ● " 2018-04-15T19: 09: 57.308Z "We can use any of these strings to create a datetime object:

image

We use GraphQLScalarType object to create parser for custom scalar

Guess you like

Origin www.cnblogs.com/liuxiaokun/p/12676812.html