Last active
December 28, 2018 16:32
-
-
Save schroedermatt/f802f21c13538ad961ce1f420fad338e to your computer and use it in GitHub Desktop.
Build GraphQL schema programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| GraphQLObjectType keeperType = newObject() | |
| .name("keeper") // required | |
| .description("A Zoo Keeper") // optional | |
| .field(newFieldDefinition() | |
| .name("id") | |
| .description("Keeper ID") | |
| .type(GraphQLLong) | |
| .build()) | |
| .field(newFieldDefinition() | |
| .name("name") | |
| .description("Keeper Name") | |
| .type(GraphQLString) | |
| .build()) | |
| .field(newFieldDefinition() | |
| .name("animals") | |
| .description("Animals that the keeper cares for.") | |
| .type(new GraphQLList(animalType)) | |
| .build()) | |
| .build() | |
| // GraphQLObjectType animalType = .. similar to above | |
| GraphQLObjectType queryType = newObject() | |
| .name("query") | |
| .description("Root Query Type") | |
| .field(newFieldDefinition() | |
| .name("keepers") | |
| .description("Overview of all Zoo Keepers") | |
| .type(new GraphQLList(keeperType)) | |
| // the magic! datafetcher could retrieve this data from anywhere. | |
| .dataFetcher({ env -> keeperDataFetcher.findAll() }) | |
| .build()) | |
| .field(newFieldDefinition() | |
| .name("animals") | |
| .description("Overview of all animals") | |
| .type(new GraphQLList(animalType)) | |
| .dataFetcher({ env -> animalDataFetcher.findAll() }) | |
| .build()) | |
| .build() | |
| this.schema = GraphQLSchema | |
| .newSchema() | |
| .query(queryType) | |
| .build() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment