Skip to content

Instantly share code, notes, and snippets.

@schroedermatt
Last active December 28, 2018 16:32
Show Gist options
  • Select an option

  • Save schroedermatt/f802f21c13538ad961ce1f420fad338e to your computer and use it in GitHub Desktop.

Select an option

Save schroedermatt/f802f21c13538ad961ce1f420fad338e to your computer and use it in GitHub Desktop.
Build GraphQL schema programmatically
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