GraphQL schema design

Quick start guide for GraphQL schema design in Moon Modeler.

1. New project

To create a new project, click New on the toolbar and select the GraphQL project type.

Graphql project

2. GraphQL types and inputs

To visually design a GraphQL schema, add new objects to the diagram. On the main toolbar, you can find the Add item icon.
Click the icon and select type of the object you wish to add to the diagram.

add object to graphql schema design

Then click the diagram area. New graphical object will appear on the diagram.

Edit properties in Right Side panel:

sidebar

Alternatively, you can double click the graphics or right-click the item and choose Edit.

edit properties

Type properties

Basic type settings can be specified in the section Details.

Fields

In section Fields, you can add new type fields. Specify new field name, datatype or enum and click Add.

  • ARR indicates Array.
  • NN represents Not Null field.

Detailed field specifics can be defined in a collapsible/expandable area. 

  • Arguments and Descriptions will be included in the generated script.
  • Sample data will appear in the diagram if the Sample data mode is activated.
fields in graphql schema diagram

Types in types

If you want to use an already existing type for new field datatype, draw a reference between the existing type and the new type.
Name of the existing type will appear as the only item in the datatype dropdown. One of the advantages of visual modeling is, that when you decide to rename the referenced type, the name will change automatically in the field definitions.

types in types

Inputs are managed in Moon Modeler in a similar way.

3. Enums

Enums can be added to a diagram and used in field definitions specified for types, inputs or interfaces.

enum

Enum used in the diagram:

enum in graphql diagram

4. Interfaces

Interfaces can be created the same way as types and you can also add fields to interfaces.

interface

Interfaces can be linked to types in two different ways. You can draw references or create implements.

When you draw a new “Implements” line between an interface and type, all the fields defined in the interface will migrate to the type automatically See fields name and appearsIn in the Droid type.

graphql - interface and references vs implements

Details:

  • When you add new fields to the interface, the new field will also be added to all referenced types.
  • If you want to remove a migrated field from a type, delete the reference line from the diagram.
  • Migrated fields are read-only and certain properties cannot be modified.
read only fields

On the following screenshot you can see interface Character and type Person. When you create a reference from the Character interface to the Person type, new field characters of datatype Character will be added to the Person type. (The fields name and appearsIn are defined only in the interface).

reference

5. References & Implements

References can be created:

  • From Type to Type
  • From Interface to Type
  • From Interface to the same Interface
  • From Type to Union
  • From Type to Input

Implements can be created:

  • From Interface to Type

To add a new reference to the diagram, click the Reference icon, then click source object (type or interface) and then click target object (type, input or union).

6. Unions

Add new union to the GraphQL schema diagram and then add references between type and union to add fields to the uinon object.

add union

Example:

graphql union

7. Queries and mutations

Queries and mutations are displayed as simple graphical objects and definition can be specified by code.

8. Code generation

To generate GraphQL schema file, click the Script icon on the main toolbar.

graphql generated schema script

9. Other features and visual elements

Notes, lines and other information that can be displayed in a diagram will help you keep the structure well documented.

graphql schema design

#